-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
77 lines (63 loc) · 2.05 KB
/
index.ts
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
import { ethers } from "ethers";
import { NETWORK_CONFIG, SUPPORTED_CHAINS } from "@smartinvoicexyz/constants";
import { gql, request } from "graphql-request";
const query = gql`
query {
invoices(first: 1000) {
network
token
deposits {
amount
}
}
}
`;
const getChain = (chainId: number) => {
const chain = SUPPORTED_CHAINS.find(chain => chain.id === chainId);
if (!chain) {
throw new Error(`Chain with id ${chainId} not found`);
}
return chain;
};
const abi = [
"function name() view returns (string)",
"function symbol() view returns (string)",
"function decimals() view returns (uint8)",
];
const main = async () => {
const invoices: Record<number, any[]> = {};
for (const network of Object.keys(NETWORK_CONFIG)) {
const config = NETWORK_CONFIG[network];
const data: any = await request(config.SUBGRAPH, query);
invoices[network] = data.invoices;
}
for (const chainId of Object.keys(invoices)) {
const chain = getChain(Number(chainId));
if (chain.testnet) {
continue;
}
console.log(`Total for ${chain.name} (${chainId}):`);
const invoicesForChain = invoices[chainId];
const totals = {};
for (const invoice of invoicesForChain) {
const { deposits, token } = invoice;
const total = deposits.reduce((acc, deposit) => acc + BigInt(deposit.amount), BigInt(0));
totals[token] = totals[token] ? totals[token] + total : total;
}
const provider = new ethers.JsonRpcProvider(chain.rpcUrls.default.http[0]);
for (const token of Object.keys(totals)) {
try {
const total = totals[token];
const contract = new ethers.Contract(token, abi, provider);
const name = await contract.name();
const symbol = await contract.symbol();
const decimals = await contract.decimals();
console.log(`${name} (${token}) => ${ethers.formatUnits(total, decimals)} ${symbol}`);
} catch (e) {
console.log(`${token} => ${totals[token]}`);
}
}
console.log(`\n`);
}
};
main().catch(console.error);