Creating a Query
Tell the ecosystem what data you're seeking.
All data reported to Fetch Oracle is associated with a unique QueryId
and a timestamp
.
When a user requests data using a tip
and when a reporter submits data using submitValue
, they have to input both the queryId
and queryData
.
The queryData
tells reporters how to fulfill the data query, while also informing voters how to verify the data in a dispute. The queryId
is defined as the keccak256 hash of the queryData
field.
In order to query the Fetch oracle you'll need to first generate queryData
and its hash, the queryId
.
Getting a Query ID and Query Data
Use the tools below to generate a queryId
and queryData:
If the existing Query Types don't fit your needs, you can define a new one.
Creating a new Query Type
To add a new data type to Fetch Oracle, you'll just need to define a new queryType. This is how you form a question so that Fetch Oracle reporters know exactly what data is being requested.
To create a new Query Type or specification for custom data you need from Fetch oracles, there are two options:
Fork the Fetch DataSpecs repository and make a pull request for a new Query type in
./types
using this template.
You'll need to determine three things: a unique queryType
name, inputs, and outputs. So let's say you want a query for getting the price of any asset in any currency. In human-readable form, your question could look like this:
What is the price of PLS/USD?
You might formally define your query like this:
Name: SpotPrice
Inputs:
1. asset (string): Asset ID (e.g. PLS
)
2. currency (string): Selected currency (e.g. USD
)
Outputs:
1. price (uint256)
- abi_type
: ufixed256x18 (18 decimals of precision)
- packed
: false
Next, you may need to incentivize reporters to fetch the answer to your question. Check out the tipping page to learn more.
Once Fetch Oracle reporters are submitting your new queryType
on chain, you can retrieve your desired data with the help of UsingFetch, which is a helper contract that provides various Fetch Oracle data getters.
First, put your question in queryData
format, which means encoding your queryType
name and arguments into bytes (see below). You'll then need to get a queryId
, which is the bytes32
unique identifier for each Fetch Oracle data feed. The queryId
is defined as the keccak256
hash of queryData
. Once you know the queryId
you'll be able to retrieve your data.
In Solidity, your contract can get data like this.
Example QueryData and QueryID
If you input pls
and usd
, respectively, the queryData
would be:
0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000953706f745072696365000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000003706c73000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000037573640000000000000000000000000000000000000000000000000000000000
and the queryId
would be
0x83245f6a6a2f6458558a706270fbcc35ac3a81917602c1313d3bfa998dcc2d4b
Next Steps
You're free to build a query at any time and start integrating it into your project.
When you reach the later stages of building your project, add an issue to Fetch Oracle's dataSpecs
repository so that data reporters know how to fulfill your query.
For the best chance of success, it's a good idea to tip reporters by funding a feed.
Last updated