BitcoinDev Logo

Atomic Swaps

Atomic swaps enable trustless peer-to-peer exchange of cryptocurrencies across different blockchains without requiring a trusted third party or centralized exchange.

Atomic swaps use hash time-locked contracts (HTLCs) to ensure that either both parties receive their funds or neither does:

Atomic Swap:
- Alice wants to trade BTC for LTC
- Bob wants to trade LTC for BTC
- Either both succeed or both fail
- No trusted intermediary needed

How Atomic Swaps Work

Process

Safety

If Alice doesn't reveal:
- Bob's HTLC expires
- Bob gets LTC back
- Alice's HTLC expires
- Alice gets BTC back

If Bob doesn't create HTLC:
- Alice's HTLC expires
- Alice gets BTC back
- No loss for Alice

Code Examples

Creating HTLC


Lightning Network Swaps

Atomic swaps can also work on Lightning Network:

Lightning Atomic Swap:
- Swap between Lightning channels
- Instant execution
- Lower fees
- Requires channel liquidity

Limitations

Requirements

  1. Compatible blockchains: Both must support HTLCs
  2. Hash function compatibility: Same hash function
  3. Timing coordination: Time locks must be coordinated
  4. Liquidity: Both parties need funds available

Challenges

  • Complexity: Requires technical knowledge
  • Timing: Time locks must be carefully set
  • Liquidity: Both chains need funds
  • Adoption: Limited user-friendly tools


Resources