Skip to content

Instantly share code, notes, and snippets.

@pepyakin
Created December 24, 2021 15:17
Show Gist options
  • Save pepyakin/2583ff3348c0188dc3b25cf3537f5f70 to your computer and use it in GitHub Desktop.
Save pepyakin/2583ff3348c0188dc3b25cf3537f5f70 to your computer and use it in GitHub Desktop.
polkadot.js script for upgrading a parachain
import { ApiPromise, WsProvider, Keyring } from '@polkadot/api'
import { cryptoWaitReady } from '@polkadot/util-crypto'
import { readFileSync } from 'fs';
async function connect(port, types) {
const provider = new WsProvider('ws://127.0.0.1:' + port)
const api = new ApiPromise({ provider, types })
await api.isReady
return api
}
async function main() {
if (process.argv.length != 4) {
console.error("wrong number of arguments");
return;
}
// The RPC port of the parachain.
let port = parseInt(process.argv[2]);
// The filename of the runtime/PVF we want to upgrade to. Usually a file
// with `.compact.compressed.wasm` extension.
let filename = process.argv[3];
console.log(`upgrading the chain with the ${filename}`);
let code = readFileSync(filename).toString('hex');
await cryptoWaitReady()
let api = await connect(port, {})
const keyring = new Keyring({ type: "sr25519" });
const alice = keyring.addFromUri("//Alice");
const unsub = await api.tx.sudo
.sudoUncheckedWeight(api.tx.system.setCodeWithoutChecks(`0x${code}`), 1)
.signAndSend(alice, (result) => {
console.log(`Current status is ${result.status}`);
if (result.status.isInBlock) {
console.log(
`Transaction included at blockHash ${result.status.asInBlock}`
);
} else if (result.status.isFinalized) {
console.log(
`Transaction finalized at blockHash ${result.status.asFinalized}`
);
} else if (result.isError) {
console.log(`Transaction Error`);
}
});
}
main().catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment