Atomic swaps are a mechanism to enable true P2P trading of crypto assets directly via the blockchain. How do atomic swaps work and what is the future of this technology?

Atomic swaps are automatic contracts for exchanging crypto assets between blockchains. The smart contract mechanism completely eliminates the need for centralized third-party organizations (exchanges or exchangers) when making transactions and frees them from counterparty risk. This is one of the few truly decentralized methods of conducting crypto asset exchange transactions.

How atomic swaps work

“Atomic” (indivisible) is a term for processes that are either executed entirely or not executed at all. In other words, an atomic swap has features that ensure that both parties to the transaction meet all predetermined conditions to complete the exchange. Compliance with the conditions is guaranteed by smart contracts. If one of the parties fails or there are technical problems, the entire operation is completely canceled.

Atomic swaps use hash-locked time-locked contracts (HTLC). These contracts use a mechanism for creating and comparing data fingerprints - a hash function. In addition, the contract sets a time limit - transactions are canceled if at least one of the parties does not fulfill the terms of the transaction within a predetermined period.

For example, two parties may agree to set a two-hour time limit for an atomic swap. In this scenario, the contract will return the deposited coins to their original owners when two hours have passed and not all conditions of the transaction have been met. The HTLC smart contract requires two cryptographic keys:

  • Hashlock key. This key ensures that the transaction will only be completed when both parties provide cryptographic evidence that they have fulfilled the terms of the transaction.

  • Timelock key. This is a security mechanism that helps transaction participants set a deadline for completing an atomic swap. The mechanism ensures that the deposited coins are returned to the participants in the transaction if the swap is not completed for one reason or another before the deadline.

How atomic swaps are executed

Let's assume that User A and User B agree to do an atomic swap for BTC and ETH. User B wants to exchange his 1 BTC for User A's 15 ETH.

First, User B needs to create a contract address to which he will send 1 BTC. After he has completed the transaction, the contract automatically generates a special key that only User B has access to. This key unlocks the money that the user sent to the smart contract.

The contract uses this key to create a hashed representation or encrypted form of the key. User B then sends a hash of the key to User A. Thus, User A only has access to the hashed form of the passcode used to lock User B's 1 BTC. This hash serves as proof that User B has locked the money in the contract, but User A has yet to cannot access them or remove them from the contract.

After receiving the hashed key, User A uses it to create his own contract address to which he deposits his 15 ETH. Once both parties have locked their money in the smart contract, User B can claim 15 ETH because they have access to the password, which reveals the key used by User A to lock the coins in the smart contract.

In the process of unlocking the contract address of User A, User B reveals his password to the second party to the transaction. User A uses it to receive 1 BTC and complete the transaction. Essentially, the entire atomic swap process comes down to the ability of both parties to provide cryptographic proof of their actions.

User B had to first encrypt the key and then send it to User A. Since he had the original key, he can claim the crypto assets that User A locked in the smart contract with the encrypted key. However, as a condition for unlocking crypto assets, User B must provide User A with the original key. User A can then access the key and use it to receive 1 BTC.

Why are atomic swaps needed?

Atomic swap is an important mechanism for blockchain interoperability because it eliminates the need for intermediaries, such as cryptocurrency exchanges, when trading crypto assets. This allows traders to conduct cross-chain transactions without relying on centralized trading platform infrastructures.

Because there are no intermediaries involved in atomic swaps, transactions are fast, more accessible, and eliminate the security incidents associated with custodial-based exchanges. Users maintain control over their crypto assets as all transactions are carried out from their personal wallets.

In addition, cross-chain trading through atomic swaps contributes to the creation of an interoperable cryptocurrency ecosystem. Atomic swaps make it easier to conduct transactions across multiple blockchains. In addition, atomic swaps eliminate counterparty risk - the transaction is either completed in full or not at all.

Development of atomic swaps

The mechanism of atomic swaps was first described by developer Sergio Lerner in 2012. The community was interested in the idea, but some swap processes were not fleshed out. A year later, Tier Nolan developed a more reliable procedure for performing atomic swaps. But it wasn't until four years later, when the Decred team performed an atomic swap between Decred and Litecoin, that the mechanism was successfully tested on a real blockchain for the first time. A few days later, Litecoin founder Charlie Lee tweeted that the process had been successfully replicated for the LTC and BTC swap.

The original design of an atomic swap required both parties to the transaction to download to their device the full version of the blockchains of the cryptocurrencies they planned to exchange. A month after Decred and Litecoin carried out the first atomic swaps, the developers of the Komodo project introduced a “lite version” of atomic swaps, requiring only the opening of special payment channels. At the same time, transaction participants do not need to download crypto asset blockchains or wait for validators to confirm transactions.

In 2017, Zcash developers demonstrated a working version of the XCAT (cross-chain atomic trades) tool, which can be used to swap BTC to ZEC and vice versa. A few months later, the first atomic swap was carried out between Bitcoin Cash and Decred.

In 2019, the Qtum platform launched atomic swaps on the Bitcoin mainnet, and Blockstream launched atomic swaps on the Liquid sidechain. There have also been attempts to create trading platforms that support atomic swaps. In 2019, the Sparkswap decentralized exchange for Bitcoin was launched with support for Lightning and atomic swaps. However, a year later the site closed due to a small number of users and low liquidity.

It is possible that the development of the DeFi industry, the general course of the community towards decentralization, as well as support for atomic swaps by the largest blockchains will give impetus to the development of the technology in the coming years. However, for now, atomic swaps remain a convenient and secure but rare form of transaction, eliminating the need for counterparties and allowing users to maintain control of their crypto assets.