Starknet
Implement atomic swaps on Starknet using Cairo contracts and SNIP-12 signatures
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:
Initiate
Start the cross-chain swap by depositing funds into the HTLC on the source chain.
Redeem
Complete the cross-chain swap by redeeming funds from the HTLC on the destination chain.
Refund
Get your funds refunded on the source chain, if swap does not happen within the settlement window.
Instant Refund
Cancel the swap and get an instant refund on the source chain during the settlement window.
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 SHA-256. 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 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.
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.
Cairo Language Features
The contract leverages Cairo’s safety features and storage optimizations:
- Storage Maps: Efficient key-value storage for orders
- Contract Address Types: Type-safe address handling
- Felt252: Native field element type for hashes and identifiers
- Array Types: Dynamic arrays for signatures and secrets
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.