I'm trying to fetch asset balances in accounts for various assets using py-substrate-interface. I know I can query the Tokens.Accounts storage function but I'm not sure which params I need to pass to the query_map function to get the balances.

For example I want to get balances of {"ForeignAsset":"5"} on acala, how would the query_map function look like.

1 Answer 1


Because it can be quitte cumbersome to compose complex params of certain storage functions, I generated composition examples for well-known runtimes (also for calls): https://polkascan.github.io/py-substrate-metadata-docs

In this specific case you can have a look at: https://polkascan.github.io/py-substrate-metadata-docs/acala/tokens/#accounts

Because you want to query a mapped storage function, you can omit params from right to left to get all results:

result = substrate.query_map('Tokens', 'Accounts', ['24YtbsBQ2CG9e6MeF71aownnpHaU3y4VugVj7HzEmSacbofK'], max_results=10)

for token, account_info in result:
    print(f'{token.value}: {account_info.value}')
    # {'LiquidCrowdloan': 13}: {'free': 120800000000, 'reserved': 0, 'frozen': 0}

It is also possible to query multiple dimensions:

result = substrate.query_map('Tokens', 'Accounts', max_results=10)

for (account, token), account_info in result:
    print(f'{account.value}: {token.value}: {account_info.value}')
    # 24YtbsBQ2CG9e6MeF71aownnpHaU3y4VugVj7HzEmSacbofK: {'LiquidCrowdloan': 13}: {'free': 120800000000, 'reserved': 0, 'frozen': 0}

Note the max_results, as this can result in many records.

Edit: However, because of the way the storage key is composed in Substrate, it is only possible to omit certain params in a strict order. So it is not possible to query a certain token without specifying the account first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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