How can we get token holders from token?
Asked Answered
R

2

8

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())
Revelation answered 20/8, 2021 at 9:59 Comment(0)
O
19

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.

Octant answered 20/8, 2021 at 13:33 Comment(2)
Thanks for your reply @Petr Hejda , But I have one doubt I'm using MetaMask to transfer tokens then how can I keep track of those transactions done through a particular contract address?Revelation
@TrimantraSoftwareSolution You can get the user address through the Ethereum provider API that MetaMask implements. Then it's just as in the second half of my answer: Get past event logs emited by the token. Since you don't need all transfers but only from/to a specific address, you can filter only topics containing the Transfer() event (topics[0]), and having sender (topics[1]) or recipient (topics[2]) the user address. Then you can build the separate (searchable and aggregable) DB from these event logs.Octant
M
7

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

Macdonald answered 30/8, 2022 at 10:23 Comment(5)
Yes, I can see. But if I want to use holders.at with my private network then how can I use it ?Revelation
Unfortunately, it currently only supports Ethereum and Polygon networks.Macdonald
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.Pimp
@HenryDeclety holders.at is mostly made for NFTs and doesn't work with such a large list of holders.Macdonald
Can you expand to show token holders and their amounts? When I input an ERC20 token it only shows their addresses, not the amounts.Shaw

© 2022 - 2024 — McMap. All rights reserved.