Atomic Swap (EVM)
Implement atomic swaps on EVM-compatible blockchains using smart contracts
Contract Architecture
Garden uses Hashed Time Lock Contracts (HTLCs) to implement atomic swap functionality on EVM chains. The contract manages the lifecycle of a swap through four main operations:
Initiate
Lock funds in the contract with a secret hash and timelock
Redeem
Claim funds by revealing the secret that matches the hash
Refund
Reclaim funds after the timelock expires
Instant Refund
Cancel the swap with mutual agreement from both parties
Core Functions
Initiate
The initiate function creates a new HTLC by locking tokens in the contract. It requires:
- Secret Hash: SHA256 hash of a secret known only to the initiator
- Timelock: Block number after which refund is possible
- Redeemer Address: Address authorized to claim the funds
- Token Amount: Amount of tokens to lock
EVM Advantage: The function interface makes it easy to integrate with existing Solidity tooling and provides clear input validation.
Redeem
The redeem function allows the redeemer to claim the locked tokens by providing the secret that hashes to the stored secret hash.
The secret must hash to the exact value stored during initiation. Once revealed, this secret enables the counterparty to claim funds on the other chain.
Refund
The refund function allows the initiator to reclaim their tokens after the timelock has expired and the redeemer has not claimed the funds.
EVM Advantage: Uses absolute block numbers for timelock, which can be more predictable than Bitcoin’s relative timelock mechanism.
Instant Refund
The instant refund function provides a way for both parties to mutually agree to cancel the swap before the timelock expires.
EVM-Specific Features
State Management
The contract tracks each swap’s state through an enumeration - a feature not available in Bitcoin scripts:
Event Logging
The contract emits events for each state transition to enable efficient off-chain monitoring: