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:
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
bn=xn+CVnote: 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.
bn<=bn+1⟹cn+1=cn+2bn−bn+1∗Xnbn+1>bn⟹cn+1=cn−2bn+1−bn∗Xnnote: 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).
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.
note: Q is updated along with the variables of A~≥0 and B~≥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.IfB~⋅(xmax(n)−xn)≤A~⋅(Dn−Ln)then setBAq=B~={Dn−Lnxmax(n)−xn⋅B~,A~, if Dn−Ln=0 if Dn−Ln=0={Dn−LnB,xmax(n)−xnA, if Dn−Ln=0 if Dn−Ln=0.IfB~⋅(xmax(n)−xn)>A~⋅(Dn−Ln)then setABq=A~=xmax(n)−xnDn−Ln⋅A~=xmax(n)−xnA.
L(xn,bn,cn,xmin(n),Cn,V)=21xmin(n)(bnxn+2cn+V⋅ln(xn+Cnxmin(n)+Cn))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=q⋅−1and thenZn+1=Zn+(((Wn−WIn)WIn∗q)∗(Ln+Zn)+(q∗Zn))note: This function determines the max revenue, the extra collateral, a pool owner can extract at time n
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.
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
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.
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
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
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