dydx

PolynomialInterestSetter

0xad91a0ddf799176a0a87a32dafe8f3dd28479918
PolynomialInterestSetter
function

getInterestRate

view

Inputs

ParameterTypeDescription
address
borrowWeiuint256
supplyWeiuint256

Outputs

TypeDescription
tuple
     function getInterestRate(
         address /* token */,
         uint256 borrowWei,
         uint256 supplyWei
     )
         external
         view
         returns (Interest.Rate memory)
     {
         if (borrowWei == 0) {
             return Interest.Rate({
                 value: 0
             });
         }
 
         PolyStorage memory s = g_storage;
         uint256 maxAPR = s.maxAPR;
 
         if (borrowWei >= supplyWei) {
             return Interest.Rate({
                 value: maxAPR / SECONDS_IN_A_YEAR
             });
         }
 
         uint256 result = 0;
         uint256 polynomial = BASE;
 
         // for each non-zero coefficient...
         uint256 coefficients = s.coefficients;
         while (true) {
             // gets the lowest-order byte
             uint256 coefficient = coefficients % 256;
 
             // if non-zero, add to result
             if (coefficient != 0) {
                 // no safeAdd since there are at most 16 coefficients
                 // no safeMul since (coefficient < 256 && polynomial <= 10**18)
                 result += coefficient * polynomial;
 
                 // break if this is the last non-zero coefficient
                 if (coefficient == coefficients) {
                     break;
                 }
             }
 
             // increase the order of the polynomial term
             // no safeDiv since supplyWei must be stricly larger than borrowWei
             polynomial = polynomial.mul(borrowWei) / supplyWei;
 
             // move to next coefficient
             coefficients >>= BYTE;
         }
 
         // normalize the result
         // no safeMul since result fits within 72 bits and maxAPR fits within 128 bits
         // no safeDiv since the divisor is a non-zero constant
         return Interest.Rate({
             value: result * maxAPR / (SECONDS_IN_A_YEAR * BASE * PERCENT)
         });
     }
 
function

getCoefficients

view

Inputs

(void)

Outputs

TypeDescription
uint256[]
     function getCoefficients()
         external
         view
         returns (uint256[] memory)
     {
         // allocate new array with maximum of 16 coefficients
         uint256[] memory result = new uint256[](16);
 
         // add the coefficients to the array
         uint256 numCoefficients = 0;
         for (
             uint256 coefficients = g_storage.coefficients;
             coefficients != 0;
             coefficients >>= BYTE
         ) {
             result[numCoefficients] = coefficients % 256;
             numCoefficients++;
         }
 
         // modify result.length to match numCoefficients
         /* solium-disable-next-line security/no-inline-assembly */
         assembly {
             mstore(result, numCoefficients)
         }
 
         return result;
     }
 
function

getMaxAPR

view

Inputs

(void)

Outputs

TypeDescription
uint256
     function getMaxAPR()
         external
         view
         returns (uint256)
     {
         return g_storage.maxAPR;
     }
 
constructor

PolynomialInterestSetter

nonpayable

Inputs

ParameterTypeDescription
paramstuple

Outputs

(void)
     constructor(
         PolyStorage memory params
     )
         public
     {
         // verify that all coefficients add up to 100%
         uint256 sumOfCoefficients = 0;
         for (
             uint256 coefficients = params.coefficients;
             coefficients != 0;
             coefficients >>= BYTE
         ) {
             sumOfCoefficients += coefficients % 256;
         }
         require(
             sumOfCoefficients == PERCENT,
             "Coefficients must sum to 100"
         );
 
         // store the params
         g_storage = params;
     }