Atomic Swap

An atomic swap is a direct trade of coins between two different blockchains without an exchange or middleman. Hashed timelock contracts make sure either both sides get paid or both get refunds.

Swapping bitcoin for another coin usually means trusting an exchange to hold both sides. An atomic swap removes that middleman. Each party locks coins on its own chain in a contract tied to the same secret, and revealing the secret to claim one side automatically lets the other side claim too. If anything stalls, timeouts return the coins. This page walks through the protocol, the timing rules that keep it safe, and the practical catches that have kept it niche.

Category
Cryptocurrency and Blockchain
Reading time
6 min
Also called
Cross-chain swap, Atomic cross-chain swap, Cross-chain atomic swap
Updated

An atomic swap in plain words

An atomic swap lets two people trade coins they hold on different blockchains without a trusted third party or a central exchange.1 Simply sending coins to each other would not work, because whoever receives first could stop cooperating.1

"Atomic" means all-or-nothing: either both transfers happen or neither does.1 The tool that makes this work is the hashed timelock contract, which pays out to whoever reveals a secret before a deadline and otherwise refunds the sender.2 See Hashed Timelock Contract.

The best-known version of the protocol is credited to a developer known as TierNolan, who proposed it in 2013.3

How an atomic swap works, step by step

Alice holds coin X and wants Bob's coin Y. The steps follow the TierNolan protocol.1

  1. Alice picks a secret

    Alice creates a random secret and shares only its hash with Bob.4

  2. Alice locks her coins

    On chain X she locks coins that Bob can claim by revealing the secret, with a refund to Alice after about 48 hours.1

  3. Bob audits and locks his coins

    Bob checks Alice's contract, then locks his coins on chain Y under the same hash, with a shorter refund time of about 24 hours.4

  4. Alice audits and claims

    Alice checks Bob's contract and claims his coins, which publishes the secret on chain Y.1

  5. Bob reads the secret and claims

    Bob takes the secret from Alice's claim and uses it to claim her coins on chain X.1

  6. Or both refund

    If the swap stalls, each side can reclaim its own coins after its timeout expires.4

Worked examples

Example 1 — The trade and its costs

$12 in fees

Hypothetically Alice swaps 1 BTC for Bob's litecoin at 800 LTC per BTC. Each side needs about two transactions: a lock and a claim.3 Assume a $3 fee per transaction.

Example 1 — The trade and its costs — calculation
Bob locks1 × 800 = 800 LTC
Transactions in total2 chains × 2 = 4
Total fees4 × $3 = $12

No exchange holds either coin at any point; the only costs are the network fees.

Example 2 — Why the timeouts differ

24-hour buffer

Alice's refund unlocks after 48 hours, Bob's after 24 hours, as in the classic protocol.1

Example 2 — Why the timeouts differ — calculation
Alice must claim within24 hours
Bob's safety margin48 − 24 = 24 hours

If Alice claims late, Bob may already have his refund; if she claims in time, Bob still has a full day to use the secret before Alice's refund opens.4

Example 3 — The free option

$6,400 option value

Bob has locked 800 LTC at a hypothetical $80. Before claiming, Alice sees litecoin fall 10%.

Example 3 — The free option — calculation
Value when locked800 × $80 = $64,000
New LTC price$80 × 0.90 = $72
Value now800 × $72 = $57,600
Drop Alice can avoid$64,000 − $57,600 = $6,400

Alice can simply walk away and wait for her refund, so the party who holds the secret holds an option on the price.1

Atomic swap vs. other ways to change coins

AspectWho holds your coins?Main risk
Atomic swapYou, until the swap completesMissed deadlines, free option, few partners
Centralised exchangeThe exchangeExchange failure or freeze. See Counterparty Risk
Bridge and wrapped tokenThe bridge's contracts or signersBridge hacks. See Cross-Chain Bridge

Where atomic swaps are used

  • Peer-to-peer coin trades

    Command-line tools exist for swaps between coins such as Bitcoin, Litecoin and Decred.4 See Decred (DCR).

  • Moving to and from sidechains

    The pegged-sidechains paper expected most transfers to use atomic swaps, avoiding the slow peg.3 See Sidechain.

  • Lightning payments

    The same hashed timelock idea links payments hop by hop on the Lightning Network.2 See Lightning Network.

  • Decentralised trading

    Atomic swaps can underpin exchanges that never take custody. See Centralised and Decentralised Exchanges.

Advantages and disadvantages

Advantages

  • No exchange or custodian ever holds your coins.
  • All-or-nothing: you get the other coin or your own coin back.
  • Uses ordinary transactions, about two on each chain.
  • No account, sign-up or withdrawal limit at a third party.

Disadvantages

  • Both chains must support hash locks and time locks.
  • Coins can stay locked for a day or two if the other side walks away.
  • The secret holder gets a free option on the price.
  • Finding a partner and agreeing a rate is up to you.

Going deeper

Read as far as you need. Each level builds on the one before it.

A locked box with one key

Beginner

Picture two boxes on two different chains, both opened by the same key. Opening one box to take your coins shows the key to everyone, so the other person can open theirs.1

If nobody opens a box in time, a timer gives each box back to its owner.1

Hash locks and time locks

Intermediate

BIP 199 describes the script: the recipient can spend with the secret plus a signature, or the sender can take the coins back with a signature after a timeout.2

It allows SHA-256 or HASH160 for the hash and either an absolute or a relative time lock.2 Both chains in a swap need to accept the same hash function so one secret unlocks both sides.

Timing and practical risks

Advanced

Each party must claim before its deadline, or the counterparty can take the refund; funds are also tied up for the whole timeout if the other side disappears.1

Before locking, each side should check the other contract's address, amount, time lock and secret hash, and keep its refund transaction safe.4

Decred's reference tools describe themselves as a proof of concept for early adopters, which reflects how hands-on on-chain swaps still are.4

Common mistakes

  1. Skipping the contract audit

    Always verify the other side's amount, address, time lock and hash before you lock coins.4

  2. Claiming too late

    Miss your deadline and the counterparty may already have taken a refund.1

  3. Ignoring the free option

    On a volatile pair, the side holding the secret can abandon the trade if the price moves against them.1

  4. Calling any cross-chain trade atomic

    Bridges and exchanges that hold your coins are not atomic swaps. See Cross-Chain Bridge.

Check your understanding

Test yourself before moving on. Each answer comes with an explanation.

Q1In a standard atomic swap, why does Bob's refund time lock expire before Alice's?
Q2Alice and Bob each lock coins, then Alice disappears without claiming. What happens?

Key points to remember

  1. An atomic swap trades coins across two blockchains without a middleman.
  2. Hashed timelock contracts make it all-or-nothing.
  3. Claiming one side reveals the secret that unlocks the other.
  4. Staggered timeouts protect the party who locks second.
  5. The secret holder has a free option, and coins can stay locked for hours or days.

Frequently asked questions

Do atomic swaps need an exchange?

No. The two parties only need to agree terms and share contract details over any channel, such as a chat.4

Can I atomic-swap any two coins?

Only if both chains support the hash and time locks the contract needs. Tools list specific supported coins.4

What happens if the other person disappears?

Your coins return to you after your time lock expires, but they stay locked until then.1

Further learning

Sources

This article is written in our own words. The sources below support the specific facts, rules and figures marked with a number in the text. Links open the original publisher’s page.

  1. Bitcoin Wiki. Atomic swap (opens in a new tab) Checked .
  2. Bitcoin Improvement Proposals. BIP 199: Hashed Time-Locked Contract transactions (opens in a new tab) Checked .
  3. Back, Corallo, Dashjr et al.. Enabling Blockchain Innovations with Pegged Sidechains (2014) (opens in a new tab) Checked .
  4. Decred (GitHub). atomicswap: on-chain atomic swaps for Decred and other cryptocurrencies (opens in a new tab) Checked .

Educational content, not financial advice. Examples use hypothetical prices and simplified costs; actual fees, margin rules and regulations vary by broker, market and country. We are not affiliated with any publisher linked above. Last reviewed . Report an error or request a source credit.