Public Data Streams

Consuming Data

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: https://github.com/0xJomo/openoracle-examples

/**
 * 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

This example shows how the Powerball data type was created: https://github.com/0xJomo/openoracle-examples/blob/main/js-example/holesky/add_new_feed_type.js

Current Preset Data Types

Smart Contract Addresses

Last updated