Skip to main content

Overview

Utilities for encoding Aave v3 Pool function calls and decoding responses. Supports repayment, withdrawal, and account data queries.

Functions

encodeAaveRepay

Encodes calldata for the Aave Pool repay() function.
function encodeAaveRepay(params: {
  asset: Address;
  amount: bigint;
  interestRateMode: bigint;
  onBehalfOf: Address;
}): Hex
asset
Address
required
The address of the reserve asset to repay
amount
bigint
required
The amount to repay (in asset’s smallest unit). Use MAX_UINT256 to repay entire debt
interestRateMode
bigint
required
The interest rate mode: 1n for stable rate, 2n for variable rate
onBehalfOf
Address
required
The address of the user whose debt will be repaid
returns
Hex
Encoded calldata for Pool.repay()
Example:
import { encodeAaveRepay, MAX_UINT256 } from "@/lib/protocols/aave";

const repayCalldata = encodeAaveRepay({
  asset: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
  amount: MAX_UINT256, // Repay entire debt
  interestRateMode: 2n, // Variable rate
  onBehalfOf: "0x1234...",
});

encodeAaveWithdraw

Encodes calldata for the Aave Pool withdraw() function.
function encodeAaveWithdraw(params: {
  asset: Address;
  amount: bigint;
  to: Address;
}): Hex
asset
Address
required
The address of the reserve asset to withdraw
amount
bigint
required
The amount to withdraw (in asset’s smallest unit)
to
Address
required
The address that will receive the withdrawn assets
returns
Hex
Encoded calldata for Pool.withdraw()
Example:
import { encodeAaveWithdraw } from "@/lib/protocols/aave";

const withdrawCalldata = encodeAaveWithdraw({
  asset: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599", // WBTC
  amount: 100000000n, // 1 WBTC (8 decimals)
  to: "0x5678...",
});

encodeAaveGetUserAccountData

Encodes calldata for the Aave Pool getUserAccountData() view function.
function encodeAaveGetUserAccountData(user: Address): Hex
user
Address
required
The address of the user to query
returns
Hex
Encoded calldata for Pool.getUserAccountData()
Example:
import { encodeAaveGetUserAccountData } from "@/lib/protocols/aave";

const calldata = encodeAaveGetUserAccountData("0x1234...");
// Use with eth_call to read user's account data

decodeAaveGetUserAccountData

Decodes the result from getUserAccountData() view function.
function decodeAaveGetUserAccountData(result: Hex): AaveUserAccountData
result
Hex
required
The raw return data from Pool.getUserAccountData()
returns
AaveUserAccountData
Decoded user account data object
Example:
import {
  encodeAaveGetUserAccountData,
  decodeAaveGetUserAccountData,
} from "@/lib/protocols/aave";
import { jsonRpcFetch } from "@/lib/rpc/jsonRpc";

const calldata = encodeAaveGetUserAccountData("0x1234...");
const result = await jsonRpcFetch<Hex>(rpcUrl, "eth_call", [
  { to: aavePoolAddress, data: calldata },
  "latest",
]);

const accountData = decodeAaveGetUserAccountData(result);
console.log(accountData.healthFactor); // e.g., 1500000000000000000n (1.5)

encodeAaveGetReserveTokensAddresses

Encodes calldata for the Aave Pool getReserveTokensAddresses() view function.
function encodeAaveGetReserveTokensAddresses(asset: Address): Hex
asset
Address
required
The address of the reserve asset
returns
Hex
Encoded calldata for Pool.getReserveTokensAddresses()

decodeAaveGetReserveTokensAddresses

Decodes the result from getReserveTokensAddresses() view function.
function decodeAaveGetReserveTokensAddresses(result: Hex): {
  aTokenAddress: Address;
  stableDebtTokenAddress: Address;
  variableDebtTokenAddress: Address;
}
result
Hex
required
The raw return data from Pool.getReserveTokensAddresses()
returns
object
Object containing the three token addresses for the reserve

Type Definitions

AaveUserAccountData

User’s account summary data from Aave v3.
type AaveUserAccountData = {
  totalCollateralBase: bigint;
  totalDebtBase: bigint;
  availableBorrowsBase: bigint;
  currentLiquidationThreshold: bigint;
  ltv: bigint;
  healthFactor: bigint;
};
totalCollateralBase
bigint
Total collateral in base currency units (e.g., USD with 8 decimals)
totalDebtBase
bigint
Total debt in base currency units
availableBorrowsBase
bigint
Available borrowing capacity in base currency units
currentLiquidationThreshold
bigint
Weighted average liquidation threshold (in basis points, e.g., 8500 = 85%)
ltv
bigint
Weighted average loan-to-value ratio (in basis points)
healthFactor
bigint
Health factor with 18 decimals (e.g., 1.5 = 1500000000000000000n). Below 1.0 means liquidatable

Constants

MAX_UINT256

const MAX_UINT256: bigint = 2n ** 256n - 1n
Maximum uint256 value, commonly used as amount parameter to repay or withdraw the entire balance. Example:
import { encodeAaveRepay, MAX_UINT256 } from "@/lib/protocols/aave";

// Repay entire debt
const calldata = encodeAaveRepay({
  asset: usdcAddress,
  amount: MAX_UINT256,
  interestRateMode: 2n,
  onBehalfOf: userAddress,
});

Build docs developers (and LLMs) love