0

I am trying to use @pooltogether/etherplex to get the token balances across several token contracts for an address at a particular block height. I have successfully used the code from https://github.com/chainstack/ethereum-token-balances, particularly the file etherplex.js contains the code I'm using. I made a few slight modifications so it pulls from a pre-curated list of tokens instead of getting a token list from Coingecko. Here it is:

const { ethers } = require('ethers');
const fs = require('fs')
const { batch, contract } = require('@pooltogether/etherplex');

const { abi, username, password, rpcEndpoint, walletAddress, blockNumber } = require('./constant.js');
const { convertToNumber, getTokens } = require('./utils');

const provider = new ethers.providers.JsonRpcProvider({
  url: rpcEndpoint,
  user: username,
  password: password,
});

const generateContractFunctionList = tokens =>
  tokens.map(({ address: tokenAddress, symbol }) =>
    contract(symbol, abi, tokenAddress).balanceOf(walletAddress),
  );

const main = async () => {
  //const { tokens } = await getTokens();
  const tokens = JSON.parse(fs.readFileSync('./tokenArrayFormatted1.json'));
  const start = new Date().getTime();
  const args = generateContractFunctionList(tokens);
  const tokenBalances = await batch.apply(null, [provider, ...args])
    .then(balances => {
      const output = {};

      Object.entries(balances).map(([symbol, { balanceOf }], index) => {
        const balance = convertToNumber(balanceOf[0]._hex, tokens[index].decimals);

        output[tokens[index].name] = `${balance} ${symbol}`;
      });

      return output;
    });

  console.log(tokenBalances);
};

main();

The result of the above code is this:

{
  'Deeper Network': '2536809291877779012962 DPR',
  'Butterfly Protocol Governance Token': '500000000000000000000 BFLY',
  PolkaFoundry: '181155501024337317854 PKF',
  'NOIA Token': '333051750809558494435 NOIA',
  'Marlin POND': '1440041248418898225293 POND',
  'Parsiq Token': '97647679865505205154 PRQ',
  'Ampleforth Governance': '2000000000000000000 FORTH',
  'Sora Token': '190218199929215762 XOR',
  Umbrella: '94213894452399722272 UMB',
  'Kylin Network': '122026806656382486733 KYL',
  'YfDAI.finance': '27412986448951619 Yf-DAI',
  'Tixl Token': '50000000000000000000 TXL',
  'BarnBridge Governance Token': '0 BARN',
  'USD Coin': '0 USDC',
  Uniswap: '0 UNI',
  DuckDaoDime: '0 DDIM',
  'DLP Duck Token': '0 DLP',
  'Darwinia Network Native Token': '0 RING',
  'aleph.im v2': '0 ALEPH',
  'Injective Token': '0 INJ',
  erowan: '0 EROWAN',
  'Matic Token': '0 MATIC',
  'APY Governance Token': '0 APY'
}

The problem is I need to find the token balances at an older block height. My API from Chainstack is connected to an archive geth node. So how do I pass a block number into the above code to get the token balances at a particular block height?

1 Answer 1

1

we've recently released a newer and expanded version of the code with the instructions, check it out https://chainstack.com/ultimate-guide-erc20-token-balance/

It specifically has the parameter to query for token balances at a block number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.