fetch_erc20_balances_by_token_list

Documentation for eth_defi.balances.fetch_erc20_balances_by_token_list function.

fetch_erc20_balances_by_token_list(web3, owner, tokens, block_identifier=None, decimalise=False)[source]

Get all current holdings of an account for a limited set of ERC-20 tokens.

If you know what tokens you are interested in, this method is much more efficient way than fetch_erc20_balances_by_transfer_event() to query token balances.

Example:

def test_portfolio_token_list(web3: Web3, deployer: str, user_1: str, usdc: Contract, aave: Contract):
    # Create a set of tokens
    tokens = {aave.address, usdc.address}
    # Load up the user with some tokens
    usdc.functions.transfer(user_1, 500).transact({"from": deployer})
    aave.functions.transfer(user_1, 200).transact({"from": deployer})
    balances = fetch_erc20_balances_by_token_list(web3, user_1, tokens)
    assert balances[usdc.address] == 500
    assert balances[aave.address] == 200
Parameters
  • tokens (Set[Union[eth_typing.evm.HexAddress, str]]) – ERC-20 list

  • block_identifier (Optional[Union[Literal['latest', 'earliest', 'pending', 'safe', 'finalized'], eth_typing.evm.BlockNumber, eth_typing.evm.Hash32, eth_typing.encoding.HexStr, hexbytes.main.HexBytes, int]]) – Fetch at specific height

  • decimalise

    If True, convert output amounts to humanised format in Python Decimal.

    Use cached TokenDetails data.

  • web3 (web3.main.Web3) –

  • owner (eth_typing.evm.HexAddress) –

Raises

BalanceFetchFailed – When you give a non-ERC-20 contract as a token.

Return type

Dict[Union[eth_typing.evm.HexAddress, str], int | decimal.Decimal]