AMM Data Structures

The LiquidityPoolData struct used in addLiquidity, addLiquidityBatch, removeLiquidity and removeLiquidityBatch operations is composed as follows:

struct LiquidityPoolData {
    address liquidityPoolAddress;
    uint256 amount;
    address tokenAddress;
    bool amountIsLiquidityPool;
    bool involvingETH;
    address receiver;
}

The LiquidityPoolData can be used to add or remove liquidity to/from an AMM, using both the LP (liquidity pool) address or one of the two tokens componing the pair (the pair can be also composed of more than two tokens in case of Balancer for example).

So, if you want to use an LP token the parameters must be passed as follows:

  • liquidityPoolAddress -> LP address chosen to execute the operation.

  • amount -> the LP token amount desired to be added/removed.

  • tokenAddress -> this parameter must be passed as address(0) because is not used in this case.

  • amountIsLiquidityPool -> true.

  • involvingETH -> a boolean value representing if ETH is involved in the LP -> this is crucial for AMMs like Uniswap where Ethereum is represented by WETH. So for example if the LP is ETH/OS involvingETH is true, if it is WETH/OSinvolvingETH is false.

  • receiver -> the receiver address of the addLiquidity or removeLiquidity operation. If it's passed as address(0), the receiver address is the msg.sender.

If you want to use the pairs tokens the parameters must be passed as follows:

  • liquidityPoolAddress -> the LP address of the pool to use.

  • amount -> the pair token amount desired to be added/removed.

  • tokenAddress -> represents the address of one of the pair tokens.

  • amountIsLiquidityPool -> false.

  • involvingETH -> a boolean value representing if ETH is involved in one of the pair tokens -> this is crucial for AMMs like Uniswap where Ethereum is represented by WETH. So for example if the LP is ETH/OS involvingETH is true, if it is WETH/OSinvolvingETH is false.

  • receiver -> the receiver address of the addLiquidity or removeLiquidity operation. If it's passed as address(0), the receiver address is the msg.sender.

The SwapData struct used in swapLiquidity and swapLiquidityBatch operations and is composed as follows:

struct SwapData {
    bool enterInETH;
    bool exitInETH;
    address[] liquidityPoolAddresses;
    address[] path;
    address inputToken;
    uint256 amount;
    address receiver;
}
  • enterInETH -> a boolean value representing if the input token is ETH (true) or not (false). So for example, if the input token is ETH enterInETH is true; if the input token is WETH enterInETH is false.

  • exitInETH -> a boolean value representing if the output token is ETH (true) or not (false). So for example, if the output token is ETH exitInETH is true, if output token is WETH exitInETH is false.

  • liquidityPoolAddresses -> array containing the liquidity pool token addresses to be used in the swap operation. The first element of the array must necessarily be an LP address containing the input token.

  • path -> array containing the path (represented by the tokens used) that the swap operation must follow.

  • inputToken -> the address of the token you want to swap.

  • amount -> the token amount to swap in the operation.

  • receiver -> the output token receiver address.If it's passed as address(0), the receiver address is the msg.sender.

For example, if an operation swaps USDC for ETH and then for OS, USDC will be the input token and OS will be the output token. The liquidityPoolAddresses array will contain the USDC/ETH LP address in the first position, and the ETH/OS LP address in the second one. The swapPath array will contain ETH in the first position, and OS in the second one.

Last updated