Skip to Content
GlowSwap Documentation 🎉
Equations

Equations

Below is a non-exhaustive list of equations used across GlowSwap.

Constant Product Formula

x * y = k

Liquidity Impact

Liquidity impact defines how much liquidity (sqrt(k)) has been added or removed from a CPAMM pool from the act of depositing, borrowing, or removing tokens.

  • A positive liquidity impact generally means that tokens have been added into the pool.
  • A negative liquidity impact generally means that tokens have been removed from the pool.

liquidity impact = sqrt((x + dx) * (y * dy)) - sqrt(x * y)

Finding dx and dy in ratio to add/remove liquidity

This writeup shows how to derive how much dx and dy to add or remove to an existing pair to reach a target amount of liquidity given ratio r.

The equation can be used to solve the two follow examples:

  1. If a liquidity pair has 100 X and 100 Y, how can we add +10 liquidity impact by adding tokens in the ratio r.

    • The equation is used to derive how much X and Y exclusive pools must pay back into the main pool when interest is being collected.
  2. If a liquidity pair has 100 X and 100 Y, how can we remove 10 liquidity impact by removing tokens in ratio r.

    • This equation can be used to calculate how many X and Y burning a certain amount of liquidity tokens will withdraw given a ratio to pull the tokens out
dy = (-b + sqrt(b^2-4ac)) / 2a

Where

a = ratio b = Token X Reserves + ratio*TokenYReserves c = K - finalK

dx can then be calculated as

dx = r*dy

Or for better accuracy with integers,

dx = finalK/(Y+dy) - X

To see the full derivation, click here

Minimum Liquidity Arb Transaction

This equation is used in interest collection. An exclusive pool can end up in a position where it doesen’t have enough interest to pay the main pool AND also maintain its minimum liquidity, but if it performs and arbitrage swap with the main pool, it can change that.

To view the full derivation, click here

Initialization Fee Formula

Initialization Fee = (Slot Fee Per Year) /365 * 30^(0.2+0.04*num_exclusive_cpmms)

Slot Fee Formula

The slot fee is calculated as a percentage of the total amount of liquidity in the exclusive pool.

The equation for updating the fee when there are fewer than 20 exclusive CPMMs is:

slot_fee_per_year *= (1-(20 - num_exclusive_cpmms)*0.01)^(days) min_slot_fee = source_cpmm_liquidity * interest_rate * 1 year / 5000 if slot_fee_per_year < min_slot_fee { slot_fee_per_year = min_slot_fee }

The equation for updating the fee when there are more than 20 exclusive CPMMs is:

slot_fee_per_year *= (1+(num_exclusive_cpmms-20)*0.01)^(days) min_slot_fee = source_cpmm_liquidity * interest_rate / 5000 if slot_fee_per_year < min_slot_fee { slot_fee_per_year = min_slot_fee }

Law of Accumulating Liquidity

Exploring the Law of Accumulating Liquidity

The Law of Accumulating Liquidity states that the collective amount of liquidity in separate CPMMs is always less than or equal to the amount of liquidity in a CPMM that combines all of the assets together. This law assumes that every CPMM has a non-zero and positive quantity of each asset.

Let’s explore some examples:

Example 1: Same Ratio CPMMs

NameUSDCGLWLiquidity
1100100100
2100100100
Combined200200200

If all CPMMs have the same ratio of assets, then the combined liquidity will be equal to the sum of the liquidity in each CPMM, as shown above.

Example 2: Different Ratio CPMMs

NameUSDCGLWLiquidity
1100100100
225400100
Combined125500250

In this example, both CPMMs start with 100 liquidity, but because the CPMMs have a different ratio of assets, the combined CPMM has 25% more liquidity than the two individual CPMMs. This effect gets more extreme as the differences between the individual CPMMs gets more extreme.

Example 3: Extreme Ratio Difference

NameUSDCGLWLiquidity
133400100
240025100
Combined433425425

In this example, the combined CPMM has more than twice as much liquidity as the sum of the liquidity in the individual CPMMs.

Example 4: Multiple CPMMs

NameUSDCGLWLiquidity
1100100100
225400100
340025100
4101010
Combined535535535

In this final example, you can see that the Law of Accumulating Liquidity holds across any number of CPMMs, with the CPMMs individually having 310 liquidity total, but combined having 535 liquidity total.

Practical Implications

The Law of Accumulating Liquidity is useful in the context of exclusive CPMMs because it means that as long as the borrower is required to adhere to the Constant Product Rule in their exclusive CPMM, the borrower is also guaranteed to be able to return as much liquidity as they borrowed, regardless of how the ratio of assets in the source CPMM and exclusive CPMM change.

The Glowswap smart contracts enforce that all exclusive CPMMs are required to adhere to the Constant Product Rule, creating safety for the LPs of the source CPMM and guaranteeing that when an exclusive CPMM is closed and liquidity is returned, the source CPMM will get back at least as much liquidity as was borrowed.

Mathematical Proof of the Law of Accumulating Liquidity

Mathematically, the Law of Accumulating Liquidity for two CPMMs can be expressed as:

sqrt(x1*y1) + sqrt(x2*y2) <= sqrt((x1 + x2) * (y1 + y2))

In the above inequality, x1 and y1 are the number of Glow tokens and USDC tokens in the first CPMM. x2 and y2 are the number of Glow tokens and USDC tokens in the second CPMM. The equation sqrt(x1*y1)+sqrt(x2*y2) evaluates the sum of the liquidity in the two CPMMs, and the equation sqrt((x1 + x2) * (y1 + y2)) evaluates the amount of liquidity in a CPMM that combines all assets together.

The inequality can be simplified by squaring both sides:

(sqrt(x1*y1) + sqrt(x2*y2))^2 <= (x1 + x2) * (y1 + y2)

Which has the result:

x1*y1 + 2*sqrt(x1*y1)*sqrt(x2*y2) + x2*y2 <= (x1 + x2) * (y1 + y2)

This can be further simplified to:

x1*y1 + 2*sqrt(x1*y1*x2*y2) + x2*y2 <= (x1 + x2) * (y1 + y2)

The right side can then be expanded to:

x1*y1 + 2*sqrt(x1*y1*x2*y2) + x2*y2 <= x1*y1 + x1*y2 + x2*y1 + x2*y2

You can then eliminate the terms that appear on both sides of the equation, resulting in:

2*sqrt(x1*y1*x2*y2) <= x1*y2 + x2*y1

You can then further simplify the equation by setting a=x1*y2 and b=x2*y1, resulting in:

2*sqrt(a*b) <= a + b

We can eliminate the remaining square root by squaring both sides:

4*a*b <= a^2 + 2*a*b + b^2

We then subtract 4*a*b from both sides, resulting in:

0 <= a^2 - 2*a*b + b^2

Using a difference of squares, we can simplify further to:

0 <= (a-b)^2

This completes the proof, as it is well known that the square of any value is greater than or equal to zero, thus demonstrating that the original inequality was also true.

Extension to Multiple CPMMs

The proof can easily be extended from two CPMMs to an arbitrary number of CPMMs. When combining the liquidity from a large number of CPMMs, one can simply combine 2 CPMMs at a time, until only one CPMM remains. Thanks to the above proof, we know that each time two CPMMs are combined, the total amount of liquidity will either stay the same or increase, thus proving that:

For any set of CPMMs that share an asset pair, the sum of the liquidity of each CPMM will always be less than or equal to the liquidity of a single CPMM that combines all of their assets.

Last updated on