Yearn4626RouterExt

Git Source

Inherits: IYearn4626RouterExt, Yearn4626Router

Extends the Yearn4626Router with additional functionality for depositing to Yearn Vault V2 and pulling tokens with Permit2.

This contract introduces two key functions: depositing to Yearn Vault V2 and pulling tokens with a signature via Permit2. The contract holds an immutable reference to a Permit2 contract to facilitate token transfers with permits.

State Variables

_PERMIT2

IPermit2 private immutable _PERMIT2;

Functions

constructor

Constructs the Yearn4626RouterExt contract.

Sets up the router with the name for the vault, WETH address, and Permit2 contract address.

constructor(string memory name_, address weth_, address permit2_) payable Yearn4626Router(name_, IWETH9(weth_));

Parameters

NameTypeDescription
name_stringThe name of the vault.
weth_addressThe address of the WETH contract.
permit2_addressThe address of the Permit2 contract.

serializedDeposits

Deposits the specified amount of assets into series of ERC4626 vaults or Yearn Vault V2.

function serializedDeposits(
    address[] calldata path,
    uint256 assetsIn,
    address to,
    uint256 minSharesOut
)
    external
    payable
    returns (uint256 sharesOut);

Parameters

NameTypeDescription
pathaddress[]The array of addresses that represents the vaults to deposit into.
assetsInuint256The amount of assets to deposit into the first vault.
toaddressThe address to which the shares will be transferred.
minSharesOutuint256The minimum amount of shares expected to be received.

Returns

NameTypeDescription
sharesOutuint256The actual amount of shares received by the to address.

serializedRedeems

Redeems the specified shares from a series of ERC4626 vaults or Yearn Vault V2.

function serializedRedeems(
    address[] calldata path,
    bool[] calldata isYearnVaultV2,
    uint256 sharesIn,
    address to,
    uint256 minAssetsOut
)
    external
    payable
    returns (uint256 assetsOut);

Parameters

NameTypeDescription
pathaddress[]The array of addresses that represents the vaults to redeem from.
isYearnVaultV2bool[]The array of boolean values that represent whether the vault is a Yearn Vault V2.
sharesInuint256The amount of shares to redeem from the first vault.
toaddressThe address to which the assets will be transferred.
minAssetsOutuint256The minimum amount of assets expected to be received.

Returns

NameTypeDescription
assetsOutuint256The actual amount of assets received by the to address.

redeemVaultV2

Redeems the specified shares from the Yearn Vault V2.

The shares must exist in this router before calling this function.

function redeemVaultV2(
    IYearnVaultV2 vault,
    uint256 shares,
    address to,
    uint256 minAssetsOut
)
    public
    payable
    returns (uint256 amountOut);

Parameters

NameTypeDescription
vaultIYearnVaultV2The Yearn Vault V2 contract instance.
sharesuint256The amount of shares to redeem.
toaddressThe address to which the assets will be transferred.
minAssetsOutuint256The minimum amount of assets expected to be received.

Returns

NameTypeDescription
amountOutuint256The actual amount of assets received by the to address.

redeemFromRouter

Redeems the specified IERC4626 vault shares that this router is holding.

function redeemFromRouter(
    IERC4626 vault,
    uint256 shares,
    address to,
    uint256 minAmountOut
)
    public
    payable
    virtual
    returns (uint256 amountOut);

Parameters

NameTypeDescription
vaultIERC4626The IERC4626 vault contract instance.
sharesuint256The amount of shares to redeem.
toaddressThe address to which the assets will be transferred.
minAmountOutuint256The minimum amount of assets expected to be received.

Returns

NameTypeDescription
amountOutuint256The actual amount of assets received by the to address.

withdrawFromRouter

Withdraws the specified assets from the IERC4626 vault.

function withdrawFromRouter(
    IERC4626 vault,
    uint256 assets,
    address to,
    uint256 maxSharesIn
)
    public
    payable
    virtual
    returns (uint256 sharesOut);

Parameters

NameTypeDescription
vaultIERC4626The IERC4626 vault contract instance.
assetsuint256The amount of assets to withdraw.
toaddressThe address to which the assets will be transferred.
maxSharesInuint256The maximum amount of vault shares expected to be burned.

Returns

NameTypeDescription
sharesOutuint256The actual amount of shares burned from the vault.

redeemStakeDaoGauge

Redeems the specified shares of the StakeDAO Gauge.

Assumes the assets withdrawn will be the the yearn vault tokens and will always be the same amount as the shares of the burned StakeDAO gauge tokens.

function redeemStakeDaoGauge(IStakeDaoGauge gauge, uint256 shares, address to) public payable returns (uint256);

Parameters

NameTypeDescription
gaugeIStakeDaoGaugeThe StakeDAO Gauge contract instance.
sharesuint256The amount of StakeDAO gauge tokens to burn.
toaddress

pullTokenWithPermit2

Pulls tokens to the contract using a signature via Permit2.

Verifies that the to address in transferDetails is the contract itself and then calls permitTransferFrom on the Permit2 contract. Reverts with InvalidTo if the to address is not the contract itself.

function pullTokenWithPermit2(
    ISignatureTransfer.PermitTransferFrom calldata permit,
    ISignatureTransfer.SignatureTransferDetails calldata transferDetails,
    bytes calldata signature
)
    public
    payable;

Parameters

NameTypeDescription
permitISignatureTransfer.PermitTransferFromThe PermitTransferFrom struct containing the permit details.
transferDetailsISignatureTransfer.SignatureTransferDetailsThe details of the transfer, including the to address.
signaturebytesThe signature to authorize the token transfer.

previewDeposits

Calculate the amount of shares to be received from a series of deposits to ERC4626 vaults or Yearn Vault V2.

function previewDeposits(
    address[] calldata path,
    uint256 assetsIn
)
    external
    view
    returns (uint256[] memory sharesOut);

Parameters

NameTypeDescription
pathaddress[]The array of addresses that represents the path from input token to output token
assetsInuint256The amount of assets to deposit into the first vault.

Returns

NameTypeDescription
sharesOutuint256[]The amount of shares to be received from each deposit. The length of the array is path.length - 1.

previewMints

Calculate the amount of assets required to mint a given amount of shares from a series of deposits to ERC4626 vaults or Yearn Vault V2.

Increment the loop index i without checking for overflow. This is safe because the loop's termination condition ensures that i will not exceed the bounds of the sharesOut array, which would be the only case where an overflow could occur.

sharesOut is the expected result at the last vault, and the path = [tokenIn, vault0, vault1, ..., vaultN]. First calculate the amount of assets in to get the desired sharesOut from the last vault, then using that amount as the next sharesOut to get the amount of assets in for the penultimate vault.

function previewMints(address[] calldata path, uint256 sharesOut) external view returns (uint256[] memory assetsIn);

Parameters

NameTypeDescription
pathaddress[]The array of addresses that represents the path from input to output.
sharesOutuint256The amount of shares to mint from the last vault.

Returns

NameTypeDescription
assetsInuint256[]The amount of assets required at each step. The length of the array is path.length - 1.

previewWithdraws

Calculate the amount of shares required to withdraw a given amount of assets from a series of withdraws from ERC4626 vaults or Yearn Vault V2.

Decrement the loop counter within an unchecked block to avoid redundant gas cost associated with underflow checking. This is safe because the loop's initialization and exit condition ensure that i will not underflow.

assetsOut is the desired result of the output token, and the path = [vault0, vault1, ..., vaultN, tokenOut]. First calculate the amount of shares in to get the desired assetsOut from the last vault, then using that amount as the next assetsOut to get the amount of shares in for the penultimate vault.

function previewWithdraws(
    address[] calldata path,
    uint256 assetsOut
)
    external
    view
    returns (uint256[] memory sharesIn);

Parameters

NameTypeDescription
pathaddress[]The array of addresses that represents the path from input to output.
assetsOutuint256The amount of assets to withdraw from the last vault.

Returns

NameTypeDescription
sharesInuint256[]The amount of shares required at each step. The length of the array is path.length - 1.

previewRedeems

Calculate the amount of assets to be received from a series of withdraws from ERC4626 vaults or Yearn Vault V2.

Decrement the loop counter without checking for overflow. This is safe because the for loop naturally ensures that i will not underflow as it is bounded by i == 0 check.

function previewRedeems(address[] calldata path, uint256 sharesIn) external view returns (uint256[] memory assetsOut);

Parameters

NameTypeDescription
pathaddress[]The array of addresses that represents the path from input to output.
sharesInuint256The amount of shares to withdraw from the first vault.

Returns

NameTypeDescription
assetsOutuint256[]The amount of assets to be received at each step. The length of the array is path.length - 1.

Errors

InsufficientShares

Error for when the number of shares received is less than the minimum expected.

error InsufficientShares();

InsufficientAssets

Error for when the amount of assets received is less than the minimum expected.

error InsufficientAssets();

RequiresMoreThanMaxShares

Error for when the amount of shares burned is more than the maximum expected.

error RequiresMoreThanMaxShares();

InvalidPermit2TransferTo

Error for when the to address in the Permit2 transfer is not the router contract.

error InvalidPermit2TransferTo();

InvalidPermit2TransferAmount

Error for when the amount in the Permit2 transfer is not the same as the requested amount.

error InvalidPermit2TransferAmount();

InvalidPathLength

Error for when the serialized deposit path is too short.

error InvalidPathLength();

PreviewPathIsTooShort

Error for when the path is too short to preview the deposits/mints/withdraws/redeems.

error PreviewPathIsTooShort();

PreviewNonVaultAddressInPath

Error for when the address in the path is not a vault.

error PreviewNonVaultAddressInPath(address invalidVault);

PreviewVaultMismatch

Error for when an address in the path does not match previous or next vault's asset.

error PreviewVaultMismatch();