-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgraph.js
51 lines (39 loc) · 1.61 KB
/
graph.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
require('isomorphic-fetch');
const ethers = require('ethers');
const { abi, bathEndpoint, walletAddress } = require('./constant.js');
const { convertToNumber, getTokens } = require('./utils');
const convertIndexToAlphetString = number => number.toString().split('')
.map(numberChar => String.fromCharCode(65 + parseInt(numberChar))).join('');
const queryTemplate = (index, { address }, callData) => `
${convertIndexToAlphetString(index)}: call(data: { to: "${address}", data: "${callData}" }) { data }`;
const retrieveTokenBalanceViaGraphQL = (tokens) => {
const ethersInterface = new ethers.utils.Interface(abi);
const callData = ethersInterface.functions.balanceOf.encode([walletAddress]);
const query = tokens.map((token, index) => {
return queryTemplate(index, token, callData);
}).join('\n');
return fetch(`${bathEndpoint}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: `{ block { ${query} } }` }),
// query block number
// body: JSON.stringify({ query: `{ block(number: 7966022) { ${query} } }` }),
})
.then(data => data.json());
};
const main = async () => {
const { tokens } = await getTokens();
const tokenBalances = await retrieveTokenBalanceViaGraphQL(tokens)
.then(({ data: { block: balances } }) => {
const output = {};
Object.entries(balances).map(([, { data: hex }], index) => {
const { name, decimals, symbol } = tokens[index];
output[name] = `${convertToNumber(hex, decimals)} ${symbol}`;
});
return output;
});
console.log(tokenBalances);
};
main();