Add examples for upgradeKey & transfer events (#542)
* Add examples for upgradeKey & transfer events * Remove CI-generated file * Update pcakage.json for examples
This commit is contained in:
@@ -11,8 +11,8 @@
|
||||
},
|
||||
|
||||
"dependencies": {
|
||||
"@polkadot/api": "^0.33.17",
|
||||
"@polkadot/rpc-provider": "^0.33.17"
|
||||
"@polkadot/api": "^0.37.6",
|
||||
"@polkadot/rpc-provider": "^0.37.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/api": "^0.33.17"
|
||||
"@polkadot/api": "^0.37.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/api": "^0.33.17"
|
||||
"@polkadot/api": "^0.37.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "04_read_storage",
|
||||
"name": "05_read_storage",
|
||||
"version": "0.2.0",
|
||||
"description": "Example showing how to query storage",
|
||||
"main": "index.js",
|
||||
@@ -10,7 +10,7 @@
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/api": "^0.33.17"
|
||||
"@polkadot/api": "^0.37.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/api": "^0.33.17"
|
||||
"@polkadot/api": "^0.37.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/api": "^0.33.17"
|
||||
"@polkadot/api": "^0.37.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# Transfer events
|
||||
|
||||
Display the events that occur during a transfer by sending a value to a random account
|
||||
|
||||
[include](index.js)
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
// 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);
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "09_transfer_events",
|
||||
"version": "0.1.0",
|
||||
"description": "Example showing how to transfer DOTs (with events)",
|
||||
"main": "index.js",
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"clean": "rimraf node_modules",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/api": "^0.37.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# Chain upgrade
|
||||
|
||||
Performs a chain upgrade using the `upgradeKey` module. This will brick your chain, so us it as an educational sample. (use `substrate --dev purge-chain` to remove DB and recover)
|
||||
|
||||
[include](index.js)
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
// 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);
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "10_upgrade_chain",
|
||||
"version": "0.2.0",
|
||||
"description": "Example showing how to upgrade via upgradeKey",
|
||||
"main": "index.js",
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"clean": "rimraf node_modules",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/api": "^0.37.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user