LiquidityCalcs

Git Source

implements calculation methods used for Fluid liquidity such as updated exchange prices, borrow rate, withdrawal / borrow limits, revenue amount.

State Variables

EXCHANGE_PRICES_PRECISION

constants as from Liquidity variables.sol

uint256 internal constant EXCHANGE_PRICES_PRECISION = 1e12;

SECONDS_PER_YEAR

Ignoring leap years

uint256 internal constant SECONDS_PER_YEAR = 365 days;

DEFAULT_EXPONENT_SIZE

uint256 internal constant DEFAULT_EXPONENT_SIZE = 8;

DEFAULT_EXPONENT_MASK

uint256 internal constant DEFAULT_EXPONENT_MASK = 0xFF;

FOUR_DECIMALS

uint256 internal constant FOUR_DECIMALS = 1e4;

TWELVE_DECIMALS

uint256 internal constant TWELVE_DECIMALS = 1e12;

X14

uint256 internal constant X14 = 0x3fff;

X15

uint256 internal constant X15 = 0x7fff;

X16

uint256 internal constant X16 = 0xffff;

X18

uint256 internal constant X18 = 0x3ffff;

X24

uint256 internal constant X24 = 0xffffff;

X33

uint256 internal constant X33 = 0x1ffffffff;

X64

uint256 internal constant X64 = 0xffffffffffffffff;

Functions

calcExchangePrices

calculates interest (exchange prices) for a token given its' exchangePricesAndConfig from storage.

function calcExchangePrices(uint256 exchangePricesAndConfig_)
    internal
    view
    returns (uint256 supplyExchangePrice_, uint256 borrowExchangePrice_);

Parameters

NameTypeDescription
exchangePricesAndConfig_uint256exchange prices and config packed uint256 read from storage

Returns

NameTypeDescription
supplyExchangePrice_uint256updated supplyExchangePrice
borrowExchangePrice_uint256updated borrowExchangePrice

calcRevenue

gets the revenueAmount_ for a token given its' totalAmounts and exchangePricesAndConfig from storage and the current balance of the Fluid liquidity contract for the token.

function calcRevenue(uint256 totalAmounts_, uint256 exchangePricesAndConfig_, uint256 liquidityTokenBalance_)
    internal
    view
    returns (uint256 revenueAmount_);

Parameters

NameTypeDescription
totalAmounts_uint256total amounts packed uint256 read from storage
exchangePricesAndConfig_uint256exchange prices and config packed uint256 read from storage
liquidityTokenBalance_uint256current balance of Liquidity contract (IERC20(token_).balanceOf(address(this)))

Returns

NameTypeDescription
revenueAmount_uint256collectable revenue amount

calcWithdrawalLimitBeforeOperate

calculates withdrawal limit before an operate execution: amount of user supply that must stay supplied (not amount that can be withdrawn). i.e. if user has supplied 100m and can withdraw 5M, this method returns the 95M, not the withdrawable amount 5M

function calcWithdrawalLimitBeforeOperate(uint256 userSupplyData_, uint256 userSupply_)
    internal
    view
    returns (uint256 currentWithdrawalLimit_);

Parameters

NameTypeDescription
userSupplyData_uint256user supply data packed uint256 from storage
userSupply_uint256current user supply amount already extracted from userSupplyData_ and converted from BigMath

Returns

NameTypeDescription
currentWithdrawalLimit_uint256current withdrawal limit updated for expansion since last interaction. returned value is in raw for with interest mode, normal amount for interest free mode!

calcWithdrawalLimitAfterOperate

calculates withdrawal limit after an operate execution: amount of user supply that must stay supplied (not amount that can be withdrawn). i.e. if user has supplied 100m and can withdraw 5M, this method returns the 95M, not the withdrawable amount 5M

function calcWithdrawalLimitAfterOperate(uint256 userSupplyData_, uint256 userSupply_, uint256 newWithdrawalLimit_)
    internal
    pure
    returns (uint256);

Parameters

NameTypeDescription
userSupplyData_uint256user supply data packed uint256 from storage
userSupply_uint256current user supply amount already extracted from userSupplyData_ and added / subtracted with the executed operate amount
newWithdrawalLimit_uint256current withdrawal limit updated for expansion since last interaction, result from calcWithdrawalLimitBeforeOperate

Returns

NameTypeDescription
<none>uint256withdrawalLimit_ updated withdrawal limit that should be written to storage. returned value is in raw for with interest mode, normal amount for interest free mode!

calcBorrowLimitBeforeOperate

calculates borrow limit before an operate execution: total amount user borrow can reach (not borrowable amount in current operation). i.e. if user has borrowed 50M and can still borrow 5M, this method returns the total 55M, not the borrowable amount 5M

function calcBorrowLimitBeforeOperate(uint256 userBorrowData_, uint256 userBorrow_)
    internal
    view
    returns (uint256 currentBorrowLimit_);

Parameters

NameTypeDescription
userBorrowData_uint256user borrow data packed uint256 from storage
userBorrow_uint256current user borrow amount already extracted from userBorrowData_

Returns

NameTypeDescription
currentBorrowLimit_uint256current borrow limit updated for expansion since last interaction. returned value is in raw for with interest mode, normal amount for interest free mode!

calcBorrowLimitAfterOperate

calculates borrow limit after an operate execution: total amount user borrow can reach (not borrowable amount in current operation). i.e. if user has borrowed 50M and can still borrow 5M, this method returns the total 55M, not the borrowable amount 5M

function calcBorrowLimitAfterOperate(uint256 userBorrowData_, uint256 userBorrow_, uint256 newBorrowLimit_)
    internal
    pure
    returns (uint256 borrowLimit_);

Parameters

NameTypeDescription
userBorrowData_uint256user borrow data packed uint256 from storage
userBorrow_uint256current user borrow amount already extracted from userBorrowData_ and added / subtracted with the executed operate amount
newBorrowLimit_uint256current borrow limit updated for expansion since last interaction, result from calcBorrowLimitBeforeOperate

Returns

NameTypeDescription
borrowLimit_uint256updated borrow limit that should be written to storage. returned value is in raw for with interest mode, normal amount for interest free mode!

calcBorrowRateFromUtilization

Calculates new borrow rate from utilization for a token

function calcBorrowRateFromUtilization(uint256 rateData_, uint256 utilization_) internal returns (uint256 rate_);

Parameters

NameTypeDescription
rateData_uint256rate data packed uint256 from storage for the token
utilization_uint256totalBorrow / totalSupply. 1e4 = 100% utilization

Returns

NameTypeDescription
rate_uint256rate for that particular token in 1e2 precision (e.g. 5% rate = 500)

calcRateV1

calculates the borrow rate based on utilization for rate data version 1 (with one kink) in 1e2 precision

function calcRateV1(uint256 rateData_, uint256 utilization_) internal pure returns (uint256 rate_);

Parameters

NameTypeDescription
rateData_uint256rate data packed uint256 from storage for the token
utilization_uint256in 1e2 (100% = 1e4)

Returns

NameTypeDescription
rate_uint256rate in 1e2 precision

calcRateV2

For rate v1 (one kink) ------------------------------------------------------ Next 16 bits => 4 - 19 => Rate at utilization 0% (in 1e2: 100% = 10_000; 1% = 100 -> max value 65535) Next 16 bits => 20- 35 => Utilization at kink1 (in 1e2: 100% = 10_000; 1% = 100 -> max value 65535) Next 16 bits => 36- 51 => Rate at utilization kink1 (in 1e2: 100% = 10_000; 1% = 100 -> max value 65535) Next 16 bits => 52- 67 => Rate at utilization 100% (in 1e2: 100% = 10_000; 1% = 100 -> max value 65535) Last 188 bits => 68-255 => blank, might come in use in future

calculates the borrow rate based on utilization for rate data version 2 (with two kinks) in 1e4 precision

function calcRateV2(uint256 rateData_, uint256 utilization_) internal pure returns (uint256 rate_);

Parameters

NameTypeDescription
rateData_uint256rate data packed uint256 from storage for the token
utilization_uint256in 1e2 (100% = 1e4)

Returns

NameTypeDescription
rate_uint256rate in 1e4 precision

getTotalSupply

For rate v2 (two kinks) ----------------------------------------------------- Next 16 bits => 4 - 19 => Rate at utilization 0% (in 1e2: 100% = 10_000; 1% = 100 -> max value 65535) Next 16 bits => 20- 35 => Utilization at kink1 (in 1e2: 100% = 10_000; 1% = 100 -> max value 65535) Next 16 bits => 36- 51 => Rate at utilization kink1 (in 1e2: 100% = 10_000; 1% = 100 -> max value 65535) Next 16 bits => 52- 67 => Utilization at kink2 (in 1e2: 100% = 10_000; 1% = 100 -> max value 65535) Next 16 bits => 68- 83 => Rate at utilization kink2 (in 1e2: 100% = 10_000; 1% = 100 -> max value 65535) Next 16 bits => 84- 99 => Rate at utilization 100% (in 1e2: 100% = 10_000; 1% = 100 -> max value 65535) Last 156 bits => 100-255 => blank, might come in use in future

reads the total supply out of Liquidity packed storage totalAmounts_ for supplyExchangePrice_

function getTotalSupply(uint256 totalAmounts_, uint256 supplyExchangePrice_)
    internal
    pure
    returns (uint256 totalSupply_);

getTotalBorrow

reads the total borrow out of Liquidity packed storage totalAmounts_ for borrowExchangePrice_

function getTotalBorrow(uint256 totalAmounts_, uint256 borrowExchangePrice_)
    internal
    pure
    returns (uint256 totalBorrow_);

Events

BorrowRateMaxCap

emitted if the calculated borrow rate surpassed max borrow rate (16 bits) and was capped at maximum value 65535

event BorrowRateMaxCap();

Errors

FluidLiquidityCalcsError

error FluidLiquidityCalcsError(uint256 errorId_);