{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/yieldspace/YieldMath.sol",
    "Parent Contracts": [],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "library YieldMath {\r\n  using Math64x64 for int128;\r\n  using Math64x64 for uint128;\r\n  using Math64x64 for int256;\r\n  using Math64x64 for uint256;\r\n  using Exp64x64 for uint128;\r\n\r\n  uint128 public constant ONE = 0x10000000000000000; // In 64.64\r\n  uint256 public constant MAX = type(uint128).max;   // Used for overflow checks\r\n  uint256 public constant VAR = 1e12;                // The logarithm math used is not precise to the wei, but can deviate up to 1e12 from the real value.\r\n\r\n  /**\r\n   * Calculate the amount of fyToken a user would get for given amount of Base.\r\n   * https://www.desmos.com/calculator/5nf2xuy6yb\r\n   * @param baseReserves base reserves amount\r\n   * @param fyTokenReserves fyToken reserves amount\r\n   * @param baseAmount base amount to be traded\r\n   * @param timeTillMaturity time till maturity in seconds\r\n   * @param k time till maturity coefficient, multiplied by 2^64\r\n   * @param g fee coefficient, multiplied by 2^64\r\n   * @return the amount of fyToken a user would get for given amount of Base\r\n   */\r\n  function fyTokenOutForBaseIn(\r\n    uint128 baseReserves, uint128 fyTokenReserves, uint128 baseAmount,\r\n    uint128 timeTillMaturity, int128 k, int128 g)\r\n  public pure returns(uint128) {\r\n    unchecked {\r\n      uint128 a = _computeA(timeTillMaturity, k, g);\r\n\r\n      // za = baseReserves ** a\r\n      uint256 za = baseReserves.pow(a, ONE);\r\n\r\n      // ya = fyTokenReserves ** a\r\n      uint256 ya = fyTokenReserves.pow(a, ONE);\r\n\r\n      // zx = baseReserves + baseAmount\r\n      uint256 zx = uint256(baseReserves) + uint256(baseAmount);\r\n      require(zx <= MAX, \"YieldMath: Too much base in\");\r\n\r\n      // zxa = zx ** a\r\n      uint256 zxa = uint128(zx).pow(a, ONE);\r\n\r\n      // sum = za + ya - zxa\r\n      uint256 sum = za + ya - zxa; // z < MAX, y < MAX, a < 1. It can only underflow, not overflow.\r\n      require(sum <= MAX, \"YieldMath: Insufficient fyToken reserves\");\r\n\r\n      // result = fyTokenReserves - (sum ** (1/a))\r\n      uint256 result = uint256(fyTokenReserves) - uint256(uint128(sum).pow(ONE, a));\r\n      require(result <= MAX, \"YieldMath: Rounding induced error\");\r\n\r\n      result = result > VAR ? result - VAR : 0; // Subtract error guard, flooring the result at zero\r\n\r\n      return uint128(result);\r\n    }\r\n  }\r\n\r\n  /**\r\n   * Calculate the amount of base a user would get for certain amount of fyToken.\r\n   * https://www.desmos.com/calculator/6jlrre7ybt\r\n   * @param baseReserves base reserves amount\r\n   * @param fyTokenReserves fyToken reserves amount\r\n   * @param fyTokenAmount fyToken amount to be traded\r\n   * @param timeTillMaturity time till maturity in seconds\r\n   * @param k time till maturity coefficient, multiplied by 2^64\r\n   * @param g fee coefficient, multiplied by 2^64\r\n   * @return the amount of Base a user would get for given amount of fyToken\r\n   */\r\n  function baseOutForFYTokenIn(\r\n    uint128 baseReserves, uint128 fyTokenReserves, uint128 fyTokenAmount,\r\n    uint128 timeTillMaturity, int128 k, int128 g)\r\n  public pure returns(uint128) {\r\n    unchecked {\r\n      uint128 a = _computeA(timeTillMaturity, k, g);\r\n\r\n      // za = baseReserves ** a\r\n      uint256 za = baseReserves.pow(a, ONE);\r\n\r\n      // ya = fyTokenReserves ** a\r\n      uint256 ya = fyTokenReserves.pow(a, ONE);\r\n\r\n      // yx = fyDayReserves + fyTokenAmount\r\n      uint256 yx = uint256(fyTokenReserves) + uint256(fyTokenAmount);\r\n      require(yx <= MAX, \"YieldMath: Too much fyToken in\");\r\n\r\n      // yxa = yx ** a\r\n      uint256 yxa = uint128(yx).pow(a, ONE);\r\n\r\n      // sum = za + ya - yxa\r\n      uint256 sum = za + ya - yxa; // z < MAX, y < MAX, a < 1. It can only underflow, not overflow.\r\n      require(sum <= MAX, \"YieldMath: Insufficient base reserves\");\r\n\r\n      // result = baseReserves - (sum ** (1/a))\r\n      uint256 result = uint256(baseReserves) - uint256(uint128(sum).pow(ONE, a));\r\n      require(result <= MAX, \"YieldMath: Rounding induced error\");\r\n\r\n      result = result > VAR ? result - VAR : 0; // Subtract error guard, flooring the result at zero\r\n\r\n      return uint128(result);\r\n    }\r\n  }\r\n\r\n  /**\r\n   * Calculate the amount of fyToken a user could sell for given amount of Base.\r\n   * https://www.desmos.com/calculator/0rgnmtckvy\r\n   * @param baseReserves base reserves amount\r\n   * @param fyTokenReserves fyToken reserves amount\r\n   * @param baseAmount Base amount to be traded\r\n   * @param timeTillMaturity time till maturity in seconds\r\n   * @param k time till maturity coefficient, multiplied by 2^64\r\n   * @param g fee coefficient, multiplied by 2^64\r\n   * @return the amount of fyToken a user could sell for given amount of Base\r\n   */\r\n  function fyTokenInForBaseOut(\r\n    uint128 baseReserves, uint128 fyTokenReserves, uint128 baseAmount,\r\n    uint128 timeTillMaturity, int128 k, int128 g)\r\n  public pure returns(uint128) {\r\n    unchecked {\r\n      uint128 a = _computeA(timeTillMaturity, k, g);\r\n\r\n      // za = baseReserves ** a\r\n      uint256 za = baseReserves.pow(a, ONE);\r\n\r\n      // ya = fyTokenReserves ** a\r\n      uint256 ya = fyTokenReserves.pow(a, ONE);\r\n\r\n      // zx = baseReserves - baseAmount\r\n      uint256 zx = uint256(baseReserves) - uint256(baseAmount);\r\n      require(zx <= MAX, \"YieldMath: Too much base out\");\r\n\r\n      // zxa = zx ** a\r\n      uint256 zxa = uint128(zx).pow(a, ONE);\r\n\r\n      // sum = za + ya - zxa\r\n      uint256 sum = za + ya - zxa; // z < MAX, y < MAX, a < 1. It can only underflow, not overflow.\r\n      require(sum <= MAX, \"YieldMath: Resulting fyToken reserves too high\");\r\n\r\n      // result = (sum ** (1/a)) - fyTokenReserves\r\n      uint256 result = uint256(uint128(sum).pow(ONE, a)) - uint256(fyTokenReserves);\r\n      require(result <= MAX, \"YieldMath: Rounding induced error\");\r\n\r\n      result = result < MAX - VAR ? result + VAR : MAX; // Add error guard, ceiling the result at max\r\n\r\n      return uint128(result);\r\n    }\r\n  }\r\n\r\n  /**\r\n   * Calculate the amount of base a user would have to pay for certain amount of fyToken.\r\n   * https://www.desmos.com/calculator/ws5oqj8x5i\r\n   * @param baseReserves Base reserves amount\r\n   * @param fyTokenReserves fyToken reserves amount\r\n   * @param fyTokenAmount fyToken amount to be traded\r\n   * @param timeTillMaturity time till maturity in seconds\r\n   * @param k time till maturity coefficient, multiplied by 2^64\r\n   * @param g fee coefficient, multiplied by 2^64\r\n   * @return the amount of base a user would have to pay for given amount of\r\n   *         fyToken\r\n   */\r\n  function baseInForFYTokenOut(\r\n    uint128 baseReserves, uint128 fyTokenReserves, uint128 fyTokenAmount,\r\n    uint128 timeTillMaturity, int128 k, int128 g)\r\n  public pure returns(uint128) {\r\n    unchecked {\r\n      uint128 a = _computeA(timeTillMaturity, k, g);\r\n\r\n      // za = baseReserves ** a\r\n      uint256 za = baseReserves.pow(a, ONE);\r\n\r\n      // ya = fyTokenReserves ** a\r\n      uint256 ya = fyTokenReserves.pow(a, ONE);\r\n\r\n      // yx = baseReserves - baseAmount\r\n      uint256 yx = uint256(fyTokenReserves) - uint256(fyTokenAmount);\r\n      require(yx <= MAX, \"YieldMath: Too much fyToken out\");\r\n\r\n      // yxa = yx ** a\r\n      uint256 yxa = uint128(yx).pow(a, ONE);\r\n\r\n      // sum = za + ya - yxa\r\n      uint256 sum = za + ya - yxa; // z < MAX, y < MAX, a < 1. It can only underflow, not overflow.\r\n      require(sum <= MAX, \"YieldMath: Resulting base reserves too high\");\r\n\r\n      // result = (sum ** (1/a)) - baseReserves\r\n      uint256 result = uint256(uint128(sum).pow(ONE, a)) - uint256(baseReserves);\r\n      require(result <= MAX, \"YieldMath: Rounding induced error\");\r\n\r\n      result = result < MAX - VAR ? result + VAR : MAX; // Add error guard, ceiling the result at max\r\n\r\n      return uint128(result);\r\n    }\r\n  }\r\n\r\n  function _computeA(uint128 timeTillMaturity, int128 k, int128 g) private pure returns (uint128) {\r\n    unchecked {\r\n      // t = k * timeTillMaturity\r\n      int128 t = k.mul(timeTillMaturity.fromUInt());\r\n      require(t >= 0, \"YieldMath: t must be positive\"); // Meaning neither T or k can be negative\r\n\r\n      // a = (1 - gt)\r\n      int128 a = int128(ONE).sub(g.mul(t));\r\n      require(a > 0, \"YieldMath: Too far from maturity\");\r\n      require(a <= int128(ONE), \"YieldMath: g must be positive\");\r\n\r\n      return uint128(a);\r\n    }\r\n  }\r\n\r\n  /**\r\n   * Estimate in Base the value of reserves at protocol initialization time.\r\n   *\r\n   * @param baseReserves base reserves amount\r\n   * @param fyTokenReserves fyToken reserves amount\r\n   * @param timeTillMaturity time till maturity in seconds\r\n   * @param k time till maturity coefficient, multiplied by 2^64\r\n   * @param c0 price of base in terms of Base, multiplied by 2^64\r\n   * @return estimated value of reserves\r\n   */\r\n  function initialReservesValue(\r\n    uint128 baseReserves, uint128 fyTokenReserves, uint128 timeTillMaturity,\r\n    int128 k, int128 c0)\r\n  external pure returns(uint128) {\r\n    unchecked {\r\n      uint256 normalizedBaseReserves = c0.mulu(baseReserves);\r\n      require(normalizedBaseReserves <= MAX);\r\n\r\n      // a = (1 - k * timeTillMaturity)\r\n      int128 a = int128(ONE).sub(k.mul(timeTillMaturity.fromUInt()));\r\n      require(a > 0);\r\n\r\n      uint256 sum =\r\n        uint256(uint128(normalizedBaseReserves).pow(uint128(a), ONE)) +\r\n        uint256(fyTokenReserves.pow(uint128(a), ONE)) >> 1;\r\n      require(sum <= MAX);\r\n\r\n      uint256 result = uint256(uint128(sum).pow(ONE, uint128(a))) << 1;\r\n      require(result <= MAX);\r\n\r\n      return uint128(result);\r\n    }\r\n  }\r\n}"
}