Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private Vault Router #73

Open
20 tasks
Gevarist opened this issue Apr 8, 2024 · 0 comments
Open
20 tasks

Private Vault Router #73

Gevarist opened this issue Apr 8, 2024 · 0 comments

Comments

@Gevarist
Copy link
Contributor

Gevarist commented Apr 8, 2024

ArrakisPrivateVaultRouter

Inherits:
IArrakisPrivateVaultRouter, ReentrancyGuard, Ownable, Pausable

State Variables

nativeToken

address public immutable nativeToken;

permit2

IPermit2 public immutable permit2;

swapper

IRouterSwapExecutor public immutable swapper;

factory

IArrakisMetaVaultFactory public immutable factory;

weth

IWETH9 public immutable weth;

Functions

onlyPrivateVault

modifier onlyPrivateVault(address vault_);

constructor

constructor(address nativeToken_, address permit2_, address swapper_, address owner_, address factory_, address weth_);

pause

function used to pause the router.

only callable by owner

function pause() external whenNotPaused onlyOwner;

unpause

function used to unpause the router.

only callable by owner

function unpause() external whenPaused onlyOwner;

AddLiquidityData

struct AddLiquidityData {
    uint256 amount0;
    uint256 amount1;
    address vault;
}

addLiquidity

addLiquidity adds liquidity to meta vault of interest (mints L tokens)

function addLiquidity(AddLiquidityData memory params_)
    external
    payable
    nonReentrant
    whenNotPaused
    onlyPrivateVault(params_.vault)
    returns (uint256 amount0, uint256 amount1);

Parameters

Name Type Description
params_ AddLiquidityData AddLiquidityData struct containing data for adding liquidity

Returns

Name Type Description
amount0 uint256 amount of token0 transferred from msg.sender to vault's module
amount1 uint256 amount of token1 transferred from msg.sender to vault's module

Check

  • addLiquidityData.amount0 or addLiquidityData.amount1 should be greater than 0.
  • check if msg.sender is a depositor.
  • check that router is a depositor.

Effects

  • get token0 and token1 from vault.
  • get active module of the vault.

Interactions

  • increase allowance addLiquidityData.amount0 and addLiquidityData.amount1 to module.
  • call ArrakisMetaVaultPrivate.fund with addLiquidityData.amount0 and addLiquidityData.amount1.

Assertions

  • check no leftovers.

SwapAndAddData

[Git Source]

struct SwapAndAddData {
    SwapData swapData;
    AddLiquidityData addData;
}

SwapData

Git Source

struct SwapData {
    bytes swapPayload;
    uint256 amountInSwap;
    uint256 amountOutSwap;
    address swapRouter;
    bool zeroForOne;
}

swapAndAddLiquidity

swapAndAddLiquidity transfer tokens to and calls RouterSwapExecutor

function swapAndAddLiquidity(SwapAndAddData memory params_)
    external
    payable
    nonReentrant
    whenNotPaused
    onlyPrivateVault(params_.addData.vault)
    returns (uint256 amount0, uint256 amount1, uint256 amount0Diff, uint256 amount1Diff);

Parameters

Name Type Description
params_ SwapAndAddData SwapAndAddData struct containing data for swap

Returns

Name Type Description
amount0 uint256 amount of token0 transferred from msg.sender to mint mintAmount
amount1 uint256 amount of token1 transferred from msg.sender to mint mintAmount
amount0Diff uint256 token0 balance difference post swap
amount1Diff uint256 token1 balance difference post swap

Checks

  • check that msg.sender is depositor.
  • check that router is a depositor.

Effects

  • get active module from vault.
  • get token0 and token1 from vault.
  • check amount0 or amount1 is greater than zero.

Interactions

  • get amount0 and amount1 from msg.sender
  • increase allowance to router.
  • do swap by calling router.
  • check that at least expectedMinReturn is returned.
  • increase allowance to module
  • call deposit function of vault.

Assertions

  • no left over sitting on the module.

RemoveLiquidityData

struct RemoveLiquidityData {
    uint256 amount0;
    uint256 amount1;
    address vault;
    address payable receiver;
}

addLiquidityPermit2

addLiquidityPermit2 adds liquidity to private vault of interest (mints LP tokens)

function addLiquidityPermit2(AddLiquidityPermit2Data memory params_)
    external
    payable
    nonReentrant
    whenNotPaused
    onlyPrivateVault(params_.addData.vault)
    returns (uint256 amount0, uint256 amount1, uint256 sharesReceived);

Parameters

Name Type Description
params_ AddLiquidityPermit2Data AddLiquidityPermit2Data struct containing data for adding liquidity

Returns

Name Type Description
amount0 uint256 amount of token0 transferred from msg.sender to vault
amount1 uint256 amount of token1 transferred from msg.sender to vault

swapAndAddLiquidityPermit2

swapAndAddLiquidityPermit2 transfer tokens to and calls RouterSwapExecutor

function swapAndAddLiquidityPermit2(SwapAndAddPermit2Data memory params_)
    external
    payable
    nonReentrant
    whenNotPaused
    onlyPrivateVault(params_.swapAndAddData.addData.vault)
    returns (uint256 amount0, uint256 amount1, uint256 sharesReceived, uint256 amount0Diff, uint256 amount1Diff);

Parameters

Name Type Description
params_ SwapAndAddPermit2Data SwapAndAddPermit2Data struct containing data for swap

Returns

Name Type Description
amount0 uint256 amount of token0 transferred from msg.sender to vault
amount1 uint256 amount of token1 transferred from msg.sender to vault
amount0Diff uint256 token0 balance difference post swap
amount1Diff uint256 token1 balance difference post swap

removeLiquidityPermit2

removeLiquidityPermit2 removes liquidity from vault and burns LP tokens

function removeLiquidityPermit2(RemoveLiquidityPermit2Data memory params_)
    external
    nonReentrant
    whenNotPaused
    onlyPrivateVault(params_.removeData.vault)
    returns (uint256 amount0, uint256 amount1);

Parameters

Name Type Description
params_ RemoveLiquidityPermit2Data RemoveLiquidityPermit2Data struct containing data for withdrawals

Returns

Name Type Description
amount0 uint256 actual amount of token0 transferred to receiver for burning burnAmount
amount1 uint256 actual amount of token1 transferred to receiver for burning burnAmount

wrapAndAddLiquidity

wrapAndAddLiquidity wrap eth and adds liquidity to meta vault of iPnterest (mints L tokens)

function wrapAndAddLiquidity(AddLiquidityData memory params_)
    external
    payable
    nonReentrant
    whenNotPaused
    onlyPrivateVault(params_.vault)
    returns (uint256 amount0, uint256 amount1, uint256 sharesReceived);

Parameters

Name Type Description
params_ AddLiquidityData AddLiquidityData struct containing data for adding liquidity

Returns

Name Type Description
amount0 uint256 amount of token0 transferred from msg.sender to vault
amount1 uint256 amount of token1 transferred from msg.sender to vault

wrapAndSwapAndAddLiquidity

wrapAndSwapAndAddLiquidity wrap eth and transfer tokens to and calls RouterSwapExecutor

function wrapAndSwapAndAddLiquidity(SwapAndAddData memory params_)
    external
    payable
    nonReentrant
    whenNotPaused
    onlyPrivateVault(params_.addData.vault)
    returns (uint256 amount0, uint256 amount1, uint256 sharesReceived, uint256 amount0Diff, uint256 amount1Diff);

Parameters

Name Type Description
params_ SwapAndAddData SwapAndAddData struct containing data for swap

Returns

Name Type Description
amount0 uint256 amount of token0 transferred from msg.sender to vault
amount1 uint256 amount of token1 transferred from msg.sender to vault
amount0Diff uint256 token0 balance difference post swap
amount1Diff uint256 token1 balance difference post swap

wrapAndAddLiquidityPermit2

wrapAndAddLiquidityPermit2 wrap eth and adds liquidity to private vault of interest (mints LP tokens)

hack to get rid of stack too depth

function wrapAndAddLiquidityPermit2(AddLiquidityPermit2Data memory params_)
    external
    payable
    nonReentrant
    whenNotPaused
    onlyPrivateVault(params_.addData.vault)
    returns (uint256 amount0, uint256 amount1, uint256 sharesReceived);

Parameters

Name Type Description
params_ AddLiquidityPermit2Data AddLiquidityPermit2Data struct containing data for adding liquidity

Returns

Name Type Description
amount0 uint256 amount of token0 transferred from msg.sender to vault
amount1 uint256 amount of token1 transferred from msg.sender to vault

wrapAndSwapAndAddLiquidityPermit2

wrapAndSwapAndAddLiquidityPermit2 wrap eth and transfer tokens to and calls RouterSwapExecutor

function wrapAndSwapAndAddLiquidityPermit2(SwapAndAddPermit2Data memory params_)
    external
    payable
    nonReentrant
    whenNotPaused
    onlyPrivateVault(params_.swapAndAddData.addData.vault)
    returns (uint256 amount0, uint256 amount1, uint256 sharesReceived, uint256 amount0Diff, uint256 amount1Diff);

Parameters

Name Type Description
params_ SwapAndAddPermit2Data SwapAndAddPermit2Data struct containing data for swap

Returns

Name Type Description
amount0 uint256 amount of token0 transferred from msg.sender to vault
amount1 uint256 amount of token1 transferred from msg.sender to vault
amount0Diff uint256 token0 balance difference post swap
amount1Diff uint256 token1 balance difference post swap

receive

hack to get rid of stack too depth

receive() external payable;

getMintAmounts

getMintAmounts used to get the shares we can mint from some max amounts.

function getMintAmounts(address vault_, uint256 maxAmount0_, uint256 maxAmount1_)
    external
    view
    returns (uint256 shareToMint, uint256 amount0ToDeposit, uint256 amount1ToDeposit);

Parameters

Name Type Description
vault_ address meta vault address.
maxAmount0_ uint256 maximum amount of token0 user want to contribute.
maxAmount1_ uint256 maximum amount of token1 user want to contribute.

Returns

Name Type Description
shareToMint uint256 maximum amount of share user can get for 'maxAmount0_' and 'maxAmount1_'.
amount0ToDeposit uint256 amount of token0 user should deposit into the vault for minting 'shareToMint'.
amount1ToDeposit uint256 amount of token1 user should deposit into the vault for minting 'shareToMint'.

_addLiquidity

function _addLiquidity(
    address vault_,
    uint256 amount0_,
    uint256 amount1_,
    uint256 shares_,
    address receiver_,
    address token0_,
    address token1_
) internal;

_swapAndAddLiquidity

function _swapAndAddLiquidity(SwapAndAddData memory params_, address token0_, address token1_)
    internal
    returns (
        uint256 amount0Use,
        uint256 amount1Use,
        uint256 amount0,
        uint256 amount1,
        uint256 sharesReceived,
        uint256 amount0Diff,
        uint256 amount1Diff
    );

_swapAndAddLiquiditySendBackLeftOver

function _swapAndAddLiquiditySendBackLeftOver(SwapAndAddData memory params_, address token0_, address token1_)
    internal
    returns (uint256 amount0, uint256 amount1, uint256 sharesReceived, uint256 amount0Diff, uint256 amount1Diff);

_removeLiquidity

function _removeLiquidity(RemoveLiquidityData memory params_) internal returns (uint256 amount0, uint256 amount1);

_permit2AddLengthOne

function _permit2AddLengthOne(
    AddLiquidityPermit2Data memory params_,
    address token0_,
    address token1_,
    uint256 amount0_,
    uint256 amount1_
) internal;

_permit2AddLengthOneOrTwo

function _permit2AddLengthOneOrTwo(
    AddLiquidityPermit2Data memory params_,
    address token0_,
    address token1_,
    uint256 amount0_,
    uint256 amount1_
) internal;

_permit2Add

function _permit2Add(
    uint256 permittedLength_,
    AddLiquidityPermit2Data memory params_,
    address token0_,
    address token1_,
    uint256 amount0_,
    uint256 amount1_
) internal;

_permit2SwapAndAddLengthOne

function _permit2SwapAndAddLengthOne(SwapAndAddPermit2Data memory params_, address token0_, address token1_) internal;

_permit2SwapAndAddLengthOneOrTwo

function _permit2SwapAndAddLengthOneOrTwo(SwapAndAddPermit2Data memory params_, address token0_, address token1_)
    internal;

_permit2SwapAndAdd

function _permit2SwapAndAdd(
    uint256 permittedLength_,
    SwapAndAddPermit2Data memory params_,
    address token0_,
    address token1_
) internal;

_getMintAmounts

function _getMintAmounts(address vault_, uint256 maxAmount0_, uint256 maxAmount1_)
    internal
    view
    returns (uint256 shareToMint, uint256 amount0ToDeposit, uint256 amount1ToDeposit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants