Skip to main content

Bin Liquidity

Introduction

Liquidity in each bin is guided by the constant sum price invariant, Px+y=LP \cdot x + y = L, where xx is the quantity of asset XX, yy is the quantity of asset YY, LL is the amount of liquidity in the bin and PP is the price defined by P=ΔyΔxP = \frac{\Delta y}{\Delta x}. This is more easily visualised by the graph below:

Price curve of constant sum formula

Bin Composition

Since PP is the gradient of the line, it is always constant within each bin.

However, unlike the constant product formula, which you may be used to from other AMMs, the price is decoupled from the reserve states of XX and YY. Put differently, given a price PP and a known amount of xx (or yy), you cannot find yy (or xx). In constant product this is possible by simply taking y=Pxy = \frac{P}{x}.

Given that the composition of reserves, xx and yy, are independent of both price and liquidity, an additional variable is used describe the reserves available in the bin. This variable, composition factor (cc), is the percentage of bin liquidity composing of yy:

cyLc \equiv \frac{y}{L}

From this equation, we can deduce xx and yy as follows:

y=cLy = cL
x=LP(1c)x = \frac{L}{P}(1 - c)

Market Aggregation

Another notable difference is that the constant sum curve intercepts both the xx and yy axes. This means the reserves of XX or YY can be depleted. In such a case, the current price moves to the next bin (either to the left or right).

In fact, in any given market, there can only be one bin that contains reserves of both XX and YY - this is the active price bin. All bins to the right of the active bin will contain only XX and all bins to the left will only contain YY.

Chart showing bin compositions

A simple way to think of this is to imagine the MAS/USDC pool. Let asset YY be USDC and asset XX be MAS. Price PP is defined by amount of USDC per MAS. Let the active bin be $1 MAS; all bins to the left contain only USDC and all bins to the right contain only MAS. If there is a lot of buying of MAS, then the active bin will move to the right once reserves of MAS is depleted from the $1 bin.

The way LB aggregates liquidity is also different to Uniswap V3. In LB, liquidity is aggregated vertically via each bin and in Uniswap V3, liquidity is aggregated horizontally. The main benefit of vertical aggregation is that it allows for liquidity to be fungible.

Comparison of Uniswap V3 vs LB liquidity aggregation

Liquidity Tokens

LB introduces a new token standard, LBToken, as the receipt token for liquidity positions.

LBToken tracks the amount of liquidity added to each bin for each user in a given pair. For all intensive purposes, it is almost the same as an ERC-1155 token, but without the functions and variables that are related to NFTs. This makes LBToken fungible, which allows vaults/farms to be easily built on top.

Liquidity Tracking

To track liquidity, we use a three level trie in which each node is a 256 bit array represented by a u256. The bottom level, depth 2, contains 2563=16,777,216256^3 = 16,777,216 slots, which contains exactly the maximum possible number of bins, 2242^{24}.

When a bin has liquidity, its slot will contain a 1, otherwise it contains a 0. If it contains a 1, then the corresponding slot in its parent will also contain a 1, and likewise, so will the corresponding slot in its grandparent.

Since we always know which bin is the active bin, using a tree structure allows us to find the next bin to its left or right that has liquidity quickly by tracking a path via its parent.

Tree data structure to track bin liquidity