{"sources":{"src/feeds/ChainlinkBasePriceFeed.sol":{"content":"// SPDX-License-Identifier: MIT\npragma solidity ^0.8.20;\n\nimport \"src/interfaces/IChainlinkFeed.sol\";\n\ninterface IAggregator {\n    function maxAnswer() external view returns (int192);\n\n    function minAnswer() external view returns (int192);\n}\n\n// Standard feed component for FiRM price feeds\n/// @dev Always return the feed price in 18 decimals\ncontract ChainlinkBasePriceFeed {\n    IChainlinkFeed public immutable assetToUsd;\n    IChainlinkFeed public immutable assetToUsdFallback;\n    uint8 public immutable assetToUsdDecimals;\n    uint8 public immutable assetToUsdFallbackDecimals;\n    string public description;\n\n    address public owner;\n    address public pendingOwner;\n\n    uint256 public assetToUsdHeartbeat;\n\n    modifier onlyOwner() {\n        require(msg.sender == owner, \"Only owner\");\n        _;\n    }\n\n    modifier onlyPendingOwner() {\n        require(msg.sender == pendingOwner, \"Only pending owner\");\n        _;\n    }\n\n    constructor(\n        address _owner,\n        address _assetToUsd,\n        address _assetToUsdFallback,\n        uint256 _assetToUsdHeartbeat\n    ) {\n        owner = _owner;\n        assetToUsd = IChainlinkFeed(_assetToUsd);\n        assetToUsdFallback = IChainlinkFeed(_assetToUsdFallback);\n        assetToUsdHeartbeat = _assetToUsdHeartbeat;\n        assetToUsdDecimals = IChainlinkFeed(_assetToUsd).decimals();\n        uint8 fallbackDecimals = 0;\n        if (address(assetToUsdFallback) != address(0)) {\n            fallbackDecimals = assetToUsdFallback.decimals();\n        }\n        assetToUsdFallbackDecimals = fallbackDecimals;\n        description = assetToUsd.description();\n    }\n\n    /**\n     * @notice Retrieves the latest round data for the asset token price feed\n     * @return roundId The round ID of the Chainlink price feed for the feed with the lowest updatedAt feed\n     * @return usdPrice The latest asset price in USD with 18 decimals\n     * @return startedAt The timestamp when the latest round of Chainlink price feed started of the lowest last updatedAt feed\n     * @return updatedAt The lowest timestamp when either of the latest round of Chainlink price feed was updated\n     * @return answeredInRound The round ID in which the answer was computed of the lowest updatedAt feed\n     */\n    function latestRoundData()\n        public\n        view\n        returns (\n            uint80 roundId,\n            int256 usdPrice,\n            uint256 startedAt,\n            uint256 updatedAt,\n            uint80 answeredInRound\n        )\n    {\n        (roundId, usdPrice, startedAt, updatedAt, answeredInRound) = assetToUsd\n            .latestRoundData();\n\n        if (isPriceStale(usdPrice, updatedAt)) {\n            if (hasFallback()) {\n                (\n                    roundId,\n                    usdPrice,\n                    startedAt,\n                    updatedAt,\n                    answeredInRound\n                ) = assetToUsdFallback.latestRoundData();\n                usdPrice = normalizePrice(usdPrice, assetToUsdFallbackDecimals);\n            } else {\n                usdPrice = normalizePrice(usdPrice, assetToUsdDecimals);\n                updatedAt = 0;\n            }\n        } else {\n            usdPrice = normalizePrice(usdPrice, assetToUsdDecimals);\n        }\n    }\n\n    /**\n     * @notice Returns the latest price only\n     * @dev Unlike chainlink oracles, the latestAnswer will always be the same as in the latestRoundData\n     * @return int256 Returns the last finalized price of the chainlink oracle\n     */\n    function latestAnswer() external view returns (int256) {\n        (, int256 latestPrice, , , ) = latestRoundData();\n        return latestPrice;\n    }\n\n    /**\n     * @notice Checks if a given price is out of the boundaries defined in the Chainlink aggregator.\n     * @param price The price to be checked.\n     * @param feed The Chainlink feed to retrieve the boundary information from.\n     * @return bool Returns `true` if the price is out of bounds, otherwise `false`.\n     */\n    function isPriceOutOfBounds(\n        int price,\n        IChainlinkFeed feed\n    ) public view returns (bool) {\n        IAggregator aggregator = IAggregator(feed.aggregator());\n        int192 max = aggregator.maxAnswer();\n        int192 min = aggregator.minAnswer();\n        return (max <= price || min >= price);\n    }\n\n    function isPriceStale(\n        int256 price,\n        uint256 updatedAt\n    ) public view returns (bool) {\n        bool stalePrice = updatedAt + assetToUsdHeartbeat < block.timestamp;\n        return stalePrice || isPriceOutOfBounds(price, assetToUsd);\n    }\n\n    function hasFallback() public view returns (bool) {\n        return address(assetToUsdFallback) != address(0);\n    }\n\n    function setHeartbeat(uint256 newHeartbeat) public onlyOwner {\n        assetToUsdHeartbeat = newHeartbeat;\n    }\n\n    function setPendingOwner(address newPendingOwner) public onlyOwner {\n        pendingOwner = newPendingOwner;\n    }\n\n    function acceptOwner() public onlyPendingOwner {\n        owner = pendingOwner;\n        pendingOwner = address(0);\n    }\n\n    function decimals() public pure returns (uint8) {\n        return 18;\n    }\n\n    function normalizePrice(\n        int256 price,\n        uint8 feedDecimals\n    ) public pure returns (int256) {\n        if (feedDecimals > 18) {\n            return price / int(10 ** (feedDecimals - 18));\n        } else if (feedDecimals < 18) {\n            return price * int(10 ** (18 - feedDecimals));\n        }\n        return price;\n    }\n}\n"},"src/interfaces/IChainlinkFeed.sol":{"content":"pragma solidity ^0.8.13;\n\ninterface IChainlinkFeed {\n    function aggregator() external view returns (address aggregator);\n\n    function decimals() external view returns (uint8 decimals);\n\n    function latestRoundData()\n        external\n        view\n        returns (\n            uint80 roundId,\n            int256 crvUsdPrice,\n            uint256 startedAt,\n            uint256 updatedAt,\n            uint80 answeredInRound\n        );\n\n    function latestAnswer() external view returns (int256 price);\n\n    function description() external view returns (string memory description);\n}\n\ninterface IChainlinkBasePriceFeed is IChainlinkFeed {\n    function assetToUsd() external view returns (IChainlinkFeed);\n\n    function assetToUsdFallback() external view returns (IChainlinkFeed);\n\n    function assetToUsdHeartbeat() external view returns (uint256 heartbeat);\n}\n"}},"matchId":"6193232","creationMatch":"exact_match","runtimeMatch":"exact_match","verifiedAt":"2025-03-07T05:56:56Z","match":"exact_match","chainId":"1","address":"0x22390B88C53D1631f673b8Dcd91860267137b2c8"}