Atomic Swap
Two parties exchange assets across chains without an intermediary; either both transfers happen or neither does.
Basically
An atomic swap lets two people trade cryptocurrencies directly. Either both sides of the trade complete, or neither side does.
Key Takeaways
- Atomic swaps are all-or-nothing cryptocurrency exchanges: two parties trade assets across different blockchains without a trusted intermediary, and HTLCs guarantee that either both transfers complete or neither does.
- Timelocks provide the safety net: if one party fails to complete their side of the trade within the deadline, the other party automatically recovers their funds through the refund path.
- Submarine swaps bridge on-chain Bitcoin and Lightning Network channels, enabling trustless loop-in and loop-out operations for channel liquidity management.
What Is an Atomic Swap?
An atomic swap is a peer-to-peer exchange of cryptocurrencies between two parties without a trusted intermediary. The term "atomic" comes from computer science and describes an operation that either completes entirely or has no effect at all. Both sides of the swap settle together, or neither side settles, so one party can never end up having sent coins while the other has not.
Traditional cryptocurrency exchanges require you to deposit funds with a centralised platform that matches orders and settles trades on your behalf. The exchange creates counterparty risk because it could be hacked, freeze withdrawals or disappear. Atomic swaps remove that risk by using Bitcoin Script, or equivalent scripting on other chains, to enforce the swap through cryptographic guarantees rather than trust.
Tier Nolan first described the concept in 2013 on the Bitcointalk forum. The first on-chain atomic swap between Bitcoin and Litecoin took place in 2017, and the technique has since been adapted for cross-layer swaps, decentralised exchange protocols and Lightning Network liquidity management.
How It Works
Atomic swaps rely on a cryptographic primitive called a hash time-locked contract (HTLC). An HTLC locks funds behind two conditions: a hash lock that requires knowledge of a secret preimage, and a timelock that sets a deadline after which the funds can be reclaimed. The same hash on both sides cryptographically links the two transactions.
Step-by-Step Process
Alice holds Bitcoin and wants Litecoin, while Bob holds Litecoin and wants Bitcoin. They execute an atomic swap as follows:
Alice reveals the secret Bob needs to claim the Bitcoin when she claims the Litecoin. Neither party can cheat, because claiming one side exposes the information required to claim the other.
Asymmetric Timelocks
Alice's HTLC has a longer timelock (24 hours) than Bob's (12 hours), and that asymmetry protects both parties. Alice knows the preimage and must claim first; equal timelocks would let her wait until the last moment, claim Bob's LTC, then let her own HTLC expire before Bob can use the revealed preimage. The longer timelock on Alice's side gives Bob enough time to react after she claims.
The party who knows the preimage, the initiator, must have the longer timelock on the contract they created. The extra time gives the counterparty enough time to complete their claim after the preimage is revealed.
HTLC Script Structure
The on-chain HTLC that powers an atomic swap uses a script with two spending paths. These paths handle the claim and the refund:
The first branch, the claim path, requires the counterparty to provide both the preimage that hashes to the embedded value and a valid signature. The second branch, the refund path, becomes available only after the timelock expires and lets the creator reclaim their funds.
Submarine Swaps
Submarine swaps are a specialised form of atomic swap that bridges on-chain Bitcoin transactions with off-chain Lightning Network payments. The name refers to how one leg of the swap "dives" beneath the blockchain surface into the Lightning layer.
These swaps are particularly important for Lightning channel management. Services like Loop and Boltz use them for loop-in and loop-out operations:
The process works similarly to a standard atomic swap. A shared payment hash links the on-chain HTLC to the Lightning HTLC, ensuring atomicity across the two layers. The submarine swaps deep dive explores the mechanism in more detail.
Submarine Swap Flow
A typical loop-out submarine swap proceeds as follows. The steps connect the on-chain and Lightning transfers:
The shared hash ensures that claiming one side reveals the secret for the other. Atomicity is maintained just as it is in a traditional cross-chain swap.
Cross-Chain Trading
The original and most straightforward use case is exchanging one cryptocurrency for another without a centralised exchange. Atomic swaps enable trustless peer-to-peer trading across any two blockchains that support hash locks and timelocks. Supported chains include Bitcoin, Litecoin and other UTXO-based chains with compatible scripting capabilities.
Lightning Liquidity Management
Lightning node operators use submarine swaps to rebalance their channels. A swap can move funds between on-chain and off-chain when a channel's balance becomes skewed, with too much on one side, without closing and reopening the channel. The swaps maintain routing capacity and underpin services like autoloop that automate liquidity management.
Decentralised Exchanges
Atomic swaps underpin several decentralised exchange protocols. Swap-based DEXs match traders peer-to-peer and execute trades via atomic swaps rather than pooling liquidity in a smart contract, as AMM-based DEXs do. Self-custody is preserved throughout the trade, so neither party ever gives up control of their keys.
Cross-Layer Bridging
Atomic swap techniques also move assets between different Layer 2 solutions, beyond Lightning submarine swaps. The core mechanism, a shared hash linking two conditional transfers, generalises to any system that supports HTLCs or equivalent conditional payment primitives. The Bitcoin Layer 2 comparison explains how different bridging approaches compare.
Chain Compatibility Requirements
Atomic swaps require both blockchains to support the same hash function, typically SHA-256, and timelock mechanisms. Chains without programmable scripts or with limited scripting capabilities cannot participate in traditional HTLC-based swaps, which limits the set of assets that can be swapped atomically. Adaptor signatures and other advanced cryptographic techniques, like those used in PTLCs, are expanding the possibilities.
Timelock Coordination
Both parties must be online and responsive within the timelock windows. Alice's funds remain locked until her timelock expires if she initiates a swap but Bob takes too long to create his matching HTLC. Variable confirmation times make safe timelock durations harder to estimate: too short and the swap may fail during network congestion, too long and funds are locked unnecessarily.
On-Chain Costs
Traditional on-chain atomic swaps require at least two transactions per party: one to create the HTLC and one to claim or refund. High fees can make small swaps uneconomical because of these transaction costs. Submarine swaps, which move one leg to Lightning, and Lightning-to-Lightning swaps reduce the on-chain footprint, which helps explain their growing popularity.
Privacy Limitations
Standard atomic swaps that use the same hash on both chains create a cross-chain linkability problem. Anyone observing both blockchains can match the hash values to correlate the two transactions, revealing that the same parties are involved. Newer protocols using Schnorr signatures and adaptor signatures can break this link by using different values on each chain while maintaining atomicity.
Free Option Problem
After the initiator creates the first HTLC, the counterparty can observe price movements before committing their side. The counterparty can walk away if the exchange rate moves unfavourably, leaving the initiator's funds locked until timeout. The "free option" problem is inherent to the two-step setup process and can be mitigated with shorter timelocks, reputation systems or fidelity bonds.
The glossary entry is for informational purposes only and does not constitute financial or investment advice. You should always do your own research before using any protocol or technology.