docs: add examples from polkadot

This commit is contained in:
ian.he
2019-04-12 17:40:12 +12:00
parent f103863fdd
commit c14a071b74
59 changed files with 462 additions and 74 deletions
@@ -0,0 +1,5 @@
# Make a simple transfer
This sample shows how to create a transaction to make a transfer from one an account to another.
<<< @/docs/examples/rx/06_make_transfer/index.js
+12 -14
View File
@@ -1,21 +1,19 @@
// Import the API, Keyring and some utility functions
const { ApiPromise } = require('@polkadot/api');
const { ApiRx } = require('@polkadot/api');
const { Keyring } = require('@polkadot/keyring');
const { stringToU8a } = require('@polkadot/util');
const ALICE_SEED = 'Alice'.padEnd(32, ' ');
const BOB_ADDR = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE';
const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
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(stringToU8a(ALICE_SEED));
// Instantiate the API
const api = await ApiRx.create().toPromise();
// Create an instance of the keyring
const keyring = new Keyring({ type: 's25519' });
// Add Alice to our keyring (with the known seed for the account)
const alice = keyring.addFomUri('//Alice');
// Create a extrinsic, transferring 12345 units to Bob.
api.tx.balances
// create transfer
@@ -23,11 +21,11 @@ async function main () {
// Sign and send the transcation
.signAndSend(alice)
// Subscribe to the status updates of the transfer
.subscribe(({ status, type }) => {
if (type === 'Finalised') {
console.log(`Successful transfer of 12345 from Alice to Bob with hash ${status.asFinalised.toHex()}`);
.subscribe(({ status }) => {
if (status.isFinalized) {
console.log(`Successful transfer of 12345 from Alice to Bob with hash ${status.asFinalized.toHex()}`);
} else {
console.log(`Staus of transfer: ${type}`);
console.log(`Staus of transfer: ${status.type}`);
}
});
}
@@ -10,7 +10,7 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.43.6"
"@polkadot/api": "^0.53.1"
},
"devDependencies": {
"rimraf": "^2.6.2"