Skip to content

Commit

Permalink
Merge 62e77b7 into 1dd3cfc
Browse files Browse the repository at this point in the history
  • Loading branch information
ltfschoen committed Oct 12, 2018
2 parents 1dd3cfc + 62e77b7 commit 53a5585
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions docs/examples/promise/01_simple_connect/index.js
@@ -1,15 +1,15 @@
// required imports
// Required imports
const { ApiPromise } = require('@polkadot/api');
const { WsProvider } = require('@polkadot/rpc-provider');

async function main () {
// initialise the provider to connect to the local node
// Initialise the provider to connect to the local node
const provider = new WsProvider('ws://127.0.0.1:9944');

// create the API and wait until ready
// Create the API and wait until ready
const api = await ApiPromise.create(provider);

// retrieve the chain & node information information via rpc calls
// Retrieve the chain & node information information via rpc calls
const [chain, nodeName, nodeVersion] = await Promise.all([
api.rpc.system.chain(),
api.rpc.system.name(),
Expand Down
8 changes: 4 additions & 4 deletions docs/examples/promise/02_listen_to_blocks/index.js
@@ -1,4 +1,4 @@
// API imports
// Import the API
const { ApiPromise } = require('@polkadot/api');

async function main () {
Expand All @@ -10,13 +10,13 @@ async function main () {
// Subscribe to the new headers on-chain. The callback is fired when new headers
// are found, the call itself returns a promise with a subscription that can be
// used to unsubscribe from the newHead subscription
const subsciptionId = await api.rpc.chain.newHead((header) => {
const subscriptionId = await api.rpc.chain.newHead((header) => {
console.log(`best #${header.blockNumber}`);
});

// id for the subscription, we can cleanup and unsubscribe via
// Id for the subscription, we can cleanup and unsubscribe via
// `api.chain.newHead.unsubscribe(subscriptionId)`
console.log(`subsciptionId: ${subsciptionId}`);
console.log(`subsciptionId: ${subscriptionId}`);
}

main().catch(console.error);
16 changes: 8 additions & 8 deletions docs/examples/promise/03_listen_to_balance_change/index.js
@@ -1,27 +1,27 @@
// import the Api
// Import the Api
const { ApiPromise } = require('@polkadot/api');

// the known account we want to use (available on dev chain, with funds)
// Known account we want to use (available on dev chain, with funds)
const Alice = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ';

async function main () {
// Create an wait for the API
// Create an await 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
// that resolves to the current on-chain value
let previous = await api.query.balances.freeBalance(Alice);

console.log(`${Alice} has an ${previous} balance`);
console.log(`${Alice} has a ${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
// Here we subscribe to any balance changes and update the on-screen value
api.query.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)
// Only display positive value changes (Since we are pulling `previous` above already,
// the initial balance change will also be zero)
if (change.isZero()) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/promise/05_read_storage/index.js
@@ -1,11 +1,11 @@
// Import the ApiPromise
// Import the API
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
// Create our 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
Expand All @@ -18,7 +18,7 @@ async function main () {
console.log(`accountNonce(${Alice}) ${accountNonce}`);
console.log(`blockPeriod ${blockPeriod.toNumber()} seconds`);

// get the balances for all validators
// Retrieve the balances for all validators
const validatorBalances = await Promise.all(
validators.map((authorityId) =>
api.query.balances.freeBalance(authorityId)
Expand Down
12 changes: 6 additions & 6 deletions docs/examples/promise/07_transfer_dots/index.js
@@ -1,4 +1,4 @@
// Import our API, Keyring and some utility functions
// Import the API, Keyring and some utility functions
const { ApiPromise } = require('@polkadot/api');
const Keyring = require('@polkadot/keyring').default;
const u8aFromUtf8 = require('@polkadot/util/u8a/fromUtf8').default;
Expand All @@ -7,27 +7,27 @@ const ALICE_SEED = 'Alice'.padEnd(32, ' ');
const BOB_ADDR = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE';

async function main () {
// create an instance of the keyring
// 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
// Instantiate the API
const api = await ApiPromise.create();

// retrieve the nonce for Alice, used to sign the transaction
// Retrieve the nonce for Alice, to be used to sign the transaction
const aliceNonce = await api.query.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
// Sign the transaction using our account
transfer.sign(alice, aliceNonce);

// send the transaction and retrieve the resulting Hash
// Send the transaction and retrieve the resulting Hash
const hash = await transfer.send();

console.log(`transfer 12345 to Bob with hash ${hash}`);
Expand Down

0 comments on commit 53a5585

Please sign in to comment.