Liquidity Iteration

An explanation of how liquidity is iterated through on duality

When swapping through liquidity via a Swap, Multi-Hop Swap, or a Taker Limit Order we iterate through the available TickLiquidity to fill the swap order. Liquidity is always iterated through in order of best to worst price. In the case of swapping Token0 (tokenIn) for Token1 (tokenOut) we iterate through tick indexes left to right (eg. -1, 0, 1, 2...) and for Token1 for Token0 we iterate from right to left (eg. 2, 1, 0, -1…) For each swap we completely exhaust the available reserves before moving onto the next tick. For TickLiquidity instances at the same TickIndex they are iterated through in a deterministic order as follows:

  • PoolReserves: In Ascending Fee order

  • LimitOrderTranches: In ascending TrancheKey order

When swapping through PoolReserves the proceeds from the swap are added to the reserves on the reciprocal side of the pool. Ie. The output of TokenA swapped through a PoolReserves at tick 1, fee 1 will moved to a PoolReserves holding TokenB at tick -1 fee 1.

Last updated