# ๐คIn Depth - How It Works

(Info via TJ Docs)

## Algorithm to Calculate Risk-Free Value (RFV)โ

This algorithm has the added benefit of never having a gap of liquidity between Floor and active price.

We simulate if there's enough `totalETH`

to absorb sell pressure if we dumped all `circLILY`

at the price in the bin `activeId - 1`

. The value of ETH if we sold all the circulating `LILY`

at that price is `circLILY * price(activeId - 1) = valueETH`

.

If `valueETH > totalETH`

, then the price `price(activeId - 1)`

is too high to absorb all sells of circulating `LILY`

so we iterate to the next bin down, `activeId - 2`

.

Once again we calculate `valueETH`

, but with a caveat this time: we simulate that the ETH in the bin above, `activeId - 1`

, has all its ETH converted to `LILY`

and then check if we have enough ETH to absorb the rest of the circulating supply of `LILY`

.

To do this, first we convert all the ETH to `LILY`

in bin `activeId - 1`

, which is `reserveETH(activeId - 1) / price(activeId - 1)`

.

Then we subtract this from the circulating supply of `LILY`

to get the new circulating supply and multiply this new circulating supply with the price in `activeId - 2`

: `valueETH = (circLILY - reserveETH(activeId - 1) / price(activeId - 1)) * price(activeId - 2)`

Then we check if `valueETH <= totalETH - reserveETH(activeId - 1)`

. If not, we iterate down to the next bin and so on.

We do this until we land on a bin where `value_ETH <= total_ETH - sum_i(reserveETH(activeId - i))`

. Once it does, it means we have found a bin, `activeId - i`

, that can absorb all the sells of `LILY`

, and thus, that bin becomes the new Floor price.

Here comes the fun part - to rebalance, we only take ETH from all bins smaller than `activeId - i`

and place them in bin `activeId - i`

. All bins with ETH above `activeId - i`

remain **untouched**. This also means that **there is never a gap of liquidity between Floor and active bin**.

That was quite hard to follow so let's illustrate it with an example below.

## Exampleโ

**Initial Distribution****โ**

Protocol seeds 2100 LILY tokens into the LILY/ETH pool which has a bin step of 0.01 ETH.

The liquidity is equally distributed with 100 LILY in each bin across 21 bins from starting price of 1 ETH per LILY to 1.2 ETH per LILY.

**First Buy****โ**

**First Buy**

**โ**

Alice is the first buyer of the pool and opts to buy 1000 LILY tokens. Because 1000 LILY has been removed from the initial bins, circulating supply is now 1000.

Assuming a 1% swap fee, the current price is now 1.09 ETH per LILY and the bins look like this:

**Rebalance****โ**

**Rebalance**

**โ**

After the buy, a rebalance is triggered by the protocol to redistribute ETH into the Floor bin.

To find the Floor price, we use an iterative algorithm that simulates whether there's enough ETH in the pool if the entire circulating supply of LILY (1000) is sold at the current price (1.09 ETH per LILY).

First we sum up the ETH in the pool and get 1055.45 ETH - this is the `totalETH`

.

Then we get the circulating supply of LILY valued in ETH, which is 1000 * 1.09 = 1090 - this is the `valueETH`

.

Because `valueETH > totalETH`

, we cannot make 1.09 the Floor price so we move down to next bin 1.08 and then we repeat the algorithm again.

First we subtract the amount of ETH currently in the 1.09 bin from `totalETH`

, which is 1055.45 - 110.09 = 945.36 ETH - this is the new `totalETH`

.

Then we get the new circulating supply if the price moved down to 1.09. We get this by finding the amount of LILY that would fill the 1.09 bin if all the ETH in there had been converted to LILY, which is 110.09/1.09 = 101 LILY.

Then we subtract current circulating supply by this number to get 1000 - 101 = 899 LILY - this is the new `circLILY`

.

We then calculate the new `valueETH`

which is 899 * 1.08 = 970.92 ETH.

As `valueETH > total_ETH`

still, we iterate down to the 1.07 bin and repeat once again.

We've already calculated the first bin where `valueETH <= totalETH`

in the table below, which is 1.04 ETH per LILY. Thus, 1.04 becomes the new Floor.

Now that we have our Floor price of 1.04, we execute our rebalance. Remember:

All bins above 1.04 remain untouched

All bins below 1.04 are rebalanced into 1.04

Therefore, we simply take all the ETH in bins 1, 1.01, 1.02 and 1.03, and put them into 1.04.

**What About Sells?****โ**

**What About Sells?**

**โ**

If Alice then sells LILY back into the pool, no rebalancing is necessary as we've already proven that the invariant holds and the current Floor price is low enough to absorb as much sell pressure.

**What if Circulating Supply Increases?****โ**

**What if Circulating Supply Increases?**

**โ**

Circulating supply can only increase if more LILY is bought from the initial bins.

As these LILY tokens are bought in exchange for ETH which is locked into the pool forever, any circulating supply increase will always have a corresponding increase in ETH in the pool that can absorb sell pressure.

## Disclaimer

Onyx Lily tokens are released via a Trader Joe v2.1 Liquidity Pool. The purchase of Onyx Lily tokens does not constitute an investment contract or any form of investment advice.

Onyx Lily no guarantees or promises about the future value or performance of the token, and the value of the token may fluctuate significantly.

Also note that it might be illegal for you to own Onyx Lily depending on your jurisdiction, check with your local laws.

Last updated