OracleRegistry

Git Source

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

NameTypeDescription
_vaultTokenaddressCore vault token
_tokensaddress[]Tokens to register initially
_oracleFeedsaddress[]Corresponding oracle feeds
_paramRegisteraddressParameter registry contract
_defaultAdminaddressAdmin address for DEFAULT_ADMIN_ROLE
_priceUpdateRoleaddressAddress granted the PRICE_UPDATE_ROLE.
_oracleManageRoleaddressAddress 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

NameTypeDescription
_vaultTokenPriceuint256New vault token price
_isCutOffPriceboolWhether 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

NameTypeDescription
_tokenaddressToken address
_oracleaddressOracle 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

NameTypeDescription
_tokenaddressToken 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

NameTypeDescription
_tokenaddressToken address
_oracleaddressNew 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

NameTypeDescription
_tokenaddressToken address to query

Returns

NameTypeDescription
oracle_addressAddress 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

NameTypeDescription
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

NameTypeDescription
timestamp_uint256Timestamp 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

NameTypeDescription
timestamp_uint256Timestamp 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

NameTypeDescription
_tokenaddressToken address

Returns

NameTypeDescription
price_uint256Latest 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

NameTypeDescription
_tokenaddressToken address
_indexuint256Index (0 = latest)

Returns

NameTypeDescription
price_uint256Historical 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

NameTypeDescription
_tokenaddressToken address
_indexuint256Index (0 = latest)

Returns

NameTypeDescription
price_uint256Historical 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

NameTypeDescription
_indexuint256Index (0 = latest)

Returns

NameTypeDescription
price_uint256Vault 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

NameTypeDescription
_indexuint256Index (0 = latest)

Returns

NameTypeDescription
price_uint256Vault 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

NameTypeDescription
_tokenaddressToken address
_indexuint256Index of cut-off update (0 = latest)

Returns

NameTypeDescription
price_uint256The 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

NameTypeDescription
_indexuint256Index of cut-off update (0 = latest)

Returns

NameTypeDescription
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

NameTypeDescription
_indexuint256Index of cut-off update (0 = latest)

Returns

NameTypeDescription
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

NameTypeDescription
_indexuint256Index of cut-off update (0 = latest)

Returns

NameTypeDescription
price_uint256Vault 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

NameTypeDescription
_indexuint256Index of cut-off update (0 = latest)

Returns

NameTypeDescription
price_uint256Vault token price

_getPrice

Retrieves the historical price of a token at a given index

  • Uses commonPriceUpdatedAt array to find the timestamp of the requested price.
  • If _checkValidity is true, reverts if the price is older than paramRegister.getPriceValidityDuration().
  • Reverts if _index is out of bounds.
  • Returns the stored price from prices[token][updatedAt].*
function _getPrice(address _token, uint256 _index, bool _checkValidity) internal view returns (uint256);

Parameters

NameTypeDescription
_tokenaddressToken address to query
_indexuint256Index of the historical price (0 = latest)
_checkValidityboolWhether to check if the price is still valid

Returns

NameTypeDescription
<none>uint256price_ Price of the token at the given index