You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python algosdk检查账户是否已选择加入ASA?

用Python Algosdk检查用户钱包是否已加入目标ASA

实现思路

核心是通过Algorand节点获取用户账户的资产持有信息,从中提取已选择加入的ASA列表,再验证目标资产索引是否在列表内。

具体步骤与代码示例

1. 初始化Algod客户端

先创建与Algorand节点的连接,可使用本地节点或第三方节点服务:

from algosdk.v2client import algod

# 替换为你的节点地址和API密钥(第三方服务如PureStake需添加headers)
algod_address = "https://mainnet-algorand.api.purestake.io/ps2"
algod_token = "你的API密钥"
headers = {"X-API-Key": algod_token}
algod_client = algod.AlgodClient(algod_token, algod_address, headers)

2. 获取用户已加入的资产列表

调用account_info接口获取账户详情,提取其中的资产ID列表:

def get_opted_in_assets(account_address):
    account_details = algod_client.account_info(account_address)
    # 从账户资产列表中提取所有ASA的ID
    opted_in_asset_ids = [asset["asset-id"] for asset in account_details["assets"]]
    return opted_in_asset_ids

3. 验证目标资产是否已加入

调用上述函数,判断目标资产索引是否存在于已加入列表中:

def check_asset_opt_in_status(account_address, target_asset_id):
    opted_in_ids = get_opted_in_assets(account_address)
    return target_asset_id in opted_in_ids

# 用法示例
user_address = "用户钱包地址"
target_asa_index = 123456  # 替换为你要检查的ASA索引
if check_asset_opt_in_status(user_address, target_asa_index):
    print("用户已加入该ASA,可执行转账操作")
else:
    print("用户未加入该ASA,无法直接转账")

补充说明

  • 即使账户内该ASA余额为0,只要之前完成过选择加入操作,assets字段中仍会保留该资产ID,因此该验证方法准确可靠。
  • 本地测试节点可省略API密钥与headers,直接使用默认地址连接。

内容的提问来源于stack exchange,提问作者Emmanuel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 12:10:16