Skip to content

Commit

Permalink
Merge pull request #1995 from aeternity/oracle-example
Browse files Browse the repository at this point in the history
docs: add Oracle example in nodejs
  • Loading branch information
davidyuk committed Jun 18, 2024
2 parents 4a8d5ac + 45f3d94 commit ec7cc8b
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ module.exports = {
'no-restricted-globals': 'off',
'import/no-unresolved': 'off',
},
}, {
files: 'examples/node/**/*',
rules: { 'no-restricted-syntax': 'off' },
}, {
files: ['tooling/**/*', 'examples/node/*', '**/*.config.js', '.eslintrc.js'],
rules: {
Expand Down
2 changes: 1 addition & 1 deletion docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This package is expected to work in these environments:
| create-react-native-app@3 (webpack@4) | mjs build is not compatible with webpack@4 [cra-webpack-4] |
| meteor@2 | |
| jest@27.5.1 | requires an environment where Buffer is instanceof Uint8Array [jest] |
| typescript>=4.1, ideally >=4.7 | requires `tsconfig.json` adjustments [typescript] |
| typescript>=4.2, ideally >=4.7 | requires `tsconfig.json` adjustments [typescript] |
| vite@3 | requires `build.target: 'es2020'` and `bigint: true` in vite.config.js [vite] |

[webpack-4]: https://github.com/webpack/webpack/issues/7482#issuecomment-394884837
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ try {
}

// using generic error classes
const {AensError, TransactionError, BaseError } = require('@aeternity/aepp-sdk')
import { AensError, TransactionError, BaseError } from '@aeternity/aepp-sdk'

try {
const spendTxResult = await aeSdk.spend(1, "ak_2tv", { onAccount: payerAccount})
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Create a [new issue](https://github.com/aeternity/aepp-sdk-js/issues/new) to sug
3. [Paying for spend tx](node/paying-for-spend-tx.mjs)
4. [Paying for contract call tx](node/paying-for-contract-call-tx.mjs)
5. [Dry-run using debug endpoint](node/dry-run-using-debug-endpoint.mjs)
6. [Oracle](node/oracle.mjs)
4 changes: 2 additions & 2 deletions examples/browser/wallet-web-extension/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ module.exports = {
configureWebpack: {
resolve: {
alias: {
'@azure/core-client': '@azure/core-client/dist-esm/src/index.js',
'@azure/core-rest-pipeline': '@azure/core-rest-pipeline/dist-esm/src/index.js',
'@azure/core-client': '@azure/core-client/dist/browser/index.js',
'@azure/core-rest-pipeline': '@azure/core-rest-pipeline/dist/browser/index.js',
},
},
},
Expand Down
50 changes: 50 additions & 0 deletions examples/node/oracle.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env node
/*
# 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.
[factorial]: https://en.wikipedia.org/wiki/Factorial
[guide]: ../../guides/oracles.md
*/
import {
Node, AeSdk, MemoryAccount, Oracle, OracleClient,
} from '@aeternity/aepp-sdk';

// Let's prepare sdk and account for Oracle
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(1e14, oracleAccount.address);
console.log('Spend done');

// Creating and registering Oracle
const oracle = new Oracle(oracleAccount, aeSdk.getContext());
await oracle.register('factorial argument', 'factorial value');
console.log('Oracle registered');

// Start listening for queries and handle them
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();
});

// Creating an Oracle client, making some queries.
// Assume it is done in a separate script/process/computer.
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}`);
}

// We don't need to handle queries anymore, so we can stop the listener.
stop();
Empty file modified examples/node/paying-for-contract-call-tx.mjs
100644 → 100755
Empty file.
Empty file modified examples/node/paying-for-spend-tx.mjs
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ nav:
- examples/node/paying-for-spend-tx.md
- examples/node/account-generalized.md
- examples/node/dry-run-using-debug-endpoint.md
- examples/node/oracle.md
- Browser:
- 'SDK tools': examples/browser/tools/index.html
- 'Aepp example': examples/browser/aepp/index.html
Expand Down
1 change: 1 addition & 0 deletions test/examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ run_node_example paying-for-contract-call-tx.mjs
run_node_example paying-for-spend-tx.mjs
run_node_example transfer-ae.mjs
run_node_example dry-run-using-debug-endpoint.mjs
run_node_example oracle.mjs

# TODO: revisit --ignore-scripts after solving https://github.com/npm/cli/issues/4202
perl -i -pe 's/"prepare"/"rem-prepare"/g' package.json
Expand Down

0 comments on commit ec7cc8b

Please sign in to comment.