fetch_erc20_balances_multicall

Documentation for eth_defi.balances.fetch_erc20_balances_multicall function.

fetch_erc20_balances_multicall(web3, address, tokens, block_identifier, decimalise=True, chunk_size=50, token_cache=LRUCache({}, maxsize=1024, currsize=0), gas_limit=10000000, raise_on_error=True)[source]

Read balance of multiple ERC-20 tokens on an address once using multicall.

Example:

def test_fetch_erc20_balances_multicall(web3):

    tokens = {
        "0x6921B130D297cc43754afba22e5EAc0FBf8Db75b",  # DogInMe
        "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",  # USDC on Base
    }

    # Velvet vault
    address = "0x9d247fbc63e4d50b257be939a264d68758b43d04"

    block_number = get_almost_latest_block_number(web3)

    balances = fetch_erc20_balances_multicall(
        web3,
        address,
        tokens,
        block_identifier=block_number,
    )

    existing_dogmein_balance = balances["0x6921B130D297cc43754afba22e5EAc0FBf8Db75b"]
    assert existing_dogmein_balance > 0

    existing_usdc_balance = balances["0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"]
    assert existing_usdc_balance > Decimal(1.0)
Parameters
  • address (Union[eth_typing.evm.HexAddress, str]) – Address of which balances we query

  • tokens (list[Union[eth_typing.evm.HexAddress, str]] | set[Union[eth_typing.evm.HexAddress, str]]) – List of ERC-20 addresses.

  • block_identifier

    Fetch at specific height.

    Must be given for a multicall.

  • chunk_size – How many ERC-20 addresses feed to multicall once

  • gas_limit – Gas limit of the multicall request

  • decimalise

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

    Use cached TokenDetails data.

  • token_cache (cachetools.Cache | None) – Cache ERC-20 decimal data.

  • raise_on_error – See BalanceFetchFailed.

  • web3 (web3.main.Web3) –

Raises

BalanceFetchFailed

balanceOf() call failed.

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

Returns

Map of token address -> balance.

If ERC-20 call failed, balance is set to None if raise_on_error is False.

Return type

dict[Union[eth_typing.evm.HexAddress, str], decimal.Decimal]