Atomex decentralized exchange: cross-chain atomic swaps on practice

Guide on what is Atomic Swap decentralized exchange with Tezos coin and Ethereum. From theory to practice with XTZ and ETH examples.

Alex
Alex   Follow

A thorny developing process is starting to bear its first fruits: Atomex - cross-chain Atomic Swap exchange - has been finally released in the mainnet. Our team has made up an exciting journey from the pure atomic swap protocol to the fully-fledged application, and now we can say “Yes, it works!”.
We want to thank all the early testers for their feedback, it is very important for us!

# The idea behind

Our goal is to create a product for crypto enthusiasts, for those who believe in ideas behind the blockchain technology and who is paranoid in a good sense.

If TCP/IP is a protocol used to transfer information on the Internet, the blockchain is the same for e-money and other classes of assets. However, there is still an interchain gap which leads to an inconvenient value separation.
The atomic swap protocol which we will examine a little further effectively closes that gap in a trustless way, relying on an essentially simple idea. Banks or another trusted third-parties are being replaced by a smart contract, and this is one of the few cases when it can be done with just the data available on-chain (without oracles or other off-chain data providers).

# Atomic swap exchange basics

Before we move on to the practical part, let’s dive a bit into the protocol details. Let’s say Alice wants to buy some ETH for XTZ and Bob has agreed to make a deal. There are two possible outcomes, either the transaction will take place or will be canceled.

# Initialization phase

  1. **Alice locks XTZ
    **Alice generates a random bytes sequence — the secret, calculates its hash and sends her XTZ to a special smart contract together with the hash, a lock_time_a value, and Bob’s XTZ address_b
    These XTZ can only be redeemed by the owner of theaddress_b if he knows the secret, and it has to be done before the lock_time_a, afterward, Alice can get these funds back.
  2. Bob locks ETH
    When Alice’s transaction is confirmed Bob sends his ETH to a special smart contract together with the same hash, lock_time_b, and Alice’s ETH addreess_a
    These ETH can only be redeemed by the owner of theaddress_a if she knows the secret, and it has to be done before the lock_time_b, afterward, Bob can get these funds back.

Requirement: lock_time_b < lock_time_a
To prevent the case when Alice first makes a refund and then redeems Bob’s funds.

# Successful redeem

  1. Alice redeems ETH
    Alice sends the secret to the contract in Ethereum blockchain and receives Bob’s ETH in return.
    The secret is revealed now and Bob can spend the locked XTZ.
  2. **Bob redeems XTZ
    **Bob sends the secretto the contract in Tezos blockchain and receives Alice’s XTZ in return.

# Refund case

We will not consider the cases when something goes wrong during the initialization phase (they are rather trivial) but will examine the case when Alice doesn’t redeem Bob’s ETH for some reason, i.e. secret is not revealed and lock_time_b has passed.

Simplified scheme of an XTZ/ETH swap (refund case)

  1. Bob refunds ETH
    Bob sends hash to the smart contract in Ethereum blockchain and gets his ETH back.
    He cannot spend Alice’s XTZ since secret is not revealed.
  2. Alice refunds XTZ
    Alice waits until lock_time_a, then sends the hash to the contract in Tezos blockchain and gets her XTZ back.
    She cannot spend Bob’s ETH because lock_time_b has already passed.

# That’s enough theory

Time to see Atomex in action! We’ll start from a multi-crypto setup with some XTZ and ETH on our balance. With regard to the above scheme we are Bob, and our counterparty is Alice.

# Create new DEX conversion

Choose the “Conversion” tab and set the ETH amount we want to exchange for XTZ. Check the deal terms and then press “Convert”.

Step 0. The user creates a new conversion

A new сonversion appears in the table with the status “In Progress”.
Actually, that’s all we need to make an atomic swap through Atomex wallet. But let’s follow the internal process spawned by our actions.

# Under the hood: Alice locks XTZ

First, check out recent operations for the Atomex smart contract in the Tezos blockchain:

Step 1. Alice locks XTZ

This is a transaction sent by Alice, these locked funds are going to be ours in the end. Mind the hash field passed via parameters.

# Under the hood: Bob (us) locks ETH

Once the XTZ transaction is confirmed, Bob sends an ETH transaction. We can see that it contains the same hash :

Step 2. Bob locks ETH

# Under the hood: Alice redeems ETH

Another contract invocation from Alice’s side. She receives our ETH in exchange for secret and Bob picks it up:

Step 3. Alice redeems ETH

# Under the hood: Bob (us) redeems XTZ

Finally, Bob unlocks XTZ using the collected secret :

Step 4. Bob redeems XTZ

The status of our conversion changes to “Completed”, everything is ok.

# Side notes

All the internal actions you saw are performed by the Atomex wallet in the background process so it’s necessary to keep it online until the swap is done. In our example of an XTZ/ETH conversion, it takes only 8 minutes for the whole thing, however, it depends on the particular blockchain confirmation time.

We are currently working on making different order types available. As soon as the Atomex swaps market will be consistent enough it will be possible to place limit orders through the Atomex app.

# How to set up Atomex wallet

Check out our tutorial.

Any feedback is highly appreciated, if you have a feature request or a bug report please welcome to our Telegram chat. And don’t forget to follow us on Twitter!

Cheers!