MixedPriceOracleV4
Inherits: IOracleOperator
Title: MixedPriceOracleV4
Author: Merge Layers Inc.
Mixed price oracle using dual feeds and staleness checks
State Variables
PRICE_DELTA_EXP
Price delta exponent (basis points denominator)
uint256 public constant PRICE_DELTA_EXP = 1e5
STALENESS_PERIOD
Default staleness period applied to feeds
uint256 public immutable STALENESS_PERIOD
ROLES
Roles contract reference
IRoles public immutable ROLES
configs
Mapping of symbols to price configs
mapping(string symbol => PriceConfig config) public configs
stalenessPerSymbol
Mapping of symbols to custom staleness
mapping(string symbol => uint256 staleness) public stalenessPerSymbol
deltaPerSymbol
Mapping of symbols to custom price deltas
mapping(string symbol => uint256 delta) public deltaPerSymbol
maxPriceDelta
Maximum allowed delta in basis points for price comparison
uint256 public maxPriceDelta = 1.5e3
Functions
onlyRole
Modifier to check if the caller has specific role
modifier onlyRole(bytes32 role) ;
Parameters
| Name | Type | Description |
|---|---|---|
role | bytes32 | Role identifier to check |
constructor
Initializes the oracle with configs, roles, and staleness
constructor(string[] memory symbols_, PriceConfig[] memory configs_, address roles_, uint256 stalenessPeriod_) ;
Parameters
| Name | Type | Description |
|---|---|---|
symbols_ | string[] | Symbols being configured |
configs_ | PriceConfig[] | Price configs for symbols |
roles_ | address | Roles contract address |
stalenessPeriod_ | uint256 | Default staleness period |
setStaleness
Sets a custom staleness for a symbol
function setStaleness(string calldata symbol, uint256 val) external onlyRole(ROLES.GUARDIAN_ORACLE());
Parameters
| Name | Type | Description |
|---|---|---|
symbol | string | Symbol to update |
val | uint256 | New staleness value |
setConfig
Sets price configuration for a symbol
function setConfig(string calldata symbol, PriceConfig calldata config) external onlyRole(ROLES.GUARDIAN_ORACLE());
Parameters
| Name | Type | Description |
|---|---|---|
symbol | string | Symbol to configure |
config | PriceConfig | Price configuration |
setMaxPriceDelta
Sets maximum allowed price delta
function setMaxPriceDelta(uint256 _delta) external onlyRole(ROLES.GUARDIAN_ORACLE());
Parameters
| Name | Type | Description |
|---|---|---|
_delta | uint256 | New max delta in basis points |
setSymbolMaxPriceDelta
Sets maximum price delta for a specific symbol
function setSymbolMaxPriceDelta(uint256 _delta, string calldata _symbol)
external
onlyRole(ROLES.GUARDIAN_ORACLE());
Parameters
| Name | Type | Description |
|---|---|---|
_delta | uint256 | New delta in basis points |
_symbol | string | Symbol to update |
getPrice
Returns price for an mToken
function getPrice(address mToken) external view returns (uint256);
Parameters
| Name | Type | Description |
|---|---|---|
mToken | address | Address of the mToken |
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint256 | Price denominated in USD with 18 decimals |
getUnderlyingPrice
Returns underlying price for an mToken
function getUnderlyingPrice(address mToken) external view override returns (uint256);
Parameters
| Name | Type | Description |
|---|---|---|
mToken | address | Address of the mToken |
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint256 | Price denominated in USD with 18 decimals adjusted for underlying decimals |
_onlyRole
Ensures caller has specific role
function _onlyRole(bytes32 role) internal view;
Parameters
| Name | Type | Description |
|---|---|---|
role | bytes32 | Role identifier to check |
_getPriceUSD
Returns USD price for a symbol using dual feeds
function _getPriceUSD(string memory symbol) internal view returns (uint256);
Parameters
| Name | Type | Description |
|---|---|---|
symbol | string | Token symbol |
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint256 | USD price with 18 decimals |
_getApi3Price
Retrieves price and last update from API3 feed
function _getApi3Price(string memory symbol) internal view returns (uint256 price, uint256 lastUpdate);
Parameters
| Name | Type | Description |
|---|---|---|
symbol | string | Token symbol |
Returns
| Name | Type | Description |
|---|---|---|
price | uint256 | Price scaled to 18 decimals |
lastUpdate | uint256 | Timestamp of last update |
_geteOraclePrice
Retrieves price and last update from eOracle feed
function _geteOraclePrice(string memory symbol) internal view returns (uint256 price, uint256 lastUpdate);
Parameters
| Name | Type | Description |
|---|---|---|
symbol | string | Token symbol |
Returns
| Name | Type | Description |
|---|---|---|
price | uint256 | Price scaled to 18 decimals |
lastUpdate | uint256 | Timestamp of last update |
_isFresh
Checks if price data is fresh
function _isFresh(uint256 updatedAt, uint256 staleness) internal view returns (bool);
Parameters
| Name | Type | Description |
|---|---|---|
updatedAt | uint256 | Timestamp of last update |
staleness | uint256 | Allowed staleness threshold |
Returns
| Name | Type | Description |
|---|---|---|
<none> | bool | True if data is within staleness window |
_getStaleness
Returns staleness for a symbol or default
function _getStaleness(string memory symbol) internal view returns (uint256);
Parameters
| Name | Type | Description |
|---|---|---|
symbol | string | Token symbol |
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint256 | Staleness period in seconds |
_absDiff
Absolute difference between two int256 values
function _absDiff(int256 a, int256 b) internal pure returns (uint256);
Parameters
| Name | Type | Description |
|---|---|---|
a | int256 | First value |
b | int256 | Second value |
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint256 | Absolute difference as uint256 |
Events
ConfigSet
Emitted when a config is set for a symbol
event ConfigSet(string symbol, PriceConfig config);
Parameters
| Name | Type | Description |
|---|---|---|
symbol | string | Symbol being configured |
config | PriceConfig | Price config stored |
StalenessUpdated
Emitted when staleness is updated for a symbol
event StalenessUpdated(string symbol, uint256 val);
Parameters
| Name | Type | Description |
|---|---|---|
symbol | string | Symbol being updated |
val | uint256 | New staleness period |
PriceDeltaUpdated
Emitted when global price delta is updated
event PriceDeltaUpdated(uint256 oldVal, uint256 newVal);
Parameters
| Name | Type | Description |
|---|---|---|
oldVal | uint256 | Previous delta value |
newVal | uint256 | New delta value |
PriceSymbolDeltaUpdated
Emitted when symbol price delta is updated
event PriceSymbolDeltaUpdated(uint256 oldVal, uint256 newVal, string symbol);
Parameters
| Name | Type | Description |
|---|---|---|
oldVal | uint256 | Previous delta |
newVal | uint256 | New delta |
symbol | string | Symbol affected |
Errors
MixedPriceOracle_Unauthorized
Error thrown when caller is unauthorized
error MixedPriceOracle_Unauthorized();
MixedPriceOracle_ApiV3StalePrice
Error thrown when API3 feed price is stale
error MixedPriceOracle_ApiV3StalePrice();
MixedPriceOracle_eOracleStalePrice
Error thrown when eOracle feed price is stale
error MixedPriceOracle_eOracleStalePrice();
MixedPriceOracle_InvalidPrice
Error thrown when price returned is invalid
error MixedPriceOracle_InvalidPrice();
MixedPriceOracle_InvalidConfig
Error thrown when provided config is invalid
error MixedPriceOracle_InvalidConfig();
MixedPriceOracle_DeltaTooHigh
Error thrown when delta exceeds allowed max
error MixedPriceOracle_DeltaTooHigh();
MixedPriceOracle_MissingFeed
Error thrown when required feed is missing
error MixedPriceOracle_MissingFeed();
MixedPriceOracle_AddressNotValid
Error thrown when roles contract address is invalid
error MixedPriceOracle_AddressNotValid();
Structs
PriceConfig
struct PriceConfig {
address api3Feed;
address eOracleFeed;
string toSymbol;
uint256 underlyingDecimals;
}