-
Notifications
You must be signed in to change notification settings - Fork 5
/
batchRemoveVotes.ts
72 lines (59 loc) · 1.89 KB
/
batchRemoveVotes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Simple code snippet to batch remove vote
*/
import { ApiPromise, WsProvider } from '@polkadot/api';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import * as fs from 'fs';
const argv = yargs(hideBin(process.argv)).options({
network: { type: 'string', demandOption: true, alias: 'n', default: 'moonbeam' },
sender: { type: 'string', demandOption: true, alias: 's' },
}).argv;
// Create Provider
let wsProvider;
if (argv['network'] === 'moonbeam') {
wsProvider = new WsProvider('wss://wss.api.moonbeam.network');
} else if (argv['network'] === 'moonriver') {
wsProvider = new WsProvider('wss://wss.api.moonriver.moonbeam.network');
} else {
console.error('Network not supported');
process.exit();
}
const main = async () => {
// Wait for Provider
const api = await ApiPromise.create({
provider: wsProvider,
noInitWarn: true,
});
await api.isReady;
// Get list of votes
const votes = await api.query.convictionVoting.votingFor.entries(argv['sender']);
let votesInfo = [];
votes.forEach(([key, exposure]: any | any) => {
votesInfo.push({
info: key.args.map((k) => k.toHuman()),
details: exposure.toHuman().Casting.votes,
});
});
let batchTxs = [];
// Batch remove vote extrinsic
console.log(`Vote Class, # of Votes`);
for (let i = 0; i < votesInfo.length; i++) {
let classInfo = votesInfo[i].info[1];
console.log(classInfo, votesInfo[i].details.length);
for (let j = 0; j < votesInfo[i].details.length; j++) {
let indexInfo = votesInfo[i].details[j] ? votesInfo[i].details[j][0] : NaN;
if (indexInfo) {
batchTxs.push(
await api.tx.convictionVoting.removeOtherVote(argv['sender'], classInfo, indexInfo)
);
}
}
}
// Batch Tx
let tx = await api.tx.utility.batch(batchTxs);
console.log(`\n Batch Call:`);
console.log(tx.toHex());
await api.disconnect();
};
main();