Liquidity Density Functions (LDFs)
Liquidity Density Functions (LDFs) are a core concept in Bunni v2 that enable efficient liquidity distribution, modification, and swaps with constant gas costs. LDFs provide a new language for specifying liquidity distributions that builds directly on top of Uniswap v3's concept of ticks.
What are LDFs?
An LDF is a normalized function that defines how liquidity is distributed over "ricks" (rounded ticks) in a Bunni v2 pool. Mathematically, an LDF is a function , where is the rick space and is the tick spacing.
Given a pool with total liquidity and liquidity at rick , we can compute using the LDF:
LDFs are normalized, meaning that .
Rick Indices
To simplify the representation of LDFs, we can use rick indices. Given tick spacing , rick , and origin , the rick index is defined as:
This allows us to represent an LDF alternatively as:
Example: Geometric Distribution
A basic example of an LDF is the geometric distribution. Given exponent and length , the geometric LDF is defined as:
This distribution allocates liquidity over ricks following a geometric pattern. By adjusting the origin , the LDF can be shifted over the rick space. By modifying and , the shape of the distribution can be changed.
Cumulative Amount Functions (CAFs)
Each LDF has two associated Cumulative Amount Functions (CAFs), and , one for each token in a pool. CAFs are essential for computing liquidity modifications and swaps.
For token₀, the CAF at rick is defined as:
where is the amount of token₀ in rick .
Similarly, for token₁:
Inverse Cumulative Amount Functions (ICAFs)
ICAFs are the inverse of CAFs and are crucial for computing swaps in constant time. For each LDF, there are two ICAFs, and , defined as:
The computation of ICAFs depends on the specific LDF. For geometric LDFs, ICAFs can be computed via basic arithmetic operations in constant time.
Composing LDFs
LDFs can be easily composed to create more complex distributions. Given two LDFs and , a new LDF can be created as:
This composition property allows for the creation of sophisticated liquidity distributions tailored to specific market conditions or strategies.