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:

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
function initiate(
    bytes32 secretHash,
    uint256 timelock,
    address redeemer,
    uint256 amount
) external payable;

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.

function redeem(
    bytes32 swapId,
    bytes32 secret
) external;

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.

function refund(bytes32 swapId) external;

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.

function instantRefund(
    bytes32 swapId,
    bytes memory initiatorSignature,
    bytes memory redeemerSignature
) external;

EVM-Specific Features

State Management

The contract tracks each swap’s state through an enumeration - a feature not available in Bitcoin scripts:

enum SwapState {
    INVALID,    // Swap doesn't exist
    OPEN,       // Funds locked, awaiting redemption
    REDEEMED,   // Secret revealed, funds claimed
    REFUNDED    // Timelock expired, funds returned
}

Event Logging

The contract emits events for each state transition to enable efficient off-chain monitoring:

event SwapInitiated(bytes32 indexed swapId, address indexed initiator, address indexed redeemer);
event SwapRedeemed(bytes32 indexed swapId, bytes32 secret);
event SwapRefunded(bytes32 indexed swapId);
event SwapInstantRefunded(bytes32 indexed swapId);