What are atomic swaps?

Atomic swaps consist of a technique that allows the quick exchange of two different cryptocurrencies, running on distinct blockchain networks. Such a process (also known as atomic cross-chain trading) is based on smart contracts, and it allows users to trade their coins directly from their personal crypto wallets. So atomic swaps are, essentially, peer to peer trades across different blockchains.

Despite being an innovative technique, the idea of cross-chain trading is under discussion for many years. Tier Nolan was likely the first to describe a complete atomic swap protocol, back in 2013. However, a trustless exchange protocol called P2PTradeX was presented by Daniel Larimer in 2012, and some people consider it to be the prototype of an atomic swap.

In the following years, many developers started to experiment with atomic swap protocols. Evidence suggests that Bitcoin, Litecoin, Komodo, and Decred communities all played an important role in the process.

Apparently, the first peer to peer atomic swaps started to take place in 2014. But it was only in 2017 that the technique became widely known by the general public - mainly because of the successful swaps between LTC/BTC and DCR/LTC.





How do atomic swaps work?

Atomic swap protocols are designed in a way that prevents any of the involved parties from cheating. To understand how they work, let’s imagine that Alice wants to trade her Litecoins (LTC) for Bob’s Bitcoins (BTC).

First, Alice deposits her LTC into a contract address that acts like a safe. When this safe is created, Alice also generates a key to access it. She then shares a cryptographic hash of this key with Bob. Note that Bob can’t access the LTC yet because he only has the hash of the key and not the key itself.

Next, Bob uses the hash provided by Alice to create another safe contract address, in which he deposits his BTC. To claim the BTC, Alice is required to use that same key and, by doing so, she reveals it to Bob (thanks to a special function called hashlock). This means that as soon as Alice claims the BTC, Bob is able to claim the LTC and the swap is complete.

The term ‘atomic’ relates to the fact that these transactions either happen entirely or not at all. If any of the parties give up or fails to do what they are supposed to, the contract is canceled, and the funds are automatically returned to their owners.

Atomic swaps can happen in two different ways: on-chain and off-chain. On-chain atomic swaps happen on either of the currency’s networks (in this case, either the Bitcoin or Litecoin blockchain). Off-chain atomic swaps, on the other hand, take place on a secondary layer. This kind of atomic swaps is usually based on bidirectional payment channels, similar to the ones used in the Lightning Network.

Technically speaking, most of these trustless trading systems are based on smart contracts that use multi-signatures and Hash Timelock Contracts (HTLC).





Hash Timelock Contracts (HTLC)

While Hash Timelock Contracts (HTLC) are an important part of the Bitcoin Lightning Network, they are also one of the key components that makes atomic swaps possible. As the name suggests, they are based on two key functions: a hashlock and a timelock.

A hashlock is what prevents funds from being spent unless a piece of data is revealed (Alice’s key in the previous example). Timelock is a function that ensures the contract can only be executed within a predefined timeframe. Consequently, the use of HTLCs removes the need for trust because they create a specific set of rules that prevent atomic swaps from executing partially.





Advantages

The biggest advantages of atomic swaps are all related to its decentralized nature. By removing the need for a centralized exchange or any other kind of mediator, cross-chain swaps can be executed by two (or more) parties without requiring them to trust each other. There is also an increased level of security because users don’t need to give their funds to a centralized exchange or third party. Instead, the trades can happen directly from users’ personal wallets.

Also, this form of peer to peer trading has much lower operational costs as trading fees are either very low or absent. Lastly, atomic swaps make it possible for trades to happen very quickly, with higher degrees of interoperability. In other words, altcoins can be swapped directly without making use of Bitcoin or Ethereum as an intermediary coin.





Limitations

There are a few conditions that need to be met for an atomic swap to take place, and these may likely present obstacles for the technique to be widely adopted. For instance, to perform an atomic swap, the two cryptocurrencies need to be based on blockchains that share the same hashing algorithm (e.g., SHA-256 for Bitcoin). Also, they also need to be compatible with HTLC and other programmable functionalities.

Other than that, atomic swaps bring up concerns about users privacy. That’s because on-chain swaps and transactions can be quickly tracked on a blockchain explorer, making it easy to link the addresses. A short-term answer to this problem is to use privacy-focused cryptocurrencies as a way to reduce exposure. Still, many developers are experimenting with the use of digital signatures in atomic swaps as a more reliable solution.





Why do they matter?

Atomic swaps have great potential to improve the cryptocurrency space and are yet to be tested on a broader scale. Cross-chain trading can eventually solve many of the problems that are part of most centralized exchanges. Although these exchanges have maintained the cryptocurrency systems up until today, there are several concerns about them. Some of these issues include:

Greater vulnerability: keeping many valuable resources in one location makes them more vulnerable to hacking, and centralized exchanges are prime targets for digital heists.

Funds mismanagement and human error: centralized exchanges are run by people. If those in important roles make mistakes or if the leaders make poor choices regarding exchange operation, users’ funds can be compromised.

Higher operational costs: centralized exchanges have higher withdrawal and trading fees

Inefficiency in regards to volume demands: when the market activity gets too intense, centralized exchanges often fail to deal with the increased trading demand, causing the system to slow down or go offline.

Regulation: in most countries, cryptocurrency regulation is far from ideal. There are still many concerns surrounding government approval and management.





Closing thoughts

Although atomic swaps are still fairly new and limitations certainly exist, this technology is driving significant changes in regards to blockchain interoperability and cross-chain trading capabilities. As such, the technique has great potential to influence the growth of the cryptocurrency industry, opening up new avenues in terms of decentralization and peer to peer monetary transfers. The chances are that atomic swaps will be used more and more in the near future, especially within decentralized exchanges.