How do I check of an account had opted-in to an ASA using the python algosdk?

128 views Asked by At

I'm trying to see if an asset has been opted-in by a user on his wallet before performing a transfer of the asset to the user, but I can't seem to find how this is done in the Python algosdk. Any lead would be appreciated.

I expect to be able to call a function that verify if the asset had been opted-in to, or at least a list of all the opted-in assets, so I can check if the asset index is present in the list.

1

There are 1 answers

1
Fabrice On

The list of assets that an account is opted in is provided by the /v2/account/{address} endpoint.

With the Python SDK, it is given by the account_information function of the V2 client.

Here is an example using the Algoexplorer TestNet API on the account GD7CZ4NH4PBZ75BSGBNXC76CYGEGEYVXYNGVNNWOYY33S64NV7VEISFGKE that is opted in asset ID 10458941 but not in asset ID 10 (at least when the post was written):

import algosdk.v2client

algod_address = "https://node.testnet.algoexplorerapi.io"
algod_token = ""

client = algosdk.v2client.algod.AlgodClient(algod_token, algod_address)

account = client.account_info("GD7CZ4NH4PBZ75BSGBNXC76CYGEGEYVXYNGVNNWOYY33S64NV7VEISFGKE")

def is_account_opted_in(account: dict, asset_id: int):
    return any(asset["asset-id"] == asset_id for asset in account["assets"])

print(is_account_opted_in(account, 10458941))
print(is_account_opted_in(account, 10))