Writing Tezos app using Python toolset from Baking Bad
Last week we held a workshop at the fantastic TQuorum event in Berlin. Although not everything went smoothly, we hope that we were able to convey the main point: it is super easy to interact with Tezos using our Python toolset.
- Setting up an environment is not necessary, just use cloud Jupyter notebooks;
- Requires a minimum of developer skills, interactive documentation will back you up till the end;
- Pre-populated credentials and keys (demo purposes only) allow to start in seconds.
In this post, we will recall the idea and the scope of the application we developed. We also attached a slightly modified notebook so that you can reproduce the experiment at any time.
Atomic swaps basics
The term “atomic swap” has a fairly general definition, and in this workshop, we will mean the following: two parties exchange two different types of assets, and these transactions must be either successful or unsuccessful.
Atomic swap solves the trust issue between parties. In the real world, it is implemented by introducing a trusted third-party, for instance, a bank or government. In the blockchain universe, we can replace third-party by a smart contract and in the result, we have a fully trustless way to exchange assets.
But as usual, things are not so simple. The swap protocol is interactive, which means that it is not enough to initialize and send transactions correctly, but also to perform some actions in a timely manner.
You can see at the simplified timeline that the swap initiator (Michel) has to act twice with a delay in time. He is also limited to a relatively short interval, and there is a non-zero possibility of losing his funds if for some reason there is no access to the blockchain at that moment.
Good news is that the last task can be fulfilled by a third party while the protocol remains secure. A small reward is paid to the anonymous hero, so there is an economic incentive to secure the atomic swap and a sort of who-is-faster challenge.
We will use an atomic swap implementation by Atomix.me. Let’s give a look at their contract in Tezos alphanet. There are three main entrypoints that change current swap state and optionally transfer funds.
- initiate adds new big_map entry with all data required for the swap protocol,
sha256(sha256(secret))is used as a key;
- redeem removes big_map entry and transfers money to the participant (he needs to know
- refund if something goes wrong you can rollback;
For the sake of simplicity, we will act for two parties and don’t request any data from other blockchains. Here is the plan of what we’ll do:
- Generate a secret, create a new swap using
initiateentry (as initiator);
- Get active swaps i.e. existing big_map entries (as third-party)
- Make a redeem using the secret we generated as if we extracted it from another blockchain.
We are now ready to move on to the practical part, first let’s install all the necessary packets:
!apt-get install libsodium-dev libsecp256k1-dev libgmp-dev
!pip install pytezos conseil
Then we create a new swap using PyTezos.
Here is our operation in better-call.dev explorer:
Next, we need to get all recent big_map entries. We will use ConseilPy to get pairs (block_level, operation_group_hash) so we can request big_map_diff with PyTezos afterward.
Finally, we can make a redeem, but we should do that using another identity (key). Go to https://faucet.tzalpha.net/ and download a json file with key data.
It works! Note that there are two internal transactions and one of them is our tips:
Learn more about our toolset
Python SDK for Tezos, providing functional for querying RPC endpoints, sending operations, interacting with smart contracts, working with cryptographic keys.
Python wrapper for the Conseil blockchain indexer by Cryptonomic providing access to the indexed Tezos blockchain data.
Better Call Dev
Michelson contract explorer displaying transaction parameters, storage and big_map diffs in a convenient format.
Join the first Eastern European Tezos Hackathon devoted to the non-fungible token standard for the Tezos platform. It’s gonna be three incredible days (6–8 Sep) with the Tezos community in Kyiv.
See you there!