Skip to content
Lattice

ERC4626AdapterLib

Git Source

Title: ERC4626AdapterLib

Author: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80)

Logic wrapping any ERC4626 vault (Yearn v3 / MetaMorpho) as a Lattice strategy. NAV via convertToAssets (no oracle). Supply-only.

Functions

erc4626AdapterStorage

function erc4626AdapterStorage() internal pure returns (ERC4626AdapterStorage storage $);

Returns

NameTypeDescription
$ERC4626AdapterStorage

__ERC4626Adapter_init

function __ERC4626Adapter_init(address target, address asset_, address vault_, address recipient_) internal;

Parameters

NameTypeDescription
targetaddress
asset_address
vault_address
recipient_address

registerInterface

function registerInterface() internal;

asset

function asset() internal view returns (address);

Returns

NameTypeDescription
<none>address

vault

function vault() internal view returns (address);

Returns

NameTypeDescription
<none>address

targetVault

function targetVault() internal view returns (address);

Returns

NameTypeDescription
<none>address

sideRewardToken

function sideRewardToken() internal view returns (address);

Returns

NameTypeDescription
<none>address

rewardRecipient

function rewardRecipient() internal view returns (address);

Returns

NameTypeDescription
<none>address

operator

function operator() internal view returns (address);

Returns

NameTypeDescription
<none>address

_checkOperator

Reverts ProtocolAdapterUnauthorized unless the caller is the wired operator. Placed at the very top of deploy/withdraw/harvest — BEFORE the reentrancy guard — so an unauthorized call never leaves the guard latched. Zero operator ⇒ always reverts.

function _checkOperator() private view;

healthFactor

function healthFactor() internal pure returns (uint256);

Returns

NameTypeDescription
<none>uint256

minHealthFactor

function minHealthFactor() internal pure returns (uint256);

Returns

NameTypeDescription
<none>uint256

isPaused

function isPaused() internal view returns (bool);

Returns

NameTypeDescription
<none>bool

totalAssetsManaged

NAV = convertToAssets(shareBalance) + idle underlying. No oracle — ERC4626 1:1.

Idle leg (NAV-gap fix): adds the adapter's idle underlying balance to the share-priced position. IVaultCore.allocateToStrategy pushes funds here with a bare transfer that does NOT deposit into the target vault, so undeployed funds sit idle until deploy() (and shares can be 0 while idle is non-zero — exactly the allocate-before- deploy state). Counting that idle keeps the vault's share price flat across the allocate→deploy window. No double-count: the idle is in neither the vault's idle nor the target shares, and after deploy() idle→~0 while shares grow by the same value, so the sum is invariant across deploy.

function totalAssetsManaged() internal view returns (uint256);

Returns

NameTypeDescription
<none>uint256

setSideRewardToken

Rejects the asset and the target vault's share token: harvest forwards the side token's whole balance raw, so either would move the position itself to the reward recipient.

function setSideRewardToken(address token) internal;

Parameters

NameTypeDescription
tokenaddress

setRewardRecipient

function setRewardRecipient(address recipient) internal;

Parameters

NameTypeDescription
recipientaddress

setOperator

Sets the authorized operator for deploy/withdraw/harvest (admin-only). Rejects address(0) so the trio cannot be opened to an unauthenticated default.

function setOperator(address operator_) internal;

Parameters

NameTypeDescription
operator_address

deploy

function deploy() internal returns (uint256 deployed);

Returns

NameTypeDescription
deployeduint256

withdraw

Recalls amount of the asset to the vault, spending undeployed idle before the position.

Idle first: totalAssetsManaged counts idle, so a recall that only unwound the position would under-deliver whenever funds sit undeployed. The remainder is withdrawn by assets through withdraw, which rounds shares up and delivers the exact amount at any price per share (a convertToShares + redeem pair floors twice). Above maxWithdraw every redeemable share is redeemed instead, and the return value reports what was actually sent.

function withdraw(uint256 amount, address to) internal returns (uint256 withdrawn);

Parameters

NameTypeDescription
amountuint256
toaddress

Returns

NameTypeDescription
withdrawnuint256

harvest

Forwards an optional side-reward token raw. Most ERC4626 vaults auto-compound, so this is usually a no-op. Never reverts the withdraw path.

function harvest() internal;

emergencyWithdraw

Redeems every redeemable share to the vault and sweeps the adapter's idle asset with it.

function emergencyWithdraw() internal returns (uint256 recovered);

Returns

NameTypeDescription
recovereduint256