Protocol Maths

POOL STRUCTURE MATHS

Bin limits

Nitro Finance uses 128.128128.128 binary fixed-point representation, where the left 128 bits represent integers and the right 128 bits represent fractional parts. The upper limit of the price is constrained by 21282^{128}, a significant value that ensures a broad range for asset pricing.

how many bins could we possibly have?

  • Bin prices follow a geometric sequence, denoted as (1+s)i(1+s)^i. We now need to find the maximum integer for ii such that the entire value is less than the upper limit of price, 21282^{128}This can be expressed as the formula ((1+s)i<2128)((1+s)^i<2^{128}).

Taking the smallest possible value of bin step(ss) which is 1 basis point, we can solve this as follows:

  • (1+0.0001)i<2128(1+0.0001)^i<2^{128}n

  • log2(1.0001)i<log2(2128)log_2 (1.0001)^i < log_2 (2^{128})

  • ilog2(1.0001)<128i⋅log2 (1.0001)<128

  • i<128log2(1.0001)887,273i<\dfrac{128}{log2 (1.0001)} ≈ 887,273,

  • i=887,273i=887,273

The above equation shows how many bins are needed to cover the entire range when ii is a positive integer, so we account for when it is negative by doubling it which equals to 2×887,272=1,774,5442\times887,272=1,774,544 bins.

Bin Indexing

the Nitro Finance system uses the uint24 data type. It's sufficient to cover the range of possible bins,

uint24 is 2241=16,777,2152^{24}-1 =16,777,215

  • Range: 0 to 16,777,215

  • Minimum Value: 0

  • Maximum Value: 16,777,215

Every bin is assigned a unique identifier (bin ID) based on a mathematical relationship between the bin's price and a fixed reference point within the system.

  • Reference Point=(16,777,2150)+12=16,777,2162=8,388,608\text{Reference Point} = \dfrac{(16,777,215 - 0) + 1}{2} = \dfrac{16,777,216}{2} = 8,388,608

bin Id=log(Price)log(1+bin Step10000)+8388608\text{bin Id} = \dfrac{log\text{(Price)}}{log(1 + \frac{\text{bin Step}}{10000})} + 8388608

  • bin Step(s) is in basis points, converted to a decimal.

  • Price is the specific price level for calculating the bin ID.

  • 8388608 is the midpoint of the range.

Bin pricing

The price of each bin is a function of the pair’s bin step and the bin’s index, The bin step parameter determines the constant percentage increase or decrease in price between each incremental bin.

  • 8388608=8388608= reference point

  • Pricebin=(1+bin step)(bin index8388608) Price_{\text{bin}} = (1 + \text{bin step})^{(\text{bin index} - 8388608)}

LIQUIDITY STRUCTURE MATHS

Bin liquidity

The total liquidity in a bin: P.(X+Zx)+(Y+Zy)=LTP.(X+Z_x)+(Y+Z_y)=L_T

Available liquidity in a bin: P.(X)+(Y)=LAP.(X)+(Y)=L_A

PP= price in terms of Y XX= base asset YY= quote asset ZyZy= collateralized quote asset ZxZx= collateralized base asset

Bin composition

The Liquidity Composition factor (Lc) determines the ratio of the base asset to the quote asset. Lc=XYLc=\dfrac{X}{Y}

The debt composition factor (Dc) determines the debt ratio.

Dc=Zy+(P.Zx)LTDc=\dfrac{Zy + (P.Z_x)}{L_T}

  • Dc = 0: There is no debt in the bin

  • Dc > 1: There is debt in the bin

BASIC INTERACTION MATHS

Adding liquidity

Adding liquidity to a bin will conserve the (Lc) of the bin. If a certain quantity of X (base asset) is determined, it is possible to compute the associated amount of Y (quote asset) to be added, and vice versa.

Liquidity added (L) = P.x+y{P.∆x} + {∆y}

Bin Shares Received (B):

When adding, you will receive bin shares representing the liquidity added. B=L×TBSLTB = \dfrac{L \times TBS}{L_T}

Removing liquidity

Removing liquidity results in the burning of Bin shares(B) which results in the receipt of assets X, and Y from the pool, and the minting of B (bin shares) associated with debt.

X,Y,B=B×(Rx,Ry,Dc×TBS)TBSX, Y, B = \dfrac{B \times (Rx, Ry, Dc \times TBS)}{TBS}

LL = liquidity added BB = bin shares TBSTBS = total bin shares LTL_T= total liquidity DcDc = debt composition RxRx= X reserve. hhh RyRy = Y reserve

Swaps

  • Selling Base Asset (X) for Quote Asset (Y): y=P×x∆y=P\times∆x

  • Buying Base Asset (X) with Quote Asset (Y): x=yP∆x=\dfrac{∆y}{P}

DEBT INTERACTION MATHS

Borrowing:

Below Active Bin:

  • Collateralize base asset (X), borrow quote asset (Y).

  • y=P×Zx∆y=P\times∆Z_x

Above Active Bin:

  • Collateralize quote asset (Y), borrow base asset (X).

  • x=ZyP∆x=\dfrac{∆Z_y}{P}

Repaying:

Below Active Bin:

  • Return borrowed Y, retrieve collateralized X.

  • Zx=yP∆Z_x=\dfrac{∆y}{P}

  • Active Bin ID ≤ Borrow Bin ID ⇒ Repayment ≠ Possible

Above Active Bin:

  • Return borrowed X, retrieve collateralized Y.

  • Zy=P×x∆Z_y=P\times∆x

  • Active Bin ID ≥ Borrow Bin ID ⇒ Repayment ≠ Possible

Debt rollover

Extends the lifespan of a debt to its default duration (Td)(T_{d})

  • TdT_d = Default debt lifespan (7 days in this case)

  • TremT_{rem}= Remaining time to expiry

  • TrollT_{roll}= New lifespan of the debt after rollover

Debt rollover is represented as:

  • Trollover=TdefaultT_{rollover} = T_{default}

regardless of TremT_{rem}=Time remaining, the lifespan is reset to TdT_d= default time once a rollover occurs.

Blacklist (Auto debt recovery)

When the remaining time of a debt reaches zero, the debt is blacklisted and becomes liquidatable.

TremT_{rem}= 0 ⇒ Blacklist⇒ Liquidatable

Blacklisted debts automatically absorb the collateral into the liquidity pool.

Buffer Range:

Buffer Range: ±bins±bins, indicating the range on either side of the active bin functioning as a safeguard to preserve liquidity for swaps and prevent liquidity depletion due to borrowing activities

±bins=BPs±bins=\dfrac{B_P}{s}

  • Buffer percent (Bp)(B_p): A predetermined percentage.

  • Bin Step (s): The rate of price change between each bin.

Implications:

  • Borrow and Debt Rollover:

Allowed={0,if within buffer range (±bins)1,otherwise\text{Allowed} = \begin{cases} 0, & \text{if within buffer range (\(\pm \text{bins}\))} \\ 1, & \text{otherwise} \end{cases}

  • Repayment and Liquidation:

Always Allowed=1\text{Always Allowed} = 1

FEE MATHS

Swap Fee (F):

The Swap Fee applies to all token exchanges within the liquidity pool.

F=Base factor×Bin step\text F=\text{Base factor} \times \text{Bin step}

  • Base Factor: A multiplier set by the protocol to adjust the fee rate as needed.

  • Bin Step: The percentage price difference between consecutive bins.

Borrow Fee (Bf)

The Borrow Fee is incurred when initiating a borrowing position.

Bf=F×BorrowfactorBf = \text F \times {Borrow factor}

  • Borrow Factor: A multiplier set by the protocol to control borrowing costs independently of swap fees.

  • Streaming of Borrow Fee: Bin fee reserveseconds in 7-days of debt=λ\dfrac{\text{Bin fee reserve}}{\text{seconds in 7-days of debt}}= λ

    • Where λλ is the per-second fee allocation from the bin's fee reserve.

Repay Fee (Rf):

The Repay Fee compensates LPs for the opportunity cost of inactive liquidity due to debt.

(Bin activation index- Debt activation index)×F(\text{Bin activation index- Debt activation index}) \times \text{F}

Bin Activation Index:

  • The number of times a bin was activated

Debt Activation Index:

  • Set when borrowing, matching the Bin Activation Index of the bin at that time.

Debt Rollover Fee:

The Debt Rollover Fee applies when a borrower extends their debt's lifespan.

DRF=F+RfDRF=\text F +\text Rf

  • F: The Swap Fee.

  • Rf​: The Repay Fee.

Last updated