What are Atomic Swaps?
Atomic Swaps are smart contract-based token exchanges that guarantee both parties receive their tokens or the transaction is completely reverted.
Key Properties
- All-or-Nothing: Swap completes fully or reverts entirely
- Trustless: No intermediary custody of funds
- Non-Custodial: You always control your keys
- On-Chain: Executed via smart contracts
- Instant Settlement: No waiting periods (on same chain)
Atomic Guarantee
Atomic Swap Feature Flag
Rainbow has an experimental atomic swaps feature:Atomic swaps are currently an experimental feature in Rainbow. Enable in Settings > Advanced > Experimental Features.
How Atomic Swaps Work
Single Transaction Execution
Prepare Swap Data
Bundle all operations:
- Token approval (if needed)
- Swap execution
- Slippage protection
- Recipient verification
Atomic Execution
Contract executes atomically:
- Validates input amount
- Executes swap through DEX
- Verifies output meets minimum
- Transfers output to recipient
- All steps succeed or all revert
Atomic Swap vs Standard Swap
- Atomic Swap
- Standard Swap
Single TransactionAdvantages:
- Guaranteed atomicity
- Lower gas (one transaction)
- Faster execution
- No approval + swap delay
- MEV protection inherent
- Requires contract support
- Less flexibility
- May need higher gas limit
Slippage Protection
Atomic swaps enforce slippage limits:Slippage Scenarios
Within Tolerance
Within Tolerance
Scenario: Price moved 2%, slippage set to 5%Result:
- Swap executes successfully
- User receives slightly less than quoted
- Still within acceptable range
- Transaction confirms
Exceeds Tolerance
Exceeds Tolerance
Scenario: Price moved 7%, slippage set to 5%Result:
- Swap would give too little output
- Contract enforces minimum
- Entire transaction reverts
- User keeps original tokens
- Gas still consumed (failed tx)
Price Improved
Price Improved
Scenario: Price moved favorably by 3%Result:
- User receives more than quoted
- No upper limit on positive slippage
- Full benefit goes to user
- Transaction succeeds
Deadline Protection
Swaps include deadline to prevent stale execution:- Execution at stale prices
- MEV attacks from delaying transaction
- Execution in different market conditions
- Unfavorable price movement impact
MEV Protection
Atomic swaps have built-in MEV resistance:How MEV Affects Swaps
Sandwich Attack:- MEV bot sees your pending swap
- Bot front-runs: Buys token before you
- Price increases
- Your swap executes at worse price
- Bot back-runs: Sells token
- Bot profits, you lose
Atomic Swap Protection
- Slippage limit: Bot can’t profit beyond tolerance
- Deadline: Limits time window for manipulation
- Atomic execution: No approval + swap gap
- Single transaction: Reduced attack surface
Cross-Chain Atomic Swaps
Cross-chain atomicity is more complex:Hash Time-Locked Contracts (HTLCs)
Cross-Chain Challenges
- Different block times: Chains confirm at different speeds
- Timelock coordination: Must account for both chains
- Failure modes: One chain succeeds, other fails
- Gas on both chains: Need native tokens on both
True cross-chain atomic swaps are rare in practice. Most “cross-chain swaps” in Rainbow use trusted bridge protocols, which are not atomic in the strict sense.
Swap Execution Flow
Detailed atomic swap execution:Gas Optimization
Atomic swaps can save gas:Gas Comparison
- Only one transaction fee
- No approval transaction overhead
- Optimized contract call
- Batch operations
Safety Features
Pre-Flight Checks
Revert Protection
Limitations
Atomic swaps have constraints:- Not all DEXs support: Requires specific contract interface
- Single liquidity source: Can’t split across multiple DEXs
- Higher initial gas: More complex single transaction
- Less common: Standard swaps more widely supported
- Experimental: Feature still in testing phase
Enabling Atomic Swaps
Understand Risks
Read warning about experimental feature:
- May have bugs
- Limited DEX support
- Higher gas if estimate wrong
- Can revert to standard swaps
You can disable atomic swaps anytime. Existing pending swaps will complete with the method they started with.
Best Practices
Start with Small Amounts
Start with Small Amounts
When first using atomic swaps:
- Test with small value
- Verify execution works
- Check gas costs
- Confirm output received
- Scale up after success
Monitor Gas Estimates
Monitor Gas Estimates
Atomic swaps gas:
- Check estimate before confirming
- Compare to standard swap
- Ensure sufficient balance for gas
- Account for gas price volatility
Set Appropriate Slippage
Set Appropriate Slippage
Slippage for atomic swaps:
- Can’t adjust after submission
- Set higher for volatile tokens
- Too low = likely to revert
- Too high = worse execution
- 2-5% typical for most pairs
Verify Token Contracts
Verify Token Contracts
Before atomic swap:
- Confirm token addresses
- Check both tokens are legitimate
- Verify you’re on correct chain
- No recovery if sent to wrong token
Future Enhancements
Planned improvements:- Multi-DEX atomic swaps: Split across sources atomically
- Better gas estimates: More accurate predictions
- Wider DEX support: More liquidity sources
- Cross-chain HTLCs: True atomic cross-chain swaps
- Batched operations: Multiple swaps in one transaction
Related Features
Swaps Overview
Complete swap functionality
Quote Fetching
How quotes are obtained
Gas Estimation
Gas calculation details