DEV Community

Laruh Development Presents
Laruh Development Presents

Posted on

What Are Atomic Swaps?

Definition

An atomic swap is a cryptocurrency trade that occurs directly between two users, without any intermediaries. These swaps are described as "atomic" because the trade either fully completes—where both parties receive the agreed assets—or fails entirely, with each party retaining their original funds.

Atomic swaps are executed wallet-to-wallet in a fully peer-to-peer (P2P) manner, ensuring that both traders maintain control of their private keys throughout the entire process, making the exchange significantly more secure than trading on centralized platforms.

Introduction to Participants

In an atomic swap, there are two main participants: the Maker (liquidity provider) and the Taker (liquidity receiver). The Maker holds one type of cryptocurrency, while the Taker holds another. The atomic swap process enables them to securely exchange these assets in a decentralized manner without relying on intermediaries.

Step-by-Step Process

Step 0: Market Maker Posts Trade Order
Before the swap begins, the Maker posts a trade order on the platform. This step, although not technically part of the atomic swap process, is essential for initiating the swap.

Step 1: Taker Initiates the Swap
The atomic swap process begins when the Taker sees the Maker’s offer and accepts it. The Taker pays a protocol fee known as the DEX fee. This fee ensures that the Taker is serious about the trade and prevents spamming of the network. Once the DEX fee is paid, the atomic swap officially starts.

Step 2: Maker Sends Payment (Maker Payment)
The Maker then generates a 32-byte secret and creates a hash of this secret. This hash is shared with the Taker, but the secret itself remains unknown to the Taker. The Maker sends the Maker Payment to a secure, temporary holding address. The nature of this address depends on the type of blockchain:

  • For UTXO-based blockchains (like Bitcoin): The payment is sent to a P2SH (Pay-to-Script-Hash) address. This address is both hash-locked and time-locked.
  • For ETH/ERC20 blockchains: The payment is sent to an etomic-swap smart contract that similarly locks the payment.

The Maker Payment is time-locked to ensure that if the swap is not completed within a set time frame (e.g., 4 hours and 20 minutes for Bitcoin), the Maker can reclaim their funds.

Step 3: Taker Sends Payment (Taker Payment)
After seeing that the Maker has sent the Maker Payment, the Taker sends their payment to a similar secure, temporary holding address. The Taker Payment is also hash-locked using the same hash provided by the Maker and time-locked, but with a shorter duration (typically half of the Maker’s time lock).

  • For UTXO-based blockchains: The Taker Payment is sent to a P2SH hash/time locked output.
  • For ETH/ERC20 blockchains: The Taker Payment is sent to an etomic-swap smart contract.

This step ensures that if the Maker does not proceed with the next step, the Taker can reclaim their funds once the shorter time lock expires.

Step 4: Maker Claims Taker Payment
The Maker sees that the Taker has sent the Taker Payment. To claim it, the Maker reveals the 32-byte secret, which is then broadcast to the blockchain network. This action allows the Maker to unlock and transfer the Taker Payment to their own address. Once the secret is revealed, it becomes publicly visible.

Step 5: Taker Claims Maker Payment
Finally, the Taker, having observed that the Maker has revealed the secret by claiming the Taker Payment, uses the same secret to unlock the Maker Payment. The Taker then transfers the Maker Payment to their own address, completing the swap.

Summary

This atomic swap process ensures a secure and trustless exchange of assets between the Maker and the Taker. The use of cryptographic mechanisms such as hash locks and time locks (HTLC protocol) guarantees that the swap will either be completed as agreed, or both parties will retain their original assets if the swap fails. The process operates in a decentralized manner, with no intermediaries involved, making it a highly secure method for peer-to-peer cryptocurrency exchanges.

ru version

Top comments (0)