High-level interface for Michelson contracts and not only

0
114

High-level interface for Michelson contracts

Major update of PyTezos library by Baking Bad

We have been preparing this release for a long time and managed to add almost everything necessary for the development of Tezos applications. We hope that our library will be useful for the participants of the IDEO hackathon, as well as for all those who are interested in Tezos and Python. We would like to thank Tezos Foundation for supporting this project, and all early PyTezos users for their feedback.

PyTezos is built on three basic principles that we try to follow in any software development:

  1. Everything should work out-of-the-box
    In PyTezos we have predefined RPC endpoints and even demo keys (please don’t abuse) so that you can start playing with little effort.
  2. Programming interface should be intuitive and self-describing
    Aside from docstrings and autocompletion, PyTezos offers inline documentation in the interactive mode (Python console or Jupyter notebook).
  3. Simplicity not at the expense of flexibility
    With PyTezos you can work at any level of abstraction and seamlessly switch between them.

In this article, we will run through the main features, alternatively, you can read the quick start guide, or just explore the API through the interactive console or Jupyter:

>>> !pip install pytezos
>>> from pytezos import pytezos
>>> pytezos

PyTezos client

High-level interaction is carried out through PyTezos client. The first thing you may probably want is to configure RPC node connection and private key used for signing operations.

  • Use .using() method to change the current shell and/or key;
  • Actual objects are accessible through .shell and .key properties;
  • Account balance can be retrieved via .account() method.
Initializing key from faucet file and setting default alphanet provider

Sending operations

Using PyTezos client, you can initialize operation content with minimal information and then .autofill() the gaps. Operations also can be chained, ran, preapplied, and of course, injected. PyTezos now supports:

  • Activate account
  • Reveal
  • Transaction
  • Origination
  • Delegation
Sending a parameterless transaction to an implicit account

Interacting with contracts

The most exciting feature introduced in the update is a high-level interface for interacting with smart contracts. It’s based on the transformation algorithm similar to the one better-call.dev uses (we will sync soon). In practice, you get an ability to work with JSON structures rather than Michelson/Micheline expressions when passing transaction parameters or reading/writing the storage.

  • Get contract interface by address using .contract() method;
  • Access storage and Big Maps with .storage() and .big_map_get() methods respectfully;
  • Call entrypoints by name and pass args/kwargs according to the docstring.

Here is how the parameter scheme for the demo contract looks like:

$parameter:
{ "transfer": [ $address , $nat ] } ||
{ "approve": [ $address , $nat ] } ||
{ "transfer_from": [ $address , $address , $nat ] } ||
{ "balance_of": $balance_of } ||
{ "allowance": [ $address , $address , $contract (pair nat nat) ] } ||
{ "create_account": [ $address , $nat ] } ||
{ "create_accounts": [ [ $address , $nat ] , ... ] }
$balance_of:
{
"address": $address,
"nat_contract": $contract (nat)
}
$address:
string /* Base58 encoded `tz` or `KT` address */
$contract:
string /* Base58 encoded `KT` address */
$nat:
int /* Natural number */

It’s autogenerated and tries to mimic the Tezos RPC docs style.

Accessing contract storage/big_map and calling an entrypoint

What’s next

Our next global step is to build a unit testing framework, where you will be able to write tests in Python and use Python objects as payload for contract invocations and storage initialization. We will also release an update for Babylon protocol in the next several weeks, as well as fixes for the inevitable bugs.

We will host a workshop at the upcoming TQuorum in Berlin, where one can learn how to build apps using PyTezos and ConseilPy. We are waiting for everyone, there will be prizes and of course branded stickers. Take laptops and good mood :)

Follow us on Twitter and join our cozy Telegram chat.

Cheers!


High-level interface for Michelson contracts and not only was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.