What is an Atomic Swap?
An Atomic Swap, also known as a cross-chain swap, is a decentralized way of exchanging cryptocurrencies or digital assets from one blockchain to another, without the need for a centralized exchange or intermediary. An atomic swap is a smart contract-based exchange process that enables users to swap assets directly with one another, in a trustless and secure manner.
In an atomic swap, the assets are locked in a smart contract and the swap is executed automatically when both parties fulfill their respective obligations. The trade is considered "atomic" because it is either completed in its entirety or not at all. This eliminates the risk of one party not following through on their end of the deal and protects both parties assets.
Atomic swaps can be performed between two parties without the need for a third-party intermediary, such as a centralized exchange. This not only increases the security of the exchange, as it eliminates the risk of a centralized exchange being hacked or going bankrupt, but it also helps to reduce the costs associated with using centralized exchanges.
Atomic swaps can be performed between different blockchains that use different consensus algorithms, such as from Bitcoin to Ethereum, or from Litecoin to Decred. This increases the flexibility and interoperability of cryptocurrencies and digital assets, as users are no longer restricted to trading only within a single blockchain ecosystem.
Simple example of an Atomic Swap
An Atomic Swap can be compared to a trade between two people. Imagine two people, one has a car and the other has a boat. They both want each other's vehicles, but they don't trust each other to make the trade fair.
An Atomic Swap acts like a trusted third party, making sure that both people get what they want at the same time. They give their vehicles to the Atomic Swap, and the swap makes sure that each person gets the item they wanted before the trade is finalized. This way, neither person has to worry about the other running off with their vehicle without giving them the trade item in return.
Common examples of Atomic Swaps
Cross-chain Atomic Swap between Bitcoin and Ethereum: A user wants to exchange their Bitcoin (BTC) for Ethereum (ETH) without using a centralized exchange. Both parties agree on the terms of the trade and use hash time-locked contracts (HTLCs) to ensure a trustless transaction. The user first sends their BTC to a multi-sig address and generates a secret key. This secret key is then hashed and included in the ETH transaction as a HTLC. The ETH is sent to the Bitcoin user, who then has a set amount of time to provide the secret key to claim the ETH. If the secret key is not provided within the agreed time, the ETH is returned to the sender.
Decentralized Exchange (DEX) Atomic Swap: A DEX platform implements an Atomic Swap mechanism for trading cryptocurrency assets directly between two users, without the need for a centralized exchange. The trade is executed through HTLCs, where both parties agree on the terms and deposit their assets into the DEX smart contract. Once the trade is executed, the assets are automatically swapped between the two parties, and the trade is settled on the blockchain. The HTLC ensures that the assets are returned to the original owners if one of the parties fails to meet the agreed terms.
Lightning Network Atomic Swap: A user wants to exchange their Bitcoin (BTC) for Litecoin (LTC) without relying on a centralized exchange. They connect to the Lightning Network and find a counterparty who agrees to the trade. Both parties deposit their respective assets into a Lightning channel and create a HTLC that locks the assets until the terms of the trade are met. The secret key is generated and hashed, and the LTC is sent to the Bitcoin user, who then has a set amount of time to provide the secret key to claim the LTC. If the secret key is not provided within the agreed time, the LTC is returned to the sender. The trade is settled on the blockchain once the Lightning channel is closed.