如何获取我拥有的代币列表? [英] How do I get list of tokens owned by me?

查看:12
本文介绍了如何获取我拥有的代币列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取给定钱包公钥的我当前拥有的令牌列表。

目前我正在使用https://api.solscan.io/account/tokens?address="PUBLIC_KEY">&price=1获取我拥有的令牌。

好的。所以我找到了这个。使用SPL令牌ID作为程序ID将返回所有用户拥有的令牌。

connection
  .getParsedTokenAccountsByOwner(
    new PublicKey("PUBLIC_KEY"),
    {
      programId: new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
    }
  )

推荐答案

我建议使用Connection类的.getParsedProgramAccounts()方法。

import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { clusterApiUrl, Connection } from "@solana/web3.js";

(async () => {
  const MY_WALLET_ADDRESS = "FriELggez2Dy3phZeHHAdpcoEXkKQVkv6tx3zDtCVP8T";
  const connection = new Connection(clusterApiUrl("devnet"), "confirmed");

  const accounts = await connection.getParsedProgramAccounts(
    TOKEN_PROGRAM_ID, // new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
    {
      filters: [
        {
          dataSize: 165, // number of bytes
        },
        {
          memcmp: {
            offset: 32, // number of bytes
            bytes: MY_WALLET_ADDRESS, // base58 encoded string
          },
        },
      ],
    }
  );
})();

详细说明链接:https://solanacookbook.com/ingredients/get-program-accounts.html#filters

这篇关于如何获取我拥有的代币列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆