An explanation of an atomic swap protocol on a decentralized cryptocurrency exchange (with Komodo BarterDex as an example)
An atomic protocol has two parties. Typically, they are called a liquidity provider and liquidity receiver. They may also be called buyers and sellers.
On the BarterDex network, once an atomic swap process starts, a description of the behavior of the parties is recorded and added to their public profiles on the BarterDex network.
The process starts with the receiver in the swap making an initial request. In most of the examples that describe how the atomic swaps work you will see names of the parties that start with A and B and this article will not be an exception. The name that starts with B (usually Bob) represents a person who has funds in Bitcoin, hence the letter B. The name that starts with A represents a person with holdings in Altcoins, hence the letter A.
Let’s say that the receiver in the atomic swap is Alice and Alice is making the initial request. To complete a swap, Alice will need two UTXOs in her cryptocurrency wallets. One of them would go towards the exchange transaction and the second one would need to cover the fee for the transaction. The fee is about 0.1% of an exchange order that goes through BarterDex. The main reason for the fee is to protect the network from spamming transactions. This is the same reason why many other cryptocurrency networks require their users to either pay a small fee to send money on the network, to do some work such as Bitcoin’s proof-of-work, or to have a balance in the account. If these requirements were not in place, attackers could easily spam the networks.
The second UTXO that Alice would need to have in her wallet would be the UTXO that would go towards a currency swap.
Bob, the liquidity provider, is on the other side of the swap. Typically, liquidity providers will be able to see requests on the network and respond to the request.
A liquidity provider also needs to have two UTXOs in his wallets. The sizes of these UTXOs, however, will be very different from the ones that Alice needs to have. Bob’s first UTXO needs to be 112.5% of the amount of the trade he wants to make. The second UTXO needs to be 100% of the trade. In total, these two UTXOs that liquidity providers need to have, equal to 212.5% of the trade, which is one of the reasons why they are called providers of liquidity in the first place. The 112.5% UTXO then becomes a security deposit with BarterDex. The network holds the deposit visible, yet it is not possible for anyone to move the funds. The funds will remain in escrow until Bob completes all the steps he needs to perform on his end for the transaction to go through or until the request from Alice expires. Assuming Bob follows the process, he will get his funds back automatically when the process is over.
The second UTXO, the one that is equal to 100% of the transaction, remains in his wallet for now.
The next step of the atomic swap is for the BarterDex network to establish a connection between Alice and Bob. This is the most difficult part of the process that BarterDex technology solves because factors that determine whether a transaction goes through or not include human psychology and motivation, experience and technical knowledge that users possess, economic factors, hardware setups of the participants of the trade, different Internet connections and providers, and more.
Assuming that the network has established a secure connection, the process from hereon is actually very simple. Alice makes a swap request and sends the fee for the transaction to the full nodes on the network. The nodes publish the request to the network. Bob sees the request, decides to reply to it, and sends out the deposit. The network then holds the deposit. Next, Alice sends the funds to Bob, but she does not do that directly. The funds go through a temporary BarterDex wallet. The only person to have access to the wallet is Bob. However, Bob doesn’t get access to the wallet right away. His next step in the process is to send the 100% UTXO to Alice. Just like with the funds that Alice sent Bob, funds that Bob is sending to Alice go to yet another special BarterDex wallet. Finally, Alice moves the funds from the wallet, BarterDex notes that Alice has spent the funds, allows Bob to move the money out of the wallet that he has access to and then refunds Bob his deposit. While some of the steps in the process may seem too complex and redundant, they are necessary in order to protect the safety of BarterDex users and their funds.