-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
114 lines (106 loc) · 2.97 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
const lcdEndpoint = "https://api.sifchain.finance";
const rpcEndpoint = "https://rpc.sifchain.finance";
const { request } = require("undici");
const fs = require("fs");
const {
DirectSecp256k1HdWallet,
EncodeObject,
Registry,
} = require("@cosmjs/proto-signing");
const { stringToPath } = require("@cosmjs/crypto");
const {
SigningStargateClient,
StargateClient,
defaultRegistryTypes,
} = require("@cosmjs/stargate");
let recompound = JSON.parse(fs.readFileSync("./recompound.json"));
const registry = new Registry([...defaultRegistryTypes]);
(async () => {
const mnemonic =
fs.readFileSync("./mnemonic.txt", "utf8").split("\n")[0] || "";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
prefix: "sif",
});
const [firstAccount] = await wallet.getAccounts();
const client = await SigningStargateClient.connectWithSigner(
rpcEndpoint,
wallet,
{ registry: registry }
);
const fee = {
amount: [
{
denom: "rowan", // Use the appropriate fee denom for your chain
amount: "10000000000000",
},
],
gas: "280000",
};
console.log("Coded with <3 by @angrymouse_hns, founder of Another.Software");
recompound.forEach((comp) => {
setInterval(async () => {
try {
let rewards = await getRewardsFor(firstAccount.address, comp.validator);
if (uRowanToRowan(rewards) >= comp.minAmount) {
let tx = await client.signAndBroadcast(
firstAccount.address,
[
{
typeUrl:
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
value: {
delegatorAddress: firstAccount.address,
validatorAddress: comp.validator,
},
},
{
typeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
value: {
delegatorAddress: firstAccount.address,
validatorAddress: comp.validator,
amount: {
denom: "rowan",
amount: (
(rewards / 100n) *
BigInt(comp.delegatePercents)
).toString(),
},
},
},
],
fee,
"SifReCompound rewards recompound. Stake now with Another.Software!\n https://another.software/sifrecompound"
);
console.log("Successfully restaked! TX id:" + tx.transactionHash);
}
} catch (e) {
return;
}
}, comp.interval);
});
// console.log(uRowanToRowan(rewards));s
// console.log(uRowanToRowan(urowan));
})();
function uRowanToRowan(uRowan) {
uRowan = BigInt(uRowan);
return parseInt(uRowan / 1000000000000n) / 1000000;
}
async function getRewardsFor(delegator, validator) {
let rewards = await (
await request(
lcdEndpoint +
"/cosmos/distribution/v1beta1/delegators/" +
delegator +
"/rewards",
{ method: "GET", maxRedirections: 3 }
)
).body.json();
let reward = rewards.rewards.find(
(reward) => reward.validator_address === validator
);
let rewardNum = 0n;
if (reward && reward.reward && reward.reward[0] && reward.reward[0].amount) {
rewardNum = BigInt(reward.reward[0].amount.split(".")[0]);
}
return rewardNum;
}