【问题标题】:How do I get list of tokens owned by me?如何获取我拥有的代币列表?
【发布时间】:2021-11-21 17:50:30
【问题描述】:

对于给定的钱包公钥,我想获取我当前拥有的代币列表。

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

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

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

【问题讨论】:

  • 感谢您的回答 - 提供 SPL 令牌 ID 作为程序 ID。我搜索了一段时间才找到这个
  • 请将您问题中的主要信息转换为答案。谢谢!

标签: blockchain solana


【解决方案1】:

查看 JSON RPC 调用 getTokenAccountsByOwner,其中所有者将是钱包的公钥。

更多信息https://docs.solana.com/developing/clients/jsonrpc-api#gettokenaccountsbyowner

【讨论】:

    【解决方案2】:

    我建议使用 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

    【讨论】:

      猜你喜欢
      • 2022-01-13
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      • 2019-06-25
      相关资源
      最近更新 更多