Keyboard shortcuts

Press or to navigate between chapters

Press ? to show this help

Press Esc to hide this help

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

Constants

Free functions

Structs

Storage
DepositEmitted when sender exchanges assets for shares and transfers those shares to owner .
WithdrawEmitted when sender exchanges shares , owned by owner , for assets and transfers those assets to receiver .
PreviewInternal helper struct containing assets, shares, and fees calculated in preview functions.
ComponentState

Enums

Event
FeeA fee amount represented either in underlying assets or in shares.

Traits

ImmutableConfigConstants 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 ….
FeeConfigTraitThe logic for calculating entry and exit fees is expected to be defined at the contract level. Defaults to no entry or exit fees….
LimitConfigTraitSets 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
ERC4626HooksTraitAllows 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….
AssetsManagementTraitDefines 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