readOracle
The readOracle
method is designed to retrieve the value from a specific oracle. This enables users to access data fetched by an oracle, which serves as a reliable source of information sourced from external APIs or other data providers.
Usage
As an example, we will utilize the CoinGecko public API oracle, which provides the Ethereum price in USD: CoinGecko Ethereum API.
const readOracleRes = await readerOrFactory.readOracle(
'QmbXhtjAJysMMA69KkB8KohsEDTZA2PXuhYdAQcHjjQFit'
); // Content ID of the Oracle
TIP
You can utilize either type of SDK instance, be it the basic IExecOracleFactory
or the IExecOracleReader
, to invoke the readOracle
method.
Parameters
import type { ReadOracleParams } from '@iexec/iexec-oracle-factory-wrapper';
paramSet or paramSetCid or oracleId Required *
TIP
The oracle
ParamSet
describes the parameters used to feed the oracle.Any different
ParamSet
has a uniqueParamSetCid
which is the Content ID of the document on IPFS. With aParamSetCid
anyone can retrieve theParamSet
from IPFS.The
OracleId
is the blockchain hash of theParamSet
it is used to store and read the value of an oracle on the Oracle contract.
- ParamSet of the Oracle to be read.
const paramSet: ParamSet = {
method: 'GET',
url: 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd',
headers: { authorization: '%API_KEY%' },
body: '',
dataset: '0x0eFf9Ba4304D5d3EB775cA9dB1F011e65C2eb0cE',
JSONPath: '$.ethereum.usd',
dataType: 'number',
};
const readOracleRes = await readerOrFactory.readOracle(
paramSet
);
- ParamSet CID of the Oracle to be read.
const paramSetCid: ParamSetCID =
'QmbXhtjAJysMMA69KkB8KohsEDTZA2PXuhYdAQcHjjQFit';
const readOracleRes = await readerOrFactory.readOracle(
paramSetCid
);
- Oracle ID of the Oracle to be read.
const oracleId: OracleID =
'0xf0f370ad33d1e3e8e2d8df7197c40f62b5bc403553b103858359687491234491';
const readOracleRes = await readerOrFactory.readOracle(
oracleId,
{
dataType: 'number', // When reading an oracle from its OracleID, the dataType must be specified.
}
);
options
dataType
When reading an oracle from its OracleID, the dataType must be specified.
const oracleId: OracleID =
'0xf0f370ad33d1e3e8e2d8df7197c40f62b5bc403553b103858359687491234491';
const dataType: DataType = 'number';
const readOracleRes = await readerOrFactory.readOracle(
oracleId,
{
dataType,
}
);
Return value
OracleValue
import type { OracleValue } from '@iexec/iexec-oracle-factory-wrapper';