How can we get token holders from token?

Question:

I have created my own ERC-20 token (AJR) and deploy on Ethereum private node, Now I want to list all the transaction by Token name.

Also, I need to list down all the token holders by using contract address or token name.

I try to fetch using web3 but I get only symbol, name, total supply, etc. but not token holders or transactions

Below is my sample code:

from web3 import Web3

Web3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))

contract_instance = Web3.eth.contract(contract_address, abi=abi)

print(contract_instance.functions.name().call())

Answers:

Token holders are not directly available through the RPC protocol and RPC wrappers such as Web3.

Information about token holders is stored on the blockchain in the token contract (or some of its dependencies), usually in the form of a mapping. Which means that you can’t just loop through all of the holders, but you need to know the address and ask for their balance.

// the key is the holder address, the value is their token amount
mapping (address => uint256) public balanceOf;

But – the ERC-20 standard defines the Transfer() event that the token contract should emit when a transfer occurs.

mapping (address => uint256) public balanceOf;

event Transfer(address indexed _from, address indexed _to, uint256 _amount);

function transfer(address _to, uint256 _amount) external returns (bool) {
    balanceOf[msg.sender] -= _amount;
    balanceOf[_to] += _amount;
    emit Transfer(msg.sender, _to, _amount);
    return true;
}

So you’ll need to build and maintain a database of holders from all Transfer() event logs emitted by this token contract. Collect past event logs to build the historic data, and subscribe to newly emitted logs to keep it up-to-date. Then you can aggregate all of this raw transfer data to the form of "address => current balance" and filter only addresses that have non-zero balance in your searchable DB.

Docs:

  • Get past event logs in Web3 – link
  • Subscribe to new event logs in Web3 – link

The same way is actually used by blockchain explorers. They scan each transaction for Transfer() events and if the emitter is a token contract, they update the token balances in their separate DB. The list of all holders (from this separate DB) is then displayed on the token detail page.

Answered By: Petr Hejda

I made a simple tool holders.at exactly for this. You can export ERC20, ERC721, and ERC1155 token holders at any block.

For example, this is a list of all token holders for 0x5a98fcbea516cf06857215779fd812ca3bef1b32 token at a block height 15000000: https://holders.at/ethereum/0x5a98fcbea516cf06857215779fd812ca3bef1b32/15000000

Answered By: jackqack

great website but unfortunately it is not working for me, I get Failed to fetch holders: 500, can you help? I would like to get the full list of token holders for the token DAI on Ethereum network.

Answered By: Henry Declety