Skip to content

Instantly share code, notes, and snippets.

@svzdvd
Created December 20, 2021 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svzdvd/6dc903584210a9910a25e5fca0f97bc8 to your computer and use it in GitHub Desktop.
Save svzdvd/6dc903584210a9910a25e5fca0f97bc8 to your computer and use it in GitHub Desktop.
from web3 import Web3
import json
wallet_address = 'YOUR_WALLET_ADDRESS'
w3 = Web3(Web3.HTTPProvider('https://bsc.getblock.io/mainnet/?api_key=GET_BLOCK_API_KEY'))
wei_balance = w3.eth.getBalance(wallet_address)
eth_balance = w3.fromWei(wei_balance, 'ether')
print(eth_balance)
wallet_address = Web3.toChecksumAddress(wallet_address)
token_contract_address = Web3.toChecksumAddress('YOUR_TOKEN_ADDRESS')
# define erc-20 contract
ABI = json.loads('[{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]')
contract = w3.eth.contract(token_contract_address, abi=ABI)
# call contract and get data from balanceOf for argument wallet_address
token_balance_wei = contract.functions.balanceOf(wallet_address).call()
# convert the value from Wei to Ether
token_balance = Web3.fromWei(token_balance_wei, 'ether')
print('Token Balance: ' + str(token_balance))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment