Skip to content

Commit

Permalink
Fix Alice's balance and runtime upgrade script
Browse files Browse the repository at this point in the history
  • Loading branch information
gztensor committed Jul 31, 2024
1 parent 0209153 commit ae24422
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions .baedeker/util/runtimeUpgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ const NODE_URL = 'ws://127.0.0.1:9946';
const SUDO_ACCOUNT_SEED = '//Alice';
const WASM_FILE_PATH = '../../wasm/node_subtensor_runtime.wasm';

function sendTransaction(call, signer) {
return new Promise((resolve, reject) => {
call.signAndSend(signer, ({ status, events, dispatchError }) => {
// Check for transaction errors
if (dispatchError) {
if (dispatchError.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = api.registry.findMetaError(dispatchError.asModule);
const { docs, name, section } = decoded;
console.error(`${section}.${name}: ${docs.join(' ')}`);
} else {
// Other, such as system, errors
console.error(dispatchError.toString());
}
reject(dispatchError.toString());
}
// Log and resolve when the transaction is included in a block
if (status.isInBlock) {
console.log(`Transaction included at blockHash ${status.asInBlock}`);
resolve(status.asInBlock);
}
}).catch((error) => {
console.error('Failed to send transaction:', error);
reject(error);
});
});
}

async function main() {
// Create a provider connected to the local node
const provider = new WsProvider(NODE_URL);
Expand All @@ -19,21 +47,28 @@ async function main() {
// Load the sudo account from the seed phrase
const sudoAccount = keyring.addFromUri(SUDO_ACCOUNT_SEED);

// Construct and execute the sudo call to increase Alice's balance
const sudoCallSetBalance = api.tx.sudo.sudo(
api.tx.balances.forceSetBalance(sudoAccount.address, `1000000000000`)
);
console.log("Increasing Alice balance...");
await sendTransaction(sudoCallSetBalance, sudoAccount);
console.log("Increasing Alice balance - done");

// Read the WASM file
const wasmCode = fs.readFileSync(WASM_FILE_PATH).toString('hex');

// Construct the sudo call to set the new code
const sudoCall = api.tx.sudo.sudo(
const sudoCallSetCode = api.tx.sudo.sudo(
api.tx.system.setCode(`0x${wasmCode}`)
);

// Send the transaction using the sudo account
await sudoCall.signAndSend(sudoAccount, ({ status }) => {
if (status.isInBlock) {
console.log(`Transaction included at blockHash ${status.asInBlock}`);
process.exit(0);
}
});
console.log("Running runtime upgrade...");
await sendTransaction(sudoCallSetCode, sudoAccount);
console.log("Running runtime upgrade - done");

process.exit(0);
}

main().catch((error) => {
Expand Down

0 comments on commit ae24422

Please sign in to comment.