Home Glossary
Bybit Learn
Bybit Learn
Aug 25, 2022

Atomic Swap

An atomic swap is an automatic exchange of cryptocurrencies from different blockchains, facilitated by a hashed timelock contract (HTLC), without the need for centralized third-party trading platforms.

An atomic swap refers to peer-to-peer (P2P) trading of cryptocurrencies from separate blockchains that’s facilitated by a time-bound smart contract known as a hashed timelock contract. The atomic swap is a technology based on these smart contracts that allows the exchange to take place without a centralized third-party trading platform once the conditions from both parties have been fulfilled. 

Also known as atomic cross-chain trading, it provides autonomy for crypto users to execute transactions between strangers under trustless conditions, without the risk of either party not fulfilling their respective contractual obligations before the transaction can take place. As indicated in the term “atomic,” the transaction either takes place when all conditions under the HTLC are accomplished, or it doesn’t take place at all. Removing the need for a centralized third-party trading platform to regulate the transaction, an atomic swap achieves a truly decentralized trading process, giving token owners total control over the transaction.

History of the Atomic Swap 

Prior to the development of the atomic swap technique, cryptocurrencies were only traded anonymously through centralized platforms such as Coinbase or Coinsquare. In 2012, the idea of a P2P crypto trading protocol was first outlined by Sergio Demian Lerner in the form of his brainchild, P2PTradeX. However, it wasn’t until a year later that Tier Nolan coined the term “atomic” to describe the transfer of cryptocurrencies bound by a hashed contract, which laid the foundation for atomic swaps. 

The actual execution only took place in 2017, between Decred and Litecoin, and was subsequently replicated for the exchange between Litecoin and Bitcoin. At the time, P2P transactions required both parties to download entire blockchains of the coins for trading, but Komodo soon introduced a simplified version that only required specific payment channels, without having to download the entire blockchain to finalize the transaction. This encouraged many decentralized exchange platforms and independent traders to use the technology for trading cryptocurrencies, bypassing centralized trading platforms.

How Does an Atomic Swap Work?

For an atomic swap to take place, both parties must be involved with the transfer of cryptocurrencies to fulfill predefined conditions for the trade to materialize. This is enforced by an HTLC, which monitors the fulfillment of these conditions within a specific time frame to automate the transaction process. 

The HTLC technology relies on an encryption mechanism known as the hash function. It includes a time limit for both parties to fulfill the conditions of the transaction within the stipulated time frame, or the transaction will be revoked. For example, if both parties have agreed to set a 3-hour window for the atomic swap to take place, the contract will ensure the tokens deposited for the exchange will be returned to their respective owners if the conditions for the trade aren’t fulfilled within three hours. 

With HTLC technology, the smart contract effectively secures the transaction for verification of transfer by both parties within the limited time frame, before the transaction is completed. This requires two key security features, namely hashlock technology and the timelock mechanism. 

The hashlock technology locks the contract with a special encrypted key that provides the depositor access to the deposited tokens of the other party — but only after both parties have deposited their respective tokens and submitted cryptographic proofs of it. The timelock mechanism is simply designed to set a deadline for the atomic swap to happen so that the deposited coins can be returned to the respective owners if the transaction isn’t completed by then, rather than being locked up indefinitely. 

Benefits of an Atomic Swap

With an atomic swap, crypto users can execute the exchange of cryptocurrencies in direct control of their own assets. The benefits of an atomic swap include allowing transactions to take place in a faster, more economical, and flexible manner, without having to go through a centralized custodial crypto trading platform and incur trading fees. It’s also equipped with enhanced security, relying on the automated aforementioned HTLC technology that comes with the assurance of getting their tokens back within an agreed time frame, and avoiding counterparty risks. 

Such a transaction provides autonomy to crypto users, thereby facilitating a transition toward a more interoperable, interconnected crypto ecosystem where transactions can take place across blockchains.