Oracles
Introduction
This guide shows you how to perform all the operations that you need within the lifecycle of oracles using the SDK.
1. Oracle: register
Let's register an oracle that responds with the temperature of the city that is included in the query.
Firstly, you need to create an instance of Oracle
class. This class requires an account that would be used to sign operations on behalf of the oracle. So one account can host only one oracle, and this oracle address would be the same as the corresponding account address except for a different prefix (ok_
instead of ak_
). This means that it's not possible to manage multiple oracles using the same account.
To register an oracle on-chain you need to provide a queryFormat
and a responseFormat
to the register
function of Oracle
class. In addition to the common transaction options you can provide the oracle specific options queryFee
and oracleTtlValue
, see transaction options.
Note:
By default the oracle will exist for the next 500 key blocks.
If you intend to keep your oracle running longer you should increase the
oracleTtlValue
and/or set up a service that automatically extends the TTL before it expires.
2. Some party: query an oracle and poll for response
Query (preferred)
After the oracle has been registered and as long as it isn't expired, everybody that knows the oracleId
can query it.
Note:
Again, take a look into the transaction options to see what (other) options you can provide.
Alternatively, you can post query and poll for response using separate methods from below.
Post query (alternative)
To post a query without waiting for a response do the below.
Poll for response (alternative)
Now you have query ID that can be used to poll for the response:
3. Oracle: poll for queries and respond
Handle queries (preferred)
Typically, the oracle itself polls for its own queries and responds as soon as possible:
This way, the oracle would respond with the temperature in a requested city. It needs to be done before the query's TTL expires.
Note:
Of course, the oracle itself would either use an API to get the current temperature for a certain city or ideally directly communicate with measuring devices located in that specific city.
As far as Oracle class is bound to a specific account provided while creation, it is not necessary to pass the
onAccount
option.
The above is the simplest way to respond to queries, though you can manually subscribe for new queries and respond to them.
Poll for queries (alternative)
To subscribe to new queries without responding to them:
Respond to query (alternative)
If the oracle recognizes that it has been queried it can respond to the query.
4. Oracle: extend
As mentioned above an Oracle has a certain TTL that can be specified when registering it. You might want to extend the TTL of the oracle before it expires. You can do that as follows:
5. Get the current state from the node
Both Oracle and OracleClient have methods to get their state from the node.
Oracle:getState
, OracleClient:getState
returns the same value as Node:getOracleByPubkey
, but without arguments (it uses the oracle address provided in the constructor).
Oracle:getQuery
, OracleClient:getQuery
corresponds to Node:getOracleQueryByPubkeyAndQueryId
, adding decodedQuery
, decodedResponse
based on the oracle type.
Example applications
ae-oracle-pricefeed NodeJS example that registers an oracle, extends it if required and responds to queries automatically.
tipping-oracle-service application that registers an oracle to check the presence of an AE address at a specific page
Last updated