Register and query Oracle
Here we will register and query an oracle returning factorial of a number. Read more about oracles in the guide section.
import { Node, AeSdk, MemoryAccount, Oracle, OracleClient } from '@aeternity/aepp-sdk';
const node = new Node('https://testnet.aeternity.io');
const oracleAccount = MemoryAccount.generate();
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [new MemoryAccount('sk_2CuofqWZHrABCrM7GY95YSQn8PyFvKQadnvFnpwhjUnDCFAWmf')],
});
await aeSdk.spend(2e14, oracleAccount.address);
console.log('Spend done');
const oracle = new Oracle(oracleAccount, aeSdk.getContext());
await oracle.register('factorial argument', 'factorial value');
console.log('Oracle registered');
const stop = oracle.handleQueries((query) => {
const arg = BigInt(query.decodedQuery);
if (arg < 0) return "argument can't be negative";
let res = 1n;
for (let i = 2n; i <= arg; i += 1n) {
res *= i;
}
return res.toString();
});
const oracleClient = new OracleClient(oracle.address, aeSdk.getContext());
for (const el of [1, 4, 20, 70, -5]) {
const response = await oracleClient.query(el.toString());
console.log(`query ${el}, response ${response}`);
}
stop();