Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions scripts/runtime-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@
// Usage:
// ./bin/cennz-cli script ./scripts/runtime-upgrade.js //<sudo URI> </path/to/new-runtime.wasm>

import fs from 'fs';
var myArgs = process.argv.slice(3);
const myArgs = process.argv.slice(3);

// <<< Change this sudo key to match you network >>>
keyring.Keyring({type: 'sr25519'}).addFromUri(myargs[1]);
const wasm = fs.readFileSync(myargs[2]);
console.log(myArgs)

const sudo = (new keyring.Keyring({ type: 'sr25519' })).addFromUri(myArgs[0]);
const wasm = fs.readFileSync(myArgs[1]);
const wasmHex = `0x${wasm.toString('hex')}`;
await api.tx.sudo.sudo(api.tx.system.setCode(wasmHex)).signAndSend(sudo);
const setCode = api.tx.system.setCode(wasmHex);
const scheduler = api.tx.sudo.sudo(api.tx.scheduler.scheduleAfter(10, null, 0, setCode));
await scheduler.signAndSend(sudo, ({ status, events }) => {
if (status.isInBlock) {
events.forEach(({ event: { data, method } }) => {
console.log('method::', method.toString());
console.log('data::', data.toString());
});
}

if (status.isFinalized) {
process.exit()
}
});


117 changes: 59 additions & 58 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import minimist from 'minimist';
import {Api, WsProvider } from '@cennznet/api';
import { Api, WsProvider } from '@cennznet/api';
import repl from 'repl';
import fs from 'fs';
import keyring from '../node_modules/@polkadot/keyring/index.js';
Expand All @@ -9,70 +9,71 @@ import * as utils from '../node_modules/@polkadot/util/index.js';
const args = minimist(process.argv.slice(2));

async function setup() {
let endpoint = 'ws://localhost:9944';
if (args.endpoint) {
if(args.endpoint === 'mainnet') {
args.endpoint = 'wss://cennznet.unfrastructure.io/public/ws';
} else if(args.endpoint === 'nikau') {
args.endpoint = 'wss://nikau.centrality.me/public/ws';
}
endpoint = args.endpoint;
}
let endpoint = 'ws://localhost:9944';
if (args.endpoint) {
if (args.endpoint === 'mainnet') {
args.endpoint = 'wss://cennznet.unfrastructure.io/public/ws';
} else if (args.endpoint === 'nikau') {
args.endpoint = 'wss://nikau.centrality.me/public/ws';
}
endpoint = args.endpoint;
}

let types = {};
if (args.types) {
console.log(`loading custom types from: '${args.types}'...`);
try {
types = JSON.parse(fs.readFileSync(args.types));
} catch (err) {
throw `Failed loading custom types from: '${args.types}' with: ${err}`;
}
console.log(`using custom types: ${JSON.stringify(types)} ✅`);
}
let types = {};
if (args.types) {
console.log(`loading custom types from: '${args.types}'...`);
try {
types = JSON.parse(fs.readFileSync(args.types));
} catch (err) {
throw `Failed loading custom types from: '${args.types}' with: ${err}`;
}
console.log(`using custom types: ${JSON.stringify(types)} ✅`);
}

// Setup API session
let provider = new WsProvider(endpoint, 10);
console.log(`connecting to: ${endpoint}...`);
global.api = await Api.create({ provider: endpoint, types })
console.log(`connected ✅`);
// Setup API session
let provider = new WsProvider(endpoint, 10);
console.log(`connecting to: ${endpoint}...`);
global.api = await Api.create({ provider: endpoint, types })
console.log(`connected ✅`);

// Setup injected helper libs / functions
// Note: we vendor the @cennznet/api.js compatible versions
global.utilCrypto = utilCrypto;
global.utils = utils;
global.keyring = keyring;
// Setup injected helper libs / functions
// Note: we vendor the @cennznet/api.js compatible versions
global.utilCrypto = utilCrypto;
global.utils = utils;
global.keyring = keyring;
global.fs = fs;

// A simple keyring with pre-populated test accounts
let toyKeyring = new keyring.Keyring({ type: 'sr25519' });
toyKeyring.alice = toyKeyring.addFromUri("//Alice");
toyKeyring.bob = toyKeyring.addFromUri("//Bob");
toyKeyring.charlie = toyKeyring.addFromUri("//Charlie");
toyKeyring.dave = toyKeyring.addFromUri("//Dave");
toyKeyring.eve = toyKeyring.addFromUri("//Eve");
global.toyKeyring = toyKeyring;
// A simple keyring with pre-populated test accounts
let toyKeyring = new keyring.Keyring({ type: 'sr25519' });
toyKeyring.alice = toyKeyring.addFromUri("//Alice");
toyKeyring.bob = toyKeyring.addFromUri("//Bob");
toyKeyring.charlie = toyKeyring.addFromUri("//Charlie");
toyKeyring.dave = toyKeyring.addFromUri("//Dave");
toyKeyring.eve = toyKeyring.addFromUri("//Eve");
global.toyKeyring = toyKeyring;
}

async function main() {
if (args.run) {
console.log(`Running user script: '${args.run}' with: api, utilCrypto, keyring, utils`);
let script = fs.readFileSync(args.run).toString();
eval(`(async () => {${script}})()`);
} else {
console.log("Launching session with: api, keyring, utilCrypto, utils");
console.log("Test accounts are available via: toyKeyring");
repl.start('> ');
}
if (args.run) {
console.log(`Running user script: '${args.run}' with: api, utilCrypto, keyring, utils`);
let script = fs.readFileSync(args.run).toString();
eval(`(async () => {${script}})()`);
} else {
console.log("Launching session with: api, keyring, utilCrypto, utils");
console.log("Test accounts are available via: toyKeyring");
repl.start('> ');
}
}

setup()
.then(() => {
main()
.catch(err => {
console.log(`error during execution: ${err}`)
process.exit(1);
});
})
.catch(err => {
console.log(`error during setup: ${err}`)
process.exit(1);
});
.then(() => {
main()
.catch(err => {
console.log(`error during execution: ${err}`)
process.exit(1);
});
})
.catch(err => {
console.log(`error during setup: ${err}`)
process.exit(1);
});