Aave
Lend, borrow & interact with Aave's smart contact suite.
Overview
Currently, the FunWallet
SDK supports the following Aave actions:
Withdraw a position from Aave on an EOA's behalf
In the future, the FunWallet SDK will support:
Withdraw a position from Aave
Open a position on Aave
Increase an Aave position's size
Decrease an Aave position's size
To learn more about Aave Module method headers, parameters & return values, visit the Aave Module API Reference.
Example Flow
It is highly recommended to test Aave on a fork of eth-mainnet. See how to test with forks here.
The following flow describes how to use the EoaAaveWithdrawal
action to exit an Aave position on an EOA's behalf. For this flow to work, we require that the EOA give conditional spend approval over its aTokens to a FunWallet
.
Import Requried Libraries
import { ethers } from "ethers";import { FunWallet, FunWalletConfig, Modules } from "@fun-wallet/sdk";const { EoaAaveWithdrawal } = Modules;
Create an ethers.Wallet EOA Instance
// An internal Fun RPC for customer testingconst rpc = "https://avalanche-fuji.infura.io/v3/4a1a0a67f6874be6bb6947a62792dab7";const provider = new ethers.providers.JsonRpcProvider(rpc);// Note that the key here will be exposed and should never be used in productionconst eoa = new ethers.Wallet(PRIV_KEY, provider);
Create & Initialize a FunWallet
const chainID = "43113" // Fuji testnetconst prefundAmt = 0.3 // amount of native gas token to prefund our FunWallet withconst API_KEY = "" // Get your API key from app.fun.xyz/api-keyconst config = new FunWalletConfig(eoa, chainID, prefundAmt)const wallet = new FunWallet(config, API_KEY)// init will perform the pre-funding of the FunWalletawait wallet.init()
Create & Add EoaAaveWithdrawal Module to a FunWallet
const eoaAaveWithdrawalModule = new EoaAaveWithdrawal();await wallet.addModule(eoaAaveWithdrawalModule);
Deploy a FunWallet to a Blockchain with the EoaAaveWithdrawal Module Attached
const deployWalletReceipt = await wallet.deploy();
Generate & Deploy Required EoaAaveWithdrawal Module Pre-Execution Transactions
// Address of aDAI on mainnetconst TOKEN_ADDRESSS = "0x028171bCA77440897B824Ca71D1c56caC55b68A3";// Use max int to withdraw the entire depositconst WITHDRAW_AMOUNT = ethers.constants.MaxInt256const moduleRequiredPreTxs = await eoaAaveWithdrawalModule.getPreExecTxs(TOKEN_ADDRESSS, WITHDRAW_AMOUNT);const reqPreTxReceipt = await wallet.deployTxs(moduleRequiredPreTxs);
Generate & Deploy Transaction that Withdraws a Position From Aave
const aaveWithdrawTx = await eoaAaveWithdrawalModule.createWithdrawTx(TOKEN_ADDRESSS, await wallet.eoa.getAddress(), WITHDRAW_AMOUNT)const aaveWithdrawalReceipt = await FunWallet.deployTx(aaveWithdrawTx);
Full example
aave-example.js
// Install necessary packages// npm install @fun-wallet/sdk ethers// Add Importsimport { ethers } from "ethers";import { FunWallet, FunWalletConfig, Modules } from "@fun-wallet/sdk";const { EoaAaveWithdrawal } = Modules;// // An internal Fun RPC for customer testingconst rpc = "https://avalanche-fuji.infura.io/v3/4a1a0a67f6874be6bb6947a62792dab7";const provider = new ethers.providers.JsonRpcProvider(rpc);// Note that the key here will be exposed and should never be used in productionconst eoa = new ethers.Wallet(PRIV_KEY, provider);// Create a FunWalletconst chainID = "43113" // Fuji testnetconst prefundAmt = 0.3 // amount of native gas token to prefund our FunWallet withconst API_KEY = "" // Get your API key from app.fun.xyz/api-keyconst config = new FunWalletConfig(eoa, chainID, prefundAmt)const wallet = new FunWallet(config, API_KEY)// init will perform the pre-funding of the FunWalletawait wallet.init()// Address of aDAI on mainnetconst TOKEN_ADDRESSS = "0x028171bCA77440897B824Ca71D1c56caC55b68A3";// Use max int to withdraw the entire depositconst WITHDRAW_AMOUNT = ethers.constants.MaxInt256const eoaAaveWithdrawalModule = new EoaAaveWithdrawal();await wallet.addModule(eoaAaveWithdrawalModule);const deployWalletReceipt = await wallet.deploy();const moduleRequiredPreTxs = await eoaAaveWithdrawalModule.getPreExecTxs(TOKEN_ADDRESSS, WITHDRAW_AMOUNT);const reqPreTxReceipt = await wallet.deployTxs(moduleRequiredPreTxs);const aaveWithdrawTx = await eoaAaveWithdrawalModule.createWithdrawTx(TOKEN_ADDRESSS, await wallet.eoa.getAddress(), WITHDRAW_AMOUNT)const aaveWithdrawalReceipt = await wallet.deployTx(aaveWithdrawTx);console.log(aaveWithdrawalReceipt)
aave-example-output.json
{userOpHash: '0xd7ea68043200a00db7bdf072842cb3669456525ab24db6032f910fe7aafd56cb',txid: '0xb98e9c5cd9451a5b7335723f223738eaa0c16b3a60f47e9d3592ff5faef54fb4',gasUsed: 173974,gasUSD: 0.09142246713}
Mentioned Classes
EoaAaveWithdrawal
Link to the EoaAaveWithdrawal Module Reference
FunWallet
Primary class in the FunWallet SDK that orchestrates the movement of assets & data
FunWalletConfig
FunWallet configuration class