Skip to content

Instantly share code, notes, and snippets.

@kianenigma
Created January 4, 2022 13:09
Show Gist options
  • Save kianenigma/a34bcc2efcf753d986c199da62adffbd to your computer and use it in GitHub Desktop.
Save kianenigma/a34bcc2efcf753d986c199da62adffbd to your computer and use it in GitHub Desktop.
The snipped use to generate the PR https://github.com/paritytech/polkadot/pull/4656
import { ApiPromise, WsProvider } from "@polkadot/api";
import axios from "axios";
import { writeFileSync } from "fs";
import { strict as assert } from 'assert'
async function findLastNicksBlock(block: number, key: string) {
while (true) {
let next = block + 1;
const data = await axios.post(`https://kusama.api.subscan.io/api/scan/block`, {
"block_num": next
}, { headers: { "X-API-Key": key } })
console.log(data.data)
// @ts-ignore
const spec = Number(data.data.data.spec_version);
console.log(next, spec);
if (spec != 1033) {
break;
}
block = next;
}
console.log(block)
}
async function leftoverNicks(api: ApiPromise) {
const key = process.env['API'] || "DEFAULT_KEY";
function bin2string(array: Uint8Array){
var result = "";
for(var i = 0; i < array.length; ++i){
result += (String.fromCharCode(array[i]));
}
return result;
}
// this should technically be the last block at which nicks pallet was IN the runtime.
let block = 569325;
const hash = await api.rpc.chain.getBlockHash(block);
const atApi = await api.at(hash);
const prefix = atApi.query.nicks.nameOf.keyPrefix();
const pairs = await api.rpc.state.getPairs(prefix, hash);
const allAccounts = (await api.query.system.account.entries()).map(([key, _]) => key.args[0]);
console.log(`fetched ${allAccounts.length} accounts at ${hash}`)
const globalTable = {};
// @ts-ignore
allAccounts.forEach((acc) => globalTable[api.registry.hash(acc.toU8a())] = acc.toHuman())
const refunds = [];
for (const [k, v] of pairs) {
const [nick, deposit] = api.createType('(Vec<U8>, Balance)', v);
const hashedKey = `0x${k.toHex().slice(-64)}`;
// @ts-ignore
const whoRaw = globalTable[hashedKey];
const who = api.createType('AccountId', whoRaw);
let currentReserved = (await api.query.system.account(who)).data.reserved;
console.log(`${who.toHuman()} with old nick ${bin2string(nick.toU8a())} had deposit ${deposit.toHuman()}, current reserved balance ${api.createType('Balance', currentReserved).toHuman()}`);
if (currentReserved.lt(deposit)) {
console.log(`\t⚠️ does not have enough reserved balance for the refund`);
}
refunds.push({who, deposit })
}
writeFileSync("refunds.json", JSON.stringify(refunds));
for (let { who, deposit } of refunds) {
console.log(`(${api.createType('AccountId', who).toU8a()}, ${deposit})`);
}
}
async function main() {
const provider = new WsProvider("ws://127.0.0.1:9924");
const api = await ApiPromise.create({ provider });
console.log(`Connected to node ${(await api.rpc.system.chain()).toHuman()} [ss58: ${api.registry.chainSS58}]`)
await leftoverNicks(api)
}
main().catch(console.error).finally(() => process.exit());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment