This repository has been archived by the owner on Jul 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalances.js
198 lines (187 loc) · 5.09 KB
/
balances.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { fromBech32, normalizeBech32, toBech32 } from "@cosmjs/encoding";
import CosmosDirectory from "./utils/CosmosDirectory.mjs";
import _ from "lodash";
import axios from "axios";
import {
setupStakingExtension,
QueryClient as CosmjsQueryClient,
setupBankExtension,
setupDistributionExtension,
setupMintExtension,
setupGovExtension,
setupIbcExtension,
} from "@cosmjs/stargate";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import fs from "fs";
const mapAsync = async (array, fn) => {
let promises = await Promise.allSettled(array.map(fn));
return promises.map((p) => {
if (p.status == "fulfilled") {
return p.value;
} else {
return false;
}
});
};
const directory = CosmosDirectory();
const makeClient = async (rpcUrl) => {
const tmClient = await Tendermint34Client.connect(rpcUrl);
return CosmjsQueryClient.withExtensions(
tmClient,
setupStakingExtension,
setupIbcExtension,
setupBankExtension,
setupDistributionExtension,
setupMintExtension,
setupGovExtension
);
};
const getIbcDenoms = async (coins, client) => {
return await mapAsync(coins, async (c) => {
if (c.denom.includes("ibc")) {
const hash = c.denom.split("/")[1];
const denomTrace = await client.ibc.transfer.denomTrace(hash);
return {
denom: denomTrace.denomTrace.baseDenom,
amount: c.amount,
ibcDenom: c.denom,
};
} else {
return c;
}
});
};
const getAddresses = (address, chains) => {
const decoded = fromBech32(address);
return chains.map((c) => ({
name: c.name,
denom: c.denom,
address: toBech32(c.prefix, decoded.data),
rpc: c.best_apis.rpc[0].address,
}));
};
const getBalances = async (addresses) => {
return await mapAsync(addresses, async (a) => {
const client = await makeClient(a.rpc);
const allBalances = await client.bank.allBalances(a.address);
const denoms = await getIbcDenoms(allBalances, client);
return {
address: a.address,
liquid: denoms,
};
});
};
const getStaked = async (addresses) => {
return await mapAsync(addresses, async (a) => {
const client = await makeClient(a.rpc);
const staked = await client.staking.delegatorDelegations(a.address);
const chainStaked = staked.delegationResponses.map((d) => {
return {
validator: d.delegation.validatorAddress,
amount: d.balance,
};
});
const total =
chainStaked.length !== 0
? chainStaked
.map((s) => parseInt(s.amount.amount))
.reduce((prev, curr) => {
return prev + curr;
})
: 0;
return {
delegations: chainStaked,
address: a.address,
total: {
amount: total,
denom: a.denom,
},
};
});
};
const getRewards = async (addresses) => {
return await mapAsync(addresses, async (a) => {
const client = await makeClient(a.rpc);
const rewards = await client.distribution.delegationTotalRewards(a.address);
const chainRewards = rewards.rewards.map((d) => {
return {
validator: d.validatorAddress,
amount: d.reward,
};
});
const total =
chainRewards.length !== 0
? chainRewards
.map((s) => parseInt(s.amount.amount))
.reduce((prev, curr) => {
return prev + curr;
})
: 0;
return {
rewards: chainRewards,
address: a.address,
total: {
amount: total,
denom: a.denom,
},
};
});
};
const getAllBalances = async (address, chains) => {
const addresses = getAddresses(address, chains);
const liquid = await getBalances(addresses);
const staked = await getStaked(addresses);
const rewards = await getRewards(addresses);
const s = _.keyBy(staked, "address");
const r = _.keyBy(rewards, "address");
const l = _.keyBy(liquid, "address");
return addresses.map((a) => {
return {
name: a.name,
address: a.address,
allBalances: {
rewards: r[a.address],
liquid: l[a.address],
staked: s[a.address],
},
};
});
};
async function run() {
let chains = await directory.getChains();
const enabled = [
"cosmoshub",
"osmosis",
"juno",
"sifchain",
"stargaze",
"crescent",
];
chains = Object.values(chains).filter((c) => enabled.includes(c.name));
chains = await mapAsync(chains, async (c) => {
const chainData = await directory.getChainData(c.name);
return {
...c,
prefix: chainData.bech32_prefix,
};
});
const balances = await getAllBalances(
"cosmos1xzzwtpwa9x4dmzgrp25g5s3e9n79a8wz7ymq7x",
chains
);
}
async function ru() {
const client = await makeClient("https://rpc.cosmoshub.pupmos.network/");
const u = await client.staking.delegatorUnbondingDelegations(
"cosmos1attyjy6kdeewf7gpxswkh38af9qtncerlgy86e"
);
//console.log(u.unbondingResponses[0].entries[0]);
const date = new Date(1653054927 * 1000);
console.log(date);
const form = new Intl.DateTimeFormat("en-US", {
dateStyle: "medium",
timeStyle: "short",
}).format(date);
console.log(form);
}
ru();