An atomic swap is an exchange of crypto assets through linked conditional transactions designed so that either both parties can complete their sides or each can recover the asset it originally locked. A common cross-chain design uses hash time-locked contracts, or HTLCs, with a shared secret and different refund deadlines. It reduces reliance on a custodial exchange, but it does not make execution instant, free, private, or riskless.
Key Takeaways
- A cross-chain atomic swap normally involves multiple transactions on two blockchains, not one indivisible transaction.
- A hashlock requires disclosure of a secret; a timelock creates a refund path if the swap is not completed.
- The refund deadline on the initiator’s chain must generally be later than the counterparty’s deadline so both parties have time to react safely.
- Each participant still faces network fees, price movement, software risk, confirmation delays, and the operational risk of missing a deadline.
- Compatible scripting features do not create liquidity. Two parties must still agree on the assets, quantities, timing, and exchange rate.
How an HTLC Atomic Swap Works
Assume Alice wants to exchange Asset A for Bob’s Asset B.
- Agreement: Alice and Bob agree on the assets, amounts, required confirmations, and refund deadlines.
- Secret creation: Alice generates a random secret and shares its cryptographic hash, but not the secret itself.
- First lock: Alice locks Asset A in a contract that Bob can claim by revealing the secret before a later deadline. Otherwise, Alice can refund it after that deadline.
- Verification: Bob verifies the amount, contract code, hash, timeout, and confirmation status.
- Second lock: Bob locks Asset B under the same hash, but with an earlier refund deadline.
- Alice redeems: Alice claims Asset B by publishing the secret on Bob’s blockchain.
- Bob redeems: Bob learns the secret from that transaction and uses it to claim Asset A before Alice’s later refund deadline.
- Refund path: If Alice never redeems Asset B, Bob can refund after his deadline, followed later by Alice’s refund on her chain.
sequenceDiagram
participant Alice
participant ChainA as Asset A chain
participant ChainB as Asset B chain
participant Bob
Alice->>ChainA: Lock Asset A with hash H and later refund time
Bob->>ChainA: Verify contract and confirmations
Bob->>ChainB: Lock Asset B with H and earlier refund time
Alice->>ChainB: Redeem Asset B and reveal secret S
Bob->>ChainB: Read S from redemption
Bob->>ChainA: Redeem Asset A using S
The sequence is atomic only in the protocol sense that the conditions give each party a completion or refund route. The blockchains do not share one consensus event, and software must monitor both chains and act before the relevant deadlines.
Hashlocks and Timelocks
A hashlock allows spending when a party supplies a secret whose hash matches the value committed in the contract. The secret is often called a preimage. Revealing it on one chain gives the counterparty the information needed to redeem on the other.
A timelock prevents the refund path from being used until a specified block height or time. Unequal deadlines are essential. If both refunds became available simultaneously, Alice could reveal the secret near expiry and leave Bob too little time to claim Asset A.
Contract details differ among networks. Time may be measured by block height or timestamp, and block production is not perfectly predictable. A safe implementation must account for confirmation requirements, chain reorganizations, fee spikes, and the time required to construct and confirm a redemption or refund.
Worked Example: Unequal Refund Windows
Suppose Alice and Bob agree to exchange 1.00 unit of Asset A for 20.00 units of Asset B.
| Event | Illustrative time | Economic effect |
|---|
| Alice’s Asset A lock confirms | Hour 0 | Alice has committed 1.00 A subject to the contract |
| Bob’s Asset B lock confirms | Hour 3 | Bob has committed 20.00 B after checking Alice’s lock |
| Bob’s refund becomes available | Hour 24 | Bob can recover 20.00 B if Alice did not redeem |
| Alice’s refund becomes available | Hour 48 | Alice can recover 1.00 A if Bob did not redeem it |
If Alice redeems at hour 10, the secret becomes visible and Bob has substantial time before hour 48 to redeem Asset A. If Alice does nothing, Bob can recover Asset B at hour 24; Alice must wait until hour 48 to recover Asset A.
The amounts returned may be lower after network fees. Both parties also bear opportunity cost while assets are locked. The time values above are illustrative, not safe settings for a particular blockchain or implementation.
What Must Be Compatible
An atomic swap requires more than two blockchains using the word smart contract. The implementation must align on:
- compatible hash functions or a design that securely links the conditions;
- transaction scripting capable of enforcing hash and time conditions;
- timeout units and enough separation between deadlines;
- transaction formats, signing rules, and fee management;
- software that can monitor both chains and extract the revealed secret;
- supported assets and networks, including token-contract details where relevant; and
- sufficient liquidity and a counterparty willing to accept the quoted rate.
Some protocols use adaptor signatures or other cryptographic constructions rather than classic HTLCs. Those designs can change privacy, compatibility, and failure assumptions, but the same analytical question remains: what ensures completion or recovery on every execution path?
Atomic Swap, Exchange Trade, or Bridge
| Method | Primary purpose | Who controls assets during execution? | Main failure exposure |
|---|
| Atomic swap | Exchange assets under linked conditions | Conditional contracts controlled by the agreed scripts | Contract error, timeout failure, chain delay, or counterparty non-completion |
| Centralized exchange trade | Trade using a venue’s account and execution system | Exchange or custodian usually controls deposited assets | Custody, insolvency, account restriction, outage, and execution risk |
| Decentralized exchange swap | Exchange assets through smart contracts, often on one network | User and protocol contracts | Contract, token, oracle, liquidity-pool, and transaction-ordering risk |
| Cross-chain bridge | Represent or move value between networks | Bridge contracts, validators, signers, or custodians | Bridge compromise, invalid minting, custody failure, or depegging |
An atomic swap exchanges ownership of two assets. A bridge usually creates, releases, or recognizes a representation of an asset on another network. The terms are not interchangeable.
Pricing and Liquidity
Atomicity addresses settlement conditionality, not price quality. Before locking funds, parties still need an exchange rate. The quoted price can be poor if there are few counterparties or if one side embeds compensation for volatility, network fees, or waiting time.
There may be no central order book showing competing bids and offers. A swap service can also introduce an intermediary at the discovery or interface layer even when the contracts themselves are non-custodial.
Risks and Limitations
- Implementation risk: incorrect scripts, secret handling, signing, or refund construction can lock or lose assets.
- Timeout risk: a participant who fails to monitor a chain or act promptly may lose the protection expected from the protocol.
- Chain risk: congestion, reorganizations, unpredictable block intervals, or fee spikes can delay a critical transaction.
- Price risk: market prices can move while contracts wait for confirmations or refunds.
- Liquidity risk: a compatible protocol may have few counterparties or poor rates for the desired size.
- Privacy leakage: linked amounts, timing, contract patterns, and a shared secret can help observers associate activity across chains.
- Asset risk: a token can have issuer, contract, freeze, blacklist, or network risks unrelated to the swap mechanism.
- Counterparty disruption: the other party can stop after the first lock, forcing funds to remain unavailable until refund.
- Interface risk: software, routing services, or quote providers can misstate transaction data even if the underlying contracts are sound.
- Legal and tax risk: non-custodial execution does not remove sanctions, reporting, tax, securities, or other legal obligations.
How to Evaluate an Atomic Swap
- Confirm the exact asset contracts and blockchain networks.
- Verify the source and audit status of the implementation without treating an audit as a guarantee.
- Inspect both amounts, hash conditions, recipient keys, and refund paths before funding.
- Confirm that the timeout order leaves a defensible response window under stressed network conditions.
- Estimate every funding, redemption, and possible refund fee.
- Decide how both chains will be monitored until completion or recovery.
- Test with non-material amounts in an appropriate environment before relying on unfamiliar software.
- Preserve transaction identifiers, contract data, timestamps, quotes, and tax records.
Common Mistakes
- Calling it one transaction: each chain records separate funding, redemption, or refund transactions.
- Assuming no intermediary means no trust: users rely on software, protocol rules, blockchain operation, and their own execution.
- Using equal deadlines: the party learning the secret second needs time to redeem safely.
- Ignoring refund fees: a refund still requires a valid transaction and sufficient network fees.
- Confusing a swap with a transfer: the swap exchanges two assets; a transfer moves an asset under one network’s rules.
- Assuming completion is reversible: after both redemption transactions are confirmed, ordinary payment reversal is generally unavailable.
Technical Sources
- Smart Contract: Code and transaction rules used to enforce conditional asset behavior.
- Blockchain: A distributed record whose consensus and finality rules govern each leg.
- Cryptocurrency Exchange: A venue or service for buying, selling, or converting crypto assets.
- Cryptocurrency Transfer: Movement of a crypto asset between addresses or account records.
- Cryptocurrency Wallet: Software or hardware used to manage keys and authorize blockchain transactions.
- Liquidity: The ability to transact without excessive price impact or delay.
FAQs
Does an atomic swap happen in one blockchain transaction?
No. A cross-chain atomic swap normally uses several conditional transactions across two independent blockchains. Atomicity refers to the linked completion and refund logic, not to a single shared transaction.
Can either party cancel an atomic swap?
A participant can stop cooperating before completion, but locked assets may remain unavailable until the relevant refund condition becomes valid. After both sides have been redeemed and confirmed, the ordinary swap is not designed for reversal.
Are atomic swaps cheaper than exchanges?
Not necessarily. Costs depend on the funding, redemption, and possible refund transactions on both networks, as well as spreads, quote quality, software fees, and the opportunity cost of locked funds.
Do atomic swaps eliminate counterparty risk?
They can reduce the risk that one party receives an asset while refusing to release the other, provided the contracts and deadlines work as intended. They do not eliminate price, software, chain, timeout, asset, privacy, or legal risks.
This page is technical and financial education, not a recommendation to use atomic-swap software or trade crypto assets. Implementations differ, and mistakes can cause irreversible loss. Obtain appropriate technical, legal, and tax guidance before a material transaction.