Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
513 lines
15 KiB
Markdown
513 lines
15 KiB
Markdown
# ERC4626
|
|
|
|
Simple ERC4626 tokenized Vault implementation.
|
|
|
|
|
|
|
|
|
|
<b>Inherits:</b>
|
|
|
|
- [`tokens/ERC20.sol`](tokens/erc20.md)
|
|
|
|
|
|
<!-- customintro:start --><!-- customintro:end -->
|
|
|
|
## Constants
|
|
|
|
### _DEFAULT_UNDERLYING_DECIMALS
|
|
|
|
```solidity
|
|
uint8 internal constant _DEFAULT_UNDERLYING_DECIMALS = 18
|
|
```
|
|
|
|
The default underlying decimals.
|
|
|
|
### _DEFAULT_DECIMALS_OFFSET
|
|
|
|
```solidity
|
|
uint8 internal constant _DEFAULT_DECIMALS_OFFSET = 0
|
|
```
|
|
|
|
The default decimals offset.
|
|
|
|
## Custom Errors
|
|
|
|
### DepositMoreThanMax()
|
|
|
|
```solidity
|
|
error DepositMoreThanMax()
|
|
```
|
|
|
|
Cannot deposit more than the max limit.
|
|
|
|
### MintMoreThanMax()
|
|
|
|
```solidity
|
|
error MintMoreThanMax()
|
|
```
|
|
|
|
Cannot mint more than the max limit.
|
|
|
|
### WithdrawMoreThanMax()
|
|
|
|
```solidity
|
|
error WithdrawMoreThanMax()
|
|
```
|
|
|
|
Cannot withdraw more than the max limit.
|
|
|
|
### RedeemMoreThanMax()
|
|
|
|
```solidity
|
|
error RedeemMoreThanMax()
|
|
```
|
|
|
|
Cannot redeem more than the max limit.
|
|
|
|
## Events
|
|
|
|
### Deposit(address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
event Deposit(
|
|
address indexed by,
|
|
address indexed owner,
|
|
uint256 assets,
|
|
uint256 shares
|
|
)
|
|
```
|
|
|
|
Emitted during a mint call or deposit call.
|
|
|
|
### Withdraw(address,address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
event Withdraw(
|
|
address indexed by,
|
|
address indexed to,
|
|
address indexed owner,
|
|
uint256 assets,
|
|
uint256 shares
|
|
)
|
|
```
|
|
|
|
Emitted during a withdraw call or redeem call.
|
|
|
|
## ERC4626 Constants
|
|
|
|
### _decimalsOffset()
|
|
|
|
```solidity
|
|
function _decimalsOffset() internal view virtual returns (uint8)
|
|
```
|
|
|
|
Override to return a non-zero value to make the inflation attack even more unfeasible.
|
|
Only used when {_useVirtualShares} returns true.
|
|
Default: 0.
|
|
- MUST NOT revert.
|
|
|
|
### _useVirtualShares()
|
|
|
|
```solidity
|
|
function _useVirtualShares() internal view virtual returns (bool)
|
|
```
|
|
|
|
Returns whether virtual shares will be used to mitigate the inflation attack.
|
|
See: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/3706
|
|
Override to return true or false.
|
|
Default: true.
|
|
- MUST NOT revert.
|
|
|
|
### decimals()
|
|
|
|
```solidity
|
|
function decimals() public view virtual override(ERC20) returns (uint8)
|
|
```
|
|
|
|
Returns the decimals places of the token.
|
|
- MUST NOT revert.
|
|
|
|
## Asset Decimals Getter Helper
|
|
|
|
### _tryGetAssetDecimals(address)
|
|
|
|
```solidity
|
|
function _tryGetAssetDecimals(address underlying)
|
|
internal
|
|
view
|
|
returns (bool success, uint8 result)
|
|
```
|
|
|
|
Helper function to get the decimals of the underlying asset.
|
|
Useful for setting the return value of `_underlyingDecimals` during initialization.
|
|
If the retrieval succeeds, `success` will be true, and `result` will hold the result.
|
|
Otherwise, `success` will be false, and `result` will be zero.
|
|
Example usage:
|
|
```solidity
|
|
(bool success, uint8 result) = _tryGetAssetDecimals(underlying);
|
|
_decimals = success ? result : _DEFAULT_UNDERLYING_DECIMALS;
|
|
```
|
|
|
|
## Accounting Logic
|
|
|
|
### totalAssets()
|
|
|
|
```solidity
|
|
function totalAssets() public view virtual returns (uint256 assets)
|
|
```
|
|
|
|
Returns the total amount of the underlying asset managed by the Vault.
|
|
- SHOULD include any compounding that occurs from the yield.
|
|
- MUST be inclusive of any fees that are charged against assets in the Vault.
|
|
- MUST NOT revert.
|
|
|
|
### convertToShares(uint256)
|
|
|
|
```solidity
|
|
function convertToShares(uint256 assets)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 shares)
|
|
```
|
|
|
|
Returns the amount of shares that the Vault will exchange for the amount of
|
|
assets provided, in an ideal scenario where all 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, during 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, i.e. what the average user should
|
|
expect to see when exchanging to and from.
|
|
|
|
### convertToAssets(uint256)
|
|
|
|
```solidity
|
|
function convertToAssets(uint256 shares)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 assets)
|
|
```
|
|
|
|
Returns the amount of assets that the Vault will exchange for the amount of
|
|
shares provided, in an ideal scenario where all 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, during 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, i.e. what the average user should
|
|
expect to see when exchanging to and from.
|
|
|
|
### previewDeposit(uint256)
|
|
|
|
```solidity
|
|
function previewDeposit(uint256 assets)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 shares)
|
|
```
|
|
|
|
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
|
|
will be minted in a deposit call in the same transaction, i.e. deposit should
|
|
return the same or more shares as `previewDeposit` if call in the same transaction.
|
|
- MUST NOT account for deposit limits like those returned from `maxDeposit` and should
|
|
always act as if the deposit will be accepted, regardless of approvals, etc.
|
|
- MUST be inclusive of deposit fees. Integrators should be aware of this.
|
|
- 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(uint256)
|
|
|
|
```solidity
|
|
function previewMint(uint256 shares)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 assets)
|
|
```
|
|
|
|
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
|
|
will 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 if the mint will be accepted, regardless of approvals, etc.
|
|
- MUST be inclusive of deposit fees. Integrators should be aware of this.
|
|
- 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(uint256)
|
|
|
|
```solidity
|
|
function previewWithdraw(uint256 assets)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 shares)
|
|
```
|
|
|
|
Allows an on-chain or off-chain user to simulate the effects of their withdrawal
|
|
at the current block, given the current on-chain conditions.
|
|
- MUST return as close to and no fewer than the exact amount of Vault shares that
|
|
will be burned in a withdraw call in the same transaction, i.e. withdraw should
|
|
return the same or fewer shares as `previewWithdraw` if call in the same transaction.
|
|
- MUST NOT account for withdrawal limits like those returned from `maxWithdraw` and should
|
|
always act as if the withdrawal will be accepted, regardless of share balance, etc.
|
|
- MUST be inclusive of withdrawal fees. Integrators should be aware of this.
|
|
- 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(uint256)
|
|
|
|
```solidity
|
|
function previewRedeem(uint256 shares)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 assets)
|
|
```
|
|
|
|
Allows an on-chain or off-chain user to simulate the effects of their redemption
|
|
at the current block, given current on-chain conditions.
|
|
- MUST return as close to and no more than the exact amount of assets that
|
|
will 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 if the redemption will be accepted, regardless of approvals, etc.
|
|
- MUST be inclusive of withdrawal fees. Integrators should be aware of this.
|
|
- 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 depositing.
|
|
|
|
## Deposit / Withdrawal Limit Logic
|
|
|
|
### maxDeposit(address)
|
|
|
|
```solidity
|
|
function maxDeposit(address to)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 maxAssets)
|
|
```
|
|
|
|
Returns the maximum amount of the underlying asset that can be deposited
|
|
into the Vault for `to`, via a deposit call.
|
|
- MUST return a limited value if `to` is subject to some deposit limit.
|
|
- MUST return `2**256-1` if there is no maximum limit.
|
|
- MUST NOT revert.
|
|
|
|
### maxMint(address)
|
|
|
|
```solidity
|
|
function maxMint(address to)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 maxShares)
|
|
```
|
|
|
|
Returns the maximum amount of the Vault shares that can be minter for `to`,
|
|
via a mint call.
|
|
- MUST return a limited value if `to` is subject to some mint limit.
|
|
- MUST return `2**256-1` if there is no maximum limit.
|
|
- MUST NOT revert.
|
|
|
|
### maxWithdraw(address)
|
|
|
|
```solidity
|
|
function maxWithdraw(address owner)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 maxAssets)
|
|
```
|
|
|
|
Returns the maximum amount of the underlying asset that can be withdrawn
|
|
from the `owner`'s balance in the Vault, via a withdraw call.
|
|
- MUST return a limited value if `owner` is subject to some withdrawal limit or timelock.
|
|
- MUST NOT revert.
|
|
|
|
### maxRedeem(address)
|
|
|
|
```solidity
|
|
function maxRedeem(address owner)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 maxShares)
|
|
```
|
|
|
|
Returns the maximum amount of Vault shares that can be redeemed
|
|
from the `owner`'s balance in the Vault, via a redeem call.
|
|
- MUST return a limited value if `owner` is subject to some withdrawal limit or timelock.
|
|
- MUST return `balanceOf(owner)` otherwise.
|
|
- MUST NOT revert.
|
|
|
|
## Deposit / Withdrawal Logic
|
|
|
|
### deposit(uint256,address)
|
|
|
|
```solidity
|
|
function deposit(uint256 assets, address to)
|
|
public
|
|
virtual
|
|
returns (uint256 shares)
|
|
```
|
|
|
|
Mints `shares` Vault shares to `to` by depositing exactly `assets`
|
|
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, such as due to deposit limit,
|
|
slippage, insufficient approval, etc.
|
|
Note: Most implementations will require pre-approval of the Vault with the
|
|
Vault's underlying `asset` token.
|
|
|
|
### mint(uint256,address)
|
|
|
|
```solidity
|
|
function mint(uint256 shares, address to)
|
|
public
|
|
virtual
|
|
returns (uint256 assets)
|
|
```
|
|
|
|
Mints exactly `shares` Vault shares to `to` by depositing `assets`
|
|
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 deposited, such as due to deposit limit,
|
|
slippage, insufficient approval, etc.
|
|
Note: Most implementations will require pre-approval of the Vault with the
|
|
Vault's underlying `asset` token.
|
|
|
|
### withdraw(uint256,address,address)
|
|
|
|
```solidity
|
|
function withdraw(uint256 assets, address to, address owner)
|
|
public
|
|
virtual
|
|
returns (uint256 shares)
|
|
```
|
|
|
|
Burns `shares` from `owner` and sends exactly `assets` of underlying tokens to `to`.
|
|
- 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, such as due to withdrawal limit,
|
|
slippage, insufficient balance, etc.
|
|
Note: Some implementations will require pre-requesting to the Vault before a withdrawal
|
|
may be performed. Those methods should be performed separately.
|
|
|
|
### redeem(uint256,address,address)
|
|
|
|
```solidity
|
|
function redeem(uint256 shares, address to, address owner)
|
|
public
|
|
virtual
|
|
returns (uint256 assets)
|
|
```
|
|
|
|
Burns exactly `shares` from `owner` and sends `assets` of underlying tokens to `to`.
|
|
- 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, such as due to withdrawal limit,
|
|
slippage, insufficient balance, etc.
|
|
Note: Some implementations will require pre-requesting to the Vault before a redeem
|
|
may be performed. Those methods should be performed separately.
|
|
|
|
## Internal Helpers
|
|
|
|
### _deposit(address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
function _deposit(address by, address to, uint256 assets, uint256 shares)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
For deposits and mints.
|
|
|
|
Emits a `Deposit` event.
|
|
|
|
### _withdraw(address,address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
function _withdraw(
|
|
address by,
|
|
address to,
|
|
address owner,
|
|
uint256 assets,
|
|
uint256 shares
|
|
) internal virtual
|
|
```
|
|
|
|
For withdrawals and redemptions.
|
|
|
|
Emits a `Withdraw` event.
|
|
|
|
### _initialConvertToShares(uint256)
|
|
|
|
```solidity
|
|
function _initialConvertToShares(uint256 assets)
|
|
internal
|
|
view
|
|
virtual
|
|
returns (uint256 shares)
|
|
```
|
|
|
|
Internal conversion function (from assets to shares) to apply when the Vault is empty.
|
|
Only used when {_useVirtualShares} returns false.
|
|
Note: Make sure to keep this function consistent with {_initialConvertToAssets}
|
|
when overriding it.
|
|
|
|
### _initialConvertToAssets(uint256)
|
|
|
|
```solidity
|
|
function _initialConvertToAssets(uint256 shares)
|
|
internal
|
|
view
|
|
virtual
|
|
returns (uint256 assets)
|
|
```
|
|
|
|
Internal conversion function (from shares to assets) to apply when the Vault is empty.
|
|
Only used when {_useVirtualShares} returns false.
|
|
Note: Make sure to keep this function consistent with {_initialConvertToShares}
|
|
when overriding it.
|
|
|
|
## Hooks To Override
|
|
|
|
### _beforeWithdraw(uint256,uint256)
|
|
|
|
```solidity
|
|
function _beforeWithdraw(uint256 assets, uint256 shares) internal virtual
|
|
```
|
|
|
|
Hook that is called before any withdrawal or redemption.
|
|
|
|
### _afterDeposit(uint256,uint256)
|
|
|
|
```solidity
|
|
function _afterDeposit(uint256 assets, uint256 shares) internal virtual
|
|
```
|
|
|
|
Hook that is called after any deposit or mint. |