Vault Operations
Execute core vault operations such as deposits and withdrawals. Users can choose to interact with the vault using either the asset amount deposited or the vault shares minted.
At the time of deposit, shares are issued at a 1:1 ratio to the assets deposited. Over time, as the vault earns interest, each share represents an increasing amount of underlying tokens.
Deposit Assets
Depositing assets into an Aave vault gives the user an amount of vault shares.
To deposit assets into an Aave vault, follow these steps.
First, determine which vault you want to deposit assets into.
Let's say we have identified the following vault object:
Vault
const vault: Vault = { __typename: "Vault", address: "0x1234567890abcdef1234567890abcdef12345678", shareName: "Aave USDC Vault Shares", shareSymbol: "avUSDC", chainId: 1, usedReserve: { __typename: "Reserve", underlyingToken: { __typename: "Currency", symbol: "USDC", name: "USD Coin", address: "0xA0b86a33E6441c8c5f0bb9b7e5e1f8bbf5b78b5c", // … }, isFrozen: false, isPaused: false, // … }, // …};
Ensure the underlying reserve is not frozen or paused.
Next, preview the deposit operation to determine the amount of vault shares that would be received for a given amount of assets.
- React
- TypeScript
- GraphQL
Use the useVaultDepositPreview hook to preview the deposit operation.
Preview the Deposit
import { useVaultDepositPreview } from "@aave/react";
// …
const [preview /* , { loading, error } */] = useVaultDepositPreview();
// …
const result = await preview({ vault: vault.address, chainId: vault.chainId, amount: bigDecimal(1000), // 1000 USDC});
// …
if (result.isErr()) { console.error(result.error);} else { // result.value: TokenAmount console.log(result.value.value); // 1000}
Next, create the execution plan for the deposit operation.
By default, the vault's underlying asset will be deposited. To deposit the aToken instead, set the asAToken parameter to true.
- React
- TypeScript
- GraphQL
Use the useVaultDeposit hook to create the execution plan for depositing assets into a vault.
Deposit Assets
import { useWalletClient } from "wagmi";import { useVaultDeposit, bigDecimal, evmAddress } from "@aave/react";
// …
const { data: walletClient } = useWalletClient();
const [deposit, depositing] = useVaultDeposit();
const execute = async () => { const result = await deposit({ chainId: vault.chainId, vault: vault.address, amount: { value: bigDecimal(1000), // 1000 USDC // Optional: If set to true, the aToken associated with the vault will be deposited // asAToken: false (default) }, depositor: evmAddress(walletClient!.account.address), });
// …};
Finally, handle the execution plan.
- React
- TypeScript
- GraphQL
Use the useSendTransaction hook for the wallet library of your choice to send the transactions in the execution plan.
Viem
import { useWalletClient } from "wagmi";import { errAsync, useVaultDeposit } from "@aave/react";import { useSendTransaction } from "@aave/react/viem";
// …
const { data: walletClient } = useWalletClient();
const [deposit, depositing] = useVaultDeposit();const [sendTransaction, sending] = useSendTransaction(walletClient);
// …
const loading = depositing.loading || sending.loading;const error = depositing.error || sending.error;
// …
const execute = async () => { const result = await deposit({ // … }).andThen((plan) => { switch (plan.__typename) { case "TransactionRequest": // Single transaction execution return sendTransaction(plan);
case "ApprovalRequired": // Approval + transaction sequence return sendTransaction(plan.approval).andThen(() => sendTransaction(plan.originalTransaction) );
case "InsufficientBalanceError": return errAsync( new Error(`Insufficient balance: ${plan.required.value} required.`) ); } });
if (result.isErr()) { console.error("Deposit failed:", result.error); } else { console.log("Deposit successful with hash:", result.value); }};
Minting vault shares is the process of creating new vault shares by depositing assets into the vault.
To mint vault shares, follow these steps.
1
First, determine which vault you want to mint shares for and the exact amount of shares to mint.
Let's say we want to mint 1000 vault shares from our identified vault object.
Vault
const vault: Vault = { __typename: "Vault", address: "0x1234567890abcdef1234567890abcdef12345678", shareName: "Aave USDC Vault Shares", shareSymbol: "avUSDC", chainId: 1, usedReserve: { __typename: "Reserve", underlyingToken: { __typename: "Currency", symbol: "USDC", name: "USD Coin", address: "0xA0b86a33E6441c8c5f0bb9b7e5e1f8bbf5b78b5c", // … }, isFrozen: false, isPaused: false, // … }, // …};
Ensure the underlying reserve is not frozen or paused.
2
Next, preview the mint operation to determine the amount of assets needed to mint the requested amount of vault shares.
- React
- TypeScript
- GraphQL
Use the useVaultMintPreview hook to preview the mint operation.
Preview the Mint
import { useVaultMintPreview } from "@aave/react";
// …
const [preview /* , { loading, error } */] = useVaultMintPreview();
// …
const result = await preview({ vault: vault.address, chainId: vault.chainId, amount: bigDecimal(1000), // 1000 vault shares});
// …
if (result.isErr()) { console.error(result.error);} else { // result.value: TokenAmount console.log(result.value.value); // 1025.5 (example: assets needed)}
3
Next, create the execution plan for the mint shares operation.
- React
- TypeScript
- GraphQL
Use the useVaultMintShares hook to create the execution plan for minting vault shares directly.
Mint Shares
import { useWalletClient } from "wagmi";import { useVaultMintShares, bigDecimal, evmAddress } from "@aave/react";
// …
const { data: walletClient } = useWalletClient();
const [mintShares, minting] = useVaultMintShares();
const execute = async () => { const result = await mintShares({ chainId: vault.chainId, vault: vault.address, shares: { amount: bigDecimal(1000), // 1000 vault shares }, minter: evmAddress(walletClient!.account.address), // sharesRecipient: evmAddress("0x1234…"), if different from minter });
// …};
4
Finally, handle the execution plan.
- React
- TypeScript
- GraphQL
Use the useSendTransaction hook for the wallet library of your choice to send the transactions in the execution plan.
Viem
import { useWalletClient } from "wagmi";import { errAsync, useVaultMintShares } from "@aave/react";import { useSendTransaction } from "@aave/react/viem";
// …
const { data: walletClient } = useWalletClient();
const [mintShares, minting] = useVaultMintShares();const [sendTransaction, sending] = useSendTransaction(walletClient);
// …
const loading = minting.loading || sending.loading;const error = minting.error || sending.error;
// …
const execute = async () => { const result = await mintShares({ // … }).andThen((plan) => { switch (plan.__typename) { case "TransactionRequest": // Single transaction execution return sendTransaction(plan);
case "ApprovalRequired": // Approval + transaction sequence return sendTransaction(plan.approval).andThen(() => sendTransaction(plan.originalTransaction) );
case "InsufficientBalanceError": return errAsync( new Error(`Insufficient balance: ${plan.required.value} required.`) ); } });
if (result.isErr()) { console.error("Mint shares failed:", result.error); } else { console.log("Mint shares successful with hash:", result.value); }};
Withdraw Assets
Withdrawing assets from an Aave vault gives the user an amount of assets. The corresponding amount of vault shares is burned.
To withdraw assets from an Aave vault, follow these steps.
First, determine which user vault position you want to withdraw assets from.
Let's say we have identified a user vault position with the following details:
Vault Position
const vault: Vault = { __typename: "Vault", address: "0x1234567890abcdef1234567890abcdef12345678", shareName: "Aave USDC Vault Shares", shareSymbol: "avUSDC", chainId: 1, usedReserve: { __typename: "Reserve", underlyingToken: { __typename: "Currency", symbol: "USDC", name: "USD Coin", address: "0xA0b86a33E6441c8c5f0bb9b7e5e1f8bbf5b78b5c", // … }, isFrozen: false, isPaused: false, // … }, userShares: { __typename: "UserVaultShares", shares: { __typename: "TokenAmount", amount: { __typename: "DecimalValue", value: "1000.0", // User has 1000 vault shares // … }, // … }, // … }, // …};
Make sure you include a user address when fetching market and reserve data—otherwise Vault.userShares will be empty.
Ensure the underlying reserve is not frozen or paused.
Next, preview the withdraw operation to determine the amount of shares that will be burned for a given amount of assets.
- React
- TypeScript
- GraphQL
Use the useVaultWithdrawPreview hook to preview the withdraw operation.
Preview the Withdraw
import { useVaultWithdrawPreview } from "@aave/react";
// …
const [preview /* , { loading, error } */] = useVaultWithdrawPreview();
// …
const result = await preview({ vault: vault.address, chainId: vault.chainId, amount: bigDecimal(1000), // 1000 USDC});
// …
if (result.isErr()) { console.error(result.error);} else { // result.value: TokenAmount console.log(result.value.value); // 1000 (shares to burn)}
Next, create the transaction request for the withdrawal operation.
By default, the vault's underlying asset will be withdrawn. To withdraw the aToken instead, set the asAToken parameter to true.
- React
- TypeScript
- GraphQL
Use the useVaultWithdraw hook to create the transaction request for withdrawing assets from a vault.
Withdraw Assets
import { useWalletClient } from "wagmi";import { useVaultWithdraw, bigDecimal, evmAddress } from "@aave/react";
// …
const { data: walletClient } = useWalletClient();
const [withdraw, withdrawing] = useVaultWithdraw();
const execute = async () => { const result = await withdraw({ chainId: vault.chainId, vault: vault.address, amount: { value: bigDecimal(500), // 500 USDC // Optional: If set to true, the aToken associated with the vault will be withdrawn // asAToken: false (default) }, sharesOwner: evmAddress(walletClient!.account.address), // recipient: evmAddress("0x1234…"), if different from sharesOwner });
// …};
Finally, send the transaction.
- React
- TypeScript
- GraphQL
Use the useSendTransaction hook for the wallet library of your choice to send the transaction.
Viem
import { useWalletClient } from "wagmi";import { useVaultWithdraw } from "@aave/react";import { useSendTransaction } from "@aave/react/viem";
// …
const { data: walletClient } = useWalletClient();
const [withdraw, withdrawing] = useVaultWithdraw();const [sendTransaction, sending] = useSendTransaction(walletClient);
const loading = withdrawing.loading || sending.loading;const error = withdrawing.error || sending.error;
// …
const execute = async () => { const result = await withdraw({ // … }).andThen(sendTransaction);
if (result.isErr()) { console.error("Withdrawal failed:", result.error); } else { console.log("Withdrawal successful with hash:", result.value); }};
Redeeming vault shares is the process of burning vault shares and receiving the underlying assets.
To redeem vault shares, follow these steps.
1
First, determine which user vault position you want to redeem shares from.
Let's say we have identified a user vault position with the following details:
Vault Position
const vault: Vault = { __typename: "Vault", address: "0x1234567890abcdef1234567890abcdef12345678", shareName: "Aave USDC Vault Shares", shareSymbol: "avUSDC", chainId: 1, usedReserve: { __typename: "Reserve", underlyingToken: { __typename: "Currency", symbol: "USDC", name: "USD Coin", address: "0xA0b86a33E6441c8c5f0bb9b7e5e1f8bbf5b78b5c", // … }, isFrozen: false, isPaused: false, // … }, userShares: { __typename: "UserVaultShares", shares: { __typename: "TokenAmount", amount: { __typename: "DecimalValue", value: "1000.0", // User has 1000 vault shares // … }, // … }, // … }, // …};
Make sure you include a user address when fetching market and reserve data—otherwise Vault.userShares will be empty.
Ensure the underlying reserve is not frozen or paused.
2
Next, preview the redeem operation to determine the amount of assets that will be received for a given amount of shares burned.
- React
- TypeScript
- GraphQL
Use the useVaultRedeemPreview hook to preview the redeem operation.
Preview the Redeem
import { useVaultRedeemPreview } from "@aave/react";
// …
const [preview /* , { loading, error } */] = useVaultRedeemPreview();
// …
const result = await preview({ vault: vault.address, chainId: vault.chainId, amount: bigDecimal(1000), // 1000 vault shares});
// …
if (result.isErr()) { console.error(result.error);} else { // result.value: TokenAmount console.log(result.value.value); // 1000 (assets to receive)}
3
Next, create the transaction request for the redeem operation.
- React
- TypeScript
- GraphQL
Use the useVaultRedeemShares hook to create the transaction request for redeeming vault shares.
Redeem Shares
import { useWalletClient } from "wagmi";import { useVaultRedeemShares, bigDecimal, evmAddress } from "@aave/react";
// …
const { data: walletClient } = useWalletClient();
const [redeemShares, redeeming] = useVaultRedeemShares();
const execute = async () => { const result = await redeemShares({ chainId: vault.chainId, vault: vault.address, shares: { amount: bigDecimal(1000), // 1000 vault shares }, sharesOwner: evmAddress(walletClient!.account.address), // recipient: evmAddress("0x1234…"), if different from sharesOwner });
// …};
4
Finally, send the transaction.
- React
- TypeScript
- GraphQL
Use the useSendTransaction hook for the wallet library of your choice to send the transaction.
Viem
import { useWalletClient } from "wagmi";import { useVaultRedeemShares } from "@aave/react";import { useSendTransaction } from "@aave/react/viem";
// …
const { data: walletClient } = useWalletClient();
const [redeemShares, redeeming] = useVaultRedeemShares();const [sendTransaction, sending] = useSendTransaction(walletClient);
const loading = redeeming.loading || sending.loading;const error = redeeming.error || sending.error;
// …
const execute = async () => { const result = await redeemShares({ // … }).andThen(sendTransaction);
if (result.isErr()) { console.error("Redeem shares failed:", result.error); } else { console.log("Redeem shares successful with hash:", result.value); }};