Atomic tips

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.

PAIR %Python %Tezos ;

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

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.

Drastically simplified XTZ/ETH swap timeline

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.

Implementation details

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;

is used as a key; redeem removes big_map entry and transfers money to the participant (he needs to know secret );

); 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 initiate entry (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.

Practical work

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: