Atomic swaps are a peer-to-peer, trustless method of exchanging coins on different blockchains, without going through a third party service like a crypto exchange.
For example, if I want to exchange BTC for LTC, and you have LTC but want BTC then currently we would both have to use a centralized exchange and sell our cryptocurrencies to buy the new cryptocurrency we want.
But there are problems with centralized exchanges, as noted in my previous blog post here about crypto hacking and insider jobs. Centralized cryptocurrency exchanges hot and cold wallets are the number one target of hackers and inside thieves with at least 2 billion dollars USD stolen from cryptocurrency investors.
Second, mismanagement is also a major problem for example QuadrigaCX CEO Gerald Cotton mismanaged client cryptocurrency funds before shutting down in 2019 - after his supposed death in India. Another example is the Mt. Gox hack where bitcoins worth $500 million were taken largely due to the CEO, Max Karpeles’s, mismanagement.
A third problem with centralized exchanges are the lack of liquidity especially during high volume spikes and sell off. Many people have complained that they can’t buy or sell during key times on smaller exchanges.
The solution is to avoid centralized exchanges.
In 2013, Tier Nolan provided the first fulsome procedure detailing how to complete an atomic swap. Tier Nolan is credited as the inventor of atomic swaps. However, it wasn’t until 2017 that Litecoin’s Charlie Lee completed the first LTC to BTC atomic swap. Soon after Decred and Litecoin completed a cross-chain atomic swap.
Atomic Swaps make use of smart contract technology, used by the Ethereum network. Smart contracts are programmable computer protocols that can be designed to facilitate a variety of functions. Smart contracts are incredibly useful protocols setting out the conditions and are the centerpiece of Atomic Swap functionality.
These trustless transactions utilize a specific type of smart contract called a Hash Timelock Contracts (HTLC). The contract use cryptographic hash functions as a way to verify data. In the case of HTLCs, the hash function is time-sensitive ensuring that the transaction is completed within a set time.
How To Conduct An Atomic Swap
For two people to exchange BTC and Litecoin a hash time-locked contract (HTLC), address is created and coins are deposited into it. The Bitcoin holder creates the address and sends some bitcoin. A unique passcode called a preimage is created for this address with an associated cryptographic hash. This takes place on the BTC blockchain and is immutable.
Once the bitcoin is deposited in the address, the hash is sent to the litecoin holder who then creates his own address using this same hash. The second person then deposits his equivalent amount of LTC into his new address on his respective blockchain. Now that both people and their tokens are ready to be swapped they make the exchange. In order for the BTC holder to retrieve his new LTC, he uses the preimage as a key to open the hash lock. Once the preimage is used it is shared on the blockchain with the second person who now can access their new BTC. Both parties now have their new (swapped) tokens and the transaction is complete.
How Do a Hash Time-Locked Contracts Work?
Atomic swaps use a hash time-locked contract (HTLC), which controls the multisignature addresses and time-locks used in the trustless transaction. This is permitted by the basic scripting language found in Bitcoin and most altcoins, including Litecoin. The term “Atomic” refers to the “all or nothing” nature of the transaction, either the transaction executes fully or not at all.
Off-chain Atomic Swaps are virtually identical in how they function to on-chain swaps; however off-chain swaps differ slightly in that they use second-layer payment channels as a means to communicate between the users.
When conducting on-chain swaps users must wait for miners to confirm both transactions just like any other operation on the blockchain. Often, this means extended wait times. Off-chain swaps use HTLCs to execute trades, with only the end result of the transaction being written to the blockchain, saving time and opening the door for more complex functionality in these smart contract swaps. Due to their quickness off-chain swaps are the favoured method for smaller, one on one trades.
Lightning Network Atomic Swaps for Bitcoin and Other Altcoins
The lightning network is specifically designed for Bitcoin. But altcoins that are forked from Bitcoin’s codebase such as Litecoin, Zcash or Doge are capable of accommodating lightning networks as well. Other altcoins, as long as they have similar or more extensive scripting capacity (such as Ethereum or Ethereum Classic) can use similar coin swapping methods.
Like atomic swaps, the lightning network utilizes hash time-locked contracts. Where atomic swaps effectively link blockchains, the lightning network links payment channels.
Lightning Network (LN) cross-chain atomic swaps (also called off-chain atomic swaps) are instantaneous requiring funds to be committed to the Lightning Network. Besides LTC and BTC working to implement atomic swaps more fully there are two other coins that are worth mentioning that use atomic swap technology: Komodo and 0x.
Komodo (KMD) (coinmarketcap.com rank: #65)
Komodo has been working steadily on the development and implementation of Atomic Swap technology. Komodo’s team was one of the first groups to write the code and develop BarterDEX, the first platform allowing Atomic Swaps. Using Bitcoin-protocol they have completed thousands of Atomic Swaps and have added ETH and DOGE as swap-supported pairs. In mid-2018, Komodo supported over 90% of all available cryptocurrency pairs on BarterDEX, with over 100,000 Atomic Swaps having already been completed using the exchange.
Komodo works by two people who agree to trade, one person places a trade order on Komodo’s DEX. Another person looking to trade accepts the offer and pays the transaction fee (0.15% of the total amount). The other person pays a transaction fee to secure the address. Once the fees are paid the atomic swap is completed. This type of trade is known as a HTLC Atomic Swap, if any portion of the process is not completed in the allotted time frame the entire transaction is cancelled and the coins are returned to the respective owners.
0x (ZRX) (coinmarketcap.com rank: #42)
0x is another project developing Atomic Swap capacity and a decentralized exchange. 0x is an open protocol that enables decentralized cryptocurrency exchanges. The protocol uses smart contracts on the Ethereum blockchain that allows users to interact and exchange tokens without the need for a trusted third party. This smart contract sets out the parameters of the trade that both parties will agree to. This contract sets out what tokens will be traded by each party, what prices, what fees, and when the transaction expires if not completed. The buyer and seller set the trade parameters and then verify them by “signing”. Traders use 0x smart contracts to perform the trade. 0x protocol only works with Ethereum assets, which limits its usage.
On-chain and off-chain Atomic Swaps have huge potential in reorganizing the crypto ecosystem. Decentralization has always been a central ideal and vision of cryptocurrency. While centralized exchanges act as a marketplace for coin buyers and sellers they are fraught with limitations and can put users at risk (eg. hack of hot and cold wallets or bankruptcy of the exchange holding your coins). The development of Atomic Swaps provides another option for users other than using a centralized exchange.
With further development and widespread adoption Atomic Swaps and decentralized exchanges could replace traditional exchanges giving the trading power back to individual users. Currently, cryptocurrencies to be traded through Atomic Swaps must use the same hash algorithm and be HTLC compatible. Moreover, not all wallets accommodate these swaps which currently limit their adoption. The number of Atomic “Swappable” crypto-pairs is increasing. Over time the technology of Atomic Swaps and decentralized exchanges will continue to develop and become more sophisticated.