UNPKG

@contractjs/aave-v3

Version:

A TypeScript utility library for Aave V3 contracts.

133 lines 5.59 kB
import { maxUint256, publicActions } from "viem"; import { poolAbi } from "./constants/abis"; import { poolAddressOn, supplyERC20Calldata, supplyETHCalldata, withdrawERC20Calldata, withdrawETHCalldata, wrappedTokenGatewayAddressOn } from "./aaveV3Utils"; export const getReserveData = async (tokenAddress, publicClient) => { if (!publicClient.chain) { throw new Error("Public client chain is not set"); } const poolAddress = poolAddressOn(publicClient.chain); const reserveTuple = await publicClient.readContract({ address: poolAddress, abi: poolAbi, functionName: 'getReserveData', args: [tokenAddress], }); return { configuration: reserveTuple[0], liquidityIndex: reserveTuple[1], liquidityRate: reserveTuple[2], // Was liquidityRate in your original variableBorrowIndex: reserveTuple[3], currentVariableBorrowRate: reserveTuple[4], currentStableBorrowRate: reserveTuple[5], lastUpdateTimestamp: reserveTuple[6], id: Number(reserveTuple[7]), // Cast uint16 to number aTokenAddress: reserveTuple[8], stableDebtTokenAddress: reserveTuple[9], variableDebtTokenAddress: reserveTuple[10], interestRateStrategyAddress: reserveTuple[11], accruedToTreasury: reserveTuple[12], }; }; export const getSupplyAprRate = async (tokenAddress, publicClient) => { const { liquidityRate } = await getReserveData(tokenAddress, publicClient); const aprRate = Number(liquidityRate) / 1e27; return aprRate; }; export const getSupplyAprPercentage = async (tokenAddress, publicClient) => { const { liquidityRate } = await getReserveData(tokenAddress, publicClient); const aprRate = Number(liquidityRate) / 1e27; const percentage = aprRate * 100; return percentage; }; export const getSupplyApyRate = async (tokenAddress, publicClient) => { const SECONDS_PER_YEAR = 31536000; const apr = await getSupplyAprRate(tokenAddress, publicClient); const ratePerSecond = apr / SECONDS_PER_YEAR; const apy = Math.pow(1 + ratePerSecond, SECONDS_PER_YEAR) - 1; return apy; }; export const getSupplyApyPercentage = async (tokenAddress, publicClient) => { const apy = await getSupplyApyRate(tokenAddress, publicClient); const percentage = apy * 100; return percentage; }; export const supplyERC20 = async (tokenAddress, amount, walletClient, options) => { if (!walletClient.chain) { throw new Error("Wallet client chain is not set"); } const extendedWalletClient = walletClient.extend(publicActions); const data = supplyERC20Calldata(tokenAddress, amount, options?.onBehalfOf ?? walletClient.account.address, options?.referralCode ?? 0); const poolAddress = poolAddressOn(walletClient.chain); const hash = await walletClient.sendTransaction({ account: walletClient.account, to: poolAddress, chain: walletClient.chain, data }); return { hash, waitForReceipt: () => extendedWalletClient.waitForTransactionReceipt({ hash }) }; }; export const supplyETH = async (amount, walletClient, options) => { if (!walletClient.chain) { throw new Error("Wallet client chain is not set"); } const extendedWalletClient = walletClient.extend(publicActions); const poolAddress = poolAddressOn(walletClient.chain); const data = supplyETHCalldata(poolAddress, options?.onBehalfOf ?? walletClient.account.address, options?.referralCode ?? 0); const hash = await walletClient.sendTransaction({ account: walletClient.account, to: wrappedTokenGatewayAddressOn(walletClient.chain), chain: walletClient.chain, value: amount, data }); return { hash, waitForReceipt: () => extendedWalletClient.waitForTransactionReceipt({ hash }) }; }; export const withdrawERC20 = async (tokenAddress, amount, to, walletClient) => { if (!walletClient.chain) { throw new Error("Wallet client chain is not set"); } const extendedWalletClient = walletClient.extend(publicActions); const poolAddress = poolAddressOn(walletClient.chain); const data = withdrawERC20Calldata(tokenAddress, amount, to); const hash = await walletClient.sendTransaction({ account: walletClient.account, to: poolAddress, chain: walletClient.chain, data }); return { hash, waitForReceipt: () => extendedWalletClient.waitForTransactionReceipt({ hash }) }; }; export const withdrawAllERC20 = async (tokenAddress, walletClient) => { return withdrawERC20(tokenAddress, maxUint256, walletClient.account.address, walletClient); }; export const withdrawETH = async (amount, to, walletClient) => { if (!walletClient.chain) { throw new Error("Wallet client chain is not set"); } const extendedWalletClient = walletClient.extend(publicActions); const poolAddress = poolAddressOn(walletClient.chain); const data = withdrawETHCalldata(poolAddress, amount, to); const hash = await walletClient.sendTransaction({ account: walletClient.account, to: wrappedTokenGatewayAddressOn(walletClient.chain), chain: walletClient.chain, data }); return { hash, waitForReceipt: () => extendedWalletClient.waitForTransactionReceipt({ hash }) }; }; export const withdrawAllETH = async (walletClient) => { return withdrawETH(maxUint256, walletClient.account.address, walletClient); }; //# sourceMappingURL=pool.js.map