Installation
How It Works
- The SDK initiates a flash loan from Aave Protocol V3 to borrow the required assets
- The borrowed assets are swapped to the desired collateral using CoW Protocol’s batch auction system
- Pre and post-execution hooks deploy adapter contracts and manage the entire swap flow
- The flash loan is automatically repaid with fees, completing the atomic transaction
Why Use Flash Loans?
- Capital Efficiency - No upfront capital needed for collateral swaps
- Atomic Execution - Borrow, swap, and repay in a single transaction
- Gas Optimization - Optimized hook architecture minimizes gas costs
- Aave Integration - Leverages Aave V3’s deep liquidity pools
- CoW Protocol Benefits - MEV protection through batch auctions
Constructor
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.string
required
Trader address
string
required
Aave aToken address
bigint
required
Required amount
string
required
Flash loan adapter address
approveCollateral
Approve the flash loan adapter to spend collateral tokens.CollateralAllowanceParams
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):- Accrue interest automatically
- Can be used for flash loan collateral swaps
- Examples:
aGnoWXDAI,aGnoUSDC
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.
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