Skip to main content

Libraries

The pools make heavy use of internal libraries to carry out mathematical operations. Internal libraries avoid external calls. This choice makes the deployment more expensive, but makes individual user transactions cheaper. The math equations are encapsulated in their own libraries where they make use of another library which is an aggregation of math libraries:
library dependency diagram

Equation Library

There are two equation libraries used, one for each curve type. Each equation library is utilized in the corresponding pool type. These libraries exist to encapsulate the different equations necessary to carry out a swap and preserve the state of the curve. They use the math libraries aggregated in the MathLibs library. see ALTBCEquations.sol

ALTBC Equation Overview

The ALTBCEquations Library contains functions for the following equations:

Vector Field Parameter: V

V>0V > 0 note: V is a constant value that will not change once the AMM has been initialized.

bn

bn=Vxn+Cb_n = \frac{V}{x_{n} + C} note: bn is a variable that will change over time and tracks the value of the parameter b before the n-th transaction. When combined with the cn parameter, it is used to calculate the price of the x token because bn and cn are used to define the TBC. bn is the slope of the TBC function f (which is a line), and it is related to the price impact of trades: if the slope is higher, a certain trade will move the price more.

Concentration Parameter: C

C>0C > 0 note: C is a constant value that will not change once the AMM has been initialized.

cn+1

bn<=bn+1    cn+1=cn+bnbn+12Xnb_n <= b_{n+1} \implies c_{n+1} = c_n + \frac{b_n - b_{n+1}}{2}*X_n bn+1>bn    cn+1=cnbn+1bn2Xnb_{n+1} > b_n \implies c_{n+1} = c_n - \frac{b_{n+1} - b_n}{2}*X_n note: cn is a variable that will change over time and tracks the value of the parameter c before the n-th transaction. It is initialized to the lower price of the pool. When combined with the bn parameter, it is used to calculate the price of the x token because bn and cn are used to define the TBC. It is the y-intercept of the TBC function f (which is a line).

Area under the TBC: Dn

Dn=12bnxn+12+cnxn+1D_n = \frac{1}{2} b_n x_{n+1}^2 + c_n x_{n+1} note: Dn is a variable that will change over time and tracks the value of the area under the TBC before the n-th transaction.

Price Function: fn

fn(x)=bnx+cnf_n(x) = b_nx + c_n fnf_n defines the price function for the TBC.

New X coordinate: Xn+1

Xn+1=2Dncn+cn2+2bnDnX_{n+1} = \frac{2D_n}{c_n + \sqrt{c_n^2 + 2b_n D_n}}

NFT Liquidity Equations and Variables

Upon construction of the pool, two NFTs are minted. These NFTs are used to seed initial liquidity into the pool. One NFT is used to track the amount of liquidity that is active in the pool, and the other NFT is used to track the amount of liquidity that is inactive in the pool.

Wactive

note: Wactive is the initial amount of liquidity in the pool. Stored in initial minted NFT as wj.

Winactive

note: Winactive is the amount of liquidity that is inactive in the pool. Stored in initial minted NFT as wj.

Total Revenue per unit of liquidity: hn

hn=Ln+ZnWnwinactive+ϕh_n = \frac{L_n + Z_n}{W_n - w_{\textnormal{inactive}}} + \phi

Last Revenue Claim: rj

rj=wj(hnrj)r_j = wj (hn - rj) note: This is a field stored in the LPToken contract. It is used to track the last revenue claim for a given LPToken.

Sum of the amount fields of all the NFTs minted by the pool: Wn

Ratio of xToken to yToken in liquidity: q

note: Q is updated along with the variables of A~0\tilde A \geq 0 and B~0\tilde B \geq 0 which represent the proposed amount of xToken and yToken to be added to the pool. A and B are the actual amount of xToken and yToken added to the pool. If B~(xmax(n)xn)A~(DnLn)\tilde B \cdot \left (x_{\textnormal{max}}^{(n)}- x_n\right ) \leq \tilde A \cdot \left (D_n - L_n \right ) then set B=B~A={xmax(n)xnDnLnB~, if DnLn0A~, if DnLn=0q={BDnLn, if DnLn0Axmax(n)xn, if DnLn=0 .\begin{aligned} B &= \tilde B \\ A &= \begin{cases} \frac{x_{\textnormal{max}}^{(n)}- x_n}{D_n - L_n} \cdot \tilde B, &\text{ if } D_n - L_n \neq 0 \\ \tilde A, &\text{ if } D_n - L_n = 0 \\ \end{cases} \\ q &= \begin{cases} \frac{B}{D_n - L_n}, &\text{ if } D_n - L_n \neq 0 \\ \frac{A}{x_{\textnormal{max}}^{(n)}- x_n}, &\text{ if } D_n - L_n = 0 \ . \\ \end{cases} \\ \end{aligned} If B~(xmax(n)xn)>A~(DnLn)\tilde B \cdot \left (x_{\textnormal{max}}^{(n)}- x_n\right ) > \tilde A \cdot \left (D_n - L_n \right ) then set A=A~B=DnLnxmax(n)xnA~q=Axmax(n)xn .\begin{aligned} A &= \tilde A \\ B &= \frac{D_n - L_n}{x_{\textnormal{max}}^{(n)}- x_n} \cdot \tilde A \\ q &= \frac{A}{x_{\textnormal{max}}^{(n)}- x_n} \ . \\ \end{aligned}

Trading Fee Ratio: ϕ\phi

initial ϕ=0\phi = 0 note: This is updated by the pool owner.

Protocol Fee Ratio: ψ\psi

initial ψ=0\psi = 0 note: This is updated by the pool owner.

Calculates extra collateral: Ln

L(xn,bn,cn,xmin(n),Cn,V)=12xmin(n)(bnxn+2cn+Vln(xmin(n)+Cnxn+Cn)){\bf L}(x_n,b_n,c_n,x_{\textnormal{min}}^{(n)},C_n,V) = \frac{1}{2} x_{\textnormal{min}}^{(n)} \left( b_n x_n + 2 c_n + V \cdot \ln\left(\frac{x_{\textnormal{min}}^{(n)} + C_n}{x_n + C_n}\right) \right) note: The equation L computes the minimum value that the parameter D can have at a certain moment, regardless of the trade sequence that is performed afterwards (no liquidity deposits or withdrawals considered). It is used to compute the amount of “extra collateral” that the pool has, which then can be given as revenue to the liquidity providers.

Balancing quantity added to L for fair LP fee accounting: Zn+1

if withdrawal: q=q1q = q \cdot -1 and then Zn+1=Zn+((WIn(WnWIn)q)(Ln+Zn)+(qZn))Z_{n+1} = Z_n + ((\frac{WIn}{(Wn - WIn)} * q) * (Ln + Zn) + (q * Zn)) note: This function determines the max revenue, the extra collateral, a pool owner can extract at time nn

MathLibs Library

This library serves as an interface between the math libraries and the equation library in order to break dependency from the latter one to the API of the former ones. see MathLibs.sol.

Math Libraries

These are libraries that carry out the actual math operation in the most optimal and precise way. The external libraries consist of:

Float128

see Float128.sol Our best effort to achieve high precision floating point numbers.

Solidity_Uint512

The Uint512 library provides the ability to perform different math functions with 512 bit integers (instead of the normal max of 256 bit). This allows for additional precision where it is needed in the equations. see Uint512.sol

Pool

The pool architecture is a monolithic contract that makes use of the equation library. This monolithic and minimalistic style of architecture intends to save gas and increase maintainability.
pool diagram
The diagram shows that this is a single contract that has its libraries as internal libraries, and that the pool itself only deals directly with one library: the equation library of the curve type chosen. see ALTBCPool.sol

Factory

The factory pattern ensures that a pool can be deployed properly:
factory diagram

Factory Base Contract

The factory contracts inherit from the FactoryBase contract. This base contract holds the logic for adjusting protocol fees and facilitates communication with allow list contracts for approved yTokens and pool deployers. see FactoryBase.sol

AllowList Contracts

The AllowList contracts are used for allowing appoved yTokens to be assigned as a pair to an xToken, as well as assigning allowed pool deployers. These allow lists are separated into their own contracts, one AllowList contract for approved yTokens and one AllowList contract for approved pool deployers. This was done in order to distinguish which address is on which allow list easily. see ALTBCFactory.sol

Bounds

Please visit next page for Bounds.