How do Atomic Swaps Empower P2P Trading
Atomic swap is a technology based on smart contract that enables the exchange of cryptocurrencies without the need for any intermediary. Also known as atomic cross-chain trading, atomic swaps facilitate exchange between two parties even if the cryptocurrencies run in different blockchain networks. The concept is leveraged by many blockchain developers for P2P exchange development.
But, what led to the need for atomic swaps? To comprehend this, let’s understand how trading happens on cryptocurrency exchange platforms.
When a user opens an account on a cryptocurrency exchange, a wallet is created for the user. This is known as a custodial wallet because although the user owns the wallet, the private key is held by the exchange. All of the transactions are executed on the exchange itself. The user does not have direct access to his wallet or digital assets.
The custodial wallets work as a safety box deposit rent in the bank. The user decides what to store in the box and when to move the assets to and from the box. However, the user cannot gain direct access to the box; a bank is needed for the same.
So, what is the problem with this solution? Having an exchange that takes care of your assets is actually good.
The problem is the centralized nature of such exchanges. A single entity has access to private keys, making them vulnerable to attacks. Furthermore, centralized exchanges rely on a middleman to operate.
P2P exchange development makes intermediary functions redundant. But, how could trusted transactions execute if there is no middleman? The answer is atomic swaps.
How do Atomic Swaps Drive P2P Transactions?
To understand the same, let’s consider the safe deposit box example again.
Say David wants to trade US Dollar with Sam in return for Euro. Since both of them have not established trust yet, this is what they do:
Both David and Sam take two strongboxes that can communicate with each other. One of the boxes has a key hidden in a small locked chamber; that is David’s box. David puts his US Dollar into the box (let’s call it Dollar box) and gives it to Sam. Sam cannot access the Dollar box because he does not have the key.
Similarly, Sam puts his Euro into his strongbox (let’s call it Euro box). He locks the box and gives it to David. David, too, cannot take money out of Euro box because he does not have the key to access the box.
Both David and Sam have each other’s box, but cannot access the same as the boxes are locked. As soon as David and Sam are ready to execute the trade, they both exchange the keys, granting each other access to their boxes.
Once the agreed upon terms are matched, the boxes unlock to execute the transaction. Conversely, the transaction terminates if the boxes are not opened within an agreed upon time-frame.
To understand the workings of atomic swaps on the actual blockchain, following are the basic terms worth comprehending in the first place:
Hash function: it creates a unique output and it is impossible for anyone with only the output to the input. For example, if the input is “123456” and the hash function gives the output of “654321”, no other number would have the output “654321”. Additionally, there is no other way to reverse the function and figure out that “123456” was used as an input.
Time-lock contract: it is a smart contract that expires after the time set for a particular transaction has elapsed.
Moving toward cryptocurrency world, say, for example, David wants to sell 1 BTC to Sam for 3 ETH. Here is how the atomic swap would work:
- David deposits his BTC to a smart contract on the Bitcoin chain. He locks it with his key “123456” and provides Sam with the hash which is “654321”.
- Sam deposits his ETH to a smart contract on the Ethereum chain and locks it with the key “654321”. Sam’s contract would only accept the original input “123456” to hash into “654321” and use it as a key.
- When David unlocks the contract using “123456”, Sam knows David’s key as it is public on Ethereum. Sam uses the key to unlock David’s contract on Bitcoin.
Both the contracts are time-locked, which means that if the transaction does not execute within the time frame, the coins will revert to their owners.
One of the main benefits of atomic swaps is that they are resistant to fraud since there is no way one party extorts the other. The Hash Timelock Contracts (HTLC) and hash functions ensure that the transaction either executes completely or terminates.
Moreover, atomic swaps eliminate the need for centralized exchange platforms, enabling P2P trades at much lower costs. Many P2P exchange platforms on the crypto market do not charge any deposit, withdrawal or trading fees. This is one of the reasons why P2P exchange development is gaining worldwide adoption.