File size: 5,673 Bytes
f998fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
  "language": "Solidity",
  "sources": {
    "src/contracts/VariableInterestRate.sol": {
      "content": "// SPDX-License-Identifier: ISC\npragma solidity ^0.8.16;\n\n// ====================================================================\n// |     ______                   _______                             |\n// |    / _____________ __  __   / ____(_____  ____ _____  ________   |\n// |   / /_  / ___/ __ `| |/_/  / /_  / / __ \\/ __ `/ __ \\/ ___/ _ \\  |\n// |  / __/ / /  / /_/ _>  <   / __/ / / / / / /_/ / / / / /__/  __/  |\n// | /_/   /_/   \\__,_/_/|_|  /_/   /_/_/ /_/\\__,_/_/ /_/\\___/\\___/   |\n// |                                                                  |\n// ====================================================================\n// ====================== VariableInterestRate ========================\n// ====================================================================\n// Frax Finance: https://github.com/FraxFinance\n\n// Primary Author\n// Drake Evans: https://github.com/DrakeEvans\n\n// Reviewers\n// Dennis: https://github.com/denett\n// Sam Kazemian: https://github.com/samkazemian\n// Travis Moore: https://github.com/FortisFortuna\n// Jack Corddry: https://github.com/corddry\n// Rich Gee: https://github.com/zer0blockchain\n\n// ====================================================================\n\nimport \"./interfaces/IRateCalculator.sol\";\n\n/// @title A formula for calculating interest rates as a function of utilization and time\n/// @author Drake Evans github.com/drakeevans\n/// @notice A Contract for calculating interest rates as a function of utilization and time\ncontract VariableInterestRate is IRateCalculator {\n    // Utilization Rate Settings\n    uint32 private constant MIN_UTIL = 75000; // 75%\n    uint32 private constant MAX_UTIL = 85000; // 85%\n    uint32 private constant UTIL_PREC = 1e5; // 5 decimals\n\n    // Interest Rate Settings (all rates are per second), 365.24 days per year\n    uint64 private constant MIN_INT = 79123523; // 0.25% annual rate\n    uint64 private constant MAX_INT = 146248476607; // 10,000% annual rate\n    uint256 private constant INT_HALF_LIFE = 43200e36; // given in seconds, equal to 12 hours, additional 1e36 to make math simpler\n\n    /// @notice The ```name``` function returns the name of the rate contract\n    /// @return memory name of contract\n    function name() external pure returns (string memory) {\n        return \"Variable Time-Weighted Interest Rate\";\n    }\n\n    /// @notice The ```getConstants``` function returns abi encoded constants\n    /// @return _calldata abi.encode(uint32 MIN_UTIL, uint32 MAX_UTIL, uint32 UTIL_PREC, uint64 MIN_INT, uint64 MAX_INT, uint256 INT_HALF_LIFE)\n    function getConstants() external pure returns (bytes memory _calldata) {\n        return abi.encode(MIN_UTIL, MAX_UTIL, UTIL_PREC, MIN_INT, MAX_INT, INT_HALF_LIFE);\n    }\n\n    /// @notice The ```requireValidInitData``` function No-op as this contract has no init data\n    function requireValidInitData(bytes calldata _initData) external pure {}\n\n    /// @notice The ```getNewRate``` function calculates the new interest rate as a function of time and utilization\n    /// @param _data abi.encode(uint64 _currentRatePerSec, uint256 _deltaTime, uint256 _utilization, uint256 _deltaBlocks)\n    /// @param _initData empty for this Rate Calculator\n    /// @return _newRatePerSec The new interest rate per second, 1e18 precision\n    function getNewRate(bytes calldata _data, bytes calldata _initData) external pure returns (uint64 _newRatePerSec) {\n        (uint64 _currentRatePerSec, uint256 _deltaTime, uint256 _utilization, ) = abi.decode(\n            _data,\n            (uint64, uint256, uint256, uint256)\n        );\n        if (_utilization < MIN_UTIL) {\n            uint256 _deltaUtilization = ((MIN_UTIL - _utilization) * 1e18) / MIN_UTIL;\n            uint256 _decayGrowth = INT_HALF_LIFE + (_deltaUtilization * _deltaUtilization * _deltaTime);\n            _newRatePerSec = uint64((_currentRatePerSec * INT_HALF_LIFE) / _decayGrowth);\n            if (_newRatePerSec < MIN_INT) {\n                _newRatePerSec = MIN_INT;\n            }\n        } else if (_utilization > MAX_UTIL) {\n            uint256 _deltaUtilization = ((_utilization - MAX_UTIL) * 1e18) / (UTIL_PREC - MAX_UTIL);\n            uint256 _decayGrowth = INT_HALF_LIFE + (_deltaUtilization * _deltaUtilization * _deltaTime);\n            _newRatePerSec = uint64((_currentRatePerSec * _decayGrowth) / INT_HALF_LIFE);\n            if (_newRatePerSec > MAX_INT) {\n                _newRatePerSec = MAX_INT;\n            }\n        } else {\n            _newRatePerSec = _currentRatePerSec;\n        }\n    }\n}\n"
    },
    "src/contracts/interfaces/IRateCalculator.sol": {
      "content": "// SPDX-License-Identifier: ISC\npragma solidity >=0.8.16;\n\ninterface IRateCalculator {\n    function name() external pure returns (string memory);\n\n    function requireValidInitData(bytes calldata _initData) external pure;\n\n    function getConstants() external pure returns (bytes memory _calldata);\n\n    function getNewRate(bytes calldata _data, bytes calldata _initData) external pure returns (uint64 _newRatePerSec);\n}\n"
    }
  },
  "settings": {
    "viaIR": true,
    "metadata": {
      "bytecodeHash": "none"
    },
    "optimizer": {
      "enabled": true,
      "runs": 725
    },
    "outputSelection": {
      "*": {
        "*": [
          "evm.bytecode",
          "evm.deployedBytecode",
          "devdoc",
          "userdoc",
          "metadata",
          "abi"
        ]
      }
    },
    "libraries": {}
  }
}