What are atomic swaps?

Atomic swaps are a method that provides the ability to quickly exchange between two cryptocurrencies running on different blockchains. This process (also known as atomic cross-chain trading) is based on smart contracts and allows users to trade coins directly from their cryptocurrency wallet.

Although the concept of cross-chain trading is innovative, it has been discussed for many years. Tyr Nolan was most likely the first to describe a complete atomic swap protocol back in 2013. However, in 2012 Daniel Larimer introduced the P2PTradeX protocol and some people consider it an earlier prototype of atomic swap.

In subsequent years, many developers began experimenting with the protocols described above. Evidence suggests that communities of cryptocurrencies such as Bitcoin, Litecoin, Komodo and Decred played a major role in the development of this technology.

Apparently, the first peer-to-peer cryptocurrency exchange using atomic swap took place in 2014, but it wasn't until 2017 that the method became widely known, thanks to such successful swap pairs as LTC/BTC and DCR/LTC.


Principle of operation

Atomic swap protocols are designed to prevent any participant from cheating. To understand how this works, let's imagine that Alice wants to exchange her Litecoins (LTC) for Bob's Bitcoins (BTC).

First, Alice makes a deposit of LTC to the contract address, which acts as a safe deposit box. Once this safe is created, Alice generates a key to access it. She then shares the cryptographic hash of this key with Bob. Note that Bob cannot access the LTC yet because he only has a hash of the key, not the key itself.

Bob then uses the hash provided by Alice to create another secure contract address to store his BTC. To receive BTC, Alice must use the same key, thus providing her currency to Bob (thanks to a special function called a hashlock). This means that once Alice demands BTC, Bob will in turn also request LTC, after which the currency exchange will be completed.

The term "atomic" refers to the fact that these transactions either occur in full or do not occur at all. If one of the parties does not fulfill its obligations, 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 swaps occur on any of the currency networks (in this case, the Bitcoin or Litecoin blockchain). Off-chain swaps occur on the secondary layer. This type of swap is typically based on bidirectional payment channels similar to those used on the Lightning Network.

From a technical perspective, most of these trust-based 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 component of Bitcoin's Lightning Network, they are also a key component that makes atomic swaps possible. As the name suggests, this type of contract is based on two key functions: hashlock and timelock.

The hashlock is responsible for preventing funds from being spent if part of the data remains inaccessible (like Alice's key in the previous example). Timelock is a feature that ensures that a contract can only be executed within a previously defined period of time. Therefore, using HTLC eliminates the need for trust because the contract creates a specific set of rules that prevent partial execution of the atomic swap.


Advantages

The most basic benefits of atomic swaps come from the decentralized nature of the feature. Eliminating the need for a centralized exchange or any other type of intermediary. Cross-chain cryptocurrency exchange can be carried out between two (or more) parties without the need to trust each other. The level of security of swaps is also much higher, since users do not need to transfer their funds to a centralized exchange or third party. Instead, transactions can occur directly between two user wallets.

In addition, this form of peer-to-peer tracking has much lower operating costs and commissions are either very small or non-existent. Atomic swaps allow transactions to be completed much faster and with a higher degree of interaction. In other words, altcoins can be exchanged directly, without using Bitcoin or Ether as an intermediate coin.


Flaws

There are several prerequisites for atomic swap, which could possibly prevent widespread adoption of the technology. For example, to perform a swap, two cryptocurrencies must be blockchain-based that use the same hashing algorithm (for example, SHA-256 as on the Bitcoin network). In addition, they should also be compatible with HTLC and other programmable features.

In addition, atomic swaps raise concerns about user privacy. This is because this type of cryptocurrency exchange within a network can be quickly tracked using a blockchain explorer, making it easier to link addresses. The short-term answer to this problem is to use privacy-focused cryptocurrencies as a way to mitigate risks. However, many developers are experimenting with using digital signatures for atomic swaps and they believe that this is a more reliable solution to this problem.


What is the importance of this technology?

Atomic swaps have great potential to improve the cryptocurrency ecosystem, but this still needs to be tested on a larger scale. Cross-chain trading could ultimately solve many of the problems of most centralized exchanges and although they support the ecosystem up until today, they have a number of problems that include:

  • Lots of vulnerabilities: Storing a large number of assets in one place affects their vulnerability and this is most reflected in centralized exchanges, which are prime targets for hacker attacks.

  • Poor management and human error: Centralized exchanges are run by people and if they make a mistake or if management makes poor choices regarding exchange operations, users' funds can quickly be compromised.

  • High transaction costs: Centralized exchanges require higher transaction fees for withdrawals and trading.

  • The challenge of handling large volumes of trades: When trading activity becomes too high, centralized exchanges often cannot cope with the increased demand, causing the system to slow down or shut down altogether.

  • Legal regulation: in most countries, legal issues regarding cryptocurrencies still remain open, as well as problems associated with the regulation, management and interpretation of transactions with digital assets by states.


Conclusion

Atomic swaps are still a work in progress for mainstream use, but the technology is driving significant changes in blockchain interoperability and cross-chain trading capabilities. Thus, this method has great potential for the growth of the cryptocurrency industry, opening up new opportunities for decentralization and peer-to-peer money transfers. Most likely, the popularity of atomic swaps will only increase in the near future, especially on decentralized exchanges.