ERC4626Component
ERC4626 Component
The ERC4626 component is an extension of ERC20 and provides an implementation of the IERC4626 interface which allows the minting and burning of “shares” in exchange for an underlying “asset”. The component leverages traits to configure fees, limits, and decimals.
CAUTION: In empty (or nearly empty) ERC-4626 vaults, deposits are at high risk of being stolen through frontrunning with a “donation” to the vault that inflates the price of a share. This is variously known as a donation or inflation attack and is essentially a problem of slippage. Vault deployers can protect against this attack by making an initial deposit of a non-trivial amount of the asset, such that price manipulation becomes infeasible. Withdrawals may similarly be affected by slippage. Users can protect against this attack as well as unexpected slippage in general by verifying the amount received is as expected, using a wrapper that performs these checks.
This implementation offers configurable virtual assets and shares to help developers mitigate
that risk. ImmutableConfig::DECIMALS_OFFSET corresponds to an offset in the decimal
representation between the underlying asset’s decimals and vault decimals. This offset also
determines the rate of virtual shares to virtual assets in the vault, which itself determines
the initial exchange rate. While not fully preventing the attack, analysis shows that the
default offset (0) makes it non-profitable even if an attacker is able to capture value from
multiple user deposits, as a result of the value being captured by the virtual shares (out of
the attacker’s donation) matching the attacker’s expected gains. With a larger offset, the
attack becomes orders of magnitude more expensive than it is profitable.
The drawback of this approach is that the virtual shares do capture (a very small) part of the value being accrued to the vault. Also, if the vault experiences losses and users try to exit the vault, the virtual shares and assets will cause the first exiting user to experience reduced losses to the detriment to the last users who will experience bigger losses.
Fully qualified path: openzeppelin_token::erc20::extensions::erc4626::erc4626::ERC4626Component
Modules
| Errors | — |
| __external_ERC4626Impl | — |
| __l1_handler_ERC4626Impl | — |
| __constructor_ERC4626Impl | — |
| __external_ERC4626MetadataImpl | — |
| __l1_handler_ERC4626MetadataImpl | — |
| __constructor_ERC4626MetadataImpl | — |
Constants
Free functions
Structs
| Storage | — |
| Deposit | Emitted when sender exchanges assets for shares and transfers those shares to owner . |
| Withdraw | Emitted when sender exchanges shares , owned by owner , for assets and transfers those assets to receiver . |
| Preview | Internal helper struct containing assets, shares, and fees calculated in preview functions. |
| ComponentState | — |
Enums
Traits
| ImmutableConfig | Constants expected to be defined at the contract level which configure virtual assets and shares. UNDERLYING_DECIMALS should match the underlying asset’s decimals. The default value is 18 …. |
| FeeConfigTrait | The logic for calculating entry and exit fees is expected to be defined at the contract level. Defaults to no entry or exit fees…. |
| LimitConfigTrait | Sets limits to the target exchange type and is expected to be defined at the contract level. It’s important to note that these limits correspond directly to the max_<OPERATION> i.e. deposit_limit… |
| ERC4626HooksTrait | Allows contracts to hook logic into deposit and withdraw transactions. This is where contracts can transfer fees. NOTE: ERC4626 preview methods must be inclusive of any entry or exit fees…. |
| AssetsManagementTrait | Defines how the ERC4626 vault manages its underlying assets. This trait provides the core asset management functionality for the vault, abstracting the actual storage and transfer mechanisms…. |
| HasComponent | — |
| InternalTrait | — |
| UnsafeNewContractStateTraitForERC4626Impl | — |
| UnsafeNewContractStateTraitForERC4626MetadataImpl | — |