An Atomic swap is a means of converting Bitcoin to an Altcoin without the need for a third party broker such as a cryptocurrency exchange. The normal procedure for converting a Bitcoin into an Altcoin or vice versa requires a third party escrow service to confirm that both parties have indeed released ownership of their coin to the other party. This means that there is a cost to the conversion. An Atomic swap removes the need for escrow services making it a cheaper type of transaction.
Atomic literally means that the conversion must be binary. Therefore the transaction must either happen or it mustn’t. As such, there should be no situation whereby one party receives a coin but the other party does not. The Atomic swap code prevents either side from steeling coin despite not having a third party moderator. The lack of a third party moderator removes the commission charge from the escrow services or equivalent third party brokerage charge.
The Atomic swap mechanism
The Atomic swap requires contracts on both blockchains. This ensures that both parties send and receive coins at the same time. When the coins are ready for transfer then the contract unlocks and both parties receive the desired coin. If either of the contracts fails then both parties get their own coin back again. The cost of this is a simple on-chain transaction mining fee rather than a broker fee.
Atomic swap thought experiment
As a quick example, person A owns 1 Bitcoin and person B owns 1 Altcoin. The generally agreed market exchange rate dictates that 1 Bitcoin is worth 1 Altcoin. Both parties agree to initiate an Atomic swap on their respective blockchains.
A basic understanding:
The contract on the Bitcoin blockchain states that person A’s Bitcoin is taken from their wallet. Similarly, the contract on the Altcoin blockchain dictates that person B’s Altcoin is taken from person B’s wallet. The act of removing coins from wallets sends signals to the other blockchain notifying of the temporary movement. Once the Bitcoin blockchain is made aware that person B’s Altcoin has been removed from their wallet, then the contract releases the Bitcoin to person B’s Bitcoin wallet. Similarly, the contract releases the Altcoin to A’s wallet once the Altcoin blockchain is aware that person A’s Bitcoin has been taken.
If either party did not have enough cryptocurrency or if something went wrong the crypto would not be released by either party. As such the contract would not continue as described above and neither party would have lost out. Consequently, coins are returned back to the original owners’ wallet. As such, person A would get their Bitcoin back and person B would receive their Altcoin back.
Combining Atomic swaps and Lightning Networks
An additional complication is that an Atomic swap can occur on the Lightning Network. This requires both parties to be on the Lightning Network of both coin blockchains. The Atomic swap contract can be activated via the Lightning Network as an off-chain transaction rather than directly on the blockchain. Being off-chain would make the transaction very cheap and quick to run. This is because as it would require mining confirmation at the time of the swap.