* Move triple backticks onto new line so it doesn't break the code snippet formatting
* Add newline between dependency imports and variable declarations
* Add link to relevant line of Github repo of eventemitter3 instead of the travis build directory
* Add full stop at end of sentence in line before `@example` in .ts files, since I noticed that was the only difference between code snippet in docs that was being rendered correctly and one that was not (if the docs were generated from it). Not sure if this actually makes any difference though, but it doesn't hurt to try. i.e. change `[[unsubscribe]]` to `[[unsubscribe]].`
* Add missing code syntax for `new Ws(defaults.WS_URL)`
* Add missing `*__example__*:`
* Add missing semi-colons throughout code
* Fix typos and some grammar
* Move import of external libraries i.e. `const BN = require('bn.js');` or `const crypto = require('crypto');` before `@polkadot` ones in code
* Change from `e` to `error` in catch blocks for readable documentation
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
const Api = require('@polkadot/api').default
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944')
|
||||
const api = new Api(provider)
|
||||
const Api = require('@polkadot/api').default;
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default;
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944');
|
||||
const api = new Api(provider);
|
||||
|
||||
async function getChain () {
|
||||
return api.system.chain()
|
||||
return api.system.chain();
|
||||
}
|
||||
|
||||
async function main () {
|
||||
const chain = await getChain()
|
||||
const chain = await getChain();
|
||||
|
||||
console.log('You are connected to chain:', chain)
|
||||
console.log('You are connected to chain:', chain);
|
||||
}
|
||||
|
||||
main().finally(_ => process.exit())
|
||||
main().finally(_ => process.exit());
|
||||
|
||||
@@ -4,6 +4,6 @@ This example shows how to subscribe to new blocks.
|
||||
|
||||
It displays the block number every time a new block is seen by the node you are connected to.
|
||||
|
||||
NOTE: The example runs until you stop it wit CTRL+C
|
||||
NOTE: The example runs until you stop it with CTRL+C
|
||||
|
||||
[include](index.js)
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
const BN = require('bn.js')
|
||||
const Api = require('@polkadot/api').default
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944')
|
||||
const api = new Api(provider)
|
||||
const BN = require('bn.js');
|
||||
const Api = require('@polkadot/api').default;
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default;
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944');
|
||||
const api = new Api(provider);
|
||||
|
||||
function main () {
|
||||
console.log('Blocks will be shown as they come in, CTRL+C to stop:')
|
||||
console.log('Blocks will be shown as they come in, CTRL+C to stop:');
|
||||
api.chain.newHead((_, header) => {
|
||||
const bnBlockNumber = new BN(header.number, 16)
|
||||
const bnBlockNumber = new BN(header.number, 16);
|
||||
|
||||
console.log('#' + bnBlockNumber.toString(10))
|
||||
})
|
||||
console.log('#' + bnBlockNumber.toString(10));
|
||||
});
|
||||
}
|
||||
|
||||
main()
|
||||
main();
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
const Api = require('@polkadot/api').default
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default
|
||||
const storage = require('@polkadot/storage').default
|
||||
const BN = require('bn.js')
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944')
|
||||
const api = new Api(provider)
|
||||
const Alice = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ'
|
||||
const BN = require('bn.js');
|
||||
const Api = require('@polkadot/api').default;
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default;
|
||||
const storage = require('@polkadot/storage').default;
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944');
|
||||
const api = new Api(provider);
|
||||
const Alice = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ';
|
||||
|
||||
console.log('You may let this example running and start the example 06 or send DOTs to ' + Alice)
|
||||
console.log('You may leave this example running and start example 06 or send DOTs to ' + Alice);
|
||||
|
||||
api.state
|
||||
.storage([[storage.staking.public.freeBalanceOf, Alice]], (_, values) => {
|
||||
console.log(`Balance of ${Alice}: ${new BN(values[0]).toString(10)} DOTs`)
|
||||
console.log(`Balance of ${Alice}: ${new BN(values[0]).toString(10)} DOTs`);
|
||||
})
|
||||
.then((subscriptionId) => {
|
||||
console.log('Balance changes subscription id:', subscriptionId)
|
||||
})
|
||||
console.log('Balance changes subscription id:', subscriptionId);
|
||||
});
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
const Keyring = require('@polkadot/util-keyring').default
|
||||
const u8aToHex = require('@polkadot/util/u8a/toHex').default
|
||||
const Encoder = new TextEncoder() // always utf-8
|
||||
const crypto = require('crypto')
|
||||
const crypto = require('crypto');
|
||||
const Keyring = require('@polkadot/util-keyring').default;
|
||||
const u8aToHex = require('@polkadot/util/u8a/toHex').default;
|
||||
const Encoder = new TextEncoder(); // always utf-8
|
||||
|
||||
// A fixed seed from an array
|
||||
const SEED1 = new Uint8Array([
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
|
||||
22, 23, 24, 25, 26, 27, 28, 29, 30, 0, 0
|
||||
])
|
||||
22, 23, 24, 25, 26, 27, 28, 29, 30, 0, 0
|
||||
]);
|
||||
|
||||
// A fixed seed from a 32 chars string
|
||||
const SEED2 = Encoder.encode('correct horse battery and staple')
|
||||
const SEED2 = Encoder.encode('correct horse battery and staple');
|
||||
|
||||
const keyring = new Keyring()
|
||||
const pair1 = keyring.addFromSeed(SEED1)
|
||||
const pair2 = keyring.addFromSeed(SEED2)
|
||||
const keyring = new Keyring();
|
||||
const pair1 = keyring.addFromSeed(SEED1);
|
||||
const pair2 = keyring.addFromSeed(SEED2);
|
||||
|
||||
console.log('Deterministic addresses (same for each run):')
|
||||
console.log(`Address 1\t ${pair1.address()}\t Seed: ${u8aToHex(SEED1)}`)
|
||||
console.log(`Address 2\t ${pair2.address()}\t Seed: ${u8aToHex(SEED2)}`)
|
||||
console.log('Deterministic addresses (same for each run):');
|
||||
console.log(`Address 1\t ${pair1.address()}\t Seed: ${u8aToHex(SEED1)}`);
|
||||
console.log(`Address 2\t ${pair2.address()}\t Seed: ${u8aToHex(SEED2)}`);
|
||||
|
||||
// A random seed
|
||||
const SEED = new Uint8Array(32)
|
||||
const SEED = new Uint8Array(32);
|
||||
|
||||
console.log('Random Addresses (random for each run):')
|
||||
console.log('Random Addresses (random for each run):');
|
||||
|
||||
for (var i = 3; i < 13; i++) {
|
||||
Buffer.from(crypto.randomFillSync(SEED))
|
||||
const pair3 = keyring.addFromSeed(SEED)
|
||||
console.log(`Address ${i}\t ${pair3.address()}\t Seed: ${u8aToHex(SEED)}`)
|
||||
Buffer.from(crypto.randomFillSync(SEED));
|
||||
const pair3 = keyring.addFromSeed(SEED);
|
||||
console.log(`Address ${i}\t ${pair3.address()}\t Seed: ${u8aToHex(SEED)}`);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Read storage
|
||||
|
||||
Many important variable are available through the storage API. This example shows how to call a few of those APIs.
|
||||
Many important variables are available through the storage API. This example shows how to call a few of those APIs.
|
||||
|
||||
[include](index.js)
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
const Api = require('@polkadot/api').default
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default
|
||||
const storage = require('@polkadot/storage').default
|
||||
const BN = require('bn.js')
|
||||
const BN = require('bn.js');
|
||||
const Api = require('@polkadot/api').default;
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default;
|
||||
const storage = require('@polkadot/storage').default;
|
||||
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944')
|
||||
const api = new Api(provider)
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944');
|
||||
const api = new Api(provider);
|
||||
|
||||
async function getAccountIndex (address) {
|
||||
return api.state.getStorage([storage.system.public.accountIndexOf, address])
|
||||
return api.state.getStorage([storage.system.public.accountIndexOf, address]);
|
||||
}
|
||||
|
||||
async function getValidators () {
|
||||
return api.state.getStorage([storage.session.public.validators])
|
||||
return api.state.getStorage([storage.session.public.validators]);
|
||||
}
|
||||
|
||||
async function getBlockPeriod () {
|
||||
return api.state.getStorage([storage.timestamp.public.blockPeriod])
|
||||
return api.state.getStorage([storage.timestamp.public.blockPeriod]);
|
||||
}
|
||||
|
||||
async function main () {
|
||||
const address = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ'
|
||||
const validators = await getValidators()
|
||||
const index = await getAccountIndex(address)
|
||||
const blockPeriod = await getBlockPeriod()
|
||||
const address = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ';
|
||||
const validators = await getValidators();
|
||||
const index = await getAccountIndex(address);
|
||||
const blockPeriod = await getBlockPeriod();
|
||||
|
||||
console.log(`BlockPeriod: ${new BN(blockPeriod).toString(10)} seconds`)
|
||||
console.log(`Account Index of ${address}: ${index.toString(10)}`)
|
||||
console.log('Current validators', validators)
|
||||
console.log(`BlockPeriod: ${new BN(blockPeriod).toString(10)} seconds`);
|
||||
console.log(`Account Index of ${address}: ${index.toString(10)}`);
|
||||
console.log('Current validators', validators);
|
||||
}
|
||||
|
||||
main().finally(_ => process.exit())
|
||||
main().finally(_ => process.exit());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Craft Extrinsic
|
||||
|
||||
In the current version, crafting an extrinsic is a rather cumbersome process. This sample guides you though it and shows how to tranfer DOTs from an account to another.
|
||||
In the current version, crafting an extrinsic is a rather cumbersome process. This sample guides you through it and shows how to tranfer DOTs from one an account to another.
|
||||
|
||||
[include](index.js)
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
const Api = require('@polkadot/api').default
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default
|
||||
const extrinsics = require('@polkadot/extrinsics').default
|
||||
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode').default
|
||||
const encodeLength = require('@polkadot/extrinsics/codec/encode/length').default
|
||||
const Keyring = require('@polkadot/util-keyring').default
|
||||
const storage = require('@polkadot/storage').default
|
||||
const BN = require('bn.js')
|
||||
const BN = require('bn.js');
|
||||
const Api = require('@polkadot/api').default;
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default;
|
||||
const extrinsics = require('@polkadot/extrinsics').default;
|
||||
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode').default;
|
||||
const encodeLength = require('@polkadot/extrinsics/codec/encode/length').default;
|
||||
const Keyring = require('@polkadot/util-keyring').default;
|
||||
const storage = require('@polkadot/storage').default;
|
||||
|
||||
const Encoder = new TextEncoder()
|
||||
const keyring = new Keyring()
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944')
|
||||
const api = new Api(provider)
|
||||
const Encoder = new TextEncoder();
|
||||
const keyring = new Keyring();
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944');
|
||||
const api = new Api(provider);
|
||||
|
||||
async function getAccountIndex (address) {
|
||||
return api.state.getStorage([storage.system.public.accountIndexOf, address])
|
||||
return api.state.getStorage([storage.system.public.accountIndexOf, address]);
|
||||
}
|
||||
|
||||
async function transfer (keyRingFrom, addressTo, amount) {
|
||||
const accountIndex = await getAccountIndex(keyRingFrom.address())
|
||||
const accountIndex = await getAccountIndex(keyRingFrom.address());
|
||||
|
||||
console.log(`Current accountIndex: ${accountIndex}`)
|
||||
console.log(`Current accountIndex: ${accountIndex}`);
|
||||
|
||||
// encode the call for signing
|
||||
const message = encodeExtrinsic(
|
||||
@@ -27,10 +27,10 @@ async function transfer (keyRingFrom, addressTo, amount) {
|
||||
accountIndex,
|
||||
extrinsics.staking.public.transfer,
|
||||
[addressTo, amount]
|
||||
)
|
||||
);
|
||||
|
||||
// get the signature
|
||||
const signature = keyRingFrom.sign(message)
|
||||
const signature = keyRingFrom.sign(message);
|
||||
|
||||
// encode the extrinsic for submission, adding the length (prefix) and the
|
||||
// signature (postfix)Rödl
|
||||
@@ -39,22 +39,18 @@ async function transfer (keyRingFrom, addressTo, amount) {
|
||||
new Uint8Array([0xff]),
|
||||
message,
|
||||
signature
|
||||
)
|
||||
);
|
||||
|
||||
return api.author.submitExtrinsic(encoded)
|
||||
return api.author.submitExtrinsic(encoded);
|
||||
}
|
||||
|
||||
const Alice = keyring.addFromSeed(Encoder.encode('Alice '))
|
||||
const addressBob = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE'
|
||||
const amount = new BN(999) // on dev chain, the fee is 1, that makes it a round 1000
|
||||
const Alice = keyring.addFromSeed(Encoder.encode('Alice '));
|
||||
const addressBob = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE';
|
||||
const amount = new BN(999); // on dev chain, the fee is 1, that makes it a round 1000
|
||||
|
||||
console.log(`Crafting and sending an extrinsic for Alice to send Bob ${amount} DOTs`)
|
||||
console.log(`Crafting and sending an extrinsic for Alice to send Bob ${amount} DOTs`);
|
||||
|
||||
transfer(Alice, addressBob, amount)
|
||||
.then(() =>
|
||||
console.log('Sent')
|
||||
)
|
||||
.catch((e) =>
|
||||
console.log('HINT: Make sure you wait for the storage to update before sending the next extrinsic', e)
|
||||
)
|
||||
.finally(_ => process.exit(0))
|
||||
.then(() => console.log('Sent'))
|
||||
.catch((error) => console.log('HINT: Make sure you wait for the storage to update before sending the next extrinsic', error))
|
||||
.finally(_ => process.exit(0));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Tranfer DOTs
|
||||
|
||||
This sample is more 'straight to the point' and shows the easiest way to transfer DOTs from an account to another.
|
||||
This sample is more 'straight to the point' and shows the easiest way to transfer DOTs from one an account to another.
|
||||
|
||||
[include](index.js)
|
||||
|
||||
@@ -1,23 +1,22 @@
|
||||
const Api = require('@polkadot/api').default
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default
|
||||
const extrinsics = require('@polkadot/extrinsics').default
|
||||
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode/uncheckedLength').default
|
||||
const encodeLength = require('@polkadot/extrinsics/codec/encode/length').default
|
||||
const Keyring = require('@polkadot/util-keyring').default
|
||||
const storage = require('@polkadot/storage').default
|
||||
const BN = require('bn.js')
|
||||
const BN = require('bn.js');
|
||||
const Api = require('@polkadot/api').default;
|
||||
const WsProvider = require('@polkadot/api-provider/ws').default;
|
||||
const extrinsics = require('@polkadot/extrinsics').default;
|
||||
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode/uncheckedLength').default;
|
||||
const Keyring = require('@polkadot/util-keyring').default;
|
||||
const storage = require('@polkadot/storage').default;
|
||||
|
||||
const Encoder = new TextEncoder()
|
||||
const keyring = new Keyring()
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944')
|
||||
const api = new Api(provider)
|
||||
const Encoder = new TextEncoder();
|
||||
const keyring = new Keyring();
|
||||
const provider = new WsProvider('ws://127.0.0.1:9944');
|
||||
const api = new Api(provider);
|
||||
|
||||
async function getAccountIndex (address) {
|
||||
return api.state.getStorage([storage.system.public.accountIndexOf, address])
|
||||
return api.state.getStorage([storage.system.public.accountIndexOf, address]);
|
||||
}
|
||||
|
||||
async function transfer (keyRingFrom, addressTo, amount) {
|
||||
const nextAccountIndex = await getAccountIndex(keyRingFrom.address())
|
||||
const nextAccountIndex = await getAccountIndex(keyRingFrom.address());
|
||||
|
||||
// encode the call for signing
|
||||
const encoded = encodeExtrinsic(
|
||||
@@ -25,19 +24,19 @@ async function transfer (keyRingFrom, addressTo, amount) {
|
||||
nextAccountIndex,
|
||||
extrinsics.staking.public.transfer,
|
||||
[addressTo, amount]
|
||||
)
|
||||
);
|
||||
|
||||
await api.author.submitExtrinsic(encoded)
|
||||
.catch(e => console.log)
|
||||
.catch(e => console.log);
|
||||
}
|
||||
|
||||
const Alice = keyring.addFromSeed(Encoder.encode('Alice '))
|
||||
const addressBob = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE'
|
||||
const amount = new BN(999) // on dev chain, the fee is 1, that makes it a round 1000
|
||||
const Alice = keyring.addFromSeed(Encoder.encode('Alice '));
|
||||
const addressBob = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE';
|
||||
const amount = new BN(999); // on dev chain, the fee is 1, that makes it a round 1000
|
||||
|
||||
console.log(`Crafting and sending an extrinsic for Alice to send Bob ${amount} DOTs`)
|
||||
console.log(`Crafting and sending an extrinsic for Alice to send Bob ${amount} DOTs`);
|
||||
|
||||
transfer(Alice, addressBob, amount)
|
||||
.then(() => console.log('Done'))
|
||||
.catch((e) => console.log(e))
|
||||
.finally(_ => process.exit(0))
|
||||
.catch((error) => console.log(error))
|
||||
.finally(_ => process.exit(0));
|
||||
|
||||
@@ -11,9 +11,9 @@ Some of the examples use the following accounts:
|
||||
- Alice: `5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ`
|
||||
- Bob: `5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE`
|
||||
|
||||
Those accounts are easy to add if you don´t have/see them. The seed of Alice´s account is `Alice␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣` and the seed of Bob is... well you guess...
|
||||
Those accounts are easy to add if you don't have/see them. The seed of Alice's account is `Alice␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣␣` and the seed of Bob is... well you guess...
|
||||
|
||||
NOTE: Note the spaces padding Alice´s key up to 32 chars.
|
||||
NOTE: Note the spaces padding Alice's key up to 32 chars.
|
||||
|
||||
## Install
|
||||
|
||||
|
||||
Reference in New Issue
Block a user