3

I’m desperately looking for a method that allows me to retrieve all token owned by a wallet using python. I should insert the wallet address as I put and the code should return the list of the token owned by that wallet. Unfortunately there are just answers for other programming language or outdated, which don’t work anymore. Last code I tried (and it returned erroneously zero) was this:

from web3 import Web3, IPCProvider

import json

token_t_abi = json.loads('[{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]')

wallet_w = '0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE'

w3 = Web3(IPCProvider())

token = w3.eth.contract(

'0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE',

abi=token_t_abi,

)

print(token.call().balanceOf(wallet_w))

2 Answers 2

0

Unfortunately, at the moment there isn't a simple function in Web3 that returns all ERC20 tokens from an address.

The simplest solution with Web3 is to loop through all ERC20 token contracts and call the balanceOf function of the smart contracts.

Simpler is by using an API that returns all tokens an address owns. The ethplorer.io API does this and is free to use. API link

To make an API call: https://api.ethplorer.io/getAddressInfo/[address]?apiKey=freekey

Example: https://api.ethplorer.io/getAddressInfo/0x0000000000000000000000000000000000000000?apiKey=freekey

>:{

2
  • Great answer. Are there similar APIs for other Chains EVMs compatible? Such as fantom or polygon
    – GKM__
    Commented Dec 23, 2021 at 13:05
  • Sort of, although one should not rely on their value per se, docs.dex.guru/resources/api is free and, and the metamask portfolio uses an api that is easily extracted for personal use at portfolio.metamask.io. Also, bitquery.io
    – Jim Zhou
    Commented Oct 14, 2022 at 22:50
0

Developer advocate at Chainstack here.

This is one of the common tasks that should have a simple way but don't, unfortunately. Getting this kind of data from the chain is certainly possible but not as straightforward as it should be; I'll give you two alternative options here, but not using web3.py directly.

Using Subgraphs

The first and best option if you want to index the data yourself is to use a Subgraph, which allows you to index data coming from smart contracts; since smart contracts manage ERC-20 tokens, this is a good solution.

Here, you can find a guide from the Chainstack dev portal showing you how to index ERC-20 tokens with Subgraphs, which include balances. You can essentially make an API from this.

You can then query the data using Python if you want to.

Indexing ERC-20 token balance using Subgraphs

Chainstack offers Subgraphs for various chains, so you can do this for multiple networks if needed.

Use third party APIs

A good solution for a quick way with close to zero setup is using an API like Covalent. You can also find an integration with Chainstack, which allows you to access their APIs.

You can use this endpoint directly if you want to use Python.

Full disclosure: I am a dev advocate at Chainstack, and feel free to reach out!

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.