get_token_balance

Function

def get_token_balance(token_address, wallet_address, decimals=False):
    token_contract = load_contract(token_address)
    token_info = get_token_info(token_address)
    token_balance = token_contract.functions.balanceOf(wallet_address).call()
    if decimals:
        return token_balance
    else:
        return float(round(from_token_decimals(token_balance, token_info['decimals']), 15))

Description

  • Load the contract from token address

  • Get the token info from token address

  • Get the token balance for the wallet address

  • If decimals is set to False then remove 18 decimal places

  • Return the token balance as integer/float