Skip to main content

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 with enhanced signature support and flexible initiation methods:

Initiate

Redeem

Refund

Instant refund

Core functions

Initiate

The initiate function creates a new HTLC by locking tokens in the contract. EVM provides different implementations for ERC20 tokens vs. native ETH:

Basic initiation

Initiation on behalf

Signature-based initiation (ERC20 only)

Uses EIP712 signatures for off-chain authorization, enabling gasless transactions where authorized third parties can initiate swaps on behalf of users.

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 using SHA256. Once revealed, this secret enables the counterparty to claim funds on the other chain. No signature required - anyone can execute if they know the secret.

Refund

The refund function allows the initiator to reclaim their tokens after the timelock has expired and the redeemer has not claimed the funds.
Uses absolute block numbers for timelock, which provides predictable settlement windows based on consistent block production times.

Instant refund

The instant refund function provides a way for the redeemer to consent to canceling the swap before the timelock expires using EIP712 signatures.
This requires the redeemer’s EIP712 signature to prevent unauthorized instant refunds. This ensures mutual consent before the settlement window expires.

EVM-specific features

Order state management

Both contracts use a struct to store swap state with different address types for native vs. ERC20 handling:

Token handling differences

ERC20 implementation:
  • Uses SafeERC20 for secure token transfers.
  • Requires token approval before initiation.
  • Transfers tokens via transferFrom and transfer.
Native ETH implementation:
  • Uses payable functions and msg.value.
  • Direct ETH transfers via .transfer().
  • No approval required.
The native ETH version simplifies the user experience by eliminating the need for token approvals, while the ERC20 version provides compatibility with all standard tokens.

EIP712 signature support

Both contracts implement EIP712 for secure off-chain message signing:

Event logging

Both contracts emit identical events for each state transition to enable efficient off-chain monitoring:

Order ID generation

Unique order identifiers are generated using SHA256 hashing with chain-specific parameters:
Chain ID inclusion prevents cross-chain replay attacks, while the parameter combination ensures each order is uniquely identifiable across the network.