OracleId examples
As the Opium Protocol OracleAggregator is designed to be data source agnostic, it is up to the developer to implement what kind of on-chain or off-chain mechanism is the most suitable to their application.
As it was explained in the previous chapter, in the Opium protocol a financial instrument is defined by two separate entities:
  • Derivative recipe
  • Oracle recipe
For the purpose of sharing a common vocabulary, we refer to the contract entity implementing the oracle recipe as OracleId.
In fact, the OracleAggregator itself uses the same terminology. In the OracleAggregator we have a nested mapping that maps the address of an OracleId to a mapping that maps the timestamp when the OracleId provided data to the OracleAggregator to the numerical value provided.
The OracleId’s value is therefore cached and made available to the Opium Protocol Core contract for the execution of the related derivative’s positions (see below):(.
1
mapping(address => mapping(uint256 => uint256)) private dataCache;
Copied!
A possible pattern for the development of OracleIds is to split their logic in an OracleId and an OracleSubId. This allows to reason more clearly on the oracle logic and enforce a better separation of concerns and single responsibility principle.
The OracleId is responsible for pushing data into the OracleAggregator contract by calling the OracleAggregator callback function and the OracleSubId is responsible for implementing the oracle-specific logic. The OracleSubId logic can be implemented using Chainlink, UMA etc..
As the most popular oracle solution in the Ethereum space thus far is Chainlink, in the following example we showcase how to implement the OracleId/OracleSubId pattern:
1
interface IChainlinkOracle {
2
function getLatestPrice() external returns(int);
3
}
4
5
contract OracleIdar is IOracleId, Ownable {
6
IOracleAggregator oracleAggregator;
7
uint256 public result;
8
9
constructor(address _registry) public {
10
IRegistry registry = IRegistry(_registry);
11
oracleAggregator = IOracleAggregator(registry.getOracleAggregator());
12
}
13
/// @notice Wrapper around Opium oracleAggregator.__callback to push the data related to the underlying's market price
14
/// @param _oracleSubId address of the oracleSubId - the data source whose result is being pushed into the OracleAggregator
15
/// @param _timestamp uint256 unix timestamp of when the data is being pushed
16
function __callback(address _oracleSubId, uint256 _timestamp) external onlyOwner {
17
require(
18
!oracleAggregator.hasData(address(this), _timestamp) && _timestamp < now,
19
"Only when no data and after timestamp allowed"
20
);
21
uint256 data = uint256(IChainlinkOracle(_oracleSubId).getLatestPrice());
22
result = data;
23
24
oracleAggregator.__callback(_timestamp, result);
25
}
26
27
function fetchData(uint256 timestamp) external payable {}
28
29
function recursivelyFetchData(
30
uint256 timestamp,
31
uint256 period,
32
uint256 times
33
) external payable {}
34
35
function calculateFetchPrice() external returns (uint256 fetchPrice) {}
36
}
Copied!
In the above code snippet, the contract implements the OracleId interface. In the OracleId __callback function, the contract fetches the required data from the OracleSubId and provides it to the OracleAggregator.
The OracleSubId is a basic implementation of a Chainlink pricefeed:
1
contract OracleSubId {
2
AggregatorV3Interface internal priceFeed;
3
4
/**
5
AAVE/ETH price feed (8 decimals)
6
*/
7
constructor() public {
8
priceFeed = AggregatorV3Interface(0x6Df09E975c830ECae5bd4eD9d90f3A95a4f88012);
9
}
10
11
function getLatestPrice() public view returns(int) {
12
(
13
uint80 roundId,
14
int price,
15
uint startedAt,
16
uint timeStamp,
17
uint80 answeredInRound
18
) = priceFeed.latestRoundData();
19
return price;
20
}
21
22
}
Copied!
Last modified 1mo ago
Copy link