From ae244222e245778d24c6c8e370b66d30da4e8b52 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Wed, 31 Jul 2024 11:31:59 -0400 Subject: [PATCH] Fix Alice's balance and runtime upgrade script --- .baedeker/util/runtimeUpgrade.js | 49 +++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/.baedeker/util/runtimeUpgrade.js b/.baedeker/util/runtimeUpgrade.js index 657178796..7ae622ba1 100644 --- a/.baedeker/util/runtimeUpgrade.js +++ b/.baedeker/util/runtimeUpgrade.js @@ -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); @@ -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) => {