-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.js
55 lines (43 loc) · 1.53 KB
/
batch.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
const Web3 = require('web3');
const { convertToNumber, getTokens, getTokenPrices } = require('./utils');
const { abi, bathEndpoint, walletAddress } = require('./constant.js');
const web3 = new Web3(new Web3.providers.HttpProvider(bathEndpoint));
const generateContractFunctionList = ({ tokens, blockNumber }) => {
const batch = new web3.BatchRequest();
tokens.map(async ({ address: tokenAddress, symbol, decimals }) => {
const contract = new web3.eth.Contract(abi);
contract.options.address = tokenAddress;
batch.add(contract.methods.balanceOf(walletAddress).call.request({}, blockNumber));
});
return batch;
};
const main = async () => {
const tokens = await getTokens();
const batch = generateContractFunctionList({ tokens });
// query block number
// const batch = generateContractFunctionList({ tokens, blockNumber: 11633038 });
const tokenBalances = [];
const { response } = await batch.execute();
let tokenIds = [];
response.forEach(({ _hex }, index) => {
const { name, decimals, symbol } = tokens[index];
if(_hex !== '0x00') {
tokenBalances.push({
balance: `${convertToNumber(_hex, decimals)}`,
...tokens[index]
})
tokenIds.push(tokens[index].address);
}
});
const prices = await getTokenPrices(tokenIds);
tokenBalances.forEach( token => {
const price = prices[token.address];
if(price) {
token.price = price;
token.currency = 'usd';
token.value = token.balance * price.usd;
}
});
console.log(tokenBalances);
};
main();