我想获取给定钱包公钥的我当前拥有的代币列表。
目前我正在使用https://api.solscan.io/account/tokens?address="PUBLIC_KEY">&price=1来获取我拥有的代币。
好的。所以我找到了这个。使用 SPL 令牌 ID 作为程序 ID 将返回所有用户拥有的令牌。
connection
.getParsedTokenAccountsByOwner(
new PublicKey("PUBLIC_KEY"),
{
programId: new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
}
)
Run Code Online (Sandbox Code Playgroud)
我建议使用.getParsedProgramAccounts()Connection 类的方法。
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
},
},
],
}
);
})();
Run Code Online (Sandbox Code Playgroud)
详细说明链接:https://solanacookbook.com/ingredients/get-program-accounts.html#filters
看一下 JSON RPC 调用getTokenAccountsByOwner,其中所有者将是钱包的公钥。
更多信息请访问https://docs.solana.com/developing/clients/jsonrpc-api#gettokenaccountsbyowner
| 归档时间: |
|
| 查看次数: |
9178 次 |
| 最近记录: |