Skip to content

Commit

Permalink
update js code
Browse files Browse the repository at this point in the history
  • Loading branch information
b-yap committed May 16, 2024
1 parent 86499dc commit 1a10416
Showing 1 changed file with 39 additions and 56 deletions.
95 changes: 39 additions & 56 deletions .github/workflows/scripts/js/parachain_authorize_upgrade.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// This code will submit a transaction to propose an `authorizeUpgrade`
// steps:
// 1. npm init -y
// 2. npm install
// 3. node parachain_authorize_upgrade.js <chain> <compressed.wasm>

const { ApiPromise, WsProvider, Keyring } = require("@polkadot/api");
const { blake2AsHex } = require("@polkadot/util-crypto");
const readline = require("node:readline/promises");
Expand All @@ -10,70 +16,47 @@ const rl = readline.createInterface({ input: stdin, output: stdout });
async function submitExtrinsic(transaction, keypair) {
console.log("Submit transaction: ", transaction);

// await new Promise((resolve, reject) => {
// transaction.signAndSend(keypair, ({ status, dispatchError }) => {
// // status would still be set, but in the case of error we can shortcut
// // to just check it (so an error would indicate InBlock or Finalized)
// 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.log(`${section}.${name}: ${docs.join(" ")}`);
// } else {
// // Other, CannotLookup, BadOrigin, no extra info
// console.log(dispatchError.toString());
// }
// reject();
// }
//
// if (status.isInBlock) {
// console.log("Success: transaction in block");
// resolve();
// }
//
// if (status.isFinalized) {
// console.log("Transaction finalized");
// }
// });
// });
}

async function democracyProposal(call, { api, submitterKeypair }) {
console.log("Preimage data", call.inner.toHex());
console.log("Preimage hash", `0x${Buffer.from(call.inner.hash).toString("hex")}`);

const submitPreimageTransaction = api.tx.preimage.notePreimage(call.inner.toHex());
await submitExtrinsic(submitPreimageTransaction, submitterKeypair);

const callLength = (call.inner.toHex().length - 2) / 2;
const externalProposeTransaction = api.tx.democracy.externalProposeMajority({
Lookup: { hash: call.inner.hash, len: callLength },
await new Promise((resolve, reject) => {
transaction.signAndSend(keypair, ({ status, dispatchError }) => {
// status would still be set, but in the case of error we can shortcut
// to just check it (so an error would indicate InBlock or Finalized)
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.log(`${section}.${name}: ${docs.join(" ")}`);
} else {
// Other, CannotLookup, BadOrigin, no extra info
console.log(dispatchError.toString());
}
reject();
}

if (status.isInBlock) {
console.log("Success: transaction in block");
resolve();
}

if (status.isFinalized) {
console.log("Transaction finalized");
}
});
});
const threshold = 3;

const councilTransaction = api.tx.council.propose(
threshold,
externalProposeTransaction,
(externalProposeTransaction.toHex().length - 2) / 2
);
await submitExtrinsic(councilTransaction, submitterKeypair);
}

async function submitTransaction(call, api) {
const keyring = new Keyring({ type: "sr25519" });

// todo: need an account
let secretQuery = "Enter the secret mnemonic seed of the council member: ";
let submitterKeypair = keyring.addFromUri("//Alice");

const submitterSecret = await rl.question(secretQuery);
let submitterKeypair = keyring.addFromUri(submitterSecret.trim());
console.log("Preimage data", call.inner.toHex());
console.log("Preimage hash", `0x${Buffer.from(call.inner.hash).toString("hex")}`);

await democracyProposal(call, {
api,
submitterKeypair
});
const submitPreimageTransaction = api.tx.preimage.notePreimage(call.inner.toHex());
// todo: uncomment if ready
// await submitExtrinsic(submitPreimageTransaction, submitterKeypair);
};

function getUrl (network) {
Expand Down

0 comments on commit 1a10416

Please sign in to comment.