CommonOracleMiddleware

Git Source

Inherits: ICommonOracleMiddleware, AccessControlDefaultAdminRules, ChainlinkOracle, PythOracle

This contract serves as a common base that must be implemented by other middleware contracts.

State Variables

ADMIN_ROLE

Gets the admin role's signature.

bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");

MIDDLEWARE_DECIMALS

The number of decimals for the returned price.

uint8 internal constant MIDDLEWARE_DECIMALS = 18;

_validationDelay

The delay (in seconds) between the moment an action is initiated and the timestamp of the price data used to validate that action.

uint256 internal _validationDelay = 24 seconds;

_lowLatencyDelay

The amount of time during which a low latency oracle price validation is available.

This value should be greater than or equal to _lowLatencyValidatorDeadline of the USDN protocol.

uint16 internal _lowLatencyDelay = 20 minutes;

Functions

constructor

constructor(address pythContract, bytes32 pythFeedId, address chainlinkPriceFeed, uint256 chainlinkTimeElapsedLimit)
    PythOracle(pythContract, pythFeedId)
    ChainlinkOracle(chainlinkPriceFeed, chainlinkTimeElapsedLimit)
    AccessControlDefaultAdminRules(0, msg.sender);

Parameters

NameTypeDescription
pythContractaddressAddress of the Pyth contract.
pythFeedIdbytes32The Pyth price feed ID for the asset.
chainlinkPriceFeedaddressAddress of the Chainlink price feed.
chainlinkTimeElapsedLimituint256The duration after which a Chainlink price is considered stale.

getDecimals

Gets the number of decimals for the price.

function getDecimals() external pure returns (uint8 decimals_);

Returns

NameTypeDescription
decimals_uint8The number of decimals.

getValidationDelay

Gets the required delay (in seconds) between the moment an action is initiated and the timestamp of the price data used to validate that action.

function getValidationDelay() external view returns (uint256 delay_);

Returns

NameTypeDescription
delay_uint256The validation delay.

getLowLatencyDelay

Gets The maximum amount of time (in seconds) after initiation during which a low-latency price oracle can be used for validation.

function getLowLatencyDelay() external view returns (uint16 delay_);

Returns

NameTypeDescription
delay_uint16The maximum delay for low-latency validation.

validationCost

Returns the cost of one price validation for the given action (in native token).

function validationCost(bytes calldata data, Types.ProtocolAction) public view virtual returns (uint256 result_);

Parameters

NameTypeDescription
databytesPrice data for which to get the fee.
<none>Types.ProtocolAction

Returns

NameTypeDescription
result_uint256cost_ The cost of one price validation (in native token).

parseAndValidatePrice

Parse and validate data and returns the corresponding price data.

The data format is specific to the middleware and is simply forwarded from the user transaction's calldata. A fee amounting to exactly validationCost (with the same data and action) must be sent or the transaction will revert.

function parseAndValidatePrice(bytes32, uint128 targetTimestamp, Types.ProtocolAction action, bytes calldata data)
    public
    payable
    virtual
    returns (PriceInfo memory price_);

Parameters

NameTypeDescription
<none>bytes32
targetTimestampuint128The target timestamp for validating the price data. For validation actions, this is the timestamp of the initiation.
actionTypes.ProtocolActionType of action for which the price is requested. The middleware may use this to alter the validation of the price or the returned price.
databytesThe data to be used to communicate with oracles, the format varies from middleware to middleware and can be different depending on the action.

Returns

NameTypeDescription
price_PriceInforesult_ The price and timestamp as PriceInfo.

setValidationDelay

Sets the validation delay (in seconds) between an action timestamp and the price data timestamp used to validate that action.

function setValidationDelay(uint256 newValidationDelay) external onlyRole(ADMIN_ROLE);

Parameters

NameTypeDescription
newValidationDelayuint256The new validation delay.

setChainlinkTimeElapsedLimit

Sets the elapsed time tolerated before we consider the price from Chainlink invalid.

function setChainlinkTimeElapsedLimit(uint256 newTimeElapsedLimit) external onlyRole(ADMIN_ROLE);

Parameters

NameTypeDescription
newTimeElapsedLimituint256The new time elapsed limit.

setPythRecentPriceDelay

Sets the amount of time after which we do not consider a price as recent.

function setPythRecentPriceDelay(uint64 newDelay) external onlyRole(ADMIN_ROLE);

Parameters

NameTypeDescription
newDelayuint64The maximum age of a price to be considered recent.

setLowLatencyDelay

Sets the new low latency delay.

function setLowLatencyDelay(uint16 newLowLatencyDelay, IUsdnProtocol usdnProtocol) external onlyRole(ADMIN_ROLE);

Parameters

NameTypeDescription
newLowLatencyDelayuint16The new low latency delay.
usdnProtocolIUsdnProtocolThe address of the USDN protocol.

withdrawEther

Withdraws the ether balance of this contract.

This contract can receive funds but is not designed to hold them. So this function can be used if there's an error and funds remain after a call.

function withdrawEther(address to) external onlyRole(ADMIN_ROLE);

Parameters

NameTypeDescription
toaddressThe address to send the ether to.

_getLowLatencyPrice

Gets the price from the low-latency oracle.

function _getLowLatencyPrice(bytes calldata data, uint128 actionTimestamp, PriceAdjustment dir, uint128 targetLimit)
    internal
    virtual
    returns (PriceInfo memory price_);

Parameters

NameTypeDescription
databytesThe signed price update data.
actionTimestampuint128The timestamp of the action corresponding to the price. If zero, then we must accept all prices younger than the recent price delay.
dirPriceAdjustmentThe direction for the low latency price adjustment.
targetLimituint128The most recent timestamp a price can have (can be zero if actionTimestamp is zero).

Returns

NameTypeDescription
price_PriceInfoThe price from the low-latency oracle, adjusted according to the price adjustment direction.

_getInitiateActionPrice

Gets the price for an initiate action of the protocol.

function _getInitiateActionPrice(bytes calldata data, PriceAdjustment dir)
    internal
    virtual
    returns (PriceInfo memory price_);

Parameters

NameTypeDescription
databytesThe low latency data.
dirPriceAdjustmentThe direction to adjust the price (when using a low latency price).

Returns

NameTypeDescription
price_PriceInfoThe price to use for the user action.

_getValidateActionPrice

Gets the price for a validate action of the protocol.

If the low latency delay is not exceeded, validate the price with the low-latency oracle(s). Else, get the specified roundId on-chain price from Chainlink. In case of chainlink price, we don't have a price adjustment, so both neutralPrice and price are equal.

function _getValidateActionPrice(bytes calldata data, uint128 targetTimestamp, PriceAdjustment dir)
    internal
    virtual
    returns (PriceInfo memory price_);

Parameters

NameTypeDescription
databytesAn optional low-latency price update or a chainlink roundId (abi-encoded uint80).
targetTimestampuint128The timestamp of the initiate action.
dirPriceAdjustmentThe direction to adjust the price (when using a low latency price).

Returns

NameTypeDescription
price_PriceInfoThe price to use for the user action.

_getLiquidationPrice

Gets the price from the low-latency oracle.

function _getLiquidationPrice(bytes calldata data) internal virtual returns (PriceInfo memory price_);

Parameters

NameTypeDescription
databytesThe signed price update data.

Returns

NameTypeDescription
price_PriceInfoThe low-latency oracle price.

_validateChainlinkRoundId

Checks that the given round ID is valid and returns its corresponding price data.

Round IDs are not necessarily consecutive, so additional computing can be necessary to find the previous round ID.

function _validateChainlinkRoundId(uint128 targetLimit, uint80 roundId)
    internal
    view
    returns (ChainlinkPriceInfo memory providedRoundPrice_);

Parameters

NameTypeDescription
targetLimituint128The timestamp of the initiate action + _lowLatencyDelay.
roundIduint80The round ID to validate.

Returns

NameTypeDescription
providedRoundPrice_ChainlinkPriceInfoThe price data of the provided round ID.

_isPythData

Checks if the passed calldata corresponds to a Pyth message.

function _isPythData(bytes calldata data) internal pure returns (bool isPythData_);

Parameters

NameTypeDescription
databytesThe calldata pointer to the message.

Returns

NameTypeDescription
isPythData_boolWhether the data is a valid Pyth message or not.