-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (114 loc) · 4.54 KB
/
index.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
const { program } = require('commander');
const { ApiPromise, WsProvider } = require('@polkadot/api');
const { spec } = require('@edgeware/node-types');
const config = require('@w3f/config');
const { Keyring } = require('@polkadot/keyring');
const MAX_HISTORY = 84; // TODO: Replace with max number of stored eras from... where?
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
const claimPayout = async (nodeUrl, cfg) => {
//
// set a timeout manually, since ApiPromise won't let us do this
// if the timeout is reached, kill the process with exitcode 1
//
console.log(`Connecting to API for ${nodeUrl}...`);
let connected;
setTimeout(() => {
if (connected) return;
console.log('Connection timed out');
process.exit(1);
}, 10000);
//
// initialize the api
//
const api = await ApiPromise.create({
provider: new WsProvider(nodeUrl),
...spec,
});
console.log('Connected');
connected = true;
const keyring = new Keyring({ type: 'sr25519', ss58Format: 2 });
const claimerPair = keyring.addFromUri(cfg.claimerWallet);
const activeEra = (await api.query.staking.activeEra()).toJSON().index;
// console.log(`Era ${activeEra}`);
const maxBatchedTransactions = 9999;
const payoutCalls = [];
if (cfg.claimList.length > 0) {
for (let iClaimList = 0; iClaimList < cfg.claimList.length; iClaimList++) {
console.log(`Processing claim ${iClaimList} for ${cfg.claimList[iClaimList].alias}`);
currentAddress = cfg.claimList[iClaimList].controllerAddress;
stashAddress = cfg.claimList[iClaimList].stashAddress;
//
// get relevant chain data
//
const ledgerPr = await api.query.staking.ledger(currentAddress);
const ledger = ledgerPr.unwrapOr(null);
// console.log(ledger);
if (!ledger) {
console.log(`No stacking found for address ${currentAddress} - is this a controller address?`)
}
else {
// let lastReward;
// lastReward = ledger.claimedRewards.pop().toNumber();
// console.log(`${lastReward} last reward`);
alreadyClaimed = ledger.claimedRewards.toJSON();
console.log(`${alreadyClaimed} previously claimed`);
// let numOfUnclaimedPayouts = activeEra - lastReward - 1;
// console.log(`${numOfUnclaimedPayouts} unclaimed eras`);
let numOfUnclaimedPayouts = MAX_HISTORY;
let start = 1; // start at one to attempt payout for all available eras (avoid missing payouts when one in the middle has already been claimed)
while (numOfUnclaimedPayouts > 0) {
let txLimit = numOfUnclaimedPayouts;
if (numOfUnclaimedPayouts > maxBatchedTransactions) {
txLimit = maxBatchedTransactions;
}
sendtx = 0;
for (let itx = start; itx <= txLimit + start - 1; itx++) {
// const idx = lastReward + itx;
const idx = activeEra - MAX_HISTORY - 1 + itx;
if (!alreadyClaimed.includes(idx)) { // if claimed, skip the era BEFORE downloading the era list
const exposure = await api.query.staking.erasStakersClipped(idx, stashAddress);
// console.log(`exposure: ${exposure.total.toBn()}`);
if (exposure.total.toBn() > 0) {
sendtx = 1;
console.log(`Adding claim for ${currentAddress}, era ${idx}`);
payoutCalls.push(api.tx.staking.payoutStakers(stashAddress, idx));
}
}
}
numOfUnclaimedPayouts -= txLimit;
start += txLimit;
}
console.log(`All payouts have been claimed for ${currentAddress}.`);
}
}
}
if (sendtx) {
try {
console.log('submit tx');
await api.tx.utility
.batch(payoutCalls)
.signAndSend(claimerPair);
}
catch (e) {
console.log(`Could not request claim for ${currentAddress}: ${e}`);
}
}
process.exit(0);
};
program
.name('edgeware-simple-payouts')
.option('-c, --config [path]', 'Path to config file.', './config/main.yaml')
.parse(process.argv);
const programOptions = program.opts();
// Load config file
const cfg = new config.Config().parse(programOptions.config);
// Pick a random WS endpoint in case one of them is down
let url = `wss://${cfg.chain}${getRandomInt(10)+1}.edgewa.re`;
if (programOptions.url) {
url = programOptions.url;
}
console.log(`Claiming payout using node at ${url}`);
claimPayout(url, cfg).then(r => console.log(`Return ${r}`))