What Are Bitcoin Atomic Swaps? A Beginner's Guide
August 7, 2023
Atomic swaps are an innovative technology that enables decentralized cross-chain crypto asset trading. Read on to learn what Bitcoin atomic swaps are and how they work.
What Are Cross-Chain Atomic Swaps?
Cross-chain atomic swaps enable two parties to trade digital assets from different blockchains without a trusted centralized intermediary.
More precisely, Bitcoin atomic swaps are automated contracts that allow two individuals to trade assets between the Bitcoin blockchain and other blockchains. Bitcoin atomic swaps can also be carried out between the Bitcoin network and the Layer-2 networks built on top of it.
Atomic swaps are grouped into two categories: on-chain and off-chain.
- On-chain atomic swaps allow parties to make trustless peer-to-peer (P2P) trades across different blockchains.
- Off-chain atomic swaps are used with Layer-2 scaling solutions like the Lightning Network.
The concept of cross-chain atomic swaps was initially proposed by Sergio Demian Lerner in 2012. Tier Nolan later described the same idea but introduced a different implementation method in 2013. It was not until 2017 that the first successful actualization of atomic swaps took place between LTC and BTC. In this first atomic swap transaction, it was Litecoin’s founder Charlie Lee who exchanged 10 LTC for 0.1167 BTC.
While this was a breakthrough moment, atomic swaps still haven’t exactly caught on. A different cross-chain technology, bridging, has become more popular instead, despite Ethereum’s founder Vitalik Buterin recommending atomic swaps over cross-chain bridges.
How Do Atomic Swaps Work?
Atomic swaps leverage smart contracts, which ensure that predefined conditions are satisfied, and only then a transaction is executed. If both parties or one party fails to meet the set requirements, the transaction is canceled, and the funds are transferred back.
The term “atomic” describes a transaction that either happens or doesn’t at all. There is nothing in between.
Atomic swaps are executed in the personal wallets of the two parties involved. They utilize smart contracts called Hash Timelock Contracts (HTLCs). An HTLC is time-bound and requires both parties to fulfill the preset conditions within a given time frame. Moreover, an HTLC uses a cryptographic hash function that converts data such as transaction details and wallet addresses into a number called a hash.
HTLCs require two encrypted security keys: hashlock and timelock.
- The person that initiates the transaction generates the hashlock key. This key makes certain that a swap is only finalized when both parties have sent proof ascertaining they have satisfied the predetermined conditions.
- A timelock key acts as a safety mechanism by setting a time limit for atomic swaps. If both or one party fails to fulfill the contract conditions within the deadline, funds are returned to both parties.
A Theoretical Example
Let’s picture a scenario where Jack wants to exchange 2,000 STX for Jill’s equivalent amount of BTC.
Jack sends his STX to the contract address of the HTLC he has created. The contract then generates a unique key for Jack that only he has access to. This key acts as a password that unlocks the STX he has deposited into the smart contract. The contract also uses the key to produce a hashed version of the key that Jack sends to Jill. That means that Jill is in the possession of the hashed form of the password needed to unlock Jack’s STX. She can only confirm that the funds are locked in the contract but can’t access them yet.
Jill then uses the hashed key to generate her own contract address and deposit her BTC. Once funds from the two parties are in the same smart contract, Jack can claim Jill’s BTC.
By unlocking Jill’s contract address, the password is revealed, allowing Jill to claim the deposited STX. Jack can unlock the BTC since he has the password that unlocks the hashed key Jill used to lock her BTC.
A Bitcoin Lightning Network Atomic Swap in Action
LNSwap is a decentralized protocol that enables fast and private P2P swaps between Bitcoin and its Layer-2 network, Stacks. That means users can swap BTC for digital assets on Stacks and vice versa.
LNSwap uses Lightning to reduce transaction fees and increase transaction speeds. The protocol also uses submarine swap technology to make on-chain Bitcoin addresses compatible with off-chain Lightning addresses. As a result, it can facilitate BTC transfers from the Bitcoin blockchain to an off-chain Lightning channel. LNSwap provides Lightning nodes for users that want to open Lightning channels with the protocol.
Like decentralized exchanges (DEXs), LNSwap relies on liquidity providers, who supply the necessary funds to make atomic swaps on the protocol. In return, liquidity providers earn rewards from the transaction fees generated by the trades taking place on the protocol.
Here is how you can swap BTC for STX (Stacks native token) on LNSwap:
- Visit LNSwap and click the “Swap” button at the top.
- Enter the amount of BTC you want to swap.
- Connect LNSwap to a Bitcoin wallet like Hiro or Xverse. The wallet should have STX to pay for the transaction fee.
- Click “Start.”
- Confirm the address that the STX will be sent to, and then click “Next.” If you don’t have STX to cover the transaction fee, check the box labeled “Use sponsored transaction.”
- Transfer BTC to the address provided to make the swap.
- Once you’ve sent the BTC, wait for LNSwap to confirm that you sent the funds.
- After about ten minutes, LNSwap will confirm that the BTC was sent, allowing you to claim STX.
- Click “Claim STX.”
- LNSwap will take you to your wallet, where you can confirm that all details are correct before clicking “Confirm.” STX will reflect in your wallet after a few minutes.
Benefits & Drawbacks of Bitcoin Atomic Swaps
Now, let’s take a look at the benefits and drawbacks of cross-chain swaps.
- Zero counterparty risk thanks to the use of HTLC. There’s no risk that one party will get the offered tokens and decline to transfer their tokens.
- They are intermediary-free.
- Atomic swaps are non-custodial since they take place in the parties’ wallets. This gives users full autonomy over their funds.
- The lack of third parties can make atomic swaps cheaper than trading on a centralized exchange.
- Atomic swaps promote blockchain interoperability.
- Atomic swaps are still relatively new, which means they haven’t been tried and tested.
- They are complex to implement, hence why adoption has been slow.
- Fiat on/off-ramps are non-existent.
- There are very few atomic swap dApps for Bitcoin.
- Limited liquidity forces atomic swap protocols to rely on liquidity pools, which are an attractive target for hackers.
The Bottom Line
Bitcoin atomic swaps provide an option for fully decentralized trading, an immensely important aspect of Bitcoin-based DeFi. They also bring interoperability to the Bitcoin ecosystem.
Unfortunately, cross-chain atomic swap technology hasn’t really caught on yet because it is complex to build. Additionally, limited liquidity compels atomic swap exchanges to rely on liquidity pools, requiring user participation while creating targets for cyberattacks.
Despite these challenges, Atomic swaps hold a lot of promise and could change the Web3 landscape significantly once its drawbacks are addressed and adoption grows.
Does Bitcoin Have Atomic Swaps?
Yes. For example, LNSwap is a decentralized protocol that facilitates atomic swaps between Bitcoin and its Layer-2 (L2) network Stacks. This enables users to swap BTC for any of the digital assets on the Stacks network and vice versa.
In other words, LNSwap makes the Bitcoin Layer-1 network interoperable with the Stacks L2 network. Therefore, LNSwap plays a critical role in Bitcoin’s DeFi ecosystem by permitting cross-chain token swaps between Bitcoin and Stacks.
Komodo’s AtomicDEX is another atomic swap for Bitcoin and other blockchain networks. It allows users to trade BTC with the native assets of other L1 chains like LTC, DOGE, and RVN.
What Is the Difference Between an Atomic Swap and a DEX?
During an atomic swap, two individuals’ funds are held in a hash timelock contract and are unlocked when both sides fulfill the preset conditions. So, whenever two people want to swap tokens, a new contract is created to facilitate the trade. However, a decentralized exchange (DEX) holds everyone’s funds in one on-chain contract, creating a honey pot that hackers could exploit.
Are Atomic Swaps Private?
Atomic swaps are semi-private. While the swap transaction itself is indistinguishable from other transactions, the individual transactions involved in the trade can be traced on the respective public blockchains. Users can boost the privacy of their atomic swaps by using protocols with enhanced privacy like LNSwap. Alternatively, users can swap tokens from two different privacy-focused blockchains to keep their trades private.