# ๐Ÿ”Œ Integration Guide > Step-by-step guide for integrating DeFi protocols into your application. --- ## ๐Ÿ“‹ Table of Contents 1. [Aave v3 Integration](#-aave-v3-integration) 2. [Uniswap v3 Integration](#-uniswap-v3-integration) 3. [Protocolink Integration](#-protocolink-integration) 4. [Compound III Integration](#-compound-iii-integration) 5. [Cross-Protocol Strategies](#-cross-protocol-strategies) --- ## ๐Ÿฆ Aave v3 Integration ### 1๏ธโƒฃ Setup ```typescript import { createWalletRpcClient } from '../src/utils/chain-config.js'; import { getAavePoolAddress } from '../src/utils/addresses.js'; const CHAIN_ID = 1; // Mainnet const walletClient = createWalletRpcClient(CHAIN_ID, privateKey); const poolAddress = getAavePoolAddress(CHAIN_ID); ``` ### 2๏ธโƒฃ Supply Collateral ```typescript // 1. Approve token await walletClient.writeContract({ address: tokenAddress, abi: ERC20_ABI, functionName: 'approve', args: [poolAddress, amount], }); // 2. Supply await walletClient.writeContract({ address: poolAddress, abi: POOL_ABI, functionName: 'supply', args: [asset, amount, account, 0], }); // 3. Enable as collateral await walletClient.writeContract({ address: poolAddress, abi: POOL_ABI, functionName: 'setUserUseReserveAsCollateral', args: [asset, true], }); ``` ### 3๏ธโƒฃ Borrow ```typescript // Note: Use variable rate (2), stable rate is deprecated in v3.3+ await walletClient.writeContract({ address: poolAddress, abi: POOL_ABI, functionName: 'borrow', args: [debtAsset, borrowAmount, 2, 0, account], }); ``` ### 4๏ธโƒฃ Flash Loans #### Single Asset ```typescript await walletClient.writeContract({ address: poolAddress, abi: POOL_ABI, functionName: 'flashLoanSimple', args: [receiverAddress, asset, amount, params, 0], }); ``` #### Multi-Asset ```typescript await walletClient.writeContract({ address: poolAddress, abi: POOL_ABI, functionName: 'flashLoan', args: [receiverAddress, assets, amounts, modes, account, params, 0], }); ``` > โš ๏ธ **Important**: Your flash loan receiver contract must: > 1. โœ… Receive the loaned tokens > 2. โœ… Perform desired operations > 3. โœ… Approve the pool for `amount + premium` > 4. โœ… Return `true` from `executeOperation` --- ## ๐Ÿ”„ Uniswap v3 Integration ### 1๏ธโƒฃ Setup ```typescript import { getUniswapSwapRouter02 } from '../src/utils/addresses.js'; const routerAddress = getUniswapSwapRouter02(CHAIN_ID); ``` ### 2๏ธโƒฃ Get Quote ```typescript // Use QuoterV2 contract to get expected output const quote = await publicClient.readContract({ address: quoterAddress, abi: QUOTER_ABI, functionName: 'quoteExactInputSingle', args: [{ tokenIn: tokenInAddress, tokenOut: tokenOutAddress, fee: 3000, // 0.3% fee tier amountIn: amountIn, sqrtPriceLimitX96: 0, }], }); ``` ### 3๏ธโƒฃ Execute Swap ```typescript // 1. Approve token await walletClient.writeContract({ address: tokenInAddress, abi: ERC20_ABI, functionName: 'approve', args: [routerAddress, amountIn], }); // 2. Execute swap await walletClient.writeContract({ address: routerAddress, abi: SWAP_ROUTER_ABI, functionName: 'exactInputSingle', args: [{ tokenIn: tokenInAddress, tokenOut: tokenOutAddress, fee: 3000, recipient: account, deadline: BigInt(Math.floor(Date.now() / 1000) + 600), amountIn: amountIn, amountOutMinimum: amountOutMin, // Apply slippage protection sqrtPriceLimitX96: 0, }], }); ``` ### 4๏ธโƒฃ TWAP Oracle ```typescript // Always use TWAP, not spot prices, to protect against manipulation // See examples/ts/uniswap-v3-oracle.ts for implementation ``` --- ## ๐Ÿ”— Protocolink Integration ### 1๏ธโƒฃ Setup ```typescript import * as api from '@protocolink/api'; import * as common from '@protocolink/common'; const CHAIN_ID = common.ChainId.mainnet; ``` ### 2๏ธโƒฃ Build Logics ```typescript // Swap logic const swapQuotation = await api.protocols.uniswapv3.getSwapTokenQuotation(CHAIN_ID, { input: { token: USDC, amount: '1000' }, tokenOut: WBTC, slippage: 100, }); const swapLogic = api.protocols.uniswapv3.newSwapTokenLogic(swapQuotation); // Supply logic const supplyQuotation = await api.protocols.aavev3.getSupplyQuotation(CHAIN_ID, { input: swapQuotation.output, }); const supplyLogic = api.protocols.aavev3.newSupplyLogic(supplyQuotation); ``` ### 3๏ธโƒฃ Execute ```typescript const routerData = await api.router.getRouterData(CHAIN_ID, { account, logics: [swapLogic, supplyLogic], }); await walletClient.sendTransaction({ to: routerData.router, data: routerData.data, value: BigInt(routerData.estimation.value || '0'), gas: BigInt(routerData.estimation.gas), }); ``` --- ## ๐Ÿ›๏ธ Compound III Integration ### 1๏ธโƒฃ Setup ```typescript import { getCompound3Comet } from '../src/utils/addresses.js'; const cometAddress = getCompound3Comet(CHAIN_ID); ``` ### 2๏ธโƒฃ Supply Collateral ```typescript // 1. Approve collateral await walletClient.writeContract({ address: collateralAddress, abi: ERC20_ABI, functionName: 'approve', args: [cometAddress, amount], }); // 2. Supply await walletClient.writeContract({ address: cometAddress, abi: COMET_ABI, functionName: 'supply', args: [collateralAddress, amount], }); ``` ### 3๏ธโƒฃ Borrow Base Asset ```typescript // In Compound III, you "borrow" by withdrawing the base asset const baseToken = await publicClient.readContract({ address: cometAddress, abi: COMET_ABI, functionName: 'baseToken', }); await walletClient.writeContract({ address: cometAddress, abi: COMET_ABI, functionName: 'withdraw', args: [baseToken, borrowAmount], }); ``` --- ## ๐Ÿ”„ Cross-Protocol Strategies ### โšก Flash Loan Arbitrage **Strategy Flow:** 1. โšก Flash loan asset from Aave 2. ๐Ÿ”„ Swap on Uniswap (or other DEX) 3. ๐Ÿ”„ Swap on different DEX/pool 4. โœ… Repay flash loan + premium 5. ๐Ÿ’ฐ Keep profit > ๐Ÿ“– See `examples/ts/flashloan-arbitrage.ts` for conceptual example. ### ๐Ÿ“ˆ Supply-Borrow-Swap **Strategy Flow:** 1. ๐Ÿ’ฐ Supply collateral to Aave 2. ๐Ÿ’ธ Borrow asset 3. ๐Ÿ”„ Swap borrowed asset 4. ๐Ÿ’ฐ Supply swapped asset back to Aave > ๐Ÿ“– See `examples/ts/supply-borrow-swap.ts` for implementation. --- ## ๐Ÿ’ก Best Practices | Practice | Description | Status | |----------|-------------|--------| | ๐Ÿ›ก๏ธ **Slippage Protection** | Always set minimum output amounts | โœ… | | โ›ฝ **Gas Costs** | Check gas costs for complex transactions | โœ… | | ๐Ÿ”ฎ **TWAP Oracles** | Never rely on spot prices alone | โœ… | | ๐Ÿงช **Test on Testnets** | Always test before mainnet | โœ… | | โš ๏ธ **Error Handling** | Handle errors gracefully | โœ… | | ๐Ÿ“Š **Monitor Positions** | Track liquidation risks | โœ… | | ๐Ÿ” **Use Permit2** | Save gas on approvals when possible | โœ… | --- ## ๐ŸŽฏ Next Steps - ๐Ÿ“– Review [Security Best Practices](./SECURITY.md) - ๐Ÿ”— Check [Chain Configuration](./CHAIN_CONFIG.md) for adding new chains - ๐Ÿ“œ Explore example contracts in `contracts/examples/` - ๐Ÿงช Run tests in `test/` --- ## ๐Ÿ“š Related Documentation - ๐Ÿ” [Security Best Practices](./SECURITY.md) - ๐Ÿ”— [Chain Configuration](./CHAIN_CONFIG.md) - ๐Ÿงช [Strategy Testing Guide](./STRATEGY_TESTING.md) - โš™๏ธ [Environment Setup](./ENV_SETUP.md)