Bin Liquidity
Introduction
Liquidity in each bin is guided by the constant sum price invariant, , where is the quantity of asset , is the quantity of asset , is the amount of liquidity in the bin and is the price defined by . This is more easily visualised by the graph below:
Bin Composition
Since 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 and . Put differently, given a price and a known amount of (or ), you cannot find (or ). In constant product this is possible by simply taking .
Given that the composition of reserves, and , are independent of both price and liquidity, an additional variable is used describe the reserves available in the bin. This variable, composition factor (), is the percentage of bin liquidity composing of :
From this equation, we can deduce and as follows:
Market Aggregation
Another notable difference is that the constant sum curve intercepts both the and axes. This means the reserves of or 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 and - this is the active price bin. All bins to the right of the active bin will contain only and all bins to the left will only contain .
A simple way to think of this is to imagine the MAS/USDC pool. Let asset be USDC and asset be MAS. Price 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.
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 slots, which contains exactly the maximum possible number of bins, .
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.