|
3 | 3 | // (c) Gearbox Foundation, 2024. |
4 | 4 | pragma solidity ^0.8.23; |
5 | 5 |
|
| 6 | +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
| 7 | +import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; |
| 8 | + |
| 9 | +import {SanityCheckTrait} from "@gearbox-protocol/core-v3/contracts/traits/SanityCheckTrait.sol"; |
| 10 | +import {PriceFeedValidationTrait} from "@gearbox-protocol/core-v3/contracts/traits/PriceFeedValidationTrait.sol"; |
| 11 | +import {IPriceFeed} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IPriceFeed.sol"; |
| 12 | + |
6 | 13 | import {IPriceFeedStore} from "../interfaces/IPriceFeedStore.sol"; |
| 14 | +import {AP_PRICE_FEED_STORE, AP_INSTANCE_MANAGER_PROXY, NO_VERSION_CONTROL} from "../libraries/ContractLiterals.sol"; |
| 15 | +import {IAddressProvider} from "../interfaces/IAddressProvider.sol"; |
| 16 | +import {PriceFeedInfo} from "../interfaces/Types.sol"; |
| 17 | + |
| 18 | +contract PriceFeedStore is Ownable, SanityCheckTrait, PriceFeedValidationTrait, IPriceFeedStore { |
| 19 | + using EnumerableSet for EnumerableSet.AddressSet; |
| 20 | + |
| 21 | + // |
| 22 | + // CONSTANTS |
| 23 | + // |
| 24 | + |
| 25 | + /// @notice Meta info about contract type & version |
| 26 | + uint256 public constant override version = 3_10; |
| 27 | + bytes32 public constant override contractType = AP_PRICE_FEED_STORE; |
| 28 | + |
| 29 | + // |
| 30 | + // VARIABLES |
| 31 | + // |
| 32 | + |
| 33 | + /// @dev Set of all known price feeds |
| 34 | + EnumerableSet.AddressSet internal _knownPriceFeeds; |
| 35 | + |
| 36 | + /// @dev Set of all known price feeds |
| 37 | + EnumerableSet.AddressSet internal _knownTokens; |
| 38 | + |
| 39 | + /// @dev Mapping from token address to its set of allowed price feeds |
| 40 | + mapping(address => EnumerableSet.AddressSet) internal _allowedPriceFeeds; |
| 41 | + |
| 42 | + /// @notice Mapping from price feed address to its data |
| 43 | + mapping(address => PriceFeedInfo) public priceFeedInfo; |
| 44 | + |
| 45 | + constructor(address _addressProvider) { |
| 46 | + address instanceManager = |
| 47 | + IAddressProvider(_addressProvider).getAddressOrRevert(AP_INSTANCE_MANAGER_PROXY, NO_VERSION_CONTROL); |
| 48 | + _transferOwnership(instanceManager); |
| 49 | + } |
| 50 | + |
| 51 | + /// @notice Returns the list of price feeds available for a token |
| 52 | + function getPriceFeeds(address token) external view returns (address[] memory) { |
| 53 | + return _allowedPriceFeeds[token].values(); |
| 54 | + } |
| 55 | + |
| 56 | + /// @notice Returns whether a price feed is allowed to be used for a token |
| 57 | + function isAllowedPriceFeed(address token, address priceFeed) external view returns (bool) { |
| 58 | + return _allowedPriceFeeds[token].contains(priceFeed); |
| 59 | + } |
| 60 | + |
| 61 | + /// @notice Returns the staleness period for a price feed |
| 62 | + function getStalenessPeriod(address priceFeed) external view returns (uint32) { |
| 63 | + return priceFeedInfo[priceFeed].stalenessPeriod; |
| 64 | + } |
| 65 | + |
| 66 | + function getKnownTokens() external view returns (address[] memory) { |
| 67 | + return _knownTokens.values(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @notice Adds a new price feed |
| 72 | + * @param priceFeed The address of the new price feed |
| 73 | + * @param stalenessPeriod Staleness period of the new price feed |
| 74 | + * @dev Reverts if the price feed's result is stale based on the staleness period |
| 75 | + */ |
| 76 | + function addPriceFeed(address priceFeed, uint32 stalenessPeriod) external onlyOwner nonZeroAddress(priceFeed) { |
| 77 | + if (_knownPriceFeeds.contains(priceFeed)) revert PriceFeedAlreadyAddedException(priceFeed); |
| 78 | + |
| 79 | + _validatePriceFeed(priceFeed, stalenessPeriod); |
| 80 | + |
| 81 | + bytes32 priceFeedType; |
| 82 | + uint256 priceFeedVersion; |
| 83 | + |
| 84 | + try IPriceFeed(priceFeed).contractType() returns (bytes32 _cType) { |
| 85 | + priceFeedType = _cType; |
| 86 | + priceFeedVersion = IPriceFeed(priceFeed).version(); |
| 87 | + } catch { |
| 88 | + priceFeedType = "PF_EXTERNAL_ORACLE"; |
| 89 | + priceFeedVersion = 0; |
| 90 | + } |
| 91 | + |
| 92 | + _knownPriceFeeds.add(priceFeed); |
| 93 | + priceFeedInfo[priceFeed].author = msg.sender; |
| 94 | + priceFeedInfo[priceFeed].priceFeedType = priceFeedType; |
| 95 | + priceFeedInfo[priceFeed].stalenessPeriod = stalenessPeriod; |
| 96 | + priceFeedInfo[priceFeed].version = priceFeedVersion; |
| 97 | + |
| 98 | + emit AddPriceFeed(priceFeed, stalenessPeriod); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @notice Sets the staleness period for an existing price feed |
| 103 | + * @param priceFeed The address of the price feed |
| 104 | + * @param stalenessPeriod New staleness period for the price feed |
| 105 | + * @dev Reverts if the price feed is not added to the global list |
| 106 | + */ |
| 107 | + function setStalenessPeriod(address priceFeed, uint32 stalenessPeriod) |
| 108 | + external |
| 109 | + onlyOwner |
| 110 | + nonZeroAddress(priceFeed) |
| 111 | + { |
| 112 | + if (!_knownPriceFeeds.contains(priceFeed)) revert PriceFeedNotKnownException(priceFeed); |
| 113 | + uint32 oldStalenessPeriod = priceFeedInfo[priceFeed].stalenessPeriod; |
| 114 | + |
| 115 | + if (stalenessPeriod != oldStalenessPeriod) { |
| 116 | + _validatePriceFeed(priceFeed, stalenessPeriod); |
| 117 | + priceFeedInfo[priceFeed].stalenessPeriod = stalenessPeriod; |
| 118 | + emit SetStalenessPeriod(priceFeed, stalenessPeriod); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @notice Allows a price feed for use with a particular token |
| 124 | + * @param token Address of the token |
| 125 | + * @param priceFeed Address of the price feed |
| 126 | + * @dev Reverts if the price feed is not added to the global list |
| 127 | + */ |
| 128 | + function allowPriceFeed(address token, address priceFeed) external onlyOwner nonZeroAddress(token) { |
| 129 | + if (!_knownPriceFeeds.contains(priceFeed)) revert PriceFeedNotKnownException(priceFeed); |
| 130 | + |
| 131 | + _allowedPriceFeeds[token].add(priceFeed); |
| 132 | + |
| 133 | + emit AllowPriceFeed(token, priceFeed); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @notice Forbids a price feed for use with a particular token |
| 138 | + * @param token Address of the token |
| 139 | + * @param priceFeed Address of the price feed |
| 140 | + * @dev Reverts if the price feed is not added to the global list or the per-token list |
| 141 | + */ |
| 142 | + function forbidPriceFeed(address token, address priceFeed) external onlyOwner nonZeroAddress(token) { |
| 143 | + if (!_knownPriceFeeds.contains(priceFeed)) revert PriceFeedNotKnownException(priceFeed); |
| 144 | + if (!_allowedPriceFeeds[token].contains(priceFeed)) revert PriceFeedIsNotAllowedException(token, priceFeed); |
| 145 | + |
| 146 | + _allowedPriceFeeds[token].remove(priceFeed); |
7 | 147 |
|
8 | | -abstract contract PriceFeedStore is IPriceFeedStore {} |
| 148 | + emit ForbidPriceFeed(token, priceFeed); |
| 149 | + } |
| 150 | +} |
0 commit comments