Keyboard shortcuts

Press or to navigate between chapters

Press ? to show this help

Press Esc to hide this help

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. Fees are calculated using FeeConfigTrait methods and automatically adjust the final asset and share amounts. Fee transfers are handled in ERC4626HooksTrait methods.

NOTE: When a vault implements fees on deposits or withdrawals (either in shares or assets), fee transfers must be handled in these hooks by library clients. This creates a non-atomic operation flow consisting of multiple state-changing steps: transferring assets, minting or burning shares, and transferring (or minting) fees. Between these steps, the vault’s state is temporarily inconsistent: the asset-to-share conversion rate does not accurately reflect the vault’s final state until all steps have completed. Therefore, it is critical to avoid making any external calls (including to the vault contract itself) or querying conversion rates during hook execution.

CAUTION: Special care must be taken when calling external contracts in these hooks. In that case, consider implementing reentrancy protections. For example, in the withdraw flow, the withdraw_limit is checked before the before_withdraw hook is invoked. If this hook performs a reentrant call that invokes withdraw again, the subsequent check on withdraw_limit will be done before the first withdrawal’s core logic (e.g., burning shares and transferring assets) is executed. This could lead to bypassing withdrawal constraints or draining funds.

See the example: https://github.com/OpenZeppelin/cairo-contracts/tree/main/packages/test_common/src/mocks/erc4626.cairo

Fully qualified path: openzeppelin_token::erc20::extensions::erc4626::erc4626::ERC4626Component::ERC4626HooksTrait

pub trait ERC4626HooksTrait<TContractState, +HasComponent<TContractState>>

Trait functions

before_deposit

Hooks into InternalImpl::_deposit. Executes logic before transferring assets and minting shares. The fee is calculated via FeeConfigTrait. Assets and shares represent the actual amounts the user will spend and receive, respectively. Asset fees are included in assets; share fees are excluded from shares.

Fully qualified path: openzeppelin_token::erc20::extensions::erc4626::erc4626::ERC4626Component::ERC4626HooksTrait::before_deposit

fn before_deposit(
    ref self: ComponentState<TContractState>,
    caller: ContractAddress,
    receiver: ContractAddress,
    assets: u256,
    shares: u256,
    fee: Option<Fee>,
)

after_deposit

Hooks into InternalImpl::_deposit. Executes logic after transferring assets and minting shares. The fee is calculated via FeeConfigTrait. Assets and shares represent the actual amounts the user will spend and receive, respectively. Asset fees are included in assets; share fees are excluded from shares.

Fully qualified path: openzeppelin_token::erc20::extensions::erc4626::erc4626::ERC4626Component::ERC4626HooksTrait::after_deposit

fn after_deposit(
    ref self: ComponentState<TContractState>,
    caller: ContractAddress,
    receiver: ContractAddress,
    assets: u256,
    shares: u256,
    fee: Option<Fee>,
)

before_withdraw

Hooks into InternalImpl::_withdraw. Executes logic before burning shares and transferring assets. The fee is calculated via FeeConfigTrait. Assets and shares represent the actual amounts the user will receive and spend, respectively. Asset fees are excluded from assets; share fees are included in shares.

Fully qualified path: openzeppelin_token::erc20::extensions::erc4626::erc4626::ERC4626Component::ERC4626HooksTrait::before_withdraw

fn before_withdraw(
    ref self: ComponentState<TContractState>,
    caller: ContractAddress,
    receiver: ContractAddress,
    owner: ContractAddress,
    assets: u256,
    shares: u256,
    fee: Option<Fee>,
)

after_withdraw

Hooks into InternalImpl::_withdraw. Executes logic after burning shares and transferring assets. The fee is calculated via FeeConfigTrait. Assets and shares represent the actual amounts the user will receive and spend, respectively. Asset fees are excluded from assets; share fees are included in shares.

Fully qualified path: openzeppelin_token::erc20::extensions::erc4626::erc4626::ERC4626Component::ERC4626HooksTrait::after_withdraw

fn after_withdraw(
    ref self: ComponentState<TContractState>,
    caller: ContractAddress,
    receiver: ContractAddress,
    owner: ContractAddress,
    assets: u256,
    shares: u256,
    fee: Option<Fee>,
)