简体   繁体   中英

How to query all addresses of a particular Ethereum token above a certain balance

I am new to working with the Ethereum blockchain and am currently looking for a way to query a particular Ethereum token contract to locate all addresses with a balance of that particular token above x amount and then save the results to a.txt file.

I have searched for solutions but most seem to either outline ways to query a specific address, or to query the main Ethereum chain itself. What I want to do is query only the addresses associated with one particular token on Ethereum.

Any help would be greatly appreciated.

The token balance of each address is stored in the contract of the token (not in the address metadata) . Usually (but not always) in a mapping (address => uint256) .

Most of the token standards ( ERC-20 , ERC-721 , etc.) define a function called balanceOf(address) , that in practice usually accesses this mapping.

So in order to get each address balance just by querying the contract storage, you would need to query the balanceOf() function for each existing address. There is 16^40 (or approx. 10^48) possible addresses, so this is practically impossible.


The other outlined way that you mention in your question - querying the Ethereum chain - is much easier.

You can get the past event logs emited by your token contract (the address field in the options).

You can also filter by the first topic to only include the Transfer() event. The topic value is keccak256 of the event signature. In your case keccak256("Transfer(address,address,uint256)") == ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef

Mind that most third-party JSON-RPC providers (such as Infura) only allow limited historical data. So if you want all historical data, you'll most likely need to connect to your own full node.


A similar method to querying the historical logs is used by the 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 balance of all tokens per each address (from this separate DB) is then displayed as the token balance on the address detail page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM