Skip to main content

ERC4626

ERC4626

Dev note

This ERC4626 has been modified with a user-defined type for Shares to prevent bugs that confuse "shares" and collateral amounts. It produces the same bytecode as OpenZeppelin's ERC4626 at 6b17b3.

For more information, please see OpenZeppelin's ERC4626 documentation.

Addresses

ChainAddress

Functions

constructor

constructor(contract IERC20 asset_) internal
Dev note

Set the underlying asset contract. This must be an ERC20-compatible contract (ERC20 or ERC777).

decimals

function decimals() public view virtual returns (uint8)
Dev note

Decimals are read from the underlying asset in the constructor and cached. If this fails (e.g., the asset has not been created yet), the cached value is set to a default obtained by super.decimals() (which depends on inheritance but is most likely 18). Override this function in order to set a guaranteed hardcoded value.

asset

function asset() public view virtual returns (address)
Dev note

Returns the address of the underlying token used for the Vault for accounting, depositing, and withdrawing.

  • MUST be an ERC-20 token contract.
  • MUST NOT revert.

totalAssets

function totalAssets() public view virtual returns (uint256)
Dev note

Returns the total amount of the underlying asset that is “managed” by Vault.

  • SHOULD include any compounding that occurs from yield.
  • MUST be inclusive of any fees that are charged against assets in the Vault.
  • MUST NOT revert.

convertToShares

function convertToShares(uint256 assets) public view virtual returns (Shares shares)
Dev note

Returns the amount of shares that the Vault would exchange for the amount of assets provided, in an ideal scenario where all the conditions are met.

  • MUST NOT be inclusive of any fees that are charged against assets in the Vault.
  • MUST NOT show any variations depending on the caller.
  • MUST NOT reflect slippage or other on-chain conditions, when performing the actual exchange.
  • MUST NOT revert.

NOTE: This calculation MAY NOT reflect the “per-user” price-per-share, and instead should reflect the “average-user’s” price-per-share, meaning what the average user should expect to see when exchanging to and from.

convertToAssets

function convertToAssets(Shares shares) public view virtual returns (uint256 assets)
Dev note

Returns the amount of assets that the Vault would exchange for the amount of shares provided, in an ideal scenario where all the conditions are met.

  • MUST NOT be inclusive of any fees that are charged against assets in the Vault.
  • MUST NOT show any variations depending on the caller.
  • MUST NOT reflect slippage or other on-chain conditions, when performing the actual exchange.
  • MUST NOT revert.

NOTE: This calculation MAY NOT reflect the “per-user” price-per-share, and instead should reflect the “average-user’s” price-per-share, meaning what the average user should expect to see when exchanging to and from.

maxDeposit

function maxDeposit(address) public view virtual returns (uint256)
Dev note

Returns the maximum amount of the underlying asset that can be deposited into the Vault for the receiver, through a deposit call.

  • MUST return a limited value if receiver is subject to some deposit limit.
  • MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of assets that may be deposited.
  • MUST NOT revert.

maxMint

function maxMint(address) public view virtual returns (Shares)
Dev note

Returns the maximum amount of the Vault shares that can be minted for the receiver, through a mint call.

  • MUST return a limited value if receiver is subject to some mint limit.
  • MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of shares that may be minted.
  • MUST NOT revert.

maxWithdraw

function maxWithdraw(address owner) public virtual returns (uint256)
Dev note

Returns the maximum amount of the underlying asset that can be withdrawn from the owner balance in the Vault, through a withdraw call.

  • MUST return a limited value if owner is subject to some withdrawal limit or timelock.
  • MUST NOT revert.

maxRedeem

function maxRedeem(address owner) public virtual returns (Shares)
Dev note

Returns the maximum amount of Vault shares that can be redeemed from the owner balance in the Vault, through a redeem call.

  • MUST return a limited value if owner is subject to some withdrawal limit or timelock.
  • MUST return balanceOf(owner) if owner is not subject to any withdrawal limit or timelock.
  • MUST NOT revert.

previewDeposit

function previewDeposit(uint256 assets) public view virtual returns (Shares)
Dev note

Allows an on-chain or off-chain user to simulate the effects of their deposit at the current block, given current on-chain conditions.

  • MUST return as close to and no more than the exact amount of Vault shares that would be minted in a deposit call in the same transaction. I.e. deposit should return the same or more shares as previewDeposit if called in the same transaction.
  • MUST NOT account for deposit limits like those returned from maxDeposit and should always act as though the deposit would be accepted, regardless if the user has enough tokens approved, etc.
  • MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees.
  • MUST NOT revert.

NOTE: any unfavorable discrepancy between convertToShares and previewDeposit SHOULD be considered slippage in share price or some other type of condition, meaning the depositor will lose assets by depositing.

previewMint

function previewMint(Shares shares) public view virtual returns (uint256)
Dev note

Allows an on-chain or off-chain user to simulate the effects of their mint at the current block, given current on-chain conditions.

  • MUST return as close to and no fewer than the exact amount of assets that would be deposited in a mint call in the same transaction. I.e. mint should return the same or fewer assets as previewMint if called in the same transaction.
  • MUST NOT account for mint limits like those returned from maxMint and should always act as though the mint would be accepted, regardless if the user has enough tokens approved, etc.
  • MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees.
  • MUST NOT revert.

NOTE: any unfavorable discrepancy between convertToAssets and previewMint SHOULD be considered slippage in share price or some other type of condition, meaning the depositor will lose assets by minting.

previewWithdraw

function previewWithdraw(uint256 assets) public view virtual returns (Shares)
Dev note

Allows an on-chain or off-chain user to simulate the effects of their withdrawal at the current block, given current on-chain conditions.

  • MUST return as close to and no fewer than the exact amount of Vault shares that would be burned in a withdraw call in the same transaction. I.e. withdraw should return the same or fewer shares as previewWithdraw if called in the same transaction.
  • MUST NOT account for withdrawal limits like those returned from maxWithdraw and should always act as though the withdrawal would be accepted, regardless if the user has enough shares, etc.
  • MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees.
  • MUST NOT revert.

NOTE: any unfavorable discrepancy between convertToShares and previewWithdraw SHOULD be considered slippage in share price or some other type of condition, meaning the depositor will lose assets by depositing.

previewRedeem

function previewRedeem(Shares shares) public view virtual returns (uint256)
Dev note

Allows an on-chain or off-chain user to simulate the effects of their redeemption at the current block, given current on-chain conditions.

  • MUST return as close to and no more than the exact amount of assets that would be withdrawn in a redeem call in the same transaction. I.e. redeem should return the same or more assets as previewRedeem if called in the same transaction.
  • MUST NOT account for redemption limits like those returned from maxRedeem and should always act as though the redemption would be accepted, regardless if the user has enough shares, etc.
  • MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees.
  • MUST NOT revert.

NOTE: any unfavorable discrepancy between convertToAssets and previewRedeem SHOULD be considered slippage in share price or some other type of condition, meaning the depositor will lose assets by redeeming.

deposit

function deposit(uint256 assets, address receiver) public virtual returns (Shares)
Dev note

Mints shares Vault shares to receiver by depositing exactly amount of underlying tokens.

  • MUST emit the Deposit event.
  • MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the deposit execution, and are accounted for during deposit.
  • MUST revert if all of assets cannot be deposited (due to deposit limit being reached, slippage, the user not approving enough underlying tokens to the Vault contract, etc).

NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token.

mint

function mint(Shares shares, address receiver) public virtual returns (uint256)
Dev note

Mints exactly shares Vault shares to receiver by depositing amount of underlying tokens.

  • MUST emit the Deposit event.
  • MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the mint execution, and are accounted for during mint.
  • MUST revert if all of shares cannot be minted (due to deposit limit being reached, slippage, the user not approving enough underlying tokens to the Vault contract, etc).

NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token.

withdraw

function withdraw(uint256 assets, address receiver, address owner) public virtual returns (Shares)
Dev note

Burns shares from owner and sends exactly assets of underlying tokens to receiver.

  • MUST emit the Withdraw event.
  • MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the withdraw execution, and are accounted for during withdraw.
  • MUST revert if all of assets cannot be withdrawn (due to withdrawal limit being reached, slippage, the owner not having enough shares, etc).

Note that some implementations will require pre-requesting to the Vault before a withdrawal may be performed. Those methods should be performed separately.

redeem

function redeem(Shares shares, address receiver, address owner) public virtual returns (uint256)
Dev note

Burns exactly shares from owner and sends assets of underlying tokens to receiver.

  • MUST emit the Withdraw event.
  • MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the redeem execution, and are accounted for during redeem.
  • MUST revert if all of shares cannot be redeemed (due to withdrawal limit being reached, slippage, the owner not having enough shares, etc).

NOTE: some implementations will require pre-requesting to the Vault before a withdrawal may be performed. Those methods should be performed separately.

_convertToShares

function _convertToShares(
uint256 assets,
enum Math.Rounding rounding
) internal view virtual returns (
Shares shares
)
Dev note

Internal conversion function (from assets to shares) with support for rounding direction.

Will revert if assets > 0, totalSupply > 0 and totalAssets = 0. That corresponds to a case where any asset would represent an infinite amount of shares.

_initialConvertToShares

function _initialConvertToShares(
uint256 assets,
enum Math.Rounding
) internal view virtual returns (
Shares shares
)
Dev note

Internal conversion function (from assets to shares) to apply when the vault is empty.

NOTE: Make sure to keep this function consistent with _initialConvertToAssets when overriding it.

_convertToAssets

function _convertToAssets(
Shares shares,
enum Math.Rounding rounding
) internal view virtual returns (
uint256 assets
)
Dev note

Internal conversion function (from shares to assets) with support for rounding direction.

_initialConvertToAssets

function _initialConvertToAssets(
Shares shares,
enum Math.Rounding
) internal view virtual returns (
uint256 assets
)
Dev note

Internal conversion function (from shares to assets) to apply when the vault is empty.

NOTE: Make sure to keep this function consistent with _initialConvertToShares when overriding it.

_deposit

function _deposit(address caller, address receiver, uint256 assets, Shares shares) internal virtual
Dev note

Deposit/mint common workflow.

_withdraw

function _withdraw(
address caller,
address receiver,
address owner,
uint256 assets,
Shares shares
) internal virtual
Dev note

Withdraw/redeem common workflow.