-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFetchBalances.tsx
More file actions
45 lines (42 loc) · 1.14 KB
/
FetchBalances.tsx
File metadata and controls
45 lines (42 loc) · 1.14 KB
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
import { StargateClient } from '@cosmjs/stargate';
import { rpcEndpoints } from '../../util';
export const fetchBalances = async (addresses: string[], chainName: string) => {
return Promise.all(
addresses.map(async address => {
console.log('address', address);
let chain = '';
if (address.startsWith('osmo1')) {
chain = 'osmosis';
} else if (address.startsWith('agoric1')) {
chain = 'agoric';
} else {
return {
address,
balances: [],
};
}
const rpcEndpoint = rpcEndpoints[chainName][chain];
try {
const balance = await fetchBalanceFromRpc(address, rpcEndpoint);
return {
address,
balances: balance,
};
} catch (e) {
console.log('e:', e);
return {
address,
balances: [],
};
}
}),
);
};
const fetchBalanceFromRpc = async (address, rpcEndpoint) => {
const client = await StargateClient.connect(rpcEndpoint);
const balances = await client.getAllBalances(address);
return balances.map(coin => ({
denom: coin.denom,
amount: coin.amount,
}));
};