Oracles
Last updated
Was this helpful?
Last updated
Was this helpful?
This guide shows you how to perform all the operations that you need within the lifecycle of using the SDK.
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 .
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.
After the oracle has been registered and as long as it isn't expired, everybody that knows the oracleId
can query it.
Note:
Alternatively, you can post query and poll for response using separate methods from below.
To post a query without waiting for a response do the below.
Now you have query ID that can be used to poll for the response:
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.
To subscribe to new queries without responding to them:
If the oracle recognizes that it has been queried it can respond to the query.
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:
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. In the same way Oracle:getQueries
, OracleClient:getQueries
corresponds to Node:getOracleQueriesByPubkey
.
Again, take a look into the to see what (other) options you can provide.
NodeJS example that registers an oracle, extends it if required and responds to queries automatically.
application that registers an oracle to check the presence of an AE address at a specific page