-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tools): add script to unlock democracy funds
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
This script is intended to run once for specific networks. | ||
Do not use it without reading the code !! | ||
This script will continuously call and wait for completion of unlockDemocracyFunds | ||
extrinsic from pallet moonbeam-lazy-migrations until the migration is completed. | ||
Ex: ./node_modules/.bin/ts-node unlock-democracy-funds.ts \ | ||
--url ws://127.0.0.1:34102 \ | ||
--account-priv-key <key> \ | ||
*/ | ||
import yargs from "yargs"; | ||
import { Keyring } from "@polkadot/api"; | ||
import "@moonbeam-network/api-augment"; | ||
import { getApiFor, NETWORK_YARGS_OPTIONS } from ".."; | ||
import { monitorSubmittedExtrinsic, waitForAllMonitoredExtrinsics } from "../utils/monitoring"; | ||
|
||
const argv = yargs(process.argv.slice(2)) | ||
.usage("Usage: $0") | ||
.version("1.0.0") | ||
.options({ | ||
...NETWORK_YARGS_OPTIONS, | ||
"account-priv-key": { type: "string", demandOption: true, alias: "account" }, | ||
}).argv; | ||
|
||
const main = async () => { | ||
// Instantiate Api | ||
const api = await getApiFor(argv); | ||
|
||
const atBlockNumber = (await api.rpc.chain.getHeader()).number.toNumber(); | ||
const atBlockHash = await api.rpc.chain.getBlockHash(atBlockNumber); | ||
const apiAt = await api.at(atBlockHash); | ||
|
||
const upgradeInfo = (await apiAt.query.system.lastRuntimeUpgrade()).unwrap(); | ||
const runtimeVersion = upgradeInfo.specVersion.toNumber(); | ||
|
||
console.log( | ||
`Current block number #${atBlockNumber} with runtime version ${api.runtimeVersion.specName.toString()}-${runtimeVersion}` | ||
); | ||
|
||
const keyring = new Keyring({ type: "ethereum" }); | ||
const account = await keyring.addFromUri(argv["account-priv-key"], null, "ethereum"); | ||
const { nonce: rawNonce } = (await api.query.system.account(account.address)) as any; | ||
let nonce = BigInt(rawNonce.toString()); | ||
|
||
const MAX_UNLOCK_DEMOCRACY_FUNDS_LIMIT = 50; | ||
|
||
let migrationCompleted = ( | ||
await api.query.moonbeamLazyMigrations.democracyLocksMigrationCompleted() | ||
).toHuman(); | ||
if (migrationCompleted === true) { | ||
console.log("Democracy locks migration already completed. Exiting..."); | ||
return; | ||
} else { | ||
console.log("Unlocking democracy funds..."); | ||
while (migrationCompleted === false) { | ||
const tx = api.tx.moonbeamLazyMigrations.unlockDemocracyFunds( | ||
MAX_UNLOCK_DEMOCRACY_FUNDS_LIMIT | ||
); | ||
await tx.signAndSend( | ||
account, | ||
{ nonce: nonce++ }, | ||
monitorSubmittedExtrinsic(api, { id: "unlock_democracy_funds" }) | ||
); | ||
await waitForAllMonitoredExtrinsics(); | ||
migrationCompleted = ( | ||
await api.query.moonbeamLazyMigrations.democracyLocksMigrationCompleted() | ||
).toHuman(); | ||
} | ||
console.log("Done!"); | ||
} | ||
|
||
await api.disconnect(); | ||
}; | ||
|
||
main(); |