Skip to content

createOracle

The createOracle method is designed to facilitate the creation of an oracle, which serves as a reliable source of real-time data from a specified Application Programming Interface (API). This method is particularly suited for scenarios where only a single data point is required from the API.

Usage

As an example, we will utilize the CoinGecko public API, which provides the Ethereum price in USD: CoinGecko Ethereum API.

js
const createOracleRes = factory
  .createOracle({
    url: 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd',
    method: 'GET',
    headers: {
      authorization: '%API_KEY%',
    },
    dataType: 'number',
    JSONPath: '$.ethereum.usd',
    apiKey: 'MY_TEST_API_KEY',
  })
  .subscribe({
    next: (data) => {
      console.log('next', data);
    },
    error: (error) => {
      console.log('error', error);
    },
    complete: () => {
      console.log('Oracle Creation Completed');
    },
  });

Return values during creation process

These are the possible events iExec may send to the subscriber:

messagesentadditional entries
ENCRYPTION_KEY_CREATEDonce if using apiKeykey: String
FILE_ENCRYPTEDonce if using apiKeyencryptedFile: Buffer
checksum: String
ENCRYPTED_FILE_UPLOADEDonce if using apiKeycid: String
multiaddr: String
DATASET_DEPLOYMENT_SIGN_TX_REQUESTonce if using apiKey
DATASET_DEPLOYMENT_SUCCESSonce if using apiKeyaddress: String
txHash: String
PUSH_SECRET_TO_SMS_SIGN_REQUESTonce if using apiKey
PUSH_SECRET_TO_SMS_SUCCESSonce if using apiKey
DATASET_ORDER_SIGNATURE_SIGN_REQUESTonce if using apiKeyorder: Object
DATASET_ORDER_SIGNATURE_SUCCESSonce if using apiKeyorder: Object
DATASET_ORDER_PUBLISH_SIGN_REQUESTonce if using apiKeyorder: Object
DATASET_ORDER_PUBLISH_SUCCESSonce if using apiKeyorderHash: String
PARAM_SET_CREATEDonceparamSet: String
ORACLE_ID_COMPUTEDonceoracleId: String
PARAM_SET_UPLOADEDoncecid: String
COMPLETEDonce

Parameters

ts
import { type 
RawParams
} from '@iexec/iexec-oracle-factory-wrapper';

url

string

The API URL to fetch data from.

js
const createOracleRes = factory
  .createOracle({
    url: 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd',
    method: 'GET',
    headers: {
      authorization: '%API_KEY%',
    },
    dataType: 'number',
    JSONPath: '$.ethereum.usd',
    apiKey: 'MY_TEST_API_KEY',
  })
  .subscribe({
    next: (data) => {
      console.log('next', data);
    },
    error: (error) => {
      console.log('error', error);
    },
    complete: () => {
      console.log('Oracle Creation Completed');
    },
  });

method

'GET' | 'POST' | 'PUT' | 'DELETE'

The HTTP method to use when making the API request (e.g., "GET").

js
const createOracleRes = factory
  .createOracle({
    url: 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd',
    method: 'GET',
    headers: {
      authorization: '%API_KEY%',
    },
    dataType: 'number',
    JSONPath: '$.ethereum.usd',
    apiKey: 'MY_TEST_API_KEY',
  })
  .subscribe({
    next: (data) => {
      console.log('next', data);
    },
    error: (error) => {
      console.log('error', error);
    },
    complete: () => {
      console.log('Oracle Creation Completed');
    },
  });

headers

Record<string, string> | undefined

Any headers required for the API request.

js
const createOracleRes = factory.createOracle({
  url: 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd',
  method: 'GET',
  headers: {
    authorization: '%API_KEY%',
  },
  dataType: 'number',
  JSONPath: '$.ethereum.usd',
  apiKey: 'MY_TEST_API_KEY',
}).subscribe({
  next: (data) => {
    console.log("next", data);
  },
  error: (error) => {
    console.log("error", error);
  },
  complete: () => {
    console.log("Oracle Creation Completed");
  },
});

dataType

DataType

The type of data to be returned (e.g., "number").

js
const createOracleRes = factory
  .createOracle({
    url: 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd',
    method: 'GET',
    headers: {
      authorization: '%API_KEY%',
    },
    dataType: 'number',
    JSONPath: '$.ethereum.usd',
    apiKey: 'MY_TEST_API_KEY',
  })
  .subscribe({
    next: (data) => {
      console.log('next', data);
    },
    error: (error) => {
      console.log('error', error);
    },
    complete: () => {
      console.log('Oracle Creation Completed');
    },
  });

JSONPath

string

The JSON path to extract the data from the API response.

js
const createOracleRes = factory
  .createOracle({
    url: 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd',
    method: 'GET',
    headers: {
      authorization: '%API_KEY%',
    },
    dataType: 'number',
    JSONPath: '$.ethereum.usd',
    apiKey: 'MY_TEST_API_KEY',
  })
  .subscribe({
    next: (data) => {
      console.log('next', data);
    },
    error: (error) => {
      console.log('error', error);
    },
    complete: () => {
      console.log('Oracle Creation Completed');
    },
  });

apiKey

string | undefined

API key if required by the data source.

js
const createOracleRes = factory
  .createOracle({
    url: 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd',
    method: 'GET',
    headers: {
      authorization: '%API_KEY%',
    },
    dataType: 'number',
    JSONPath: '$.ethereum.usd',
    apiKey: 'MY_TEST_API_KEY',
  })
  .subscribe({
    next: (data) => {
      console.log('next', data);
    },
    error: (error) => {
      console.log('error', error);
    },
    complete: () => {
      console.log('Oracle Creation Completed');
    },
  });

Return value

ts
import { type CreateOracleMessage } from '@iexec/iexec-oracle-factory-wrapper';
Module '"@iexec/iexec-oracle-factory-wrapper"' has no exported member 'CreateOracleMessage'.

Observable<CreateOracleMessage>