MixedPriceOracleV4

Git Source

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

NameTypeDescription
rolebytes32Role identifier to check

constructor

Initializes the oracle with configs, roles, and staleness

constructor(string[] memory symbols_, PriceConfig[] memory configs_, address roles_, uint256 stalenessPeriod_) ;

Parameters

NameTypeDescription
symbols_string[]Symbols being configured
configs_PriceConfig[]Price configs for symbols
roles_addressRoles contract address
stalenessPeriod_uint256Default staleness period

setStaleness

Sets a custom staleness for a symbol

function setStaleness(string calldata symbol, uint256 val) external onlyRole(ROLES.GUARDIAN_ORACLE());

Parameters

NameTypeDescription
symbolstringSymbol to update
valuint256New staleness value

setConfig

Sets price configuration for a symbol

function setConfig(string calldata symbol, PriceConfig calldata config) external onlyRole(ROLES.GUARDIAN_ORACLE());

Parameters

NameTypeDescription
symbolstringSymbol to configure
configPriceConfigPrice configuration

setMaxPriceDelta

Sets maximum allowed price delta

function setMaxPriceDelta(uint256 _delta) external onlyRole(ROLES.GUARDIAN_ORACLE());

Parameters

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

NameTypeDescription
_deltauint256New delta in basis points
_symbolstringSymbol to update

getPrice

Returns price for an mToken

function getPrice(address mToken) external view returns (uint256);

Parameters

NameTypeDescription
mTokenaddressAddress of the mToken

Returns

NameTypeDescription
<none>uint256Price denominated in USD with 18 decimals

getUnderlyingPrice

Returns underlying price for an mToken

function getUnderlyingPrice(address mToken) external view override returns (uint256);

Parameters

NameTypeDescription
mTokenaddressAddress of the mToken

Returns

NameTypeDescription
<none>uint256Price denominated in USD with 18 decimals adjusted for underlying decimals

_onlyRole

Ensures caller has specific role

function _onlyRole(bytes32 role) internal view;

Parameters

NameTypeDescription
rolebytes32Role identifier to check

_getPriceUSD

Returns USD price for a symbol using dual feeds

function _getPriceUSD(string memory symbol) internal view returns (uint256);

Parameters

NameTypeDescription
symbolstringToken symbol

Returns

NameTypeDescription
<none>uint256USD 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

NameTypeDescription
symbolstringToken symbol

Returns

NameTypeDescription
priceuint256Price scaled to 18 decimals
lastUpdateuint256Timestamp 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

NameTypeDescription
symbolstringToken symbol

Returns

NameTypeDescription
priceuint256Price scaled to 18 decimals
lastUpdateuint256Timestamp of last update

_isFresh

Checks if price data is fresh

function _isFresh(uint256 updatedAt, uint256 staleness) internal view returns (bool);

Parameters

NameTypeDescription
updatedAtuint256Timestamp of last update
stalenessuint256Allowed staleness threshold

Returns

NameTypeDescription
<none>boolTrue if data is within staleness window

_getStaleness

Returns staleness for a symbol or default

function _getStaleness(string memory symbol) internal view returns (uint256);

Parameters

NameTypeDescription
symbolstringToken symbol

Returns

NameTypeDescription
<none>uint256Staleness period in seconds

_absDiff

Absolute difference between two int256 values

function _absDiff(int256 a, int256 b) internal pure returns (uint256);

Parameters

NameTypeDescription
aint256First value
bint256Second value

Returns

NameTypeDescription
<none>uint256Absolute difference as uint256

Events

ConfigSet

Emitted when a config is set for a symbol

event ConfigSet(string symbol, PriceConfig config);

Parameters

NameTypeDescription
symbolstringSymbol being configured
configPriceConfigPrice config stored

StalenessUpdated

Emitted when staleness is updated for a symbol

event StalenessUpdated(string symbol, uint256 val);

Parameters

NameTypeDescription
symbolstringSymbol being updated
valuint256New staleness period

PriceDeltaUpdated

Emitted when global price delta is updated

event PriceDeltaUpdated(uint256 oldVal, uint256 newVal);

Parameters

NameTypeDescription
oldValuint256Previous delta value
newValuint256New delta value

PriceSymbolDeltaUpdated

Emitted when symbol price delta is updated

event PriceSymbolDeltaUpdated(uint256 oldVal, uint256 newVal, string symbol);

Parameters

NameTypeDescription
oldValuint256Previous delta
newValuint256New delta
symbolstringSymbol 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;
}