Introduction to Atomic Swaps

Introduction to Atomic Swaps

If you’ve ever tried changing between two different cryptocurrencies, you’ve probably noticed the high switching costs it involves. You might be thinking it’s not that hard, but let’s think about it for a moment. If, for example, you want to trade Token A in Binance Smart Chain for Token B in Polygon Chain, you’ll need to do several things:

First, the assets are locked or “frozen” on the blockchain where they live using a smart contract or, if smart contracts are not supported, another mechanism. Next, new tokens of an equal amount are created on the receiving blockchain. When the user wants to redeem the assets, the equivalent tokens are burned, and then the original assets are unlocked. This process prevents the assets from being used in any way on both chains at the same time. As you can imagine this situation is less than optimal and time-consuming. In a truly decentralized world, users will be able to switch crypto to crypto directly without any third party, instantly and feeless.

Note: Decentralized exchanges only work for assets which are on the same blockchain. As the blockchain technology progresses further, we are going to see a number of separate blockchain come to the market and this is why we need Atomic Swaps.

What are Atomic Swaps?

An Atomic Swap allows users to trade two cryptocurrencies directly, without the need for an intermediary or trust. In my particular case, it will allow me to exchange Token A(BSC) for Token B(Polygon) with complete strangers directly from my wallet.

The term Atomic comes from computer science. It means that either the operation is conducted as planned or it isn’t conducted at all. With an Atomic Swap I will either exchange my coins with the other party exactly as we agreed, or it won’t happen at all.

How does an Atomic Swap works?

So, now that we know how useful Atomic Swap is, let’s discuss how we can implement one technically.

Atomic Swaps can be implemented with Hashed Time Locked Contracts(HTLC).

Continuing our previous analogy of swapping Token A(BSC) with Token B(POLYGON):

Owner of either Token A or Token B will first pick a “secret key”. Let’s assume owner of Token A in this case. Then the owner of Token A will calculate the hash of the “secret”. Then Token A owner will deploy a HTLC on Binance Smart Chain and send Token A to this contract. This HTLC contract is coded to release the Token A to the Token B holder only if they know the secret but at this stage they don’t know the secret yet. The hash which was calculated previously is used to check that the sender of the transaction knows the secret. Now, the owner of Token B will deploy another HTLC on Polygon Chain and will send Token B to this contract. If the amount sent by both the user is correct as agreed upon and everything checks out, both the users can withdraw their end of the bargain. The moment, the owner of the secret key uses it to open one of the contract to withdraw, the key becomes available to the other user too and they can use it to withdraw their end of the bargain and the transaction is completed!

Now, a couple of things to note:

Firstly, when the secret was revealed by the Token A holder, the tokens on Binance Smart Chain was still secure because the smart contract will only release the token to the address of Token B holder and if anyone else tried to steal the tokens with the “secret”, it just wouldn’t work.

Secondly, what happens if the Token A holder never withdraws and in turn never shares the secret with the Token B holder? Will both the funds be locked forever?

The answer is No. To tackle this problem, in the HTLC contract, there are measures to return the Token B to its original owner if the owner of Token A does not withdraw their tokens within a certain pre-defined time period.

And lastly, what if the original owner of Token A withdraws the Token B just before the withdrawal period gets over? Will he get to keep both the tokens?

The answer is No. To tackle this problem, the withdrawal period of Token B is kept shorter than the withdrawal period of Token A. This ensure, both the users have just enough time to hold each end of their bargain and if that doesn’t happens, the tokens gets returned to their original owners.