|
| 1 | +# Oracle Usage |
| 2 | + |
| 3 | +This guide describes the basic operations on [Oracle](https://github.com/aeternity/protocol/blob/master/oracles/oracles.md) using [Aeternity JS SDK](https://github.com/aeternity/aepp-sdk-js) |
| 4 | + |
| 5 | +## Main Flow |
| 6 | + |
| 7 | + - `Register` an `oracle` (prepare and broadcast `oracle-register`) |
| 8 | + ```js |
| 9 | + const sdkInstance = await Universal({ ... }) // Init Universal instance |
| 10 | + |
| 11 | + // First argument is oracle query format and second oracle query response format |
| 12 | + // queryFee is optional and default to 30000 |
| 13 | + // oracleTll is optional and default to { type: 'delta', value: 500 } |
| 14 | + const oracle = await client.registerOracle("{'city': str}", "{'tmp': num}", { queryFee, oracleTtl }) |
| 15 | + ``` |
| 16 | + |
| 17 | + - Extend oracle (prepare and broadcast `oracle-extend` transaction) |
| 18 | + ```js |
| 19 | + const ttlToExtend = { type: 'delta', value: 500 } |
| 20 | + const oracle = await sdkInstance.getOracleObject(oracleId) |
| 21 | + |
| 22 | + // extend oracle ttl to 500 blocks |
| 23 | + const oracleEntended = await oracle.extendOracle(ttlToExtend) |
| 24 | + // or |
| 25 | + const oracleEntended = await sdkInstance.extendOracleTtl(oracleId, ttlToExtend, options) |
| 26 | + ``` |
| 27 | + |
| 28 | + - Post query to oracle (prepare and broadcast `oracle-query` transaction) |
| 29 | + ```js |
| 30 | + // queryFee is optional and default to 30000 |
| 31 | + // queryTtl is optional and default to { type: 'delta', value: 10 } |
| 32 | + // responseTtl is optional and default to { type: 'delta', value: 10 } |
| 33 | + const options = { fee, ttl, nonce, queryFee, queryTtl, responseTtl } |
| 34 | + // Get oracle object |
| 35 | + const oracle = await sdkInstance.getOracleObject(oracleId) |
| 36 | + |
| 37 | + const query = await oracle.postQuery("{'city': 'Berlin'}", options) |
| 38 | + // or |
| 39 | + const query = await sdkInstance.postQueryToOracle(oracleId, "{'city': 'Berlin'}", options) |
| 40 | + ``` |
| 41 | + |
| 42 | + - Poll for queries |
| 43 | + ```js |
| 44 | + const stopPolling = await oracle.pollQueries( |
| 45 | + (queries) => { |
| 46 | + console.log(queries) // log all new queries |
| 47 | + }, |
| 48 | + { interval: 1000 } // poll every second |
| 49 | + ) |
| 50 | + ``` |
| 51 | + - Poll for query response |
| 52 | + ```js |
| 53 | + const query = await sdkInstance.getQueryObject(oracleId, queryId) |
| 54 | +
|
| 55 | + // Poll for query reponse |
| 56 | + const response = await query.pollForResponse({ attempts: 2, interval: 1000 }) |
| 57 | + // or |
| 58 | + const response = await sdkInstance.pollForQueryResponse(oracleId, queryId, { attempts: 2, interval: 1000 }) |
| 59 | + // decode query response |
| 60 | + console.log(query.decode()) |
| 61 | + ``` |
| 62 | + - Respond to query (prepare and broadcast `oracle-responde` transaction) |
| 63 | + ```js |
| 64 | + const options = { ttl, fee, nonce, onAccount } |
| 65 | + const query = await sdkInstance.getQueryObject(oracleId, queryId) |
| 66 | + |
| 67 | + await query.respond({ tmp: 10 }, options) |
| 68 | + // or |
| 69 | + await sdkInstance.respondToQuery(oracleId, queryId, { tmp: 10 }, options) |
| 70 | + ``` |
| 71 | + |
| 72 | +## Related links |
| 73 | + - [Oracle protocol](https://github.com/aeternity/protocol/blob/master/oracles) |
| 74 | + - [Oracle SDK API Docs](https://github.com/aeternity/aepp-sdk-js/blob/develop/docs/api/ae/oracle.md) |
| 75 | + |
0 commit comments