Viem extensions for ERC-4626 tokenized vaults
npm install viem-erc4626Viem extensions for ERC-4626 tokenized vaults
  
Install viem, viem-erc20, and viem-erc4626 as dependencies:
``sh`
npm install viem viem-erc20 viem-erc4626
This package provides ESM-friendly helpers for interacting with ERC-4626 vault contracts using viem.
All the methods are named after the ERC-4626:
Returns the remaining number of tokens that spender will be allowed to spend on behalf of owner through transferFrom. View docs
`ts`
allowance(client, { address, owner, spender });
- client: Client — from viem — (required)Address
- address: — ERC-20 token contract address (required)Address
- owner: — Address that owns the tokens (required)Address
- spender: — Address that is allowed to spend the tokens (required)
Example:
`ts`
import { allowance } from "viem-erc4626/actions";
const allowanceAmount = await allowance(client, {
address: "0x12345678912345678912345678912345678912345",
owner: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
spender: "0x1234567891234567891234567891234567891234",
});
Sets amount as the allowance of spender over the caller's tokens. View docs
`ts`
approve(client, { address, spender, amount });
- client: Client — from viem — (required)Address
- address: — ERC-20 token contract address (required)Address
- spender: — Address to approve for spending tokens (required)bigint
- amount: — Amount of tokens to approve (required)
Example:
`ts`
import { approve } from "viem-erc4626/actions";
const hash = await approve(client, {
address: "0x12345678912345678912345678912345678912345",
spender: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
amount: BigInt("1000000000000000000"), // 1 token
});
Returns the address of the underlying asset of the vault. View docs
`ts`
asset(client, { address });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)
Example:
`ts`
import { asset } from "viem-erc4626/actions";
const assetAddress = await asset(client, {
address: "0x12345678912345678912345678912345678912345",
});
Returns the amount of tokens owned by account. View docs
`ts`
balanceOf(client, { address, account });
- client: Client — from viem — (required)Address
- address: — ERC-20 token contract address (required)Address
- account: — Address to check the balance of (required)
Example:
`ts`
import { balance } from "viem-erc4626/actions";
const tokenBalance = await balanceOf(client, {
address: "0x12345678912345678912345678912345678912345",
account: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
});
Returns the amount of assets that would be exchanged by the vault for the amount of shares provided. View docs
`ts`
convertToAssets(client, { address, shares });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)bigint
- shares: — Amount of shares to convert (required)
Example:
`ts`
import { convertToAssets } from "viem-erc4626/actions";
const assetAmount = await convertToAssets(client, {
address: "0x12345678912345678912345678912345678912345",
shares: BigInt("1000000000000000000"), // 1 share
});
Returns the amount of shares that would be exchanged by the vault for the amount of assets provided. View docs
`ts`
convertToShares(client, { address, assets });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)bigint
- assets: — Amount of assets to convert (required)
Example:
`ts`
import { convertToShares } from "viem-erc4626/actions";
const shareAmount = await convertToShares(client, {
address: "0x12345678912345678912345678912345678912345",
assets: BigInt("1000000000000000000"), // 1 asset
});
Returns the number of decimals used to get its user representation. View docs
`ts`
decimals(client, { address });
- client: Client — from viem — (required)Address
- address: — ERC-20 token contract address (required)
Example:
`ts`
import { decimals } from "viem-erc4626/actions";
const tokenDecimals = await decimals(client, {
address: "0x12345678912345678912345678912345678912345",
});
Mints vault shares to receiver by depositing exactly assets of underlying tokens. View docs
`ts`
deposit(client, { address, assets, receiver });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)bigint
- assets: — Amount of assets to deposit (required)Address
- receiver: — Address to receive the vault shares (required)
Example:
`ts`
import { deposit } from "viem-erc4626/actions";
const hash = await deposit(client, {
address: "0x12345678912345678912345678912345678912345",
assets: BigInt("1000000000000000000"), // 1 asset
receiver: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
});
Returns the maximum amount of the underlying asset that can be deposited into the vault for the receiver. View docs
`ts`
maxDeposit(client, { address, receiver });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)Address
- receiver: — Address that would receive the vault shares (required)
Example:
`ts`
import { maxDeposit } from "viem-erc4626/actions";
const maxDepositAmount = await maxDeposit(client, {
address: "0x12345678912345678912345678912345678912345",
receiver: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
});
Returns the maximum amount of the vault shares that can be minted for the receiver. View docs
`ts`
maxMint(client, { address, receiver });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)Address
- receiver: — Address that would receive the vault shares (required)
Example:
`ts`
import { maxMint } from "viem-erc4626/actions";
const maxMintAmount = await maxMint(client, {
address: "0x12345678912345678912345678912345678912345",
receiver: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
});
Returns the maximum amount of vault shares that can be redeemed from the owner balance. View docs
`ts`
maxRedeem(client, { address, owner });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)Address
- owner: — Address that owns the vault shares (required)
Example:
`ts`
import { maxRedeem } from "viem-erc4626/actions";
const maxRedeemAmount = await maxRedeem(client, {
address: "0x12345678912345678912345678912345678912345",
owner: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
});
Returns the maximum amount of the underlying asset that can be withdrawn from the owner balance. View docs
`ts`
maxWithdraw(client, { address, owner });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)Address
- owner: — Address that owns the vault shares (required)
Example:
`ts`
import { maxWithdraw } from "viem-erc4626/actions";
const maxWithdrawAmount = await maxWithdraw(client, {
address: "0x12345678912345678912345678912345678912345",
owner: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
});
Mints exactly shares vault shares to receiver by depositing assets of underlying tokens. View docs
`ts`
mint(client, { address, shares, receiver });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)bigint
- shares: — Amount of shares to mint (required)Address
- receiver: — Address to receive the vault shares (required)
Example:
`ts`
import { mint } from "viem-erc4626/actions";
const hash = await mint(client, {
address: "0x12345678912345678912345678912345678912345",
shares: BigInt("1000000000000000000"), // 1 share
receiver: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
});
Returns the name of the token. View docs
`ts`
name(client, { address });
- client: Client — from viem — (required)Address
- address: — ERC-20 token contract address (required)
Example:
`ts`
import { name } from "viem-erc4626/actions";
const tokenName = await name(client, {
address: "0x12345678912345678912345678912345678912345",
});
Allows an on-chain or off-chain user to simulate the effects of their deposit at the current block. View docs
`ts`
previewDeposit(client, { address, assets });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)bigint
- assets: — Amount of assets to preview deposit (required)
Example:
`ts`
import { previewDeposit } from "viem-erc4626/actions";
const expectedShares = await previewDeposit(client, {
address: "0x12345678912345678912345678912345678912345",
assets: BigInt("1000000000000000000"), // 1 asset
});
Allows an on-chain or off-chain user to simulate the effects of their mint at the current block. View docs
`ts`
previewMint(client, { address, shares });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)bigint
- shares: — Amount of shares to preview mint (required)
Example:
`ts`
import { previewMint } from "viem-erc4626/actions";
const requiredAssets = await previewMint(client, {
address: "0x12345678912345678912345678912345678912345",
shares: BigInt("1000000000000000000"), // 1 share
});
Allows an on-chain or off-chain user to simulate the effects of their redeem at the current block. View docs
`ts`
previewRedeem(client, { address, shares });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)bigint
- shares: — Amount of shares to preview redeem (required)
Example:
`ts`
import { previewRedeem } from "viem-erc4626/actions";
const expectedAssets = await previewRedeem(client, {
address: "0x12345678912345678912345678912345678912345",
shares: BigInt("1000000000000000000"), // 1 share
});
Allows an on-chain or off-chain user to simulate the effects of their withdraw at the current block. View docs
`ts`
previewWithdraw(client, { address, assets });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)bigint
- assets: — Amount of assets to preview withdraw (required)
Example:
`ts`
import { previewWithdraw } from "viem-erc4626/actions";
const requiredShares = await previewWithdraw(client, {
address: "0x12345678912345678912345678912345678912345",
assets: BigInt("1000000000000000000"), // 1 asset
});
Burns exactly shares from owner and sends assets of underlying tokens to receiver. View docs
`ts`
redeem(client, { address, shares, receiver, owner });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)bigint
- shares: — Amount of shares to redeem (required)Address
- receiver: — Address to receive the underlying assets (required)Address
- owner: — Address that owns the vault shares (required)
Example:
`ts`
import { redeem } from "viem-erc4626/actions";
const hash = await redeem(client, {
address: "0x12345678912345678912345678912345678912345",
shares: BigInt("1000000000000000000"), // 1 share
receiver: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
owner: "0x1234567891234567891234567891234567891234",
});
Returns the symbol of the token, usually a shorter version of the name. View docs
`ts`
symbol(client, { address });
- client: Client — from viem — (required)Address
- address: — ERC-20 token contract address (required)
Example:
`ts`
import { symbol } from "viem-erc4626/actions";
const tokenSymbol = await symbol(client, {
address: "0x12345678912345678912345678912345678912345",
});
Returns the total amount of the underlying asset that is managed by the vault. View docs
`ts`
totalAssets(client, { address });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)
Example:
`ts`
import { totalAssets } from "viem-erc4626/actions";
const totalAssetAmount = await totalAssets(client, {
address: "0x12345678912345678912345678912345678912345",
});
Returns the amount of tokens in existence. View docs
`ts`
totalSupply(client, { address });
- client: Client — from viem — (required)Address
- address: — ERC-20 token contract address (required)
Example:
`ts`
import { totalSupply } from "viem-erc4626/actions";
const tokenTotalSupply = await totalSupply(client, {
address: "0x12345678912345678912345678912345678912345",
});
Burns shares from owner and sends exactly assets of underlying tokens to receiver. View docs
`ts`
withdraw(client, { address, assets, receiver, owner });
- client: Client — from viem — (required)Address
- address: — ERC-4626 vault contract address (required)bigint
- assets: — Amount of assets to withdraw (required)Address
- receiver: — Address to receive the underlying assets (required)Address
- owner: — Address that owns the vault shares (required)
Example:
`ts``
import { withdraw } from "viem-erc4626/actions";
const hash = await withdraw(client, {
address: "0x12345678912345678912345678912345678912345",
assets: BigInt("1000000000000000000"), // 1 asset
receiver: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
owner: "0x1234567891234567891234567891234567891234",
});