Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
191 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Listen to balance changes | ||
|
||
This example shows how to instantiate a Polkadot API object and use it to connect to a node and retrieve balance updates. | ||
|
||
[include](index.js) |
35 changes: 35 additions & 0 deletions
35
docs/examples/promise/03_listen_to_balance_change/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// import the Api | ||
const { ApiPromise } = require('@polkadot/api'); | ||
|
||
// the known account we want to use (available on dev chain, with funds) | ||
const Alice = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ'; | ||
|
||
async function main () { | ||
// Create an wait for the API | ||
const api = await ApiPromise.create(); | ||
|
||
// Retrieve the initial balance. Since the call has no callback, it is simply a promise | ||
// the resolves to the current on-chain value | ||
let previous = await api.st.balances.freeBalance(Alice); | ||
|
||
console.log(`${Alice} has an ${previous} balance`); | ||
console.log(`You may leave this example running and start example 06 or transfer any value to ${Alice}`); | ||
|
||
// Here we subscribe to any balance changes and updates the on-screen value | ||
api.st.balances.freeBalance(Alice, (current) => { | ||
// Calculate the delta | ||
const change = current.sub(previous); | ||
|
||
// Only display positive value changes (Since we are pulling previous above already, | ||
// the intiial balance change will also be zero) | ||
if (change.isZero()) { | ||
return; | ||
} | ||
|
||
previous = current; | ||
|
||
console.log(`Balance of ${Alice}: ${current}, ${change} change`); | ||
}); | ||
} | ||
|
||
main().catch(console.error); |
18 changes: 18 additions & 0 deletions
18
docs/examples/promise/03_listen_to_balance_change/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "03_listen_to_balance_change", | ||
"version": "0.2.0", | ||
"description": "Example showing how to subscribe to balance change", | ||
"main": "index.js", | ||
"author": "chevdor", | ||
"license": "MIT", | ||
"scripts": { | ||
"clean": "rimraf node_modules", | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"@polkadot/api": "^0.31.18" | ||
}, | ||
"devDependencies": { | ||
"rimraf": "^2.6.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Read storage | ||
|
||
Many important variables are available through the storage API. This example shows how to call a few of those APIs. | ||
|
||
[include](index.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Import the ApiPromise | ||
const { ApiPromise } = require('@polkadot/api'); | ||
|
||
// Our address for Alice on the dev chain | ||
const Alice = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ'; | ||
|
||
async function main () { | ||
// Create out API with a default connection to the local node | ||
const api = await ApiPromise.create(); | ||
|
||
// Make our basic chain state/storage queries, all in one go | ||
const [accountNonce, blockPeriod, validators] = await Promise.all([ | ||
api.st.system.accountNonce(Alice), | ||
api.st.timestamp.blockPeriod(), | ||
api.st.session.validators() | ||
]); | ||
|
||
console.log(`accountNonce(${Alice}) ${accountNonce}`); | ||
console.log(`blockPeriod ${blockPeriod.toNumber()} seconds`); | ||
|
||
// get the balances for all validators | ||
const validatorBalances = await Promise.all( | ||
validators.map((authorityId) => | ||
api.st.balances.freeBalance(authorityId) | ||
) | ||
); | ||
|
||
console.log('validators', validators.map((authorityId, index) => ({ | ||
address: authorityId.toString(), | ||
balance: validatorBalances[index].toString() | ||
}))); | ||
} | ||
|
||
main().catch(console.error).finally(_ => process.exit()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "04_read_storage", | ||
"version": "0.2.0", | ||
"description": "Example showing how to transfer DOTs", | ||
"main": "index.js", | ||
"author": "chevdor", | ||
"license": "MIT", | ||
"scripts": { | ||
"clean": "rimraf node_modules", | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"@polkadot/api": "^0.31.18" | ||
}, | ||
"devDependencies": { | ||
"rimraf": "2.6.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Tranfer DOTs | ||
|
||
This sample shows how to create a transaction to make a transfer from one an account to another. | ||
|
||
[include](index.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Import our API, Keyring and some utility functions | ||
const { ApiPromise } = require('@polkadot/api'); | ||
const Keyring = require('@polkadot/util-keyring').default; | ||
const u8aFromUtf8 = require('@polkadot/util/u8a/fromUtf8').default; | ||
|
||
const ALICE_SEED = 'Alice'.padEnd(32, ' '); | ||
const BOB_ADDR = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE'; | ||
|
||
async function main () { | ||
// create an instance of the keyring | ||
const keyring = new Keyring(); | ||
|
||
// Add Alice to our keyring (with the known seed for the account) | ||
const alice = keyring.addFromSeed(u8aFromUtf8(ALICE_SEED)); | ||
|
||
// instantiate the API | ||
const api = await ApiPromise.create(); | ||
|
||
// retrieve the nonce for Alice, used to sign the transaction | ||
const aliceNonce = await api.st.system.accountNonce(alice.address()); | ||
|
||
// Create a extrinsic, transferring 12345 units to Bob. We can also create, | ||
// sign and send in one operation (as per the samples in the Api documentation), | ||
// here we split it out for the sake of readability | ||
const transfer = api.tx.balances.transfer(BOB_ADDR, 12345); | ||
|
||
// sign the transaction using our account | ||
transfer.sign(alice, aliceNonce); | ||
|
||
// send the transaction and retrieve the resulting Hash | ||
const hash = await transfer.send(); | ||
|
||
console.log(`transfer 12345 to Bob with hash ${hash}`); | ||
} | ||
|
||
main().catch(console.error).finally(_ => process.exit()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "07_transfer_dots", | ||
"version": "0.2.0", | ||
"description": "Example showing how to transfer DOTs", | ||
"main": "index.js", | ||
"author": "chevdor", | ||
"license": "MIT", | ||
"scripts": { | ||
"clean": "rimraf node_modules", | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"@polkadot/api": "^0.31.18" | ||
}, | ||
"devDependencies": { | ||
"rimraf": "2.6.2" | ||
} | ||
} |