{"sources":{"@openzeppelin/contracts/access/Ownable.sol":{"content":"// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)\n\npragma solidity ^0.8.20;\n\nimport {Context} from \"../utils/Context.sol\";\n\n/**\n * @dev Contract module which provides a basic access control mechanism, where\n * there is an account (an owner) that can be granted exclusive access to\n * specific functions.\n *\n * The initial owner is set to the address provided by the deployer. This can\n * later be changed with {transferOwnership}.\n *\n * This module is used through inheritance. It will make available the modifier\n * `onlyOwner`, which can be applied to your functions to restrict their use to\n * the owner.\n */\nabstract contract Ownable is Context {\n    address private _owner;\n\n    /**\n     * @dev The caller account is not authorized to perform an operation.\n     */\n    error OwnableUnauthorizedAccount(address account);\n\n    /**\n     * @dev The owner is not a valid owner account. (eg. `address(0)`)\n     */\n    error OwnableInvalidOwner(address owner);\n\n    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);\n\n    /**\n     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.\n     */\n    constructor(address initialOwner) {\n        if (initialOwner == address(0)) {\n            revert OwnableInvalidOwner(address(0));\n        }\n        _transferOwnership(initialOwner);\n    }\n\n    /**\n     * @dev Throws if called by any account other than the owner.\n     */\n    modifier onlyOwner() {\n        _checkOwner();\n        _;\n    }\n\n    /**\n     * @dev Returns the address of the current owner.\n     */\n    function owner() public view virtual returns (address) {\n        return _owner;\n    }\n\n    /**\n     * @dev Throws if the sender is not the owner.\n     */\n    function _checkOwner() internal view virtual {\n        if (owner() != _msgSender()) {\n            revert OwnableUnauthorizedAccount(_msgSender());\n        }\n    }\n\n    /**\n     * @dev Leaves the contract without owner. It will not be possible to call\n     * `onlyOwner` functions. Can only be called by the current owner.\n     *\n     * NOTE: Renouncing ownership will leave the contract without an owner,\n     * thereby disabling any functionality that is only available to the owner.\n     */\n    function renounceOwnership() public virtual onlyOwner {\n        _transferOwnership(address(0));\n    }\n\n    /**\n     * @dev Transfers ownership of the contract to a new account (`newOwner`).\n     * Can only be called by the current owner.\n     */\n    function transferOwnership(address newOwner) public virtual onlyOwner {\n        if (newOwner == address(0)) {\n            revert OwnableInvalidOwner(address(0));\n        }\n        _transferOwnership(newOwner);\n    }\n\n    /**\n     * @dev Transfers ownership of the contract to a new account (`newOwner`).\n     * Internal function without access restriction.\n     */\n    function _transferOwnership(address newOwner) internal virtual {\n        address oldOwner = _owner;\n        _owner = newOwner;\n        emit OwnershipTransferred(oldOwner, newOwner);\n    }\n}\n"},"@openzeppelin/contracts/access/Ownable2Step.sol":{"content":"// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable2Step.sol)\n\npragma solidity ^0.8.20;\n\nimport {Ownable} from \"./Ownable.sol\";\n\n/**\n * @dev Contract module which provides access control mechanism, where\n * there is an account (an owner) that can be granted exclusive access to\n * specific functions.\n *\n * The initial owner is specified at deployment time in the constructor for `Ownable`. This\n * can later be changed with {transferOwnership} and {acceptOwnership}.\n *\n * This module is used through inheritance. It will make available all functions\n * from parent (Ownable).\n */\nabstract contract Ownable2Step is Ownable {\n    address private _pendingOwner;\n\n    event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);\n\n    /**\n     * @dev Returns the address of the pending owner.\n     */\n    function pendingOwner() public view virtual returns (address) {\n        return _pendingOwner;\n    }\n\n    /**\n     * @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.\n     * Can only be called by the current owner.\n     */\n    function transferOwnership(address newOwner) public virtual override onlyOwner {\n        _pendingOwner = newOwner;\n        emit OwnershipTransferStarted(owner(), newOwner);\n    }\n\n    /**\n     * @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.\n     * Internal function without access restriction.\n     */\n    function _transferOwnership(address newOwner) internal virtual override {\n        delete _pendingOwner;\n        super._transferOwnership(newOwner);\n    }\n\n    /**\n     * @dev The new owner accepts the ownership transfer.\n     */\n    function acceptOwnership() public virtual {\n        address sender = _msgSender();\n        if (pendingOwner() != sender) {\n            revert OwnableUnauthorizedAccount(sender);\n        }\n        _transferOwnership(sender);\n    }\n}\n"},"@openzeppelin/contracts/utils/Context.sol":{"content":"// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)\n\npragma solidity ^0.8.20;\n\n/**\n * @dev Provides information about the current execution context, including the\n * sender of the transaction and its data. While these are generally available\n * via msg.sender and msg.data, they should not be accessed in such a direct\n * manner, since when dealing with meta-transactions the account sending and\n * paying for execution may not be the actual sender (as far as an application\n * is concerned).\n *\n * This contract is only required for intermediate, library-like contracts.\n */\nabstract contract Context {\n    function _msgSender() internal view virtual returns (address) {\n        return msg.sender;\n    }\n\n    function _msgData() internal view virtual returns (bytes calldata) {\n        return msg.data;\n    }\n\n    function _contextSuffixLength() internal view virtual returns (uint256) {\n        return 0;\n    }\n}\n"},"@openzeppelin/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"},"contracts/interfaces/Chainlink/AggregatorV3Interface.sol":{"content":"// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\ninterface AggregatorV3Interface {\n  function decimals() external view returns (uint8);\n\n  function description() external view returns (string memory);\n\n  function version() external view returns (uint256);\n\n  function latestAnswer() external view returns (uint256);\n\n  function getRoundData(uint80 _roundId)\n    external\n    view\n    returns (\n      uint80 roundId,\n      int256 answer,\n      uint256 startedAt,\n      uint256 updatedAt,\n      uint80 answeredInRound\n    );\n\n  function latestRoundData()\n    external\n    view\n    returns (\n      uint80 roundId,\n      int256 answer,\n      uint256 startedAt,\n      uint256 updatedAt,\n      uint80 answeredInRound\n    );\n}\n"},"contracts/price-oracle/BNBPriceOracle.sol":{"content":"// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.20;\n\nimport { Math } from \"@openzeppelin/contracts/utils/math/Math.sol\";\n\nimport { SpotPriceOracleBase } from \"./SpotPriceOracleBase.sol\";\nimport { IPriceOracle } from \"./interfaces/IPriceOracle.sol\";\nimport { AggregatorV3Interface } from \"../interfaces/Chainlink/AggregatorV3Interface.sol\";\n\n// Uncomment this line to use console.log\n// import \"hardhat/console.sol\";\n\ncontract BNBPriceOracle is SpotPriceOracleBase, IPriceOracle {\n  /*************\n   * Constants *\n   *************/\n\n  /// @notice The Chainlink BNB/USD price feed.\n  /// @dev See comments of `_readSpotPriceByChainlink` for more details.\n  bytes32 public immutable Chainlink_BNB_USD_Spot;\n\n  /// @notice The Binance Oracle BNB/USD price feed. Compatible with Chainlink.\n  bytes32 public immutable BinanceOracle_BNB_USD_Spot;\n\n  /*************\n   * Variables *\n   *************/\n\n  /// @notice The value of maximum price deviation, multiplied by 1e18.\n  uint256 public maxPriceDeviation;\n\n  /***************\n   * Constructor *\n   ***************/\n\n  constructor(\n    address _spotPriceOracle,\n    bytes32 _Chainlink_BNB_USD_Spot,\n    bytes32 _BinanceOracle_BNB_USD_Spot\n  ) SpotPriceOracleBase(_spotPriceOracle) {\n    require(_spotPriceOracle != address(0), \"ErrorZeroAddress\");\n    require(_Chainlink_BNB_USD_Spot != bytes32(0), \"ErrorEmptyChainlinkSpot\");\n    require(_BinanceOracle_BNB_USD_Spot != bytes32(0), \"ErrorEmptyBinanceOracleSpot\");\n\n    Chainlink_BNB_USD_Spot = _Chainlink_BNB_USD_Spot;\n    BinanceOracle_BNB_USD_Spot = _BinanceOracle_BNB_USD_Spot;\n\n    _updateMaxPriceDeviation(1e16); // 1%\n  }\n\n  /*************************\n   * Public View Functions *\n   *************************/\n\n  /// @notice Return the BNB/USD spot price.\n  /// @return chainlinkPrice The spot price from Chainlink price feed.\n  /// @return minPrice The minimum spot price among all available sources.\n  /// @return maxPrice The maximum spot price among all available sources.\n  function getBNBUSDSpotPrice() external view returns (uint256 chainlinkPrice, uint256 minPrice, uint256 maxPrice) {\n    (chainlinkPrice, minPrice, maxPrice) = _getBNBUSDSpotPrice();\n  }\n\n  /// @inheritdoc IPriceOracle\n  /// @dev The price is valid iff |maxPrice-minPrice|/minPrice < maxPriceDeviation\n  function getPrice() public view override returns (uint256 anchorPrice, uint256 minPrice, uint256 maxPrice) {\n    (anchorPrice, minPrice, maxPrice) = _getBNBUSDSpotPrice();\n\n    uint256 cachedMaxPriceDeviation = maxPriceDeviation; // gas saving\n    // use anchor price when the price deviation between anchor price and min price exceed threshold\n    if ((anchorPrice - minPrice) * PRECISION > cachedMaxPriceDeviation * minPrice) {\n      minPrice = anchorPrice;\n    }\n\n    // use anchor price when the price deviation between anchor price and max price exceed threshold\n    if ((maxPrice - anchorPrice) * PRECISION > cachedMaxPriceDeviation * anchorPrice) {\n      maxPrice = anchorPrice;\n    }\n  }\n\n  /// @inheritdoc IPriceOracle\n  function getExchangePrice() public view returns (uint256) {\n    (, uint256 price, ) = getPrice();\n    return price;\n  }\n\n  /// @inheritdoc IPriceOracle\n  function getLiquidatePrice() external view returns (uint256) {\n    return getExchangePrice();\n  }\n\n  /// @inheritdoc IPriceOracle\n  function getRedeemPrice() external view returns (uint256) {\n    (, , uint256 price) = getPrice();\n    return price;\n  }\n\n  /************************\n   * Restricted Functions *\n   ************************/\n\n  /// @notice Update the value of maximum price deviation.\n  /// @param newMaxPriceDeviation The new value of maximum price deviation, multiplied by 1e18.\n  function updateMaxPriceDeviation(uint256 newMaxPriceDeviation) external onlyOwner {\n    _updateMaxPriceDeviation(newMaxPriceDeviation);\n  }\n\n  /**********************\n   * Internal Functions *\n   **********************/\n\n  /// @dev Internal function to update the value of maximum price deviation.\n  /// @param newMaxPriceDeviation The new value of maximum price deviation, multiplied by 1e18.\n  function _updateMaxPriceDeviation(uint256 newMaxPriceDeviation) private {\n    uint256 oldMaxPriceDeviation = maxPriceDeviation;\n    if (oldMaxPriceDeviation == newMaxPriceDeviation) {\n      revert ErrorParameterUnchanged();\n    }\n\n    maxPriceDeviation = newMaxPriceDeviation;\n\n    emit UpdateMaxPriceDeviation(oldMaxPriceDeviation, newMaxPriceDeviation);\n  }\n\n  /// @dev Internal function to calculate the BNB/USD spot price.\n  /// @return chainlinkPrice The spot price from Chainlink price feed, multiplied by 1e18.\n  /// @return minPrice The minimum spot price among all available sources, multiplied by 1e18.\n  /// @return maxPrice The maximum spot price among all available sources, multiplied by 1e18.\n  function _getBNBUSDSpotPrice() internal view returns (uint256 chainlinkPrice, uint256 minPrice, uint256 maxPrice) {\n    // BNB/USD\n    chainlinkPrice = _readSpotPriceByChainlink(Chainlink_BNB_USD_Spot);\n    uint256 binancePrice = _readSpotPriceByChainlink(BinanceOracle_BNB_USD_Spot);\n    minPrice = Math.min(chainlinkPrice, binancePrice);\n    maxPrice = Math.max(chainlinkPrice, binancePrice);\n  }\n}\n"},"contracts/price-oracle/SpotPriceOracleBase.sol":{"content":"// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.26;\n\nimport { Ownable } from \"@openzeppelin/contracts/access/Ownable.sol\";\nimport { Ownable2Step } from \"@openzeppelin/contracts/access/Ownable2Step.sol\";\n\nimport { AggregatorV3Interface } from \"../interfaces/Chainlink/AggregatorV3Interface.sol\";\nimport { ISpotPriceOracle } from \"./interfaces/ISpotPriceOracle.sol\";\n\nabstract contract SpotPriceOracleBase is Ownable2Step {\n  /**********\n   * Errors *\n   **********/\n\n  /// @dev Thrown when the given encodings are invalid.\n  error ErrorInvalidEncodings();\n\n  /// @dev Thrown when update some parameters to the same value.\n  error ErrorParameterUnchanged();\n\n  /*************\n   * Constants *\n   *************/\n\n  /// @dev The precision for oracle price.\n  uint256 internal constant PRECISION = 1e18;\n\n  /// @dev The address of `SpotPriceOracle` contract.\n  address immutable spotPriceOracle;\n\n  /***************\n   * Constructor *\n   ***************/\n\n  constructor(address _spotPriceOracle) Ownable(_msgSender()) {\n    spotPriceOracle = _spotPriceOracle;\n  }\n\n  /**********************\n   * Internal Functions *\n   **********************/\n\n  /// @dev The encoding is below.\n  /// ```text\n  /// |  32 bits  | 64 bits |  160 bits  |\n  /// | heartbeat |  scale  | price_feed |\n  /// |low                          high |\n  /// ```\n  function _readSpotPriceByChainlink(bytes32 encoding) internal view returns (uint256) {\n    address aggregator;\n    uint256 scale;\n    uint256 heartbeat;\n    assembly {\n      aggregator := shr(96, encoding)\n      scale := and(shr(32, encoding), 0xffffffffffffffff)\n      heartbeat := and(encoding, 0xffffffff)\n    }\n    (, int256 answer, , uint256 updatedAt, ) = AggregatorV3Interface(aggregator).latestRoundData();\n    if (answer <= 0) revert(\"invalid\");\n    if (block.timestamp - updatedAt > heartbeat) revert(\"expired\");\n    return uint256(answer) * scale;\n  }\n\n  /// @dev Internal function to calculate spot price by encodings.\n  ///\n  /// The details of the encoding is below\n  /// ```text\n  /// |   1 byte   |    ...    |    ...    | ... |    ...    |\n  /// | num_source | source[0] | source[1] | ... | source[n] |\n  ///\n  /// source encoding:\n  /// |  1 byte  | 32 bytes | 32 bytes | ... | 32 bytes |\n  /// | num_pool |  pool[0] |  pool[1] | ... |  pool[n] |\n  /// 1 <= num_pool <= 3\n  ///\n  /// The encoding of each pool can be found in `SpotPriceOracle` contract.\n  /// ```\n  /// @return prices The list of prices of each source, multiplied by 1e18.\n  function _getSpotPriceByEncoding(bytes memory encodings) internal view returns (uint256[] memory prices) {\n    uint256 ptr;\n    uint256 length;\n    assembly {\n      ptr := add(encodings, 0x21)\n      length := byte(0, mload(sub(ptr, 1)))\n    }\n    prices = new uint256[](length);\n    for (uint256 i = 0; i < length; i++) {\n      uint256 encoding1;\n      uint256 encoding2;\n      uint256 encoding3;\n      assembly {\n        let cnt := byte(0, mload(ptr))\n        ptr := add(ptr, 0x01)\n        if gt(cnt, 0) {\n          encoding1 := mload(ptr)\n          ptr := add(ptr, 0x20)\n        }\n        if gt(cnt, 1) {\n          encoding2 := mload(ptr)\n          ptr := add(ptr, 0x20)\n        }\n        if gt(cnt, 2) {\n          encoding3 := mload(ptr)\n          ptr := add(ptr, 0x20)\n        }\n      }\n      if (encoding1 == 0) {\n        revert ErrorInvalidEncodings();\n      } else if (encoding2 == 0) {\n        prices[i] = _readSpotPrice(encoding1);\n      } else if (encoding3 == 0) {\n        prices[i] = _readSpotPrice(encoding1, encoding2);\n      } else {\n        prices[i] = _readSpotPrice(encoding1, encoding2, encoding3);\n      }\n    }\n  }\n\n  /// @dev Internal function to calculate spot price of single pool.\n  /// @param encoding The encoding for the pool.\n  /// @return price The spot price of the source, multiplied by 1e18.\n  function _readSpotPrice(uint256 encoding) private view returns (uint256 price) {\n    price = ISpotPriceOracle(spotPriceOracle).getSpotPrice(encoding);\n  }\n\n  /// @dev Internal function to calculate spot price of two pools.\n  /// @param encoding1 The encoding for the first pool.\n  /// @param encoding2 The encoding for the second pool.\n  /// @return price The spot price of the source, multiplied by 1e18.\n  function _readSpotPrice(uint256 encoding1, uint256 encoding2) private view returns (uint256 price) {\n    unchecked {\n      price = (_readSpotPrice(encoding1) * _readSpotPrice(encoding2)) / PRECISION;\n    }\n  }\n\n  /// @dev Internal function to calculate spot price of three pools.\n  /// @param encoding1 The encoding for the first pool.\n  /// @param encoding2 The encoding for the second pool.\n  /// @param encoding3 The encoding for the third pool.\n  /// @return price The spot price of the source, multiplied by 1e18.\n  function _readSpotPrice(\n    uint256 encoding1,\n    uint256 encoding2,\n    uint256 encoding3\n  ) private view returns (uint256 price) {\n    unchecked {\n      price = (_readSpotPrice(encoding1, encoding2) * _readSpotPrice(encoding3)) / PRECISION;\n    }\n  }\n}\n"},"contracts/price-oracle/interfaces/IPriceOracle.sol":{"content":"// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\ninterface IPriceOracle {\n  /**********\n   * Events *\n   **********/\n\n  /// @notice Emitted when the value of maximum price deviation is updated.\n  /// @param oldValue The value of the previous maximum price deviation.\n  /// @param newValue The value of the current maximum price deviation.\n  event UpdateMaxPriceDeviation(uint256 oldValue, uint256 newValue);\n\n  /*************************\n   * Public View Functions *\n   *************************/\n\n  /// @notice Return the oracle price with 18 decimal places.\n  /// @return anchorPrice The anchor price for this asset, multiplied by 1e18. It should be hard to manipulate,\n  ///         like time-weighted average price or chainlink spot price.\n  /// @return minPrice The minimum oracle price among all available price sources (including twap), multiplied by 1e18.\n  /// @return maxPrice The maximum oracle price among all available price sources (including twap), multiplied by 1e18.\n  function getPrice() external view returns (uint256 anchorPrice, uint256 minPrice, uint256 maxPrice);\n\n  /// @notice Return the oracle price for exchange with 18 decimal places.\n  function getExchangePrice() external view returns (uint256);\n\n  /// @notice Return the oracle price for liquidation with 18 decimal places.\n  function getLiquidatePrice() external view returns (uint256);\n\n  /// @notice Return the oracle price for redemption with 18 decimal places.\n  function getRedeemPrice() external view returns (uint256);\n}\n"},"contracts/price-oracle/interfaces/ISpotPriceOracle.sol":{"content":"// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\ninterface ISpotPriceOracle {\n  /// @notice Return spot price with 18 decimal places.\n  ///\n  /// @dev encoding for single route\n  /// |   8 bits  | 160 bits |  88  bits  |\n  /// | pool_type |   pool   | customized |\n  /// assume all base and quote token has no more than 18 decimals.\n  ///\n  /// + pool_type = 0: UniswapV2\n  ///   customized = |   1  bit   |   8  bits   |   8 bits   | ... |\n  ///                | base_index | base_scale | quote_scale | ... |\n  /// + pool_type = 1: UniswapV3\n  ///   customized = |   1  bit   |   8 bits   |   8  bits   | ... |\n  ///                | base_index | base_scale | quote_scale | ... |\n  /// + pool_type = 2: Balancer V2 Weighted\n  ///   customized = |   3  bit   |    3 bit    |   8 bits   |   8  bits   | ... |\n  ///                | base_index | quote_index | base_scale | quote_scale | ... |\n  /// + pool_type = 3: Balancer V2 Stable\n  ///   customized = |   3 bits   |   3  bits   | ... |\n  ///                | base_index | quote_index | ... |\n  /// + pool_type = 4: Curve Plain\n  ///   customized = | 3 bits |   3 bits   |   3  bits   |     1  bits     |  8 bits  | ... |  8 bits  | ... |\n  ///                | tokens | base_index | quote_index | has_amm_precise | scale[0] | ... | scale[n] | ... |\n  /// + pool_type = 5: Curve Plain with oracle\n  ///   customized = |   1  bit   |   1 bit   |... |\n  ///                | base_index | use_cache | ... |\n  /// + pool_type = 6: Curve Plain NG\n  ///   customized = |   3 bits   |   3  bits   |   1 bit   | ... |\n  ///                | base_index | quote_index | use_cache | ... |\n  /// + pool_type = 7: Curve Crypto\n  ///   customized = |   1  bit   | ... |\n  ///                | base_index | ... |\n  /// + pool_type = 8: Curve TriCrypto\n  ///   customized = |   2 bits   |   2  bits   | ... |\n  ///                | base_index | quote_index | ... |\n  /// + pool_type = 9: ERC4626\n  ///   customized = |       1  bit       | ... |\n  ///                | base_is_underlying | ... |\n  /// + pool_type = 10: ETHLSD, wstETH, weETH, ezETH\n  ///   customized = |    1 bit    | ... |\n  ///                | base_is_ETH | ... |\n  /// + pool_type = 11: BalancerV2CachedRate\n  ///   customized = |   3 bits   | ... |\n  ///                | base_index | ... |\n  /// + pool_type = 12: Aerodrome CL\n  ///   customized = |   1  bit   |   8 bits   |   8  bits   | ... |\n  ///                | base_index | base_scale | quote_scale | ... |\n  /// + pool_type = 13: PancakeV3\n  ///   customized = |   1  bit   |   8 bits   |   8  bits   | ... |\n  ///                | base_index | base_scale | quote_scale | ... |\n  ///\n  /// @param encoding The encoding of the price source.\n  /// @return spotPrice The spot price with 18 decimal places.\n  function getSpotPrice(uint256 encoding) external view returns (uint256 spotPrice);\n}\n\ninterface ISpotPriceOracleOwnable is ISpotPriceOracle {\n  /// @notice Update the reader for a specific pool type.\n  /// @param poolType The pool type.\n  /// @param reader The reader address.\n  function updateReader(uint256 poolType, address reader) external;\n}\n"}},"matchId":"48621951","creationMatch":"exact_match","runtimeMatch":"exact_match","verifiedAt":"2026-09-10T15:18:29Z","match":"exact_match","chainId":"56","address":"0x45dCDFcE8C0D163708Eaa47ab5e72280AF3eFa3E"}