2

I'm trying to build a node.js server that using the web3.js to get a map of all accounts that have a token and the number of that token they all have. The output I'm trying to get is the map key being the address and the map value is the amount of the token they have. I also want to get the data from a single block to avoid wrong balances should someone transfer some of there tokens to another account during the read of the blockchain.

I managed to find a tutorial online on how to check an account and get JSON of all tokens and how much the account has but there doesn't seem to be any tutorials on an effective way of getting all accounts that have a target token.

Can this be done with a vanilla ERC20 or do I need to add in additional functionality to get an array of holders and the amount they have?

As of right now, I have been adding an accounts method that returns the _balances. Added to the standard ERC20 token to get the data. but no luck so far on getting a map of [address|amount].

What I have so far.

"use strict";
import http from 'http';
import Web3 from 'web3';
import Tx from 'ethereumjs-tx';
const abi = [{"...."}];

const web3 = new Web3(process.env.GETH_IPC);
const contractAddress = process.env.TOKEN_ADDRESS;
const myContract = web3.eth.Contract(abi, contractAddress);

//will add an accounts method that returns _balances
const balances = myContract.methods.accounts().encodeABI();

console.log('results:', balances);

the added method to the ERC20 token:

function accounts() public view returns (mapping memory) {
    return _balances;
}

preferably I would like to be able to get this data with a vanilla ERC20 with no modifications to the standard.

Patrick W. McMahon
  • 3,488
  • 1
  • 20
  • 31

1 Answers1

0

Your ERC20 contract can have a method that returns the entire mapping but this isn't the most cost effective considering that the number of addresses containing tokens will eventually become very large.

If you want to retrieve the balance for a handful of accounts then you can execute multiple calls to standard ERC20 function balanceOf. You will need to provide an address for each account.

Tim Hysniu
  • 1,446
  • 13
  • 24