LinFormula
This contract provides functions for calculating the purchase and sale return values on a linear curve using a scaling factor. The formula converts a given reserve token amount into a token return and the sale of a token amount into a reserve token return.
State Variables
OPTIMAL_TERM_MAX_VAL
Value that represents the point at which the optimal function approximations are still effective
MAX_SCALE
Max scaling factor in parts per million
PURE_LINEAR_SCALE
1:1 scaling factor in parts per million
WAD
Wei as decimal precision for calculations
Functions
_calculateLinPurchaseReturn
Returns the purchase value for a given amount (in the reserve token) as a conversion into the main token.
Parameters
supply
uint256
token total supply
reserveBalance
uint256
balance of the reserve token
initialCost
uint256
initial cost of the token
scalingFactor
uint32
scaling factor, represented in ppm, 1-1000000
depositAmount
uint256
deposit amount, in reserve token
Returns
purchaseReturn
uint256
return amount
_calculateLinSaleReturn
Returns the sale value for a given amount (in the main token) as a conversion into the reserve token.
Parameters
supply
uint256
token total supply
reserveBalance
uint256
balance of the reserve token
initialCost
uint256
initial cost of the token
scalingFactor
uint32
scaling factor, represented in ppm, 1-1000000
sellAmount
uint256
amount of tokens to sell
Returns
saleReturn
uint256
return of the conversion
_totalCostOfTokens
Returns the total cost of tokens up to the current token.
Parameters
currentToken
uint256
current token number
scalingFactor
uint32
scaling factor, represented in ppm, 1-1000000
initialCost
uint256
initial cost of the token
Returns
totalCost
uint256
total cost of tokens up to the current token
_currentTokenCost
Returns the cost of the current token.
Parameters
currentToken
uint256
current token number
scalingFactor
uint32
scaling factor, represented in ppm, 1-1000000
initialCost
uint256
initial cost of the token
Returns
currentTokenCost
uint256
current token cost
_calculateInitialCostAdjustment
Returns the initial cost adjustment based on the scaling factor.
Calculates the difference between the initial cost and the incremental value applied to each token after the first.
Parameters
initialCost
uint256
initial cost of the token
scalingFactor
uint32
scaling factor, represented in ppm, 1-1000000
Returns
initialCostAdjustment
uint256
initial cost adjustment
_calculateTokenCount
Returns the amount of whole tokens the deposit can still cover.
Parameters
depositAmount
uint256
deposit amount, in reserve token
reserveBalance
uint256
balance of the reserve token
initialCost
uint256
initial cost of the token
scalingFactor
uint32
currentToken
uint256
current token number
Returns
tokenCount
uint256
amount of whole tokens the deposit can still cover
_generalSqrt
Returns the square root of a given value as proposed in EIP-7054.
Parameters
x
uint256
value to calculate the square root of
Returns
z
uint128
square root of the value
_optimalSqrt
Returns the square root of a given value using the Babylonian method.
Parameters
y
uint256
value to calculate the square root of
Returns
z
uint256
square root of the value
_getLength
Returns the length of a given number.
Parameters
x
uint256
number to calculate the length of
Returns
length
uint256
length of the number