What Is an Atomic Swap?
Atomic swaps enable peer-to-peer exchanges of crypto assets across separate blockchain networks. Through the use of a “virtual vault” known as a time-bound smart contract, funds can only be unlocked when both parties deposit the correct amount of assets.
Securely moving assets across various blockchain networks is critical for unlocking trapped liquidity and attracting more users to the space—while maintaining Web3’s core value of trust-minimization. Atomic swaps are a way for two people to trade tokenized assets across different blockchain networks without relying on a centralized intermediary to facilitate the transaction. This provides DeFi users with a way to maintain high levels of decentralization as they move across the multi-chain Web3 ecosystem.
<div class="educational-divider sections-divider"></div>
What Are Atomic Swaps?
Atomic swaps enable peer-to-peer exchanges of crypto tokens across different blockchain networks that only execute if both parties each deposit a predetermined amount of tokens to the exchange contract. This enables any two users to swap digital tokens without relying on a third party to facilitate the transaction—thereby reducing counterparty risks.
“Atomicity” is a software term that refers to database transactions that only execute in full or not at all. Named accordingly, atomic swaps either receive the necessary token deposits from each user and perform the swap or return all deposited tokens to their original owner.
<div class="educational-divider sections-divider"></div>
How Do Atomic Swaps Work?
Atomic swaps use a hash timelock contract (HTLC) which acts as a “virtual vault” or “cryptographic escrow account” that keeps user funds safe and only executes when the correct amount of tokens has been deposited to the contract. Each user must acknowledge receipt of tokens within a specified interval to unlock them.
Hashed Timelock Contract (HTLC)
An HTLC is a time-bound smart contract where a private key and cryptographic hash are used to control access to funds. Each party must meet all of the swap agreements for it to be finalized, otherwise, tokens revert to their original owner.
An HTCL consists of two core security features:
- Hashlock key—Both parties must submit cryptographic proofs verifying that they have met their side of the swap contract.
- Timelock key—If the proofs are not submitted within a preset time limit, the deposited coins are returned to the original owner.
Example of an Atomic Swap
- Alice agrees to swap 10 X tokens with Bob for 10 Y tokens. They create an HTLC that will expire in one hour.
- Alice creates a contract address and deposits her 10 X tokens to it. This generates a private key that only Alice has access to. Alice creates a cryptographic hash of the private key and sends it to Bob.
- Bob uses the hash to verify that Alice has deposited 10 X tokens to the contract address. Bob cannot access the funds because he only has the hash, not the actual private key.
- Bob uses the hash to generate a new contract address where he deposits his 10 Y tokens. Now both parties have deposited their funds to the contract.
- Because Bob created the address using the hash of Alice’s private key, Alice is able to claim the 10 Y tokens Bob deposited. She does this and, in the process, reveals the private key to Bob. If Bob does not complete the transaction before the timelock expires, the tokens claimed by Alice will revert to Bob.
- Now Bob uses the private key to withdraw the 10 X tokens and finalize the transaction.
- With the swap completed within one hour, the contract cannot revert and Alice has successfully swapped her 10 X tokens with Bob for his 10 Y tokens.
<div class="educational-divider sections-divider"></div>
Advantages of Atomic Swaps
Atomic swaps offer traders benefits that are unavailable from some other solutions.
- Reduce counterparty risks—There is no need to entrust funds to a centralized third party to facilitate the transaction. Traders maintain complete control over their assets.
- Deeper liquidity—Making assets tradeable across different blockchain networks makes those assets more liquid.
- Direct asset-to-asset swaps—Users can buy crypto assets directly without needing to first swap to a highly liquid stablecoin and make multiple transactions. Users can trade any token they like in a decentralized environment.
- Guaranteed outcome—Traders have guarantees that their contract will execute as described or they will receive their funds back.
- Lower costs—Peer-to-peer swaps can incur lower fees than relying on a third-party administrator.
<div class="educational-divider sections-divider"></div>
Disadvantages of Atomic Swaps
DEXs and centralized exchanges remain in high demand due to some of the cons of using atomic swaps to trade tokenized assets.
- Ease-of-use—Each counterparty must agree on the amount and price of the transaction, the length of the timelock, exchange data, and hashes, and wait for transactions to be processed. This may become a timely and complex process that can be especially difficult for beginners.
- Privacy concerns—Atomic swaps take multiple blocks to be completed, which can alert malicious actors, giving them time to track addresses and target traders.
- Compatibility—You cannot perform atomic swaps across all blockchain networks. Each blockchain must use the same hashing algorithm for atomic swaps to work.
<div class="educational-divider sections-divider"></div>
Atomic Swaps vs. Bridges
While atomic swaps enable the peer-to-peer exchange of native assets, cross-chain bridges provide a connection between blockchains. Bridges can facilitate the transfer of wrapped assets via locking/unlocking or minting/burning mechanisms. To date, over $2.6B has been lost in exploits due to cross-chain bridge hacks, which is why the Web3 ecosystem is rapidly adopting a superior cross-chain solution.
<div class="educational-divider sections-divider"></div>
Cross-Chain Communications Powered By Chainlink CCIP
Traders and protocols can overcome the limitations of both bridges and atomic swaps by using Chainlink CCIP, the cross-chain interoperability standard that enables secure, efficient, reliable, and programmatic token transfers across various blockchains—including those that use different hashing algorithms. For example, Synthetix is using CCIP to power its Synth Teleporters, enabling liquidity to flow seamlessly across its multichain ecosystem. Explore how CCIP unlocks Web3’s cross-chain future.