Files
api/docs/examples/promise/10_upgrade_chain/index.js
T
Jaco Greeff 7658399800 Add examples for upgradeKey & transfer events (#542)
* Add examples for upgradeKey & transfer events

* Remove CI-generated file

* Update pcakage.json for examples
2019-01-07 18:05:22 +01:00

53 lines
1.9 KiB
JavaScript
Executable File

// Import the API & Provider and some utility functions
const { ApiPromise } = require('@polkadot/api');
const { WsProvider } = require('@polkadot/rpc-provider');
// import the test keyring (already has dev keys for Alice, Bob, Charlie, Eve & Ferdie)
const testKeyring = require('@polkadot/keyring/testing');
async function main () {
// 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 (optional provider passed through)
const api = await ApiPromise.create(provider);
// retrieve the upgrade key from the chain state
const adminId = await api.query.upgradeKey.key();
// get the nonce for the admin key
const adminNonce = await api.query.system.accountNonce(adminId);
// find the actual keypair in the keyring (if this is an changed value, the key
// needs to be added to the keyring before - this assumes we have defaults, i.e.
// Alice as the key - and this exists on the test keyring)
const keyring = testKeyring.default();
const adminPair = keyring.getPair(adminId.toString());
console.log('Upgrading from', adminId.toString(), 'with nonce', adminNonce.toString());
// preform a chain upgrade, effectively bricking the chain, passing through
// a hex value, although a valid Uint8Array will also work here
api.tx.upgradeKey
.upgrade('0xdeadbeef')
.sign(adminPair, adminNonce)
.send(({ events = [], type }) => {
console.log('Transaction status:', type);
if (type === 'Finalised') {
console.error('You have just upgraded and bricked your chain');
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);