-
Notifications
You must be signed in to change notification settings - Fork 32
/
claimRewardsMultiChains.js
96 lines (83 loc) · 3.96 KB
/
claimRewardsMultiChains.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
import {
QueryClient, setupDistributionExtension, setupBankExtension, setupStakingExtension, setupTxExtension, setupGovExtension
} from "@cosmjs/stargate";
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import fs from "fs";
import SigningClient from "./utils/SigningClient.js";
import { getSigner } from "./utils/helpers.js";
const loadJSON = (path) => JSON.parse(fs.readFileSync(new URL(path, import.meta.url)));
const chainsMap = loadJSON('./assets/chains.json');
async function getQueryClient(rpcEndpoint) {
const tendermint34Client = await Tendermint34Client.connect(rpcEndpoint);
const queryClient = QueryClient.withExtensions(
tendermint34Client,
setupBankExtension,
setupStakingExtension,
setupTxExtension,
setupGovExtension,
setupDistributionExtension
);
return queryClient;
}
async function withdrawRewards(client, address, validators) {
let ops = [];
for (let validator of validators) {
let msg = {
typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
value: {
delegatorAddress: address,
validatorAddress: validator
},
};
ops.push(msg);
}
let result = await client.signAndBroadcast(address, ops, '', '');
return result;
}
async function start(chain, mnemonicOrKey) {
try {
const rpcEndpoint = chain.rpc;
let wallet = await getSigner(chain, mnemonicOrKey);
let client = new SigningClient(chain, wallet);
const [account] = await wallet.getAccounts();
const queryClient = await getQueryClient(rpcEndpoint);
let balances = await queryClient.bank.balance(account.address, chain.denom);
console.log(`${account.address} has ${balances.amount / Math.pow(10, chain.exponent)} ${chain.symbol}`);
let rewards = await queryClient.distribution.delegationTotalRewards(account.address);
let totalDelegated = 0;
if (rewards.total.length > 0) {
let validators = [];
let totalRewards = rewards.total[rewards.total.length - 1].amount / Math.pow(10, chain.exponent + 18);
console.log(`${account.address} has ${totalRewards} ${chain.symbol} rewards available to claim`);
for (let reward of rewards.rewards) {
validators.push(reward.validatorAddress);
let delegation = await queryClient.staking.delegation(account.address, reward.validatorAddress);
if (delegation.delegationResponse.balance.amount > 0) {
totalDelegated += delegation.delegationResponse.balance.amount / Math.pow(10, chain.exponent);
console.log(`${account.address} is currently delegating ${delegation.delegationResponse.balance.amount / Math.pow(10, chain.exponent)} ${chain.symbol} to ${reward.validatorAddress}`)
}
}
if (rewards.total[rewards.total.length - 1].amount > chain.claim_min * Math.pow(10, chain.exponent + 18)) {
console.log(`${account.address} is withdrawing ${totalRewards} ${chain.symbol}...`)
let result = await withdrawRewards(client, account.address, validators);
let code = result.code;
if (code == 0) {
console.log(`${account.address} withdrawn rewards from ${validators}: ${result.transactionHash}`)
} else {
console.log(`${account.address} FAILED to withdraw rewards from ${validators}. Reason: ${result.rawLog}`);
}
}
}
if (totalDelegated > 0) {
console.log(`${account.address} Total Delegated ${totalDelegated} ${chain.symbol}`)
console.log('\n')
}
} catch (err) {
console.log(err)
console.log("Error in start: " + err);
}
}
const mnemonicOrKey = 'Put mnemonic or private key here';
for (let chainName in chainsMap) {
start(chainsMap[chainName], mnemonicOrKey);
}