Consumers specify a symbol and publish the request as a task on OpenLayer's smart contract, then when the task is completed, they can read the results from the smart contract. These could be done either within another smart contract, or from off-chain.
The APIs to get data is similar with most existing major data oracles. In addition we provided on-demand pull model so real time updates could be requested for the less frequently updated feeds.
You can also find examples here:
/**
* Request new data fetch from another smart contract
*
* Network: Holesky
* Data Feed: GOLD/USD
* Task Type: 1
* Address: 0x12345....abcde
*/
function requestLatestGoldPrice() {
priceFeed = OpenOracleCommonDataFeed("0x12345....abcde");
priceFeed.requestNewReport(1 /* taskType for GOLD/USD */);
}
/**
* Reading latest data from another smart contract
*
* Network: Holesky
* Data Feed: GOLD/USD
* Task Type: 1
* Address: 0x12345....abcde
*/
function readLatestGoldPrice() {
priceFeed = OpenOracleCommonDataFeed("0x12345....abcde");
(
int answer,
/*uint startedAt*/,
/*uint timeStamp*/,
) = priceFeed.latestRoundData();
}
// Request new data fetch from Javascript
const addr = "0x12345....abcde"
const priceFeed = new web3.eth.Contract(OpenOracleCommonDataFeed, addr)
const taskType = 1 // GOLD/USD
priceFeed.methods
.requestNewReport(taskType)
.call()
.then((requestData) => {
console.log("Request Hash:", requestData[0])
})
// Read latest data fetch from Javascript
const addr = "0x12345....abcde"
const priceFeed = new web3.eth.Contract(OpenOracleCommonDataFeed, addr)
priceFeed.methods
.latestRoundData()
.call()
.then((resultData) => {
console.log("Result:", parseInt(resultData[0]) / 100.0)
}
Consuming data with Parameters
Certain data types are requested with a parameter or input. The example below shows a data feed for points on soccer teams. Apart from specifying the contract address, for each data request, parameters on league, season and team need to be specified too.
// Request new data fetch from Javascript
const addr = "0x12345....abcde"
const priceFeed = new web3.eth.Contract(OpenOracleCommonDataFeed, addr)
const taskType = 14 // Soccer Data
// Fetch Manchester United computed score for season 2021
const taskData = ethers.utils.solidityPack(["uint32","uint64","uint32"],[39,2021,33])
priceFeed.methods
.requestNewReportWithData(taskType, taskData)
.call()
.then((requestData) => {
console.log("Request Hash:", requestData[0])
})
// Read latest data fetch from Javascript
const addr = "0x12345....abcde"
const priceFeed = new web3.eth.Contract(OpenOracleCommonDataFeed, addr)
priceFeed.methods
.latestRoundData()
.call()
.then((resultData) => {
console.log("Result:", parseInt(resultData[0]))
}
Consuming rich task responses
Data types with taskType >= 15 (including the ones customly created) will consist one or multiple fields in the response. This allows more fields to be included for subsequent self serve computations.
// Request new data fetch from Javascript
const addr = "0x12345....abcde"
const priceFeed = new web3.eth.Contract(OpenOracleCommonDataFeed, addr)
const taskType = 16 // Powerball Data
// Fetch latest Powerball drawing
priceFeed.methods
.requestNewReport(taskType)
.call()
.then((requestData) => {
console.log("Request Hash:", requestData[0])
})
// Read latest data fetch from Javascript
const addr = "0x12345....abcde"
const priceFeed = new web3.eth.Contract(OpenOracleCommonDataFeed, addr)
priceFeed.methods
.latestRoundData()
.call()
.then((resultData) => {
console.log("Result:", parseHexString(resultData[0]))
}
// Utility function to parse resul
function parseHexString(hexString) {
if(hexString.startsWith('0x')){
hexString = hexString.slice(2);
}
const result = {};
let index = 0;
while (index < hexString.length) {
const length = parseInt(hexString.slice(index, index + 4), 16);
index += 4;
const data = hexString.slice(index, index + length);
index += length;
const decodedData = Buffer.from(data, 'hex').toString();
result[`data_${Object.keys(result).length}`] = decodedData;
}
return result;
}
Add a new data type
At the moment OpenLayer is beta testing adding custom new data types with JSON APIs.
Adding a new data type requires you to specify
The url of the API, or where the data comes from.
The paths to the JSON response fields that should be included in the data stream
Current Preset Data Types
TaskType
Description
0
Oil price
1
Gold price
2
Silver price
3
Platinum price
4
Palladium price
5
Copper price
6
Rbob Gas price
7
Natural Gas price
8
Brent Crude Oil price
9
Corn price
10
Soybean price
11
Rough Rice price
12
Cocoa price
13
Lumber price
14
Soccer team stats (computed with wins-loses+goals-againstGoals)
15
Ignore
16
Latest Powerball drawing
Smart Contract Addresses
Network
Address
Holesky (17000)
0xB233eE56e57f7eB1B1144b28214Abc74b273d3D5
Plume Testnet (161221135)
0x9B1d74AAC508Bf95C8A0e08458093bca8E9D3cB3
Camp Testnet V2 (325000)
0xabc6C2f445d253Db6C1f62602E911c9E8cf90880
This example shows how the Powerball data type was created: