What Are Atomic Swaps? The Most Comprehensive Guide Ever!
Atomic Swaps has the potential of completely revolutionizing the money transfer system in the crypto world. To put it in simple terms, atomic swaps will enable people to directly trade with one another wallet-to-wallet.
Since 2012, the concept of a trustless, peer-to-peer cryptocurrency has been a pretty hot topic. In July 2012, a developer by the name of Sergio Demian Lerner created the first draft of a trustless exchange protocol. The idea was pretty appealing, however, it wasn’t really fleshed out.
The breakthrough in atomic swap research happened around May 2013, when TIer Nolan provided the first full account of a procedure for atomic swaps. Tier Nolan is widely credited as the inventor of atomic swaps.
In this guide, we are going to look into how atomic swaps work and the advantages that they are going to bring into the ecosystem.
Problems With Centralized Exchanges
Suppose Alice has Bitcoin and wants to sell them for Litecoin. Similarly, we have Bob who has Litecoin but wants some Bitcoin instead. Normally what would have happened is that both of them would have had to go a centralized exchange, sell their cryptos and buy newer cryptos. However, there are a lot of problems with these exchanges.
#1 Hack Vulnerability
Centralized exchanges always have the risk of getting hacked. Probably the most infamous example of this is Coincheck which got hacked for $550 million worth of NEM. The worst part is that this hack greatly reduced crypto sentiment in Japan, a country that was traditionally known to be very crypto friendly.
#2 Subject to Mismanagement
The infamous Mt. Gox hack where bitcoins worth $500 million were robbed happened directly as a result of CEO Max Karpeles’s inept management. As Andreas Anatopoulos put it:
“Magic The Gathering Online Exchange (Mt. Gox) is a systemic risk to bitcoin, a death trap for traders and a business run by the clueless.”
#3 Volume Demands
Exchanges can’t deal with changes in demand, especially when there is a sudden increase in demand. Do know why BCH’s value nearly went down by half on 12th November?
Turns out, there was a sudden rise in demand and most exchanges couldn’t cope. Bithumb, in particular, suffered 90 mins of downtime and lost 60,000 BTC in volume.
#4 Subject to Government Regulation
Because the centralized exchanges are registered in particular countries, they are subject to the whims of the government,
Because of the reasons stated above, centralized exchanges are not the ideal way to go forward for mainstream adoption.
What Are Atomic Swaps?
Atomic swap is a peer-to-peer exchange of cryptocurrencies from one party to another, without going through a third party service like a crypto exchange. During this entire process, the users have full control and ownership of their private keys.
On September 20, 2017, Decred and Litecoin did the first known successful implementation of the atomic swap.
Another interesting thing to note about atomic swaps:
- They can either be directly executed between separate blockchains with different native coins
- Or, they can also be executed via off-chain channels that are offshoots of the main blockchain.
Atomic swap is also known as cross-chain trading.
How Does Atomic Swaps Work?
To give a very simplistic explanation. Two parties who are going to engage in atomic swaps decide on a shared secret. The two parties will share their cryptos if and only if their secrets match. So, this way, if somebody else barges into this exchange, they won’t be able to get their hands on any of the coins because they will not know this secret.
Ok, so now you know the concept, but how does it actually work?
In order to execute this, something known as Hashed Timelock Contracts or HTLCs are used. If you are familiar with the lightning network then you should know how hashed timelock contracts work. Right now we will just give you a brief description of what hashed timelock contracts are.
What are Hashed Time Contracts?
Hashed timelock contracts are a special form of payment channels. Payment channels are basically off-chain state channels which deal with payments.
A state channel is a two-way communication channel between participants which enable them to conduct interactions, which would normally occur on the blockchain, off the blockchain. What this will do is that it will decrease transaction time exponentially since you are no longer dependent on a third party like a miner to valid your transaction.
So what are the requirements to do an off-chain state channel?
- A segment of the blockchain state is locked via multi-signature or some sort of smart contract, which is agreed upon by a set of participants.
- The participants interact with each other by signing transactions among each other without submitting anything to the miners.
- The entire transaction set is then added to the blockchain.
The state channels can be closed at a point which is predetermined by the participants. Closing can happen because of one of the following reasons:
- Time lapsed eg. the participants can agree to open a state channel and close it after 2 hours.
- It could be based on the total amount of transactions done eg. close the chain after $100 worth of transactions have taken place.
Hashed timelock contracts or “HTLCs” are one of the most convenient applications of the payment channels.
So, what is an HTLC?
Earlier iterations of payment channels which use “timelocks”. An HTLC “extends” that by introducing “Hashlocks” along with the timelocks.
The HTLC enables opening up of payment channels where funds can get transferred between parties prior to a pre-agreed deadline. These payments get acknowledged via the submission of cryptographic proofs. Along with that, another brilliant feature of the HTLCs is that it allows a party to forfeit the payment given to them and return it to the payer. The idea is to use a multisignature transaction system that holds both traders accountable for a swap to be successful.
Atomic Swaps: Underneath the Hood
Take a look at the image above which has been taken from Coin Central. Let’s see what is going on here.
- Alice has some BTC and Bob has some LTC. They want to swap the coins with each other.
- The two of them then open up a payment channel. The instigator of this swap (let’s say Alice) then creates a contract address
- He contract address is like a multi-lock safe that takes care of both of their funds
- By creating the address, Alice deposits her BTC and produces a value as well.
- The value acts like the key while the hash generated from it will pretty much act like a lock for the safe
- Upon doing this, Alice then sends the hash to Bob.
- Bob generates a contract address using the hash that has been given to him by Alice
- Bob sends his LTC to this contract address
- Only Alice can unlock the litecoin in this address because she has the value which generates that particular hash.
- Alice can get her LTC by signing a transaction for Bob’s contract address and Bob can retrieve the BTC by signing a transaction for Alice contract address
- However, until now, Bob doesn’t know the value that generates the hash. So, how exactly is he going to unlock the address?
- When Alice signs Bob’s contract address with the value, she unlocks the address and reveals the value to Bob as well.
- Bob, now knowing the value, signs off the transaction for Alice’s address and retrieves his BTC
So, to summarize the process.
Alice creates the value and generates its hash which is used to create the contract address and deposits her BTC there. She then sends the hash to Bob. Bob then generates the contract address through the hash and sends his LTC there. In order to get her hands on the LTC, Alice will have to unlock the address by using her value. Upon unlocking and getting her coins, the value of the key is given to Bob. Bob then uses the value to get his coins.
Image Credit: @n1ckler
On-Chain and Off-Chain Atomic Swaps
Atomic swaps, as we have stated earlier, can either be conducted on-chain or off-chain.
On-Chain Atomic Swaps
What Decred and Litecoin achieved was an on-chain atomic swap. On-Chain swaps takes place on either currency’s blockchain. In order to do this though, both currencies must:
- Support HTLC
- Have the same hashing algorithm
Off-chain swap, on the otherhand, allows you to do atomic swaps off the blockchain. These basically take place on what is more commonly known as “layer 2”, Bitcoin and Litecoin engaged in the first ever off-chain atomic swap in November of 2017 by using the Bitcoin Lightning Network.
How Different Exchanges Achieve Atomic Swaps
Different companies have different ways of executing the atomic swaps.
Komodo is a decentralized exchange and it is impossible to understate their contribution to the atomic swaps research. In fact, let’s take a tiny detour and go back in time. Just a year after Nolan presented the idea of atomic swap protocols, Komodo’s Lead Developer jl777 wrote the code that allowed some of the very first swaps.
In the beginning, this code only allowed atomic swaps between NXT assets. However, jl777 then upgraded the code to enable NXT assets to be exchanged with any Bitcoin-protocol coins.
During summer 2017, Komodo took the next step towards mainstream adoption of atomic swaps. They developed BarterDEX, which was the first GUI for a fully atomic-swap-powered crypto trading marketplace. Komodo soon began integrating dozens of Bitcoin-protocol coins and publicly performing thousands of atomic swaps on BarterDEX.
The month after the Litecoin-Decred atomic swap, Komodo came up with a method of doing atomic swap trading with Electrum servers.
After that, in Feb 2018, Komodo managed to connect Ethereum and Bitcoin-protocol coins with a swap between ETH and DOGE. This way, Komodo was the first blockchain project to achieve this feat.
By March 2018, Komodo’s atomic swap technology supported trades between 95% of all coins and tokens in existence. At the time of writing, over 110,000 atomic swaps have been performed on BarterDEX, Komodo’s decentralized exchange (DEX).
How Komodo Works
Suppose Bob has BTC and would like some KMD (Komodo tokens) in exchange. Similarly, Alice has KMD and wants some BTC in return. How is this atomic swap going to work out?
- Bob first posts a trade order on Komodo’s DEX platform
- Alice sees the offer and accepts it
- Alice then commits to the trade by paying 0.15% of the total trade amount as atomic swap fee. Bob hasn’t paid any transaction fees yet
- Once she pays the fee, the atomic swap begins
- Bob then sends his deposit to secure the address. No one will have access to these funds until the time period of the trade has either run out or the trade gets completed. The deposit must be 112% of the amount of the order that was originally posted
- Alice sends her KMD tokens to another secure address. Same as Bob, nobody has access to it
- If the trade falls apart, then the swap will be timed out and canceled. When this event happens, Bob gets his BTC back and Alice gets her KMD back
- Otherwise, Bob sends his BTC payment to Alice and finishes his part of the deal.
- After Alice claims Bob’s payment, Bob gains the ability to get Alice’s KMD payment
- The process is now complete.
Blockchain.io is implementing atomic swaps by combining centralized and decentralized components. Their model is made to encourage trade while entrusting trust.
The order book is going to be centralized to ensure high liquidity and also offers the user a choice to use atomic swaps for decentralized trade settlement.
The coins that have to be traded are locked up in a escrow smart contract, which can be freely traded. The trades happen centrally on the exchange’s platform. When the trade is done, they cash out.
Advantages of Atomic Swaps
- Interoperability between the different assets is a huge problem right now in cryptocurrencies. Atomic swaps are going to bring users of all these different coins together to help them interact with each other.
- Atomic swap makes the crypto ecosystem more “currency agnostic”. Because people with different crypto holdings will now be able to interact with each other, it is pretty likely that people will be more open to diversifying their holdings instead of just depending on a few coins.
- Atomic Swaps will open the doors to trustless and fee-less decentralized exchanges.
- Central exchanges are centralized and hence open to a host of attacks. Atomic swaps remove the need for having a 3rd party and makes the trade as direct as possible.
- External attacks aside, centralized exchanges are also suspect to internal maintenance issues and corruption. Wallet maintenance” or disabled withdrawals are especially two big problems. Atomic swaps are going to give you complete control over your money.
- Direct wallet-to-wallet trading epitomizes decentralization in its purest form. Exchanges are constantly targeted for regulation purposes which makes the whole trading process increasingly centralized.
- Since atomic swap directly connects two wallets to each other, it removes all the steps and confirmations required by centralized exchanges. It is a faster option.
- One of the best features of cross swap is the removal of intermediary tokens. Eg. if you have LTC and want to buy Decred in a normal exchange, you will have to sell your LTC for BTC and then buy your decred tokens. Via atomic swaps, you can get this trade done in one go.
- Exchange usually levy a lot of fees and charges, especially when you are trying to withdraw your coins back to your wallet. Some exchanges also have pretty questionable fee-structures.
Limitations of Atomic Swaps
The first limitation that atomic swaps face in its current iteration is that three conditions need to be met for two cryptocurrencies to engage in atomic swaps.
- The cryptocurrencies must have a hash algorithm which is inherent to both of them
- Both the cryptos must be capable of initiating hashed timelock contracts
- Must have specialized programming functionalities.
As of right now, these characteristics are going to greatly limit the number of cryptos that can actually take part in these swaps. However, that’s not the worst part.
It will, unfortunately, also limit the number of companies and users that can experiment with them right now. This will increase the time it will take the general masses to get used to this new technology.
Yes, we realize that we have listed “speed” as one of the advantages, but here us out.
In its current iteration, atomic swap still needs tons of refinement and enhancement before it becomes fast enough to handle large volumes of data. This one area where the lightning network can aid atomic swaps in a major way.
#3 Lack of Compatibility
It is true that more wallets are coming out which are going to adopt atomic swap technology, however, the fact remains that the total number of compatible wallet and exchanges is still really low. Greater support from more exchanges will inevitably lead to more broad scale use and research.
Lack of scalability and interoperability are two of the biggest problems that the crypto world is facing as of right now. With atomic swaps, we have a solution which can kill two birds with one stone. This is a piece of technology which has the serious potential of taking us to the next evolution of crypto trading. Let’s hope that more research work and adoption takes place in the near future.
Get started today and earn 4 bonus blocks
Already have an account? Sign In