Garden uses Hashed Time Lock Contracts (HTLCs) to implement atomic swap functionality on Sui. The contract leverages Sui’s object model with a shared registry pattern for efficient order management:
The redeem function allows the redeemer to claim the locked tokens by providing the secret that hashes to the stored secret hash.
Copy
Ask AI
public fun redeem_swap<CoinType>( orders_reg: &mut OrdersRegistry<CoinType>, order_id: vector<u8>, secret: vector<u8>, ctx: &mut TxContext,)
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.
The instant refund function provides a way for the redeemer to consent to canceling the swap before the timelock expires using Ed25519 signatures.
Copy
Ask AI
public fun instant_refund<CoinType>( orders_reg: &mut OrdersRegistry<CoinType>, order_id: vector<u8>, signature: vector<u8>, ctx: &mut TxContext,)
This requires the redeemer’s Ed25519 signature to prevent unauthorized instant refunds. This ensures mutual consent before the settlement window expires.
Uses transfer::public_transfer for secure transfers.
No manual balance tracking required.
Sui’s Coin objects provide superior safety compared to manual balance management, as coins cannot be accidentally lost or duplicated due to Move’s resource type system.
The contract emits events for each state transition to enable efficient off-chain monitoring:
Copy
Ask AI
public struct Initiated has copy, drop { order_id: vector<u8>, secret_hash: vector<u8>, amount: u64,}public struct Redeemed has copy, drop { order_id: vector<u8>, secret_hash: vector<u8>, secret: vector<u8>,}public struct Refunded has copy, drop { order_id: vector<u8>,}
Chain ID inclusion prevents cross-chain replay attacks, while the parameter combination ensures each order is uniquely identifiable across the network.