Contract architecture
Garden uses Hashed Time Lock Contracts (HTLCs) to implement atomic swap functionality on Starknet. The contract manages the lifecycle of a swap through four main operations with enhanced signature support and flexible initiation methods:Core functions
Initiate
The initiate function creates a new HTLC by locking ERC20 tokens in the contract. Starknet provides three flexible initiation methods:Basic initiation
Initiation on behalf
Signature-based initiation
Uses SNIP-12 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.Starknet uses block number-based timing similar to Ethereum, providing predictable settlement windows based on Starknet’s consistent block production.
Instant refund
The instant refund function provides a way for the redeemer to consent to canceling the swap before the timelock expires using SNIP-12 signatures.This requires the redeemer’s SNIP-12 signature to prevent unauthorized instant refunds. This ensures mutual consent before the settlement window expires.
Starknet-specific features
Order state management
The contract uses a Cairo struct to store swap state with efficient storage patterns:Poseidon-based order IDs
Starknet uses the Poseidon hash function for generating unique order identifiers:Poseidon hashing is optimized for zero-knowledge proofs and provides efficient computation on Starknet while ensuring order uniqueness.
SNIP-12 signature validation
The contract implements SNIP-12 (Starknet Improvement Proposal 12) for secure off-chain message signing:Event logging
The contract emits events for each state transition to enable efficient off-chain monitoring:ERC20 token integration
Unlike native currency swaps, Starknet HTLC works with ERC20 tokens:The contract uses OpenZeppelin’s ERC20 interface for secure token transfers with proper allowance and balance checks.
Gas optimization
Starknet’s unique fee model enables several optimizations:Parameter validation is grouped into a single function to optimize for Starknet’s execution model while maintaining security.