Return application local state for a specific account with IndexerClient

I would try to return application local state for a specific account. I’m trying to use indexer with PureStake.

#initialize an indexerClient 
    headers = {
   "X-API-Key": "my_key_purestake",
    }
    indexer_client = indexer.IndexerClient("", "https://testnet-algorand.api.purestake.io/idx2", headers)
    

I found this method v2client.indexer — algosdk documentation (py-algorand-sdk.readthedocs.io) but does not appear in visual studio, as if it were not available.

In fact, if I try to call it:

#initialize an indexerClient 
    headers = {
   "X-API-Key": "my_key_purestake",
    }
    indexer_client = indexer.IndexerClient("", "https://testnet-algorand.api.purestake.io/idx2", headers)
    indexer_client.lookup_account_application_local_state(player1_address)

it returns an error:

c:/Users/loren/OneDrive/Desktop/algorand_project/project/contracts/my_sasso_carta_forbici/main.py
Traceback (most recent call last):
File “c:\Users\loren\OneDrive\Desktop\algorand_project\project\contracts\my_sasso_carta_forbici\main.py”, line 500, in
main()
File “c:\Users\loren\OneDrive\Desktop\algorand_project\project\contracts\my_sasso_carta_forbici\main.py”, line 414, in main
indexer_client.lookup_account_application_local_state(player1_address)
AttributeError: ‘IndexerClient’ object has no attribute ‘lookup_account_application_local_state’

Not sure about “lookup_account_application_local_state” but you can find local state of all apps for an account using “account_info”.

Here is the documentation:
https://py-algorand-sdk.readthedocs.io/en/latest/algosdk/v2client/indexer.html?#algosdk.v2client.indexer.IndexerClient.account_info

The response contains local states of all apps that the account has opted-in. You need to iterate over the local states and find it based on the app-id.

3 Likes

Ok perfect! I have implemented method like this:

#account_address: public key account, app_id: application id, indexer: IndexerClient
def read_local_state (account_address, app_id, indexer) -> None:
    account_info = indexer.account_info(account_address)
    dict_account  = account_info['account']
    dict_local_state = dict_account['apps-local-state']
    for x in dict_local_state:
        if x['id'] == app_id:
            dict_key_value = x['key-value']
            for y in dict_key_value :
                key = y['key'] #key in b64
                key_b64_bytes = key.encode('ascii')
                msg_bytes = base64.b64decode(key_b64_bytes)
                key_ascii = msg_bytes.decode('ascii') #key in ascii
                value = y['value']
                print("key:",key_ascii, ", value:", value)
1 Like

Perfect. please mark the above as a solution.

2 Likes