What is an Atomic Swap?
Atomic swaps are considered among the most decentralized and trustless ways to trade cryptocurrencies across different blockchains because they do not depend on a third-party crypto exchange or a liquidity pool.
How Do Atomic Swaps Work?
Atomic swaps use special smart contracts called hash timelock contracts to facilitate trustless peer-to-peer cross-chain trading. These contracts act as vaults or escrow accounts where the trade participants deposit their tokens. The trade is only executed when predetermined conditions are met, like depositing a certain amount of crypto.
Once the cryptocurrencies are deposited in the hash timelock contract, each party must submit cryptographic proofs in order to execute the contract. If the proofs are not presented within the specified time limit, the smart contract will automatically return the deposited crypto tokens to their respective owners.
Hash Timelock Contract
To get a better understanding of the definition of atomic swaps, let’s read a little more about hash timelock contracts.
A hash timelock contract is a time-bound smart contract that is controlled by a private key and cryptographic hash. In order to execute the contract, the trading parties must meet all the predetermined conditions within a specified time frame otherwise, the deposited tokens will be sent back to their owners.
Atomic Swaps Explained With Examples
Here is an atomic swap example of how cross-chain trading of crypto can be executed in a peer-to-peer manner:
- Beth wants B tokens but has only A tokens. She reaches out to Charlie, who owns B tokens. They agree on the exchange rate of 2:1 and finalize that Beth will give 10 A tokens for Charlie’s 5 tokens.
- Beth creates a hash timelock contract that will expire on 21 September, 1 PM BST, and deposits 10 A tokens. In doing so, Beth receives a private key to the contract. She creates a cryptographic hash of the private key and sends it to Charlie.
- Charlie uses the cryptographic hash to verify whether Beth has deposited the agreed amount of tokens into the contract. Since Charlie does not have the private key to the contract, he cannot access the tokens deposited by Beth.
- Charlie uses the cryptographic hash received from Beth to create a new contract address where he deposits 5 B tokens. Since the new address was created using the hash of Beth’s private key, she can claim the 5 B tokens deposited by Charlie. When Beth claims the funds, the private keys to the contract are revealed to Charlie.
- Charlie can now claim the 10 A tokens deposited by Beth and complete the swap. If Charlie does not complete the transaction within the time limit, the tokens claimed by Beth will go back to Charlie.
If the atomic swap is completed within the time frame, the contract cannot be reverted, and the transaction will be final.
Advantages and Disadvantages of Atomic Swaps
- Removal of third-party risk: Atomic swaps do not involve third-party crypto exchanges or centralized entities for trading. Traders have complete control over the transaction.
- Liquidity for exotic pairs: Traders on crypto exchanges will often find it difficult to trade exotic token pairs due to low liquidity on the exchange. Atomic swaps do not depend on liquidity pools to facilitate trading.
- Lower costs: Users incur hidden costs when using third-party exchanges. Therefore, it is cheaper to trade using atomic swaps than on third-party exchanges.
- Direct swaps: When using decentralized crypto exchanges (DEX), traders often have to convert their tokens to stablecoins or bridge their cryptos in order to complete a desired trade. Atomic swaps eliminate these extra steps.
- Guarantee and security: Atomic swaps are coded to execute only when all the required conditions are met. If the swap is not complete within the specified time frame, the tokens are returned to their owners. However, once finalized, atomic swaps cannot be reverted.
- Complexity: The biggest drawback of atomic swaps is they are difficult and complex to use. Users need to know how to create hash timelock contracts. Conditions such as exchange rates and time limits must be agreed upon beforehand. The majority of crypto users prefer using centralized and decentralized crypto exchanges as it is simple and easy to use.
- Attacks: The crypto world is full of malicious actors looking to make a quick buck. This is a big concern for atomic swaps, since these swaps may take multiple blocks to be completed, an attacker may track the contract addresses and target the trade.
- Cross-chain compatibility: Cross-chain atomic swaps can only be facilitated between two compatible blockchains that use the same hashing algorithm.
Are Atomic Swaps Better Than Bridges?
Atomic swaps are often compared with cross-chain bridges. The two technologies facilitate the movement of cryptocurrencies between different blockchains. They solve the ever-present problem of isolation that blockchain networks suffer from.
However, the way atomic swaps and bridges facilitate cross-chain token transfer is completely different from one another.
Bridges are easy to use. All you need to do is connect your crypto wallet to a cross-chain bridge and pay transaction fees, and you can transfer your cryptocurrencies from one blockchain to another.
The process is so easy that crypto wallets like MetaMask aggregate available cross-chain bridges for users allowing cross-chain swaps directly on the wallet’s interface.
However, bridges suffer from centralization risks and are frequently targeted by hackers. Bridges are often secured by external validators making them risky. As of September 2023, over $2.66 billion worth of cryptocurrencies have been stolen from cross-chain.
On the contrary, the peer-to-peer nature of cross-chain swaps facilitated by atomic swaps makes them the go-to option if security is a user’s biggest concern.
More importantly, atomic swaps allow the transfer of native tokens, whereas bridges facilitate cross-chain transactions by creating wrapped tokens and lock-and-mint mechanisms.
Atomic swap is an underused technology in the crypto industry. As more people become aware of the technology and the crypto community becomes more concerned about security, we could see growth in atomic swap use.
We are already seeing the development of decentralized exchanges that use atomic swap technology, where the exchange uses a decentralized order book and not a liquidity pool to facilitate trades.