Installation
How It Works
1
Borrow tokens via Aave flash loan
The SDK initiates a flash loan from Aave Protocol V3 to borrow the required assets
2
Execute CoW Protocol swap
The borrowed assets are swapped to the desired collateral using CoW Protocol’s batch auction system
3
Use CoW hooks to manage flow
Pre and post-execution hooks deploy adapter contracts and manage the entire swap flow
4
Repay flash loan automatically
The flash loan is automatically repaid with fees, completing the atomic transaction
Why Use Flash Loans?
- Capital Efficiency - No upfront capital required to swap collateral
- Atomic Execution - The entire operation succeeds or reverts atomically
- Gas Optimization - Single transaction for complex multi-step operations
- Aave Integration - Leverage Aave V3’s flash loan infrastructure
- CoW Protocol Benefits - MEV protection and optimal execution via batch auctions
Constructor
AaveCollateralSwapSdk
AaveCollateralSwapSdkOptions
Optional SDK configuration
Methods
collateralSwap
Execute a complete flash loan-based collateral swap with automatic approval handling.CollateralSwapParams
required
Swap parameters
TradingSdk
required
Initialized TradingSdk instance
string
Unique identifier for the created order
getSwapQuoteParams
Prepare quote parameters for manual quote fetching.CollateralSwapParams
required
Same parameters as collateralSwap
getOrderPostingSettings
Generate order settings and get the flash loan adapter instance address.CollateralSwapParams
required
Swap parameters
TradeParameters
required
Parameters used for quote
QuoteResults
required
Results from getQuote
getCollateralAllowance
Check the current collateral token allowance for the flash loan adapter.CollateralAllowanceParams
required
approveCollateral
Approve the flash loan adapter to spend collateral tokens.CollateralApprovalParams
required
Same structure as getCollateralAllowance params
Complete Examples
Basic Collateral Swap
Advanced with Manual Approval
Understanding Collateral Tokens
What are aTokens?
When you deposit assets into Aave, you receive aTokens (interest-bearing tokens):- aGnoWXDAI - Aave WXDAI on Gnosis Chain
- aGnoUSDC - Aave USDC on Gnosis Chain
- Accrue interest automatically
- Can be used for flash loan collateral swaps
Common aTokens on Gnosis Chain
Flash Loan Fees
Aave flash loans charge a fee (typically 0.05%):Hook Architecture
The SDK uses CoW Protocol hooks to orchestrate the flash loan:Pre-Hook (300,000 gas default)
- Deploys the Aave adapter contract deterministically
- Transfers the flash loan to the adapter
- Sets up swap parameters
Post-Hook (600,000 gas default)
- Executes collateral swap via the adapter
- Repays Aave flash loan with fees
- Transfers remaining tokens to owner
Gas limits can be customized per operation or set as SDK defaults.
Error Handling
Insufficient flash loan amount
Insufficient flash loan amount
Error: Flash loan amount doesn’t cover fee + swapSolution: Increase amount or adjust flash loan fee
Slippage too tight
Slippage too tight
Error: Slippage tolerance too low for market conditionsSolution: Increase slippageBps
Order expired
Order expired
Error: Order validity period too shortSolution: Increase validFor
Insufficient collateral allowance
Insufficient collateral allowance
Error: Adapter doesn’t have approval to spend collateralSolution: Approve collateral or use permit
Limitations
- Only supports Aave V3 flash loans
- Requires sufficient liquidity in Aave pools
- Flash loan fees apply (typically 0.05%)
- Subject to CoW Protocol order limits
- Network-specific contract deployments required
Security Considerations
- Set appropriate slippage tolerances
- Verify token addresses and decimals
- Monitor transaction execution
- Be aware of flash loan fees and costs
- Ensure sufficient collateral balance
Related APIs
- TradingSdk - Core trading functionality
- OrderBookApi - Order book operations
- Hooks - CoW Protocol hooks documentation