creation_sourcecode: {{
    "language": "Solidity",
    "sources": {
      "src/core/SmartLTV.sol": {
        "content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.2 <0.9.0;\n\nimport {Pythia} from \"./Pythia.sol\";\nimport {Math} from \"../../lib/openzeppelin-contracts/contracts/utils/math/Math.sol\";\nimport {RiskData} from \"../interfaces/RiskData.sol\";\nimport {RiskyMath} from \"../lib/RiskyMath.sol\";\nimport {ErrorLib} from \"../lib/ErrorLib.sol\";\n\n/// @title Smart Loan-to-Value (LTV) Calculation Contract\n/// @author bprotocol\n/// @notice This contract calculates the Loan-to-Value (LTV) ratio based on market and risk data.\n/// @dev The contract utilizes the Pythia contract for data verification and the RiskyMath library for mathematical operations.\ncontract SmartLTV {\n  /// @notice defines the max mantissa when computing the ltv\n  ///         everything higher than this is 0% ltv\n  uint256 public immutable MAX_MANTISSA = 10.4621863982718417e18;\n\n  /// @notice Address of the Pythia contract used for signature verification\n  Pythia public immutable PYTHIA;\n\n  /// @notice Address of the trusted relayer for verifying data authenticity\n  address public immutable TRUSTED_RELAYER;\n\n  /// @notice Initializes the SmartLTV contract with Pythia and TRUSTED_RELAYER addresses.\n  /// @param pythia The Pythia contract address used for data verification.\n  /// @param relayer The address of the trusted relayer.\n  constructor(Pythia pythia, address relayer) {\n    PYTHIA = pythia;\n    TRUSTED_RELAYER = relayer;\n  }\n\n  /// @notice Calculates the Loan-to-Value ratio based on provided market conditions and risk data.\n  /// @dev The function performs signature verification, time and chain ID checks, asset matching, and then calculates LTV.\n  ///      It reverts in case of signature mismatch, data timeout, wrong chain ID, or asset mismatches.\n  /// @param collateralAsset Address of the collateral asset.\n  /// @param debtAsset Address of the debt asset.\n  /// @param d the supply cap, used in LTV calculation.\n  /// @param beta The liquidation bonus, used in the LTV calculation.\n  /// @param minClf The min confidence level factor targeted\n  /// @param riskData Struct containing various risk parameters like volatility, liquidity, last update time, and chain ID.\n  /// @param v Component of the Ethereum signature\n  /// @param r Component of the Ethereum signature\n  /// @param s Component of the Ethereum signature\n  /// @return The calculated Loan-to-Value ratio.\n  /// @custom:revert INVALID_SIGNER If the signature verification fails.\n  /// @custom:revert TIMEOUT If the risk data is older than 1 day.\n  /// @custom:revert WRONG_CHAINID If the risk data chain ID does not match the current chain.\n  /// @custom:revert COLLATERAL_MISMATCH If the collateral asset in risk data does not match the provided collateral asset.\n  /// @custom:revert DEBT_MISMATCH If the debt asset in risk data does not match the provided debt asset.\n  function ltv(\n    address collateralAsset,\n    address debtAsset,\n    uint d,\n    uint beta,\n    uint minClf,\n    RiskData memory riskData,\n    uint8 v,\n    bytes32 r,\n    bytes32 s\n  ) public view returns (uint) {\n    // first verify the signature\n    address signer = PYTHIA.getSigner(riskData, v, r, s);\n\n    // invalid signature\n    if (signer != TRUSTED_RELAYER) {\n      revert ErrorLib.INVALID_SIGNER(signer, TRUSTED_RELAYER);\n    }\n\n    // timeout\n    if (riskData.lastUpdate < block.timestamp - 1 days) {\n      revert ErrorLib.TIMEOUT();\n    }\n\n    // chain id\n    if (riskData.chainId != block.chainid) {\n      revert ErrorLib.WRONG_CHAINID(riskData.chainId, block.chainid);\n    }\n\n    // check collateral asset is the same\n    if (riskData.collateralAsset != collateralAsset) {\n      revert ErrorLib.COLLATERAL_MISMATCH(riskData.collateralAsset, collateralAsset);\n    }\n\n    // check debt asset is the same\n    if (riskData.debtAsset != debtAsset) {\n      revert ErrorLib.DEBT_MISMATCH(riskData.debtAsset, debtAsset);\n    }\n\n    // check liquidation bonus\n    if(beta < riskData.liquidationBonus) {\n      revert ErrorLib.WRONG_LIQUIDATION_BONUS(beta, riskData.liquidationBonus);\n    }\n\n\n    // LTV  = e ^ (-c * sigma / sqrt(l/d)) - beta\n    // with c = confidence level factor\n    // sigma = volatility\n    // l = liquidity\n    // d = cap\n    // beta = liquidation bonus\n    uint cTimesSigma = (minClf * riskData.volatility) / 1e18;\n    uint sqrtValue = RiskyMath.sqrt((1e18 * riskData.liquidity) / d) * 1e9;\n    uint mantissa = ((1 << 59) * cTimesSigma) / sqrtValue;\n\n    // when mantissa is higher than MAX_MANTISSA, ltv is 0%\n    if (mantissa >= MAX_MANTISSA) {\n      return 0;\n    }\n\n    uint expResult = RiskyMath.generalExp(mantissa, 59);\n    (, uint256 computedLTV) = Math.trySub((1e18 * (1 << 59)) / expResult, beta);\n    return computedLTV;\n  }\n}\n"
      },
      "src/core/Pythia.sol": {
        "content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.2 <0.9.0;\n\nimport {RiskData} from \"../interfaces/RiskData.sol\";\n\n/// @title Pythia Contract for Risk Data Signature Verification\n/// @author bprotocol\n/// @notice This contract is designed to manage and verify signatures for risk data, using EIP712 typed data standard.\n/// @dev It uses EIP712 domain and type hashes for structuring and verifying data,\n///      providing a function to easily recover the signer's address from signed risk data.\ncontract Pythia {\n  struct EIP712Domain {\n    string name;\n    string version;\n    uint256 chainId;\n    address verifyingContract;\n  }\n\n  /// @notice Type hash for the EIP712 domain, used in constructing the domain separator.\n  bytes32 public constant EIP712DOMAIN_TYPEHASH =\n    keccak256(\"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)\");\n\n  /// @notice Type hash for RiskData, used in the EIP712 typed data signing process.\n  bytes32 public constant RISKDATA_TYPEHASH =\n    keccak256(\n      \"RiskData(address collateralAsset,address debtAsset,uint256 liquidity,uint256 volatility,uint256 liquidationBonus,uint256 lastUpdate,uint256 chainId)\"\n    );\n\n  /// @notice Immutable EIP712 domain separator, unique to this contract and its deployment environment.\n  bytes32 public immutable DOMAIN_SEPARATOR;\n\n  /// @notice The chain ID on which this contract is deployed, immutable and set at contract deployment.\n  uint256 public immutable chainId;\n\n  /// @notice Initializes the contract, setting up the domain separator with the contract's details.\n  constructor() {\n    chainId = block.chainid;\n\n    EIP712Domain memory domain = EIP712Domain({\n      name: \"SPythia\",\n      version: \"0.0.1\",\n      chainId: chainId,\n      verifyingContract: address(this)\n    });\n    DOMAIN_SEPARATOR = keccak256(\n      abi.encode(\n        EIP712DOMAIN_TYPEHASH,\n        keccak256(bytes(domain.name)),\n        keccak256(bytes(domain.version)),\n        domain.chainId,\n        domain.verifyingContract\n      )\n    );\n  }\n\n  /// @notice Hashes a RiskData struct using the EIP712 risk data type hash.\n  /// @param data The RiskData struct to be hashed.\n  /// @return The keccak256 hash of the encoded RiskData struct.\n  function hashStruct(RiskData memory data) public pure returns (bytes32) {\n    return\n      keccak256(\n        abi.encode(\n          RISKDATA_TYPEHASH,\n          data.collateralAsset,\n          data.debtAsset,\n          data.liquidity,\n          data.volatility,\n          data.liquidationBonus,\n          data.lastUpdate,\n          data.chainId\n        )\n      );\n  }\n\n  /// @notice Recovers the signer's address from the provided EIP712 signature and RiskData.\n  /// @dev Uses the EIP712 standard for typed data signing to recover the address.\n  /// @param data The signed RiskData struct.\n  /// @param v The recovery byte of the signature.\n  /// @param r Half of the ECDSA signature pair.\n  /// @param s Half of the ECDSA signature pair.\n  /// @return The address of the signer who signed the provided RiskData.\n  function getSigner(RiskData memory data, uint8 v, bytes32 r, bytes32 s) public view returns (address) {\n    bytes32 digest = keccak256(abi.encodePacked(\"\\x19\\x01\", DOMAIN_SEPARATOR, hashStruct(data)));\n    return ecrecover(digest, v, r, s);\n  }\n}\n"
      },
      "lib/openzeppelin-contracts/contracts/utils/math/Math.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/Math.sol)\n\npragma solidity ^0.8.20;\n\n/**\n * @dev Standard math utilities missing in the Solidity language.\n */\nlibrary Math {\n    /**\n     * @dev Muldiv operation overflow.\n     */\n    error MathOverflowedMulDiv();\n\n    enum Rounding {\n        Floor, // Toward negative infinity\n        Ceil, // Toward positive infinity\n        Trunc, // Toward zero\n        Expand // Away from zero\n    }\n\n    /**\n     * @dev Returns the addition of two unsigned integers, with an overflow flag.\n     */\n    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n        unchecked {\n            uint256 c = a + b;\n            if (c < a) return (false, 0);\n            return (true, c);\n        }\n    }\n\n    /**\n     * @dev Returns the subtraction of two unsigned integers, with an overflow flag.\n     */\n    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n        unchecked {\n            if (b > a) return (false, 0);\n            return (true, a - b);\n        }\n    }\n\n    /**\n     * @dev Returns the multiplication of two unsigned integers, with an overflow flag.\n     */\n    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n        unchecked {\n            // Gas optimization: this is cheaper than requiring 'a' not being zero, but the\n            // benefit is lost if 'b' is also tested.\n            // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522\n            if (a == 0) return (true, 0);\n            uint256 c = a * b;\n            if (c / a != b) return (false, 0);\n            return (true, c);\n        }\n    }\n\n    /**\n     * @dev Returns the division of two unsigned integers, with a division by zero flag.\n     */\n    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n        unchecked {\n            if (b == 0) return (false, 0);\n            return (true, a / b);\n        }\n    }\n\n    /**\n     * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.\n     */\n    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n        unchecked {\n            if (b == 0) return (false, 0);\n            return (true, a % b);\n        }\n    }\n\n    /**\n     * @dev Returns the largest of two numbers.\n     */\n    function max(uint256 a, uint256 b) internal pure returns (uint256) {\n        return a > b ? a : b;\n    }\n\n    /**\n     * @dev Returns the smallest of two numbers.\n     */\n    function min(uint256 a, uint256 b) internal pure returns (uint256) {\n        return a < b ? a : b;\n    }\n\n    /**\n     * @dev Returns the average of two numbers. The result is rounded towards\n     * zero.\n     */\n    function average(uint256 a, uint256 b) internal pure returns (uint256) {\n        // (a + b) / 2 can overflow.\n        return (a & b) + (a ^ b) / 2;\n    }\n\n    /**\n     * @dev Returns the ceiling of the division of two numbers.\n     *\n     * This differs from standard division with `/` in that it rounds towards infinity instead\n     * of rounding towards zero.\n     */\n    function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {\n        if (b == 0) {\n            // Guarantee the same behavior as in a regular Solidity division.\n            return a / b;\n        }\n\n        // (a + b - 1) / b can overflow on addition, so we distribute.\n        return a == 0 ? 0 : (a - 1) / b + 1;\n    }\n\n    /**\n     * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or\n     * denominator == 0.\n     * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by\n     * Uniswap Labs also under MIT license.\n     */\n    function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {\n        unchecked {\n            // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use\n            // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256\n            // variables such that product = prod1 * 2^256 + prod0.\n            uint256 prod0 = x * y; // Least significant 256 bits of the product\n            uint256 prod1; // Most significant 256 bits of the product\n            assembly {\n                let mm := mulmod(x, y, not(0))\n                prod1 := sub(sub(mm, prod0), lt(mm, prod0))\n            }\n\n            // Handle non-overflow cases, 256 by 256 division.\n            if (prod1 == 0) {\n                // Solidity will revert if denominator == 0, unlike the div opcode on its own.\n                // The surrounding unchecked block does not change this fact.\n                // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.\n                return prod0 / denominator;\n            }\n\n            // Make sure the result is less than 2^256. Also prevents denominator == 0.\n            if (denominator <= prod1) {\n                revert MathOverflowedMulDiv();\n            }\n\n            ///////////////////////////////////////////////\n            // 512 by 256 division.\n            ///////////////////////////////////////////////\n\n            // Make division exact by subtracting the remainder from [prod1 prod0].\n            uint256 remainder;\n            assembly {\n                // Compute remainder using mulmod.\n                remainder := mulmod(x, y, denominator)\n\n                // Subtract 256 bit number from 512 bit number.\n                prod1 := sub(prod1, gt(remainder, prod0))\n                prod0 := sub(prod0, remainder)\n            }\n\n            // Factor powers of two out of denominator and compute largest power of two divisor of denominator.\n            // Always >= 1. See https://cs.stackexchange.com/q/138556/92363.\n\n            uint256 twos = denominator & (0 - denominator);\n            assembly {\n                // Divide denominator by twos.\n                denominator := div(denominator, twos)\n\n                // Divide [prod1 prod0] by twos.\n                prod0 := div(prod0, twos)\n\n                // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.\n                twos := add(div(sub(0, twos), twos), 1)\n            }\n\n            // Shift in bits from prod1 into prod0.\n            prod0 |= prod1 * twos;\n\n            // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such\n            // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for\n            // four bits. That is, denominator * inv = 1 mod 2^4.\n            uint256 inverse = (3 * denominator) ^ 2;\n\n            // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also\n            // works in modular arithmetic, doubling the correct bits in each step.\n            inverse *= 2 - denominator * inverse; // inverse mod 2^8\n            inverse *= 2 - denominator * inverse; // inverse mod 2^16\n            inverse *= 2 - denominator * inverse; // inverse mod 2^32\n            inverse *= 2 - denominator * inverse; // inverse mod 2^64\n            inverse *= 2 - denominator * inverse; // inverse mod 2^128\n            inverse *= 2 - denominator * inverse; // inverse mod 2^256\n\n            // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.\n            // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is\n            // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1\n            // is no longer required.\n            result = prod0 * inverse;\n            return result;\n        }\n    }\n\n    /**\n     * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.\n     */\n    function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {\n        uint256 result = mulDiv(x, y, denominator);\n        if (unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0) {\n            result += 1;\n        }\n        return result;\n    }\n\n    /**\n     * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded\n     * towards zero.\n     *\n     * Inspired by Henry S. Warren, Jr.'s \"Hacker's Delight\" (Chapter 11).\n     */\n    function sqrt(uint256 a) internal pure returns (uint256) {\n        if (a == 0) {\n            return 0;\n        }\n\n        // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.\n        //\n        // We know that the \"msb\" (most significant bit) of our target number `a` is a power of 2 such that we have\n        // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.\n        //\n        // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`\n        // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`\n        // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`\n        //\n        // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.\n        uint256 result = 1 << (log2(a) >> 1);\n\n        // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,\n        // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at\n        // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision\n        // into the expected uint128 result.\n        unchecked {\n            result = (result + a / result) >> 1;\n            result = (result + a / result) >> 1;\n            result = (result + a / result) >> 1;\n            result = (result + a / result) >> 1;\n            result = (result + a / result) >> 1;\n            result = (result + a / result) >> 1;\n            result = (result + a / result) >> 1;\n            return min(result, a / result);\n        }\n    }\n\n    /**\n     * @notice Calculates sqrt(a), following the selected rounding direction.\n     */\n    function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {\n        unchecked {\n            uint256 result = sqrt(a);\n            return result + (unsignedRoundsUp(rounding) && result * result < a ? 1 : 0);\n        }\n    }\n\n    /**\n     * @dev Return the log in base 2 of a positive value rounded towards zero.\n     * Returns 0 if given 0.\n     */\n    function log2(uint256 value) internal pure returns (uint256) {\n        uint256 result = 0;\n        unchecked {\n            if (value >> 128 > 0) {\n                value >>= 128;\n                result += 128;\n            }\n            if (value >> 64 > 0) {\n                value >>= 64;\n                result += 64;\n            }\n            if (value >> 32 > 0) {\n                value >>= 32;\n                result += 32;\n            }\n            if (value >> 16 > 0) {\n                value >>= 16;\n                result += 16;\n            }\n            if (value >> 8 > 0) {\n                value >>= 8;\n                result += 8;\n            }\n            if (value >> 4 > 0) {\n                value >>= 4;\n                result += 4;\n            }\n            if (value >> 2 > 0) {\n                value >>= 2;\n                result += 2;\n            }\n            if (value >> 1 > 0) {\n                result += 1;\n            }\n        }\n        return result;\n    }\n\n    /**\n     * @dev Return the log in base 2, following the selected rounding direction, of a positive value.\n     * Returns 0 if given 0.\n     */\n    function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {\n        unchecked {\n            uint256 result = log2(value);\n            return result + (unsignedRoundsUp(rounding) && 1 << result < value ? 1 : 0);\n        }\n    }\n\n    /**\n     * @dev Return the log in base 10 of a positive value rounded towards zero.\n     * Returns 0 if given 0.\n     */\n    function log10(uint256 value) internal pure returns (uint256) {\n        uint256 result = 0;\n        unchecked {\n            if (value >= 10 ** 64) {\n                value /= 10 ** 64;\n                result += 64;\n            }\n            if (value >= 10 ** 32) {\n                value /= 10 ** 32;\n                result += 32;\n            }\n            if (value >= 10 ** 16) {\n                value /= 10 ** 16;\n                result += 16;\n            }\n            if (value >= 10 ** 8) {\n                value /= 10 ** 8;\n                result += 8;\n            }\n            if (value >= 10 ** 4) {\n                value /= 10 ** 4;\n                result += 4;\n            }\n            if (value >= 10 ** 2) {\n                value /= 10 ** 2;\n                result += 2;\n            }\n            if (value >= 10 ** 1) {\n                result += 1;\n            }\n        }\n        return result;\n    }\n\n    /**\n     * @dev Return the log in base 10, following the selected rounding direction, of a positive value.\n     * Returns 0 if given 0.\n     */\n    function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {\n        unchecked {\n            uint256 result = log10(value);\n            return result + (unsignedRoundsUp(rounding) && 10 ** result < value ? 1 : 0);\n        }\n    }\n\n    /**\n     * @dev Return the log in base 256 of a positive value rounded towards zero.\n     * Returns 0 if given 0.\n     *\n     * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.\n     */\n    function log256(uint256 value) internal pure returns (uint256) {\n        uint256 result = 0;\n        unchecked {\n            if (value >> 128 > 0) {\n                value >>= 128;\n                result += 16;\n            }\n            if (value >> 64 > 0) {\n                value >>= 64;\n                result += 8;\n            }\n            if (value >> 32 > 0) {\n                value >>= 32;\n                result += 4;\n            }\n            if (value >> 16 > 0) {\n                value >>= 16;\n                result += 2;\n            }\n            if (value >> 8 > 0) {\n                result += 1;\n            }\n        }\n        return result;\n    }\n\n    /**\n     * @dev Return the log in base 256, following the selected rounding direction, of a positive value.\n     * Returns 0 if given 0.\n     */\n    function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {\n        unchecked {\n            uint256 result = log256(value);\n            return result + (unsignedRoundsUp(rounding) && 1 << (result << 3) < value ? 1 : 0);\n        }\n    }\n\n    /**\n     * @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.\n     */\n    function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {\n        return uint8(rounding) % 2 == 1;\n    }\n}\n"
      },
      "src/interfaces/RiskData.sol": {
        "content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.2 <0.9.0;\n\nstruct Signature {\n  uint8 v;\n  bytes32 r;\n  bytes32 s;\n}\n\nstruct RiskData {\n  address collateralAsset;\n  address debtAsset;\n  uint256 liquidity;\n  uint256 volatility;\n  uint256 liquidationBonus;\n  uint256 lastUpdate;\n  uint256 chainId;\n}\n\nstruct SignedRiskData {\n  RiskData riskData;\n  uint8 v;\n  bytes32 r;\n  bytes32 s;\n}\n"
      },
      "src/lib/RiskyMath.sol": {
        "content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.2 <0.9.0;\n\n/// @title RiskyMath Library\n/// @author bprotocol\n/// @notice Provides advanced mathematical functions to assist in risk calculations for DeFi applications.\nlibrary RiskyMath {\n  /// @notice Computes an exponential function with base 'e' for risk calculations.\n  /// @param _x The exponent to use in the calculation.\n  /// @param _precision The precision for the calculation.\n  /// @return The result of the exponential function.\n  function generalExp(uint256 _x, uint8 _precision) internal pure returns (uint256) {\n    uint256 xi = _x;\n    uint256 res = 0;\n\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x3442c4e6074a82f1797f72ac0000000; // add x^02 * (33! / 02!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x116b96f757c380fb287fd0e40000000; // add x^03 * (33! / 03!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x045ae5bdd5f0e03eca1ff4390000000; // add x^04 * (33! / 04!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x00defabf91302cd95b9ffda50000000; // add x^05 * (33! / 05!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x002529ca9832b22439efff9b8000000; // add x^06 * (33! / 06!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x00054f1cf12bd04e516b6da88000000; // add x^07 * (33! / 07!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000a9e39e257a09ca2d6db51000000; // add x^08 * (33! / 08!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x000012e066e7b839fa050c309000000; // add x^09 * (33! / 09!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x000001e33d7d926c329a1ad1a800000; // add x^10 * (33! / 10!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000002bee513bdb4a6b19b5f800000; // add x^11 * (33! / 11!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x00000003a9316fa79b88eccf2a00000; // add x^12 * (33! / 12!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000000048177ebe1fa812375200000; // add x^13 * (33! / 13!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000000005263fe90242dcbacf00000; // add x^14 * (33! / 14!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x000000000057e22099c030d94100000; // add x^15 * (33! / 15!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000000000057e22099c030d9410000; // add x^16 * (33! / 16!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x00000000000052b6b54569976310000; // add x^17 * (33! / 17!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x00000000000004985f67696bf748000; // add x^18 * (33! / 18!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x000000000000003dea12ea99e498000; // add x^19 * (33! / 19!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x00000000000000031880f2214b6e000; // add x^20 * (33! / 20!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x000000000000000025bcff56eb36000; // add x^21 * (33! / 21!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x000000000000000001b722e10ab1000; // add x^22 * (33! / 22!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000000000000000001317c70077000; // add x^23 * (33! / 23!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x00000000000000000000cba84aafa00; // add x^24 * (33! / 24!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x00000000000000000000082573a0a00; // add x^25 * (33! / 25!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x00000000000000000000005035ad900; // add x^26 * (33! / 26!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x000000000000000000000002f881b00; // add x^27 * (33! / 27!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000000000000000000000001b29340; // add x^28 * (33! / 28!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x00000000000000000000000000efc40; // add x^29 * (33! / 29!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000000000000000000000000007fe0; // add x^30 * (33! / 30!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000000000000000000000000000420; // add x^31 * (33! / 31!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000000000000000000000000000021; // add x^32 * (33! / 32!)\n    xi = (xi * _x) >> _precision;\n    res += xi * 0x0000000000000000000000000000001; // add x^33 * (33! / 33!)\n\n    // divide by 33! and then add x^1 / 1! + x^0 / 0!\n    return res / 0x688589cc0e9505e2f2fee5580000000 + _x + (1 << _precision);\n  }\n\n  /// @notice Calculates the square root of a given number.\n  /// @dev Implements an efficient algorithm for square root calculation, such as the Babylonian method or Bit Shifting method.\n  /// @param x The number to calculate the square root of.\n  /// @return y The calculated square root.\n  function sqrt(uint256 x) internal pure returns (uint256 y) {\n    uint256 z = (x + 1) / 2;\n    y = x;\n    while (z < y) {\n      y = z;\n      z = (x / z + z) / 2;\n    }\n  }\n}\n"
      },
      "src/lib/ErrorLib.sol": {
        "content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.2 <0.9.0;\n\n/**\n * @title Error Library for Custom Errors\n * @author bprotocol, la-tribu.xyz\n * @notice defines custom errors used across the contracts of this repository\n */\nlibrary ErrorLib {\n  /// @notice Error for mismatch in the count of risk data entries.\n  /// @param a The count of market allocations.\n  /// @param b The count of risk data entries.\n  error INVALID_RISK_DATA_COUNT(uint256 a, uint256 b);\n\n  /// @notice Error for mismatch in the count of signatures.\n  /// @param a The count of risk data entries.\n  /// @param b The count of signatures.\n  error INVALID_SIGNATURE_COUNT(uint256 a, uint256 b);\n\n  /// @notice Error for an invalid signer in data verification.\n  /// @param dataSigner The address of the signer from the data.\n  /// @param expected The expected signer's address.\n  error INVALID_SIGNER(address dataSigner, address expected);\n\n  /// @notice Error for data that has timed out (e.g., stale risk data).\n  error TIMEOUT();\n\n  /// @notice Error for mismatched chain ID in data verification.\n  /// @param chainId The chain ID from the data.\n  /// @param current The current chain ID.\n  error WRONG_CHAINID(uint256 chainId, uint256 current);\n\n  /// @notice Error for mismatched collateral addresses.\n  /// @param c1 The collateral address from the data.\n  /// @param c2 The expected collateral address.\n  error COLLATERAL_MISMATCH(address c1, address c2);\n\n  /// @notice Error for mismatched debt addresses.\n  /// @param d1 The debt address from the data.\n  /// @param d2 The expected debt address.\n  error DEBT_MISMATCH(address d1, address d2);\n\n  /// @notice Error for when the LTV is too high.\n  /// @param ltv The current LTV.\n  /// @param maxLtv The maximum allowed LTV.\n  error LTV_TOO_HIGH(uint256 ltv, uint256 maxLtv);\n\n  /// @notice Error for when the liquidation bonus is too high.\n  /// @param beta The current liquidation bonus.\n  /// @param liquidationBonus The liquidation bonus in the risk data.\n  error WRONG_LIQUIDATION_BONUS(uint256 beta, uint256 liquidationBonus);\n}\n"
      }
    },
    "settings": {
      "remappings": [
        "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
        "core/=scripts/core/",
        "ds-test/=lib/forge-std/lib/ds-test/src/",
        "erc4626-tests/=lib/metamorpho/lib/erc4626-tests/",
        "forge-std/=lib/forge-std/src/",
        "goerli/=scripts/goerli/",
        "metamorpho/=lib/metamorpho/src/",
        "morpho-blue/=lib/metamorpho/lib/morpho-blue/src/",
        "morpho/=scripts/morpho/",
        "murky/=lib/metamorpho/lib/universal-rewards-distributor/lib/murky/src/",
        "openzeppelin-contracts/=lib/openzeppelin-contracts/",
        "openzeppelin/=lib/metamorpho/lib/universal-rewards-distributor/lib/openzeppelin-contracts/contracts/",
        "solmate/=lib/metamorpho/lib/universal-rewards-distributor/lib/solmate/src/",
        "universal-rewards-distributor/=lib/metamorpho/lib/universal-rewards-distributor/src/"
      ],
      "optimizer": {
        "enabled": true,
        "runs": 200
      },
      "metadata": {
        "useLiteralContent": false,
        "bytecodeHash": "ipfs",
        "appendCBOR": true
      },
      "outputSelection": {
        "*": {
          "*": [
            "evm.bytecode",
            "evm.deployedBytecode",
            "devdoc",
            "userdoc",
            "metadata",
            "abi"
          ]
        }
      },
      "evmVersion": "paris",
      "viaIR": false,
      "libraries": {}
    }
  }}