OracleRegistry
Inherits: AccessControl, Constants, IOracleRegistry
Author: luoyhang003
Manages prices for vaultToken and other registered tokens, including updates from oracles and cut-off prices.
Uses OpenZeppelin AccessControl for role-based permissioning
Stores historical prices and supports both common and cut-off price updates
Performs deviation checks to prevent extreme price manipulations
State Variables
PRICE_UPDATE_ROLE
Role allowed to update prices
Only addresses with this role can call updatePrices
Calculated as keccak256("PRICE_UPDATE_ROLE").
bytes32 public constant PRICE_UPDATE_ROLE = keccak256("PRICE_UPDATE_ROLE");
ORACLE_MANAGE_ROLE
Role allowed to manage oracles
Only addresses with this role can call addTokenOracle, removeTokenOracle, updateTokenOracle
Calculated as keccak256("ORACLE_MANAGE_ROLE").
bytes32 public constant ORACLE_MANAGE_ROLE = keccak256("ORACLE_MANAGE_ROLE");
paramRegister
Parameter registry contract
Used to fetch tolerances, price update intervals, and validity durations
IParamRegistry public paramRegister;
vaultToken
Vault token address
Core token whose price is specially managed and checked against exchange rate tolerances
address public immutable vaultToken;
tokens
List of all registered tokens
Used for iteration during price updates and retrieving historical prices
address[] public tokens;
commonPriceUpdatedAt
Timestamps of common price updates
Index corresponds to historical versions of price updates
uint256[] public commonPriceUpdatedAt;
cutOffPriceUpdateAt
Timestamps of cut-off price updates
Only updated when _isCutOffPrice is true during updatePrices
uint256[] public cutOffPriceUpdateAt;
prices
Token => Timestamp => Price mapping
Stores all historical prices for each token
mapping(address => mapping(uint256 => uint256)) public prices;
_oracles
Token => OracleFeed mapping
Internal mapping to track which oracle feed is used for each token
mapping(address => IOracleFeed) private _oracles;
Functions
constructor
Initializes OracleRegistry
Performs zero-address checks and ensures token/oracle arrays have the same length
Initializes initial prices and emits PriceUpdated events
constructor(
address _vaultToken,
address[] memory _tokens,
address[] memory _oracleFeeds,
address _paramRegister,
address _defaultAdmin,
address _priceUpdateRole,
address _oracleManageRole
);
Parameters
| Name | Type | Description |
|---|---|---|
_vaultToken | address | Core vault token |
_tokens | address[] | Tokens to register initially |
_oracleFeeds | address[] | Corresponding oracle feeds |
_paramRegister | address | Parameter registry contract |
_defaultAdmin | address | Admin address for DEFAULT_ADMIN_ROLE |
_priceUpdateRole | address | Address granted the PRICE_UPDATE_ROLE. |
_oracleManageRole | address | Address granted the ORACLE_MANAGE_ROLE. |
updatePrices
Updates prices for vault token and other registered tokens
Reverts if price deviates beyond tolerances or updates are too frequent
Updates all registered tokens based on their oracle feeds
function updatePrices(uint256 _vaultTokenPrice, bool _isCutOffPrice) external onlyRole(PRICE_UPDATE_ROLE);
Parameters
| Name | Type | Description |
|---|---|---|
_vaultTokenPrice | uint256 | New vault token price |
_isCutOffPrice | bool | Whether the update is a cut-off price |
addTokenOracle
Adds a new token and its oracle feed
Only callable by ORACLE_MANAGE_ROLE
Reverts if token already exists or any address is zero
function addTokenOracle(address _token, address _oracle) external onlyRole(ORACLE_MANAGE_ROLE);
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | Token address |
_oracle | address | Oracle feed address |
removeTokenOracle
Removes a token and its oracle feed
Uses swap-and-pop for efficient removal from array
Only callable by ORACLE_MANAGE_ROLE
function removeTokenOracle(address _token) external onlyRole(ORACLE_MANAGE_ROLE);
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | Token address to remove |
updateTokenOracle
Updates an existing token’s oracle feed
Only callable by ORACLE_MANAGE_ROLE
Reverts if token does not exist or any address is zero
function updateTokenOracle(address _token, address _oracle) external onlyRole(ORACLE_MANAGE_ROLE);
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | Token address |
_oracle | address | New oracle feed address |
getOracle
Returns the oracle feed address for a given token
Returns zero address if token is not registered
function getOracle(address _token) external view returns (address oracle_);
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | Token address to query |
Returns
| Name | Type | Description |
|---|---|---|
oracle_ | address | Address of the oracle feed |
getTokens
Returns all registered token addresses
Useful for iterating over all tokens to fetch historical prices
function getTokens() external view returns (address[] memory tokens_);
Returns
| Name | Type | Description |
|---|---|---|
tokens_ | address[] | Array of token addresses |
latestPricesUpdatedAt
Returns the timestamp of the latest common price update
Reverts if no updates exist
function latestPricesUpdatedAt() public view returns (uint256 timestamp_);
Returns
| Name | Type | Description |
|---|---|---|
timestamp_ | uint256 | Timestamp of latest update |
latestCutOffPriceUpdatedAt
Returns the timestamp of the latest cut-off price update
Reverts if no cut-off updates exist
function latestCutOffPriceUpdatedAt() external view returns (uint256 timestamp_);
Returns
| Name | Type | Description |
|---|---|---|
timestamp_ | uint256 | Timestamp of latest cut-off update |
peek
Returns the latest price of a token from its oracle
Performs deviation check to prevent extreme values
function peek(address _token) public view returns (uint256 price_);
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | Token address |
Returns
| Name | Type | Description |
|---|---|---|
price_ | uint256 | Latest price from oracle |
peekAt
Returns historical price for a token at a given index
Checks price validity (time not expired)
function peekAt(address _token, uint256 _index) public view returns (uint256 price_);
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | Token address |
_index | uint256 | Index (0 = latest) |
Returns
| Name | Type | Description |
|---|---|---|
price_ | uint256 | Historical price |
onlyPeekAt
Returns historical price without validity check
Useful for auditing or internal calculations
function onlyPeekAt(address _token, uint256 _index) public view returns (uint256 price_);
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | Token address |
_index | uint256 | Index (0 = latest) |
Returns
| Name | Type | Description |
|---|---|---|
price_ | uint256 | Historical price |
getVaultTokenPrice
Returns the historical price of the vault token at a given index
Checks price validity
function getVaultTokenPrice(uint256 _index) public view returns (uint256 price_);
Parameters
| Name | Type | Description |
|---|---|---|
_index | uint256 | Index (0 = latest) |
Returns
| Name | Type | Description |
|---|---|---|
price_ | uint256 | Vault token price |
onlyGetVaultTokenPrice
Returns the historical price of the vault token without validity check
Useful for internal calculations
function onlyGetVaultTokenPrice(uint256 _index) public view returns (uint256 price_);
Parameters
| Name | Type | Description |
|---|---|---|
_index | uint256 | Index (0 = latest) |
Returns
| Name | Type | Description |
|---|---|---|
price_ | uint256 | Vault token price |
getCutOffPrice
Returns token price at a cut-off update
Reverts if price is expired or index out of bounds
function getCutOffPrice(address _token, uint256 _index) external view returns (uint256 price_);
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | Token address |
_index | uint256 | Index of cut-off update (0 = latest) |
Returns
| Name | Type | Description |
|---|---|---|
price_ | uint256 | The cut-off price for the token |
getCutOffPrices
Returns token addresses and prices at a cut-off update
Reverts if price is expired or index out of bounds
function getCutOffPrices(uint256 _index) external view returns (address[] memory tokens_, uint256[] memory prices_);
Parameters
| Name | Type | Description |
|---|---|---|
_index | uint256 | Index of cut-off update (0 = latest) |
Returns
| Name | Type | Description |
|---|---|---|
tokens_ | address[] | Array of token addresses |
prices_ | uint256[] | Array of prices for each token |
onlyGetCutOffPrices
Same as getCutOffPrices but does not check validity
Useful for internal auditing or calculation
function onlyGetCutOffPrices(uint256 _index)
external
view
returns (address[] memory tokens_, uint256[] memory prices_);
Parameters
| Name | Type | Description |
|---|---|---|
_index | uint256 | Index of cut-off update (0 = latest) |
Returns
| Name | Type | Description |
|---|---|---|
tokens_ | address[] | Array of token addresses |
prices_ | uint256[] | Array of prices for each token |
getVaultTokenCutOffPrice
Returns the vault token cut-off price at a given index
Checks validity
function getVaultTokenCutOffPrice(uint256 _index) external view returns (uint256 price_);
Parameters
| Name | Type | Description |
|---|---|---|
_index | uint256 | Index of cut-off update (0 = latest) |
Returns
| Name | Type | Description |
|---|---|---|
price_ | uint256 | Vault token price |
onlyGetVaultTokenCutOffPrice
Returns the vault token cut-off price without validity check
Useful for internal calculations
function onlyGetVaultTokenCutOffPrice(uint256 _index) external view returns (uint256 price_);
Parameters
| Name | Type | Description |
|---|---|---|
_index | uint256 | Index of cut-off update (0 = latest) |
Returns
| Name | Type | Description |
|---|---|---|
price_ | uint256 | Vault token price |
_getPrice
Retrieves the historical price of a token at a given index
- Uses
commonPriceUpdatedAtarray to find the timestamp of the requested price. - If
_checkValidityis true, reverts if the price is older thanparamRegister.getPriceValidityDuration(). - Reverts if
_indexis out of bounds. - Returns the stored price from
prices[token][updatedAt].*
function _getPrice(address _token, uint256 _index, bool _checkValidity) internal view returns (uint256);
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | Token address to query |
_index | uint256 | Index of the historical price (0 = latest) |
_checkValidity | bool | Whether to check if the price is still valid |
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint256 | price_ Price of the token at the given index |