An atomic swap is a decentralized, peer-to-peer exchange of cryptocurrencies from separate blockchain networks. It does not require a trusted third party or a centralized intermediary.
Using a specialized smart contract called a Hash Timelock Contract (HTLC), atomic swaps ensure that the transaction either completes successfully with both parties receiving the agreed-upon assets or fails. If the swap fails, the funds are returned to their original owners.
Atomic swaps use HTLCs as virtual vaults to lock funds and enforce swap conditions. Both parties deposit their respective cryptocurrencies into the HTLC, each locked with a cryptographic hash and a timelock.
The transaction proceeds only if both deposits are made correctly within the set time frame. If either party does not fulfill their part, the smart contract automatically refunds the assets to their original owners.
Consider a scenario where Alice wants to swap 10 X tokens with Bob for 10 Y tokens. They initiate an atomic swap by creating an HTLC that expires in one hour. Alice deposits her 10 X tokens into the contract, generating a private key and a cryptographic hash. Bob verifies the deposit and then deposits his 10 Y tokens into a new contract using the same hash. Alice can claim Bob's tokens by revealing the private key, which Bob then uses to withdraw Alice's tokens. If the swap is not completed within the hour, the funds are automatically returned to their original owners.