Liquidity Iteration
When swapping through liquidity via a 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 (from the taker's perspective.) For each swap, we completely exhaust the available reserves before moving on to the next tick. For TickLiquidity
instances at the same TickIndex
they are iterated through in a deterministic order as follows:
PoolReserves
take priorityLimitOrderTranches
are iterated through when Pool Reserve liquidity is depletedLimitOrderTranches
are iterated through in alphabetical order of theirTrancheKey
(LimitOrderTranche.Key.TrancheKey
.)
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
will be moved to a PoolReserves
holding TokenB.
Example liquidity Placement
DEX Deposits (0 fee):
- Deposit 1:Amounts: 10
ATOM
10USDC
. Price: 10USDC
perATOM
. Tick index . - Deposit 2:Amounts: 10
ATOM
10USDC
. Price: 9USDC
perATOM
. Tick index . - Deposit 3:Amounts: 10
ATOM
10USDC
. Price: 8USDC
perATOM
. Tick index .
DEX Limit Orders:
- Limit Order 1:Amount: 10
ATOM
. Price:ATOM
perUSDC
: Tick index
- Tick Index offers the cheapest
USDC
perATOM
spent. it yieldsUSDC
perATOM
- Tick Index offers the cheapest
ATOM
perUSDC
spent. it yieldsATOM
perUSDC
- It is visible that iterating left to right will always yield the best price regardless of the token being swapped.
Example Liquidity Iteration: Swap
Alice Performs a Swap using a Taker Limit Order
. She wants to swap USDC
for ATOM
at the best possible price.
The first available
TickLiquidity
holdingATOM
is aLimitOrderTranche
at tick . Since this is a limit order, when swapped through and depleted the liquidity is removed from state and theUSDC
Alice paid is credited to the limit orderReceiver
Alice swaps up to
USDC
using this pool before depleting the reserves. This will net herATOM
for the swap:
Alice still has
USDC
she needs to swap, so we move to the next available tick: This tick offersATOM
at a price ofATOM
perUSDC
and is of typePoolReserve
. Any USDC Alice pays for this swap will be placed in the corresponding poolReserves of the Pool (USDC @ Tick .) Alice swaps the remainder of herUSDC
here, resulting in an additionalATOM
:Done. Alice has swapped
USDC
forATOM
with an average price ofATOM
perUSDC
.
NOTE: For the clarity of this example we are assuming that the PoolReserves
have a fee of zero. In a real world scenario almost all PoolReserves
will have a fee. In a scenario with fees TokenIn
will be added to ). Learn more about fees in the PoolReserves section.
Example liquidity Placement Post Swap
We are still displaying the tick liquidity at as a visual aid. In reality this tick is depleted and removed
There are different behaviors a taker limit order can take when swapping. for example, here we were not sensitive to Price. Learn more about the order types and their unique behaviors in the Order Types section.