Publisher avatar
LearnCrypto
7 min read

How do atomic swaps work?

How do atomic swaps work?

What is an atomic swap?

Atomic swaps are also known as cross-chain swaps or atomic cross-chain trading – they refer to the process of exchanging one cryptocurrency for another one without the need for an intermediary. 

Atomic swaps provide a greater level of decentralisation to cryptocurrency exchanges by removing centralised control imposed by intermediaries.  

Centralised exchanges (CEXs) are known for their high trading volumes and experiences that mirror traditional trading of stocks and fiat currencies. While CEXs provide liquidity and fair market prices, they are typically custodial and impose centralised control. 

On the other hand, decentralised exchanges (DEXs) wanted to develop a non-custodial infrastructure that aligns with the ethos of decentralised finance (DeFi). This is where atomic swaps jump in – atomic swaps enable peer-to-peer (P2P) transactions between users on two separate blockchains. 

Therefore, using cryptographic protocols and self-executing smart contracts, atomic swaps provide individuals with the possibility to trade divergent cryptocurrencies in a secure and decentralised manner. Atomic swaps solve the inefficiencies of exchanging cryptocurrencies through centralised exchanges.

A little bit of history

In 2012, the developer Sergio Demian Lerner laid down the initial version of a trustless exchange protocol. However, the idea of peer-to-peer (P2P) transactions without the meddling of a third party was invented by Tier Nolan in 2013; Tier Nolan, who is widely acknowledged as the pioneer developer of atomic swaps, laid down the basics for how to make crypto exchanges using divergent blockchains. 

Four years later, the atomic swap technology was brought to life by Charlie Lee, the founder of Litecoin. Lee tweeted how he managed to do a cross-chain atomic swap with the LTC/BTC pair, and exchanged 10 LTC for 0.1167 BTC. This was the first time an atomic swap was successfully executed. 

loading...

Ever since that milestone was achieved, a wide array of decentralised exchanges (DEXs) have been using this technology to provide new crypto trading solutions. 

Why are atomic swaps important for decentralised finance (DeFi)?

If you want to exchange cryptocurrency on a typical centralised exchange (CEX), you have to go through a number of steps. For example, after you have registered an account on a CEX that supports your desired trading pair and completed the KYC procedure, you have to transfer the cryptocurrency you want to exchange for another, convert it to the desired cryptocurrency and withdraw it to your wallet. The last two steps both include trading fees, and sometimes a longer waiting period. 

These steps can present an additional financial burden and sometimes even a security risk due to crypto asset custody. Typically, CEXs store funds in custodial wallets and retain control of the user’s private keys.  

To solve these issues and stick to the ethos of DeFi, DEXs enabled atomic swaps which are claimed to reduce potential pain points for crypto users. In other words, atomic swaps are considered to enable DeFi and resolve problems associated with trading on CEXs.

What is the difference between atomic swaps and bridges?

Blockchain bridges refer to technical solutions for transferring data back and forth between blockchains. Even though they both bring to the table blockchain interoperability, cross-chain swaps differ from cross-chain bridges.

loading...

In other words, blockchain bridges typically include creating a wrapped token and then transferring an equivalent amount on the other blockchain. Users can trade the wrapped token on the target blockchain or retrieve the crypto assets on the source blockchain. 

On the other hand, an atomic swap provides P2P exchanges between two parties on separate blockchains, and there are no intermediaries involved. Atomic swaps need the same hashing algorithm, while blockchain bridges don’t. 

If you’re interested in finding out more about bridges, we suggest reading this article: ‘What are Blockchain Bridges & how do they work?’

How do they work?

The term ‘atomic’ in ‘atomic swap’ stems from the software jargon – within database systems, atomicity is when a transaction is all or nothing; the transaction either executes fully or not at all.  

The term ‘atomic swap’ illustrates an exchange of two crypto assets that is automatically completed in total when certain conditions are met or not at all. 

If there are no intermediaries, how do we know that atomic swaps work and that all parties play fair? The answer lies in the use of smart contracts and cryptography. Put simply, smart contracts create a virtual vault that is used to perform the agreed swap of cryptocurrencies.  

To use the virtual vault, the first party sends funds to a specific contract address where it is locked. The second party receives a notification to confirm the first deposit, and then locks their funds into a second contract address. Once the smart contract confirms that both parties have locked their funds, they can get their swapped funds. 

That is to say, an atomic swap lays down a mechanism where both sides of the crypto trade must meet all predetermined requirements for the trade to be completed. 

What kind of smart contracts are used for atomic swaps?

Atomic swaps utilise Hashed Time Lock Contracts (HTLCs) which refers to a type of smart contract that facilitates a trustless exchange of crypto assets. As mentioned above, the automated process self-executes once all the predetermined conditions within the contract are met.

Hashed Time Lock Contracts (HTLCs) are made of two important features – the hashlock and the timelock. Let’s explain them briefly.

Hashlock

A hashlock refers to a cryptographically hidden key created by the user who initiated the transaction. It provides that the contract is locked with a unique cryptographic key and presents a data piece ensuring that the atomic swap is completed only when both parties approve the transaction. 

The Timelock mechanism

Imagine the timelock component as a deadline for the swap. The timelock mechanism ensures that the desired transaction is completed within a certain amount of time, and if it doesn’t execute, it returns the deposited funds. In other words, the timelock mechanism assists in securing the transaction. 

Timelocks are created as a Check-Lock-Time-Verify command (CLTV) or Check Sequence Verify (CSV).  

The CLTV command means that funds within a particular transaction are locked or released based on time. On the other hand, CSV means that funds are locked or released after a certain number of blocks are generated.  

If both parties don’t approve of the atomic swap before the determined time, the timelock assumes the role of a safety mechanism and the transaction doesn’t happen. 

The atomic swap process - A practical example

To explain atomic swap trading simply, let's lay down a practical example. Imagine two parties, Susie and James, want to exchange crypto assets using an atomic swap. 

Susie deposits her crypto assets into an HTCL address. As mentioned above, it assumes the role of a virtual vault which can only be unlocked using a special key.  

Susie proceeds with sharing a cryptographic hash of the special key with James, who deposits his crypto assets into a second address created using the same cryptographic hash. 

As soon as James deposits his crypto assets, Susie can unlock the transaction using the special key obtained during her initial deposit. Once Susie unlocks the transaction, James can access his share.  

When both parties can access their funds, the atomic swap is considered as completed. 

To truly understand atomic cross-chain trading and how atomic swaps work, it is crucial to gain a higher level of technical knowledge. We work with Engest Technology to provide crypto trading knowledge and technical expertise in the Learn Crypto Trading courseCheck out  Learn Crypto Academy to access this exclusive crypto trading educational series!

 Are atomic swaps trackable?

Unlike registering on a centralised exchange, atomic swaps don’t require users to go through the KYC process. They are designed to ensure anonymity, meaning they don’t directly expose transaction details and the identity of parties involved. 

However, blockchains used for atomic swaps are typically public ledgers where transactions are recorded and visible. Therefore, individual transactions involved in the atomic swap can still be tracked on underlying blockchains.

The main advantages of atomic swaps

Atomic swaps’ first benefit is its decentralised nature; users that want a truly decentralised financial system can be independent of CEXs and conduct wallet-to-wallet transactions. 

Secondly, the underlying technology employed by atomic swaps such as Hashlock and Timelock components of HTCL smart contracts, provide increased security to crypto traders – if a conflict or delay happens, they can get their assets back. 

Peer-to-peer trading brings to the table lower costs since the use of atomic swaps reduces trading fees and operational costs often associated with CEXs. 

Finally, atomic swaps provide a greater level of altcoin trading; many exchange platforms don’t allow users to directly exchange all types of altcoins. In other words, atomic swaps solve this issue by enabling increased trading flexibility when it comes to altcoins.

The main drawbacks of atomic swaps

Atomic swaps, in its current form, present several challenges to crypto traders as well. Since they require the exchange of hashed cryptographic information, users need to have an increased level of technical knowledge to understand the underlying technology.  

Secondly, atomic swaps can only be used concerning cryptocurrencies that belong to blockchain networks with similar hashing algorithms. This limits the options for crypto traders.  

Since atomic swaps include a predetermined time frame of the Timelock component, they can take more time to execute. As a result, the transaction in question is active on the blockchain for longer times which gives malicious actors more time to interrupt it and cause potential privacy concerns. 

Finally, atomic swaps don’t provide crypto-to-fiat exchanges. Therefore, users who wish to exchange fiat currencies must use crypto exchange platforms.

The bottom line

As an emerging technology, atomic swaps will likely continue to evolve and expand its use on more blockchains. If your desired blockchain network doesn’t support atomic swaps yet, there is a possibility that it will in the future because they hold potential to change the way crypto assets are traded. 

This novel technology provides several benefits such as decentralisation, increased security, and reduced transaction fees. However, at the same time, atomic swaps face certain limitations such as compatibility, liquidity and potential privacy issues that need to be addressed and resolved.