Contract Name:
EOFeedAdapter
Contract Source Code:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
import { IEOFeedManager } from "../interfaces/IEOFeedManager.sol";
import { IEOFeedAdapter } from "./interfaces/IEOFeedAdapter.sol";
import { Initializable } from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import { InvalidAddress } from "../interfaces/Errors.sol";
/**
* @title EOFeedAdapter
* @notice Price feed adapter contract
*/
contract EOFeedAdapter is IEOFeedAdapter, Initializable {
/// @dev Feed manager contract
IEOFeedManager private _feedManager;
/// @dev Feed version
uint256 private _version;
/// @dev Feed description
string private _description;
// next 2 variables will be packed in 1 slot
/// @dev Feed id
uint16 private _feedId;
/// @dev Decimals of the rate
uint8 private _decimals;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
/**
* @notice Initialize the contract
* @param feedManager The feed manager address
* @param feedId Feed id
* @param feedDecimals The decimals of the rate
* @param feedDescription The description of feed
* @param feedVersion The version of feed
*/
function initialize(
address feedManager,
uint16 feedId,
uint8 feedDecimals,
string memory feedDescription,
uint256 feedVersion
)
external
initializer
{
if (feedManager == address(0)) revert InvalidAddress();
_feedManager = IEOFeedManager(feedManager);
_feedId = feedId;
_decimals = feedDecimals;
_description = feedDescription;
_version = feedVersion;
}
/**
* @notice Get the price for the round
* @param
* @return roundId The round id
* @return answer The price
* @return startedAt The timestamp of the start of the round
* @return updatedAt The timestamp of the end of the round
* @return answeredInRound The round id in which the answer was computed
*/
function getRoundData(uint80) external view returns (uint80, int256, uint256, uint256, uint80) {
IEOFeedManager.PriceFeed memory priceData = _feedManager.getLatestPriceFeed(_feedId);
return (
uint80(priceData.eoracleBlockNumber),
int256(priceData.value),
priceData.timestamp,
priceData.timestamp,
uint80(priceData.eoracleBlockNumber)
);
}
/**
* @notice Get the latest price
* @return roundId The round id
* @return answer The price
* @return startedAt The timestamp of the start of the round
* @return updatedAt The timestamp of the end of the round
* @return answeredInRound The round id in which the answer was computed
*/
function latestRoundData() external view returns (uint80, int256, uint256, uint256, uint80) {
IEOFeedManager.PriceFeed memory priceData = _feedManager.getLatestPriceFeed(_feedId);
return (
uint80(priceData.eoracleBlockNumber),
int256(priceData.value),
priceData.timestamp,
priceData.timestamp,
uint80(priceData.eoracleBlockNumber)
);
}
/**
* @notice Get the latest price
* @return int256 The price
*/
function latestAnswer() external view returns (int256) {
IEOFeedManager.PriceFeed memory priceData = _feedManager.getLatestPriceFeed(_feedId);
return int256(priceData.value);
}
/**
* @notice Get the latest timestamp
* @return uint256 The timestamp
*/
function latestTimestamp() external view returns (uint256) {
IEOFeedManager.PriceFeed memory priceData = _feedManager.getLatestPriceFeed(_feedId);
return priceData.timestamp;
}
/**
* @notice Get the price for the round (round is not used, the latest price is returned)
* @param
* @return int256 The price
*/
function getAnswer(uint256) external view returns (int256) {
IEOFeedManager.PriceFeed memory priceData = _feedManager.getLatestPriceFeed(_feedId);
return int256(priceData.value);
}
/**
* @notice Get the timestamp for the round (round is not used, the latest timestamp is returned)
* @param
* @return uint256 The timestamp
*/
function getTimestamp(uint256) external view returns (uint256) {
IEOFeedManager.PriceFeed memory priceData = _feedManager.getLatestPriceFeed(_feedId);
return priceData.timestamp;
}
/**
* @notice Get the id of the feed
* @return uint16 The feed id
*/
function getFeedId() external view returns (uint16) {
return _feedId;
}
/**
* @notice Get the decimals of the rate
* @return uint8 The decimals
*/
function decimals() external view returns (uint8) {
return _decimals;
}
/**
* @notice Get the description of the feed
* @return string The description
*/
function description() external view returns (string memory) {
return _description;
}
/**
* @notice Get the version of the feed
* @return uint256 The version
*/
function version() external view returns (uint256) {
return _version;
}
/**
* @notice Get the latest round
* @return uint256 The round id, eoracle block number
*/
function latestRound() external view returns (uint256) {
IEOFeedManager.PriceFeed memory priceData = _feedManager.getLatestPriceFeed(_feedId);
return priceData.eoracleBlockNumber;
}
// slither-disable-next-line unused-state,naming-convention
// solhint-disable-next-line ordering
uint256[50] private __gap;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
import { IEOFeedVerifier } from "./IEOFeedVerifier.sol";
interface IEOFeedManager {
/**
* @dev Price feed structure
* @param value Price feed value
* @param timestamp Price feed timestamp (block timestamp in eoracle chain when price feed rate is aggregated)
* @param eoracleBlockNumber eoracle block number
*/
struct PriceFeed {
uint256 value;
uint256 timestamp;
uint256 eoracleBlockNumber;
}
/**
* @dev Event emitted when a price feed is updated
* @param feedId Feed id
* @param rate Price feed value
* @param timestamp Price feed timestamp
*/
event RateUpdated(uint16 indexed feedId, uint256 rate, uint256 timestamp);
/**
* @notice Update the price for a feed
* @param input A merkle leaf containing price data and its merkle proof
* @param checkpoint Checkpoint data containing eoracle chain metadata and the data merkle root
* @param signature Aggregated signature of the checkpoint
* @param bitmap Bitmap of the validators who signed the checkpoint
*/
function updatePriceFeed(
IEOFeedVerifier.LeafInput calldata input,
IEOFeedVerifier.Checkpoint calldata checkpoint,
uint256[2] calldata signature,
bytes calldata bitmap
)
external;
/**
* @notice Update the price for multiple feeds
* @param inputs Array of leafs to prove the price feeds
* @param checkpoint Checkpoint data
* @param signature Aggregated signature of the checkpoint
* @param bitmap Bitmap of the validators who signed the checkpoint
*/
function updatePriceFeeds(
IEOFeedVerifier.LeafInput[] calldata inputs,
IEOFeedVerifier.Checkpoint calldata checkpoint,
uint256[2] calldata signature,
bytes calldata bitmap
)
external;
/**
* @notice Set the whitelisted publishers
* @param publishers Array of publisher addresses
* @param isWhitelisted Array of booleans indicating whether the publisher is whitelisted
*/
function whitelistPublishers(address[] memory publishers, bool[] memory isWhitelisted) external;
/**
* @notice Get the latest price for a feed
* @param feedId Feed id
* @return PriceFeed struct
*/
function getLatestPriceFeed(uint16 feedId) external view returns (PriceFeed memory);
/**
* @notice Get the latest price feeds for multiple feeds
* @param feedIds Array of feed ids
* @return Array of PriceFeed structs
*/
function getLatestPriceFeeds(uint16[] calldata feedIds) external view returns (PriceFeed[] memory);
/**
* @notice Check if a publisher is whitelisted
* @param publisher Address of the publisher
* @return Boolean indicating whether the publisher is whitelisted
*/
function isWhitelistedPublisher(address publisher) external view returns (bool);
/**
* @notice Check if a feed is supported
* @param feedId feed Id to check
* @return Boolean indicating whether the feed is supported
*/
function isSupportedFeed(uint16 feedId) external view returns (bool);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
/**
* @title IEOFeedAdapter
* @notice Interface for the EOFeedAdapter contract.
* @dev compatible of AggregatorV3Interface from CL.
*/
interface IEOFeedAdapter {
// slither-disable-next-line missing-inheritance
function initialize(
address feedManager,
uint16 feedId,
uint8 feedDecimals,
string memory feedDescription,
uint256 feedVersion
)
external;
function getFeedId() external view returns (uint16);
function decimals() external view returns (uint8);
function description() external view returns (string memory);
function version() external view returns (uint256);
// getRoundData and latestRoundData should both raise "No data present"
// if they do not have data to report, instead of returning unset values
// which could be misinterpreted as actual reported values.
function getRoundData(uint80 roundId_)
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
function latestRoundData()
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
// v2 interface
function latestAnswer() external view returns (int256);
function latestTimestamp() external view returns (uint256);
function latestRound() external view returns (uint256);
function getAnswer(uint256 roundId) external view returns (int256);
function getTimestamp(uint256 roundId) external view returns (uint256);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (proxy/utils/Initializable.sol)
pragma solidity ^0.8.20;
/**
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
* behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
*
* The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
* reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
* case an upgrade adds a module that needs to be initialized.
*
* For example:
*
* [.hljs-theme-light.nopadding]
* ```solidity
* contract MyToken is ERC20Upgradeable {
* function initialize() initializer public {
* __ERC20_init("MyToken", "MTK");
* }
* }
*
* contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
* function initializeV2() reinitializer(2) public {
* __ERC20Permit_init("MyToken");
* }
* }
* ```
*
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
* possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
*
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
*
* [CAUTION]
* ====
* Avoid leaving a contract uninitialized.
*
* An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
* contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
* the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
*
* [.hljs-theme-light.nopadding]
* ```
* /// @custom:oz-upgrades-unsafe-allow constructor
* constructor() {
* _disableInitializers();
* }
* ```
* ====
*/
abstract contract Initializable {
/**
* @dev Storage of the initializable contract.
*
* It's implemented on a custom ERC-7201 namespace to reduce the risk of storage collisions
* when using with upgradeable contracts.
*
* @custom:storage-location erc7201:openzeppelin.storage.Initializable
*/
struct InitializableStorage {
/**
* @dev Indicates that the contract has been initialized.
*/
uint64 _initialized;
/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool _initializing;
}
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.Initializable")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant INITIALIZABLE_STORAGE = 0xf0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00;
/**
* @dev The contract is already initialized.
*/
error InvalidInitialization();
/**
* @dev The contract is not initializing.
*/
error NotInitializing();
/**
* @dev Triggered when the contract has been initialized or reinitialized.
*/
event Initialized(uint64 version);
/**
* @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
* `onlyInitializing` functions can be used to initialize parent contracts.
*
* Similar to `reinitializer(1)`, except that in the context of a constructor an `initializer` may be invoked any
* number of times. This behavior in the constructor can be useful during testing and is not expected to be used in
* production.
*
* Emits an {Initialized} event.
*/
modifier initializer() {
// solhint-disable-next-line var-name-mixedcase
InitializableStorage storage $ = _getInitializableStorage();
// Cache values to avoid duplicated sloads
bool isTopLevelCall = !$._initializing;
uint64 initialized = $._initialized;
// Allowed calls:
// - initialSetup: the contract is not in the initializing state and no previous version was
// initialized
// - construction: the contract is initialized at version 1 (no reininitialization) and the
// current contract is just being deployed
bool initialSetup = initialized == 0 && isTopLevelCall;
bool construction = initialized == 1 && address(this).code.length == 0;
if (!initialSetup && !construction) {
revert InvalidInitialization();
}
$._initialized = 1;
if (isTopLevelCall) {
$._initializing = true;
}
_;
if (isTopLevelCall) {
$._initializing = false;
emit Initialized(1);
}
}
/**
* @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
* contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
* used to initialize parent contracts.
*
* A reinitializer may be used after the original initialization step. This is essential to configure modules that
* are added through upgrades and that require initialization.
*
* When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
* cannot be nested. If one is invoked in the context of another, execution will revert.
*
* Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
* a contract, executing them in the right order is up to the developer or operator.
*
* WARNING: Setting the version to 2**64 - 1 will prevent any future reinitialization.
*
* Emits an {Initialized} event.
*/
modifier reinitializer(uint64 version) {
// solhint-disable-next-line var-name-mixedcase
InitializableStorage storage $ = _getInitializableStorage();
if ($._initializing || $._initialized >= version) {
revert InvalidInitialization();
}
$._initialized = version;
$._initializing = true;
_;
$._initializing = false;
emit Initialized(version);
}
/**
* @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
* {initializer} and {reinitializer} modifiers, directly or indirectly.
*/
modifier onlyInitializing() {
_checkInitializing();
_;
}
/**
* @dev Reverts if the contract is not in an initializing state. See {onlyInitializing}.
*/
function _checkInitializing() internal view virtual {
if (!_isInitializing()) {
revert NotInitializing();
}
}
/**
* @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
* Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
* to any version. It is recommended to use this to lock implementation contracts that are designed to be called
* through proxies.
*
* Emits an {Initialized} event the first time it is successfully executed.
*/
function _disableInitializers() internal virtual {
// solhint-disable-next-line var-name-mixedcase
InitializableStorage storage $ = _getInitializableStorage();
if ($._initializing) {
revert InvalidInitialization();
}
if ($._initialized != type(uint64).max) {
$._initialized = type(uint64).max;
emit Initialized(type(uint64).max);
}
}
/**
* @dev Returns the highest version that has been initialized. See {reinitializer}.
*/
function _getInitializedVersion() internal view returns (uint64) {
return _getInitializableStorage()._initialized;
}
/**
* @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
*/
function _isInitializing() internal view returns (bool) {
return _getInitializableStorage()._initializing;
}
/**
* @dev Returns a pointer to the storage namespace.
*/
// solhint-disable-next-line var-name-mixedcase
function _getInitializableStorage() private pure returns (InitializableStorage storage $) {
assembly {
$.slot := INITIALIZABLE_STORAGE
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
/*//////////////////////////////////////////////////////////////////////////
EOFeedManager
//////////////////////////////////////////////////////////////////////////*/
error CallerIsNotWhitelisted(address caller);
error MissingLeafInputs();
error FeedNotSupported(uint16 feedId);
error SymbolReplay(uint16 feedId);
/*//////////////////////////////////////////////////////////////////////////
EOFeedVerifier
//////////////////////////////////////////////////////////////////////////*/
error CallerIsNotFeedManager();
error InvalidInput();
error InvalidProof();
error InvalidAddress();
error InvalidEventRoot();
error VotingPowerIsZero();
error AggVotingPowerIsZero();
error InsufficientVotingPower();
error SignatureVerificationFailed();
error ValidatorIndexOutOfBounds();
error ValidatorSetTooSmall();
error SenderNotAllowed(address sender);
/*//////////////////////////////////////////////////////////////////////////
EOFeedRegistryAdapter
//////////////////////////////////////////////////////////////////////////*/
error FeedAlreadyExists();
error BaseQuotePairExists();
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
interface IEOFeedVerifier {
/**
* @dev Leaf input structure
* @param leafIndex Index of the leaf
* @param unhashedLeaf Unhashed leaf data
* abi encoded (uint256 id, address sender, address receiver, bytes memory data)
* where bytes memory data = abi encoded (uint16 feedId, uint256 rate, uint256 timestamp)
* @param proof Merkle proof of the leaf
*/
struct LeafInput {
uint256 leafIndex;
bytes unhashedLeaf;
bytes32[] proof;
}
/**
* @dev Checkpoint structure
* @param epoch Epoch number
* @param blockNumber Block number
* @param eventRoot Event root of the merkle tree
* @param blockHash Block hash
* @param blockRound Block round
*/
struct Checkpoint {
uint256 epoch;
uint256 blockNumber;
bytes32 eventRoot;
bytes32 blockHash;
uint256 blockRound;
}
/**
* @dev Validator structure
* @param _address Validator address
* @param blsKey Validator BLS key
* @param votingPower Validator voting power
*/
struct Validator {
address _address;
uint256[4] blsKey;
uint256 votingPower;
}
/**
* @dev Event emitted when the validator set is updated
* @param currentValidatorSetLength Length of the current validator set
* @param currentValidatorSetHash Hash of the current validator set
* @param totalVotingPower Total voting power of the current validator set
*/
event ValidatorSetUpdated(
uint256 currentValidatorSetLength, bytes32 currentValidatorSetHash, uint256 totalVotingPower
);
/**
* @dev Event emitted when the feed manager is set
* @param feedManager Address of the feed manager
*/
event FeedManagerSet(address feedManager);
/**
* @notice Verifies leaf, processes checkpoint,
* returns leaf data in case if checkpoint is valid and leaf is part of the merkle tree
* @param input leaf input data and proof (LeafInput)
* @param checkpoint Checkpoint data (Checkpoint)
* @param signature Aggregated signature of the checkpoint
* @param bitmap Bitmap of the validators who signed the checkpoint
* @return leafData Leaf data, abi encoded (uint16 feedId, uint256 rate, uint256 timestamp)
*/
function verify(
LeafInput memory input,
Checkpoint calldata checkpoint,
uint256[2] calldata signature,
bytes calldata bitmap
)
external
returns (bytes memory leafData);
/**
* @notice Verifies multiple leaves, processes checkpoint,
* returns leaf data in case if checkpoint is valid and leaves are part of the merkle tree
* @param inputs Exit leaves inputs
* @param checkpoint Checkpoint data
* @param signature Aggregated signature of the checkpoint
* @param bitmap Bitmap of the validators who signed the checkpoint
*/
function batchVerify(
LeafInput[] memory inputs,
Checkpoint calldata checkpoint,
uint256[2] calldata signature,
bytes calldata bitmap
)
external
returns (bytes[] memory);
/**
* @notice Function to set a new validator set
* @param newValidatorSet The new validator set to store
*/
function setNewValidatorSet(Validator[] calldata newValidatorSet) external;
/**
* @notice Sets the address of the feed manager.
* @param feedManager_ The address of the new feed manager.
*/
function setFeedManager(address feedManager_) external;
/**
* @notice Sets allowed sender for exit events
* @param senders Addresses of the allowed senders
* @param allowed Boolean value to set the sender as allowed or not
*/
function setAllowedSenders(address[] calldata senders, bool allowed) external;
}