* Add examples for upgradeKey & transfer events * Remove CI-generated file * Update pcakage.json for examples
53 lines
1.7 KiB
JavaScript
Executable File
53 lines
1.7 KiB
JavaScript
Executable File
// Import the API & Provider and some utility functions
|
|
const { ApiPromise } = require('@polkadot/api');
|
|
|
|
// import the test keyring (already has dev keys for Alice, Bob, Charlie, Eve & Ferdie)
|
|
const testKeyring = require('@polkadot/keyring/testing');
|
|
|
|
// utility function for random values
|
|
const { randomAsU8a } = require('@polkadot/util-crypto');
|
|
|
|
// some constants we are using in this sample
|
|
const ALICE = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ';
|
|
const AMOUNT = 10000;
|
|
|
|
async function main () {
|
|
// create an instance of our testign keyring
|
|
const keyring = testKeyring.default();
|
|
|
|
// Create the API and wait until ready
|
|
const api = await ApiPromise.create();
|
|
|
|
// get the nonce for the admin key
|
|
const aliceNonce = await api.query.system.accountNonce(ALICE);
|
|
|
|
// find the actual keypair in the keyring
|
|
const alicePair = keyring.getPair(ALICE);
|
|
|
|
// create a new random recipient
|
|
const recipient = keyring.addFromSeed(randomAsU8a(32)).address();
|
|
|
|
console.log('Sending', AMOUNT, 'from', alicePair.address(), 'to', recipient, 'with nonce', aliceNonce.toString());
|
|
|
|
// Do the transfer and track the actual status
|
|
api.tx.balances
|
|
.transfer(recipient, AMOUNT)
|
|
.sign(alicePair, aliceNonce)
|
|
.send(({ events = [], status, type }) => {
|
|
console.log('Transaction status:', type);
|
|
|
|
if (type === 'Finalised') {
|
|
console.log('Completed at block hash', status.value.toHex());
|
|
console.log('Events:');
|
|
|
|
events.forEach(({ phase, event: { data, method, section } }) => {
|
|
console.log('\t', phase.toString(), `: ${section}.${method}`, data.toString());
|
|
});
|
|
|
|
process.exit(0);
|
|
}
|
|
});
|
|
}
|
|
|
|
main().catch(console.error);
|