forked from silentnoname/cosmos-learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaimRewardsMultiChains.js
126 lines (114 loc) · 4.48 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
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
import {
QueryClient, setupDistributionExtension, SigningStargateClient
} from "@cosmjs/stargate";
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import { coins, Secp256k1HdWallet } from '@cosmjs/launchpad';
import { stringToPath } from "@cosmjs/crypto";
import { LCDClient, MnemonicKey, MsgWithdrawDelegatorReward, Fee } from '@terra-money/terra.js';
import dotenv from 'dotenv';
import { chainMap } from "./assets/chains.js";
dotenv.config();
//0:low fee
//1:low fee or no fee
const MODE = 1;
async function getQueryClient(rpcEndpoint) {
const tendermint34Client = await Tendermint34Client.connect(rpcEndpoint);
const queryClient = QueryClient.withExtensions(
tendermint34Client,
setupDistributionExtension
);
return queryClient;
}
async function withdrawRewards(client, chain, address, validators, totalReward) {
let ops = [];
for (let validator of validators) {
let msg = {
typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
value: {
delegatorAddress: address,
validatorAddress: validator
},
};
ops.push(msg);
}
const fee = {
amount: coins(chain.min_tx_fee[MODE], chain.denom),
gas: "" + chain.gas * validators.length,
};
let result = await client.signAndBroadcast(address, ops, fee, '');
if (result.code > 0) {
console.log(`${address} failed to claim ${totalReward} ${chain.symbol}. ${result.rawLog}`);
} else {
console.log(`${address} claimed ${totalReward} ${chain.symbol}. Tx Hash: ${result.transactionHash}`);
}
}
async function withdrawRewardsTerra(terra, wallet, chain, address, validators, totalReward) {
const msgs = validators.map((addr) => new MsgWithdrawDelegatorReward(address, addr));
let minFee = chain.min_tx_fee[MODE];
const fee = new Fee(chain.gas, { uluna: minFee });
console.log(`${address} is ready to claim rewards...`);
wallet.createAndSignTx({
msgs: msgs,
fee: fee,
}).then(tx => terra.tx.broadcast(tx))
.then(result => {
if (result.code > 0) {
console.log(`${address} failed to claim ${totalReward} ${chain.symbol}. ${result.raw_log}`);
} else {
console.log(`${address} claimed ${totalReward} ${chain.symbol}. Tx Hash: ${result.txhash}`);
}
}).catch(err => {
console.log(`${address} failed to claim ${totalReward} ${chain.symbol}. ${err}`);
});
}
async function start(mnemonic, chain) {
const rpcEndpoint = chain.rpc;
const wallet = await Secp256k1HdWallet.fromMnemonic(
mnemonic,
{
hdPaths: chain.hd_path ? [stringToPath(chain.hd_path)] : undefined,
prefix: chain.prefix
}
);
const [account] = await wallet.getAccounts();
try {
const queryClient = await getQueryClient(rpcEndpoint);
let delegationRewards = await queryClient.distribution.delegationTotalRewards(account.address);
let validators = [];
let totalRewards = 0;
if (delegationRewards.total.length > 0) {
for (let reward of delegationRewards.rewards) {
validators.push(reward.validatorAddress);
}
for (let total of delegationRewards.total) {
if (total.denom == chain.denom) {
totalRewards += Number(total.amount) / (1e18 * Math.pow(10, chain.exponent));
}
}
}
if (totalRewards > chain.claim_min && validators.length > 0) {
const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet);
if (chain.name == "terra") {
const terra = new LCDClient({
URL: chain.rest,
chainID: chain.chain_id,
});
const mk = new MnemonicKey({
mnemonic: mnemonic
});
const wallet = terra.wallet(mk);
await withdrawRewardsTerra(terra, wallet, chain, account.address, validators, totalRewards);
} else {
await withdrawRewards(client, chain, account.address, validators, totalRewards);
}
}
} catch (err) {
console.log(`${account.address} claimed failed. ${err.message}`);
}
}
let keys = process.env.MNEMONICS.split(',');
for (const [k, chain] of Object.entries(chainMap)) {
for (let key of keys) {
start(key, chain);
}
}