1

I am trying to get the balance in my wallet of specific tokens with no luck. I have found examples online on how to do this task using web3.js, but the methods used seem to be different. I also found this helpful example: How to view custom token balance in ether wallet using web3, however it doesn't seem to be working for me.

Is anyone able to provide a short script that can do this?

As an example, on the polygon network I expect the wallet address 0xBeD5658e57F54301a23a899674b6593bBf039ab5 to contain 0.882 USDT. Is there anyway to get this programmatically?

2

You need the token's ABI and utilize the function balanceOf. Example snippet below:

token = w3.eth.contract(address={token address}, abi={token abi}) # declaring the token contract
token_balance = token.functions.balanceOf({your address}).call() # returns int with balance, without decimals

You can find the token ABI from the block explorer, from the code tab of your token. In your case, here you can find the ABI for USDT's contract. You will need to parse JSON before being able to use it in Python.

Edit: I just realized the USDT contract in Polygon doesn't have balanceOf in its ABI. I think you should be able to utilize the ETH ABI no problem.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.