An atomic swap uses smart contract technology to swap between different cryptocurrencies without having to rely on an intermediary, such as a centralised exchange.
The concept of an atomic swap was conceived in 2012 by Sergio Demian Lerner with P2PTradeX. The idea of an atomic swap was refined over the years, eventually leading to an improvement of the code, with Decred successfully conducting an on-chain swap with Litecoin and making atomic swaps more accessible by open sourcing their code.
Atomic swaps utilise Hash Timelock Contracts (HTLCs), which are in turn made up of Timelocks and Multi-signature addresses. HTLCs provide; a hashlock (so only the participants of the trade can exchange coins and so no one else can barge in on the trade) and a timelock (to ensure that the trade is finalised by a certain time and that no party can cheat the other).
There are two types of atomic swaps; on-chain and off-chain. On-chain atomic swaps are direct transfers of two cryptocurrencies that are recorded on their respective blockchains. Off-chain atomic swaps are conducted using payment channels and are not recorded on any blockchain until the payment channels are closed.
On-Chain Atomic Swaps
For a trade to be facilitated through an on-chain atomic swap, both parties must submit transactions on their respective blockchains. Let’s suppose that Alice has 1 bitcoin (BTC) and wants to trade it for Bob’s 140 litecoin (LTC).
First, Alice creates a contract on the Bitcoin blockchain::
- Alice specifies the contract’s bitcoin address,
- Alice picks a random number (a ‘secret’),
- And then hashes that secret to produce a ‘lock’,
- Alice then creates a transaction of 1 BTC to the contract’s address,
- And finally sends Bob the lock.
The timelock component of the HTLC ensure that Alice receives her 1 BTC back after a certain time period, say 48 hours. The hashlock part of the HTLC guarantees that only Bob can claim the 1 BTC if he can provide the secret that is associated with the lock set up by Alice. Bob does not yet know the secret since hashing is a one-way function.
Next, Bob must create a contract on the Litecoin blockchain:
- Bob specifies the contract’s litecoin address,
- Then Bob uses the ‘lock’ provided by Alice,
- And finally Bob creates a transaction of 140 LTC to the contract’s address.
This time Bob creates a HTLC on the Litecoin chain, where Alice can only claim the LTC if she has the secret that is hashed to create the lock. The timelock component ensures that Bob can claim his 140 LTC back after 48 hours.
Alice can now use the random number, or ‘secret’, she used earlier to unlock Bob’s contract on the Litecoin chain and retrieve 140 LTC. Since the secret is published to the public ledger, Bob will now be able to see the secret that is hashed to create the lock used by Alice and claim the 1 BTC.
Alice and Bob have exchanged coins without relying on an intermediary. The trade is secure since no one will know the secret until it is too late and both parties have claimed their funds. When Bob receives the lock from Alice, he can verify it is the same lock by looking at the Bitcoin blockchain. The atomic swap is either executed completely or does not happen at all. If Alice does not claim the 140 LTC, then both parties are refunded automatically after 48 hours. Alice cannot claim the LTC without revealing the secret to Bob.
On-chain swaps are lengthy since the payments needs to be confirmed by miners and throughput is limited by the scalability of each blockchain involved. On-chain swaps are not very private either since each transaction is recorded on the public ledger.
Off-Chain Atomic Swaps
For an off-chain atomic swap, payment channels from different blockchains are linked together to transfer value. For instance, the Lightning Network can route payments from Bob to Alice and vice versa since Lightning is supported by both Bitcoin and Litecoin. Payment channels are an off-chain network that construct the Lightning Network.
By connecting two or more users using smart contract technology, payment channels enable transactions to take place without being recorded on the main blockchain. However, once the payment channel is closed, the final balance is mined and added to the main chain.
Payment channels already have a time lock, so adding a hash lock is trivial to set up an off-chain atomic swap. Off-chain swaps are more complicated to perform but are faster and provide more throughput than on-chain swaps. Off-chain atomic swaps also preserve the user’s privacy better, since the swaps are not published directly to the public ledger.
The Future of Atomic Swaps
Off-chain swaps are a promising avenue of further research and development for decentralised exchanges. At the moment, off-chain swaps are complicated but their integration in wallets and exchanges should see more widespread use. The development of the Lightning Network is also a prerequisite for a wider roll-out of off-chain atomic swaps.
A related development is the submarine swap which combines off-chain and on-chain swaps by bridging the Lightning Network to Bitcoin’s blockchain. Submarine swaps use a Lightning invoice in the hashing process instead of an on-chain transaction to transfer bitcoins from a Lightning payment channel to a standard Bitcoin address.