Skip to main content
Git Source Author: @oscarsernarosero @mpetersoCode55 @cirsteve @palmerg4

State Variables

FLOAT_2

packedFloat constant FLOAT_2 = packedFloat.wrap(0x7f6c00000000000000000000000000000f0bdc21abb48db201e86d4000000000);

FLOAT_NEG_1

packedFloat constant FLOAT_NEG_1 = packedFloat.wrap(0x7f6d00000000000000000000000000000785ee10d5da46d900f436a000000000);

FLOAT_0

packedFloat constant FLOAT_0 = packedFloat.wrap(0);

FLOAT_1

packedFloat constant FLOAT_1 = packedFloat.wrap(0x7f6c00000000000000000000000000000785ee10d5da46d900f436a000000000);

FLOAT_WAD

packedFloat constant FLOAT_WAD =
    packedFloat.wrap(57507338264406853159277167054180511853162945875507645848942038639672188469248);

Functions

calculateBn

The result will be a packedFloat Bn is equal to V / (Xn + C) in the spec This function calculates B(n) and stores it in the tbc definition as b.
function calculateBn(ALTBCDef storage altbc, packedFloat Xn) internal;
Parameters
NameTypeDescription
altbcALTBCDefthe tbc definition
XnpackedFloatthe X value at n

calculatefx

The result for f(x) will be a packedFloat. This equation is used to calculate the spot price of the x token and is equal to (bn * x) + cn This function calculates f(x) at n.
function calculatefx(ALTBCDef storage altbc, packedFloat x) internal view returns (packedFloat result);
Parameters
NameTypeDescription
altbcALTBCDefthe tbc definition
xpackedFloatvalue for x at n
Returns
NameTypeDescription
resultpackedFloatthe calculated f(x), this value will be a packedFloat

calculateDn

The result for Dn will be a packedFloat This equation is used to calculate the area under the curve at n and is equal to (1/2)(bnx^2) + cnx This function calculates D at n.
function calculateDn(ALTBCDef storage altbc, packedFloat x) internal view returns (packedFloat result);
Parameters
NameTypeDescription
altbcALTBCDefthe tbc definition
xpackedFloatvalue for x at n
Returns
NameTypeDescription
resultpackedFloatresult

calculateH

This method is implemented using packedFloats and the float128 library This equation in the spec is equal to (Ln + Zn) / (Wn - wInactive) + phi This function calculates h at n which is the total revenue per unit of liquidity at time n.
function calculateH(ALTBCDef storage altbc, packedFloat L, packedFloat W, packedFloat wInactive, packedFloat phi)
    internal
    view
    returns (packedFloat result);
Parameters
NameTypeDescription
altbcALTBCDef
LpackedFloatthe x coordinate
WpackedFloatthe total amount of units of liquidity in circulation
wInactivepackedFloat
phipackedFloatthe total amount of units of liquidity in circulation
Returns
NameTypeDescription
resultpackedFloatthe calculated h

calculateXofNPlus1

This method is implemented using packedFloats and the float128 library This equation in the spec is equal to 2Dn / (c + sqrt(c^2 + 2bDn)) This function calculates the value of Xn+1.
function calculateXofNPlus1(ALTBCDef storage altbc, packedFloat Dn) internal view returns (packedFloat newX);
Parameters
NameTypeDescription
altbcALTBCDefthe tbc definition
DnpackedFloatthe area under the curve.
Returns
NameTypeDescription
newXpackedFloatthe calculated Xn+1

calculateC

This function calculates the parameter c and stores it in the tbc definition.
function calculateC(ALTBCDef storage altbc, packedFloat Xn, packedFloat oldBn) internal;
Parameters
NameTypeDescription
altbcALTBCDefthe tbc definition.
XnpackedFloatthe x coordinate
oldBnpackedFloatthe previous state of b

calculateLastRevenueClaim

The result for last revenue claim will be a Float. This function calculates the last revenue claim to be stored in the associated LPToken variable rj. The result will be a WAD value.
function calculateLastRevenueClaim(packedFloat hn, packedFloat wj, packedFloat r_hat, packedFloat w_hat)
    internal
    pure
    returns (packedFloat);
Parameters
NameTypeDescription
hnpackedFloatThe revenue parameter. Expected to be a Float.
wjpackedFloatThe share of the pool’s liquidity the associated LPToken represents. Expected to be a Float.
r_hatpackedFloatThe current last revenue claim value of the associated LPToken. Expected to be a Float.
w_hatpackedFloatThe current liquidity amount of the associated LPToken. Expected to be a Float.

calculateL

This function calculates the parameter L.
function calculateL(ALTBCDef storage altbc, packedFloat Xn) internal view returns (packedFloat result);
Parameters
NameTypeDescription
altbcALTBCDefthe tbc definition.
XnpackedFloatthe x coordinate
Returns
NameTypeDescription
resultpackedFloatthe calculate L parameter.

calculateZ

This function calculates the parameter Z, which is a balancing quantity used to ensure fair LP accounting.
function calculateZ(
    ALTBCDef storage altbc,
    packedFloat Ln,
    packedFloat Wn,
    packedFloat WIn,
    packedFloat q,
    bool withdrawal
) internal;
Parameters
NameTypeDescription
altbcALTBCDefthe tbc definition.
LnpackedFloatthe liquidity parameter.
WnpackedFloatthe total amount of units of liquidity in circulation.
WInpackedFloatthe total amount of units of liquidity in circulation.
qpackedFloatthe liquidity units to receive in exchange for A and B
withdrawalboolthe boolean value for withdrawal

calculateQ

This function calculates q.
function calculateQ(
    ALTBCDef storage altbc,
    packedFloat Xn,
    packedFloat _A,
    packedFloat _B,
    packedFloat L,
    packedFloat Dn
) internal view returns (packedFloat A, packedFloat B, packedFloat q);
Parameters
NameTypeDescription
altbcALTBCDefthe tbc definition.
XnpackedFloatthe x coordinate
_ApackedFloatThe amount of incoming X Token.
_BpackedFloatThe amount of incoming collateral.
LpackedFloatthe liquidity parameter.
DnpackedFloatThe current area under the curve.
Returns
NameTypeDescription
ApackedFloatthe actual amount to take for token x
BpackedFloatthe actual amount to take for token y
qpackedFloatthe liquidity units to receive in exchange for A and B

calculateRevenueAvailable

This function calculates the revenue available for a given LPToken.
function calculateRevenueAvailable(packedFloat wj, packedFloat hn, packedFloat rj)
    internal
    pure
    returns (packedFloat result);
Parameters
NameTypeDescription
wjpackedFloatThe share of the pool’s liquidity the associated LPToken represents.
hnpackedFloatThe revenue parameter.
rjpackedFloatThe last revenue claim for the associated LPToken.
Returns
NameTypeDescription
resultpackedFloatThe calculated revenue available for the LPToken.

_liquidityUpdateHelper

This function updates related tbc variables when a liquidity deposit or withdrawal is made
function _liquidityUpdateHelper(ALTBCDef storage altbc, packedFloat Xn, packedFloat multiplier)
    internal
    returns (packedFloat x);
Parameters
NameTypeDescription
altbcALTBCDefthe tbc definition.
XnpackedFloatthe x coordinate.
multiplierpackedFloatThe value for multiplier for pool state
Returns
NameTypeDescription
xpackedFloatThe updated x value.