Garden uses Hashed Time Lock Contracts (HTLCs) to implement atomic swap functionality on Solana. The program manages the lifecycle of a swap through four main operations using Program Derived Addresses (PDAs) for state management:
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. No signature required — anyone can execute if they know the secret.
The program uses a PDA (Program Derived Address) to store swap state, derived from deterministic seeds:
Copy
Ask AI
#[account]#[derive(InitSpace)]pub struct SwapAccount { amount_lamports: u64, // SOL amount in base units expiry_slot: u64, // Absolute slot for expiry initiator: Pubkey, // Initiator's public key redeemer: Pubkey, // Redeemer's public key secret_hash: [u8; 32], // SHA256 hash of secret}
Custom error types provide clear feedback for failed operations:
Copy
Ask AI
#[error_code]pub enum SwapError { #[msg("The provided initiator is not the original initiator of this swap")] InvalidInitiator, #[msg("The provided redeemer is not the original redeemer of this swap")] InvalidRedeemer, #[msg("The provided secret does not correspond to the secret hash of this swap")] InvalidSecret, #[msg("Attempt to perform a refund before expiry time")] RefundBeforeExpiry,}