Atomic Swaps

Updated: September 24, 2025

What is an atomic swap (in plain terms)
– An atomic swap is a direct, trust-minimized exchange of cryptocurrency between two different blockchains. “Atomic” refers to the all-or-nothing nature of the trade: either both sides complete, or the entire exchange cancels and funds return to their original owners.

Key definitions
– Blockchain: a distributed ledger that records transactions for a particular cryptocurrency.
– Smart contract: a program on a blockchain that runs automatically when predefined conditions are met.
– Hash Time-Locked Contract (HTLC): a type of smart contract that ties a cryptographic hash (proof-of-secret) to a time limit; it enables conditional, time-limited transfer of funds across chains.
– Counterparty risk: the danger that the other party in a trade will take your funds without completing their side.
– Decentralized exchange (DEX): a trading platform that does not rely on a central intermediary.

Why atomic swaps exist (what problem they solve)
– Different cryptocurrencies live on different blockchains and normally require going through an exchange (or multiple trades) to convert one token to another. Atomic swaps let two parties exchange tokens directly across chains, removing intermediaries and reducing the number of steps and counterparty exposure.

Core mechanism (how an atomic swap works, in simple steps)
1. Secret and hash: Party A generates a random secret value S and computes its cryptographic hash H = hash(S). H is shared; S is kept private.
2. Lock on chain A: Party A locks their coin on blockchain A into an HTLC that can be claimed if the redeemer reveals S before deadline T1, otherwise the funds can be refunded to A after T1.
3. Lock on chain B: Party B locks their coin on blockchain B into an HTLC that uses the same hash H, but with a shorter deadline T2 Bob’s refund timeout (T_B). Typical pattern: T_A = 48 hours, T_B = 24 hours.

2) Secret and hash
– Alice generates a random secret S and computes H = Hash(S) (commonly SHA-256).
– Alice shares H (not S) with Bob.

3) Alice funds the BTC HTLC
– Alice creates and broadcasts an HTLC on the BTC chain locking 0.1 BTC to a script that allows redemption only with preimage S before time T_A or else refund to Alice after T_A.
– Wait for required confirmations.

4) Bob funds the LTC HTLC
– After confirming Alice’s BTC HTLC onchain, Bob creates and broadcasts an HTLC on the LTC chain locking 5 LTC using the same hash H and timeout T_B.
– Wait for required confirmations.

5) Redeem and reveal
– Alice redeems the LTC HTLC by providing S onchain to claim 5 LTC. Redeeming reveals S in the LTC transaction history.
– Bob reads S from the LTC redemption transaction and uses it to redeem the BTC HTLC, claiming 0.1 BTC.

6) Failure/refund paths
– If Bob never funds the LTC HTLC, Alice refunds her BTC after T_A.
– If Alice doesn’t redeem Bob’s LTC HTLC within T_B, Bob refunds his LTC after T_B.
– Proper time ordering (T_A > T_B) ensures neither party can be cheated if the other disappears.

Worked numeric example with fees
Assumptions:
– Alice ↔ Bob swap: 0.1 BTC for 5 LTC (1 BTC = 50 LTC).
– BTC network fee: 0.0005 BTC per HTLC transaction.
– LTC network fee: 0.001 LTC per HTLC transaction.
– Both parties pay fees on the transactions they broadcast.

Cashflows (best-case successful swap):
– Alice funds BTC HTLC: pays 0.1 BTC + 0.0005 BTC fee (broadcast fee) = 0.1005 BTC out of pocket initially.
– Bob funds LTC HTLC: pays 5 LTC + 0.001 LTC fee = 5.001 LTC out of pocket initially.
– Alice redeems LTC: she pays LTC redemption fee (assume included above when she broadcasts redeem; fees vary by implementation). If Alice pays the LTC redeem fee (0.001 LTC), net LTC received ≈ 5.000 – 0.001 = 4.999 LTC.
– Bob redeems BTC: when Bob redeems BTC he may pay the BTC fee (0.0005 BTC) — net BTC received ≈ 0.1005 – 0.0005 = 0

.1000 BTC.

Net result (best-case, both redeem):
– Alice: paid 0.1005 BTC out of pocket; received ≈ 4.999 LTC. Effective exchange rate = 0.1005 BTC / 4.999 LTC ≈ 0.020103 BTC per LTC (≈ 49.75 LTC per BTC).
– Bob: paid 5.001 LTC out of pocket; received ≈ 0.1000 BTC. Effective exchange rate = 0.1000 BTC / 5.001 LTC ≈ 0.019996 BTC per LTC (≈ 50.01 LTC per BTC).

Worked step-by-step (numbers above):
1. Alice funds BTC HTLC: 0.1000 BTC principal + 0.0005 BTC broadcast fee = 0.1005 BTC out.
2. Bob funds LTC HTLC: 5 LTC principal + 0.001 LTC broadcast fee = 5.001 LTC out.
3. Alice redeems LTC: pays LTC redeem fee (assumed 0.001 LTC) and receives ≈ 4.999 LTC.
4. Bob observes Alice’s redeem, extracts secret, redeems BTC and pays BTC fee (assumed 0.0005 BTC), receiving ≈ 0.1000 BTC.

What if the swap fails (refund path)?
– If one party walks away or cannot broadcast/confirm their redeem, the other uses the timelock refund path after expiry.
– Refunds require broadcasting a refund transaction and paying another on-chain fee; that raises the net cost for the refunder.
Example (Bob fails to redeem; Alice refunds BTC after timelock):
1. Alice still paid 0.1005 BTC initially.
2. To reclaim BTC she broadcasts a refund Tx; assume refund broadcast fee = 0.0005 BTC.
3.