Notum logo

Blog

All

Crypto

Blockchain

How-to Guide

Tokens & Coins

Product Updates

Announcements

Lending

Trading

Staking

DeFi

How Do Atomic Swaps Work? | Notum

By Notum

Apr 14, 20224 min read

image

Centralized and Decentralized Crypto Exchanges

Centralized crypto exchanges (CEXs) like Gemini, Huobi, or Kucoin are trading institutions for digital assets. CEXs provide trading operations that imitate and improve upon trading platforms for such conventional assets as stocks and commodities. These platforms are called "centralized" because they are custodial, and that means they are responsible for governing deposited funds and wallets. That's like a bank manages access to trading accounts, the same way centralized crypto exchanges handle access to users' private keys. These keys act like debit card PINs, giving access to user accounts at all times.

A CEX provides asset liquidity, governs trading pairs and order books, checks fair market prices, and connects buyers and sellers with instant trading. Centralized exchanges are important to adopting digital assets, but advanced crypto traders prefer to control everything on their own and to have greater autonomy in keeping crypto. Here’s the moment when Decentralized exchanges (DEXs) appear looking for a possibility to integrate trading capacities with non-custodial nature.

Decentralized exchanges allow investors to trade digital assets without utilizing an exchange-based wallet. Instead, users can trade crypto directly from their non-custodial wallet — which means only they hold the private keys. However, different DEX platforms utilize different trading mechanisms to achieve this overall dynamic. DEX structures and features are continually evolving. 


What are Atomic Swaps?

Atomic swaps could be defined as automatic exchange contracts that let two parties trade tokens from two different blockchains. It is sometimes called atomic cross-chain trading, and such a mechanism removes the need for centralized third parties when executing trades. This system preserves the autonomy of crypto users and makes trustless transactions possible, so users do not need to know each other to have deals. 

Thanks to the peer-to-peer (P2P) nature of atomic swaps, it is widely believed to be one of the few purely decentralized trading techniques within the crypto world. 

For example, Litecoin founder Charlie Lee exchanged litecoin (LTC) and bitcoin (BTC) via atomic swap in 2017, and a Komodo platform facilitated atomic swaps across 95% of all cryptocurrencies.

 

How do atomic swaps work?

Traders can have total control of their cryptocurrency at every step of the exchange process when they use atomic swap. Users pay only standard blockchain fees, like ETH gas fees, for example, without a third party or any fees associated with an exchange platform.

“Atomic” is a word used to name processes that would either finalize or would not start at all. An atomic swap comes with functionalities that guarantee that two sides of trade accomplish all prearranged conditions before the trade is completed. That’s possible by incorporating smart contracts, which are self-executed programs that apply the conditions leading to the success of a transaction. 

An atomic swap uses a Hashed Timelock Contract (HTLC), which acts as a two-way virtual safe. This contract uses a complicated mathematical-based encryption mechanism called a hash function. It also introduces a time limit, so the transactions are reversed if one of the parties involved does not carry out their sides of the bargain within a preset time frame. 

Let’s have a look at an example, there are two parties involved, and they agreed to set a three-hour time limit for the atomic swap. Thus, the contract will return the deposited coins to their original owners when 3 hours pass and not all of the trading requirements have been fulfilled. 

There’s another important detail about the HTLC is that it demands two cryptography or encrypted keys:

  • Hashlock key: This key makes sure that trades are only finished when both parties give cryptographic proofs that they have accomplished their part of the transaction. 

     
  • Timelock key: This one is made for safety reasons, and it helps traders to define a deadline for atomic swaps. The deposited coins are returned to traders when the swap is not completed for one reason or the other before the deadline passes.

     

How Do Atomic Swaps Execute?

The process is much clearer with an example. So, let’s imagine there are two people, Ada and George, who have agreed to make a trade with bitcoin and ethereum. Ada wants to trade 2 BTC in exchange for Georges’s 30 ETH. First, Ada creates a contract address where she will send her 2 BTC. When she deposits her assets, the contract automatically generates a unique key that only Ada can access. This key is a kind of password that unlocks the funds Ada just sent to the smart contract. 

The contract uses this key to create a hashed representation (an encrypted form) of the key. Next, Ada sends that hash to George. In this case, George only has access to the hashed form of the passcode used to lock Ada’s 2 BTC. George can confirm that he has locked the assets within the contract, but he still can’t t access or withdraw the assets.

After getting the hashed key, George uses the key to create his own contract address where he can deposit his 30 ETH. Both parties have locked their funds within the smart contract, so Ada needs to claim the 30 ETH. She can do this because she has access to the passcode that unlocks the key used by George to lock her coins on the smart contract. While the unlocking process of George’s contract address, Ada also reveals the passcode to George. George can use this passcode to ask for his 2 BTC and, by doing this, finish the trade. 

The whole process is tied to the capability of both parties to send cryptographic proofs. Ada had to initially encrypt a key and then send the encrypted key to George. Since he has the original key, he can claim the coins that Ada used the encrypted key to lock. The necessary condition for unlocking such coins is that Ada must submit the original key to George. This way, George can access the key and use it to claim the 2 BTC. 

 

Follow us on various social media to get more fruitful information and be always updated! We'll be happy to meet you here, on our blog, and also on TwitterRedditTelegramPinterest, Instagram, and YouTube! 💚  Download a SimpleHold wallet as an extension on your favorite browser Brave, Opera, Firefox, or Chrome, and also a mobile app on iOS and Android.