From 7ca589094b3c0e9beedcd8f6c7094477877a7ee0 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 8 Jul 2019 14:02:00 +0200 Subject: [PATCH] Example with basic at and multi queries (#1076) * Example with at and basic multi queries * 05 --- .../examples/promise/05_read_storage/index.js | 4 +-- .../promise/05_read_storage_at/README.md | 5 ++++ .../promise/05_read_storage_at/index.js | 30 +++++++++++++++++++ .../promise/05_read_storage_at/package.json | 18 +++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 docs/examples/promise/05_read_storage_at/README.md create mode 100755 docs/examples/promise/05_read_storage_at/index.js create mode 100644 docs/examples/promise/05_read_storage_at/package.json diff --git a/docs/examples/promise/05_read_storage/index.js b/docs/examples/promise/05_read_storage/index.js index 5376c92d21..cef8535e2e 100755 --- a/docs/examples/promise/05_read_storage/index.js +++ b/docs/examples/promise/05_read_storage/index.js @@ -3,7 +3,7 @@ const { ApiPromise } = require('@polkadot/api'); // Our address for Alice on the dev chain -const Alice = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'; +const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'; async function main () { // Create our API with a default connection to the local node @@ -16,7 +16,7 @@ async function main () { api.query.session.validators() ]); - console.log(`accountNonce(${Alice}) ${accountNonce}`); + console.log(`accountNonce(${ALICE}) ${accountNonce}`); console.log(`blockPeriod ${blockPeriod.toNumber()} seconds`); if (validators && validators.length > 0) { diff --git a/docs/examples/promise/05_read_storage_at/README.md b/docs/examples/promise/05_read_storage_at/README.md new file mode 100644 index 0000000000..eb7192fad7 --- /dev/null +++ b/docs/examples/promise/05_read_storage_at/README.md @@ -0,0 +1,5 @@ +# Read storage, at a specific blockhash + +In addition to querying the latest storage, you can make storage queries at a specific blockhash. Be aware that the node applies a pruning strategy and typically only keeps the last 256 blocks, unless run in archive mode. + +<<< @/docs/examples/promise/07_read_storage_at/index.js diff --git a/docs/examples/promise/05_read_storage_at/index.js b/docs/examples/promise/05_read_storage_at/index.js new file mode 100755 index 0000000000..ceebaaea77 --- /dev/null +++ b/docs/examples/promise/05_read_storage_at/index.js @@ -0,0 +1,30 @@ +// @ts-check +// Import the API +const { ApiPromise } = require('@polkadot/api'); + +// Our address for Alice on the dev chain +const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'; +const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty'; + +async function main () { + // Create our API with a default connection to the local node + const api = await ApiPromise.create(); + + // retrieve the last block header, extracting the hash and parentHash + const { hash, parentHash } = await api.rpc.chain.getHeader(); + + console.log(`last header hash ${hash.toHex()}`); + + // retrieve the balance at the preceding block for Alice. For at queries + // the format is always `.at(, ...params)` + const balance = await api.query.balances.freeBalance.at(parentHash, ALICE); + + console.log(`Alice's balance at ${parentHash.toHex()} was ${balance}`); + + // now perform a multi query, returning multiple balances at once + const balances = await api.query.balances.freeBalance.multi([ALICE, BOB]); + + console.log(`Current balances for Alice and Bob are ${balances[0]} and ${balances[1]}`); +} + +main().catch(console.error).finally(_ => process.exit()); diff --git a/docs/examples/promise/05_read_storage_at/package.json b/docs/examples/promise/05_read_storage_at/package.json new file mode 100644 index 0000000000..eef922cd4e --- /dev/null +++ b/docs/examples/promise/05_read_storage_at/package.json @@ -0,0 +1,18 @@ +{ + "name": "05_read_storage_at", + "version": "0.2.0", + "description": "Example showing how to query storage at a specific block", + "main": "index.js", + "author": "Jaco Greeff ", + "license": "MIT", + "scripts": { + "clean": "rimraf node_modules", + "start": "node index.js" + }, + "dependencies": { + "@polkadot/api": "*" + }, + "devDependencies": { + "rimraf": "^2.6.2" + } +}