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.
/** * Request new data fetch from another smart contract * * Network: Holesky * Data Feed: GOLD/USD * Task Type: 1 * Address: 0x12345....abcde */functionrequestLatestGoldPrice() { 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 */functionreadLatestGoldPrice() { priceFeed =OpenOracleCommonDataFeed("0x12345....abcde"); (int answer,/*uint startedAt*/,/*uint timeStamp*/, ) = priceFeed.latestRoundData();}
// Request new data fetch from Javascriptconstaddr="0x12345....abcde"constpriceFeed=newweb3.eth.Contract(OpenOracleCommonDataFeed, addr)consttaskType=1// GOLD/USDpriceFeed.methods.requestNewReport(taskType).call().then((requestData) => {console.log("Request Hash:", requestData[0]) })// Read latest data fetch from Javascriptconstaddr="0x12345....abcde"constpriceFeed=newweb3.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 Javascriptconstaddr="0x12345....abcde"constpriceFeed=newweb3.eth.Contract(OpenOracleCommonDataFeed, addr)consttaskType=14// Soccer Data// Fetch Manchester United computed score for season 2021consttaskData=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 Javascriptconstaddr="0x12345....abcde"constpriceFeed=newweb3.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 Javascriptconstaddr="0x12345....abcde"constpriceFeed=newweb3.eth.Contract(OpenOracleCommonDataFeed, addr)consttaskType=16// Powerball Data// Fetch latest Powerball drawingpriceFeed.methods.requestNewReport(taskType).call().then((requestData) => {console.log("Request Hash:", requestData[0]) })// Read latest data fetch from Javascriptconstaddr="0x12345....abcde"constpriceFeed=newweb3.eth.Contract(OpenOracleCommonDataFeed, addr)priceFeed.methods.latestRoundData().call().then((resultData) => {console.log("Result:",parseHexString(resultData[0])) }// Utility function to parse resulfunctionparseHexString(hexString) {if(hexString.startsWith('0x')){ hexString =hexString.slice(2); }constresult= {};let index =0;while (index <hexString.length) {constlength=parseInt(hexString.slice(index, index +4),16); index +=4;constdata=hexString.slice(index, index + length); index += length;constdecodedData=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