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:
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.solALTBC Equation Overview
The ALTBCEquations Library contains functions for the following equations:Vector Field Parameter: V
note: V is a constant value that will not change once the AMM has been initialized.bn
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
note: C is a constant value that will not change once the AMM has been initialized.cn+1
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
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
defines the price function for the TBC.New X coordinate: Xn+1
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
Last Revenue Claim: 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 and 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 then set If then setTrading Fee Ratio:
initial note: This is updated by the pool owner.Protocol Fee Ratio:
initial note: This is updated by the pool owner.Calculates extra collateral: Ln
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: and then note: This function determines the max revenue, the extra collateral, a pool owner can extract at timeMath 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.solPool
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.
Factory
The factory pattern ensures that a pool can be deployed properly: