Created
May 22, 2019 09:34
Star
You must be signed in to star a gist
Sudo change user balance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-check | |
const fs = require('fs'); | |
// Import the API & Provider and some utility functions | |
const { ApiPromise, WsProvider } = require('@polkadot/api'); | |
// import the test keyring (already has dev keys for Alice, Bob, Charlie, Eve & Ferdie) | |
const testKeyring = require('@polkadot/keyring/testing'); | |
// some constants we are using in this sample | |
const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty'; | |
const FREE_AMOUNT = '10000'; | |
const RESERVED_AMOUNT = 10000; | |
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.sudo.key(); | |
// 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 already exists on the test keyring) | |
const keyring = testKeyring.default(); | |
const adminPair = keyring.getPair(adminId.toString()); | |
const bobPair = keyring.getPair(BOB); | |
const existingBobBalance = await api.query.balances.freeBalance(bobPair); | |
console.log(`Bob has an existing balance of ${existingBobBalance}`); | |
const setBobBalance = await api.tx.balances | |
.setBalance(BOB, FREE_AMOUNT, RESERVED_AMOUNT); | |
// preform the actual chain upgrade via the sudo module | |
await api.tx.sudo | |
.sudo(setBobBalance) | |
.signAndSend(adminPair, async ({ events = [], status }) => { | |
console.log('Status of setting the balance of Bob using Sudo:', status.type); | |
if (status.isFinalized) { | |
console.error('You have just changed the balance of Bob using Sudo'); | |
console.log('Completed at block hash', status.asFinalized.toHex()); | |
console.log('Events:'); | |
events.forEach(({ phase, event: { data, method, section } }) => { | |
console.log('\t', phase.toString(), `: ${section}.${method}`, data.toString()); | |
}); | |
const newBobBalance = await api.query.balances.freeBalance(bobPair); | |
console.log(`Bob has a new balance of ${newBobBalance}`); | |
process.exit(0); | |
} | |
}); | |
} | |
main().catch((error) => { | |
console.error(error); | |
process.exit(-1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment