如何在 SOLANA 上使用 web3 js 从令牌地址获取元数据
Posted
技术标签:
【中文标题】如何在 SOLANA 上使用 web3 js 从令牌地址获取元数据【英文标题】:How to get Metadata from a Token adress using web3 js on SOLANA 【发布时间】:2021-12-22 07:33:21 【问题描述】:由于该方法,我得到了 publicKey 拥有的令牌列表:
connection
.getParsedTokenAccountsByOwner(
new PublicKey(publicKey.toBase58()),
programId: TOKEN_PROGRAM_ID
)
.then((b) =>
const owner = b?.value?.[0].account.owner;
const pb = b?.value?.[0].pubkey;
const nonZeroAccounts = b?.value?.filter(
(obj) => obj.account.data.parsed.info.tokenAmount.uiAmount > 0
);
setTokens(JSON.stringify(nonZeroAccounts, null, 2));
拥有 1 个令牌的帐户的返回值,例如:
"account":
"data":
"parsed":
"info":
"isNative": false,
"mint": "example",
"owner": "example",
"state": "initialized",
"tokenAmount":
"amount": "1",
"decimals": 0,
"uiAmount": 1,
"uiAmountString": "1"
,
"type": "account"
,
"program": "spl-token",
"space": 165
,
"executable": false,
"lamports": 2039280,
"owner":
"_bn": "example"
,
"rentEpoch": 245
,
"pubkey":
"_bn": "example"
现在我有了这些信息,我想知道如何获取该令牌的元数据。感谢您的帮助。
编辑 (12.18.2021)
请立即使用
import * as metadata from "@metaplex-foundation/mpl-token-metadata";
const nftsmetadata:metadata.MetadataData[] = await metadata.Metadata.findDataByOwner(connection, publicKey);
【问题讨论】:
【参考方案1】:不确定这是否完全解决了您的问题,但我获取钱包中所有代币的元数据的一种方法是使用 metaplex 库:
import programs from "@metaplex/js"
const tokenMetadata = programs.metadata.Metadata.findByOwnerV2(connection, walletPublicKey);
console.log(JSON.stringify(tokenMetadata));
打印此测试钱包的 tokenMetadata 如下所示:
[
"pubkey": "HPswFDfXcmpvj6RSvJoiuTe6r1wGuwJe9HgzkfhpHKUc",
"info":
"executable": false,
"owner":
"_bn": "0b7065b1e3d17c45389d527f6b04c3cd58b86c731aa0fdb549b6d1bc03f82946"
,
"lamports": 5616720,
"data":
"type": "Buffer",
"data": [
4, 60, 149, 84, 75, 223, 131, 43, 238, 37, 212, 193, 171, 139, 29,
162, 158, 45, 52, 157, 244, 236, 53, 189, 60, 118, 83, 187, 176, 3,
87, 134, 164, 70, 147, 118, 78, 64, 38, 124, 25, 241, 242, 54, 51, 83,
80, 221, 253, 4, 95, 165, 233, 112, 107, 151, 166, 96, 148, 162, 114,
132, 111, 55, 68, 32, 0, 0, 0, 89, 111, 117, 114, 32, 67, 111, 108,
108, 101, 99, 116, 105, 111, 110, 32, 35, 52, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200, 0,
0, 0, 104, 116, 116, 112, 115, 58, 47, 47, 97, 114, 119, 101, 97, 118,
101, 46, 110, 101, 116, 47, 121, 57, 105, 54, 104, 50, 97, 107, 65,
100, 80, 105, 90, 112, 98, 65, 45, 114, 65, 110, 57, 87, 97, 101, 68,
110, 83, 76, 85, 73, 78, 84, 117, 106, 109, 69, 113, 101, 118, 72, 66,
117, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 2, 0, 0, 0, 148, 111, 225, 158, 161, 28, 45, 38, 87,
197, 46, 209, 25, 126, 164,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": 4,
"updateAuthority": "55VXi3ANYmig6eP8oewpz5KFzzjyT87RiX4mw2bfjchy",
"mint": "5kVxPxTbu9k4Yu6zUtBgJ4CYKo7whX3PyQDd12zpdjz3",
"data":
"name": "Your Collection #4",
"symbol": "",
"uri": "https://arweave.net/y9i6h2akAdPiZpbA-rAn9WaeDnSLUINTujmEqevHBu0",
"sellerFeeBasisPoints": 0,
"creators": [
"address": "AzSHgm4CEQr3akEvh7AppmZw3oDYWunu18FoM61XRwA5",
"verified": 1,
"share": 0
,
"address": "EvEttHAwkVks2s5zEZjoPNVDBkhumTyvLTvaD5JQ3XCJ",
"verified": 0,
"share": 100
]
,
"primarySaleHappened": 1,
"isMutable": 1
]
【讨论】:
以上是关于如何在 SOLANA 上使用 web3 js 从令牌地址获取元数据的主要内容,如果未能解决你的问题,请参考以下文章
如何获取在与@solana/web3.js 的交易中转移的自定义令牌数量?
如何使用 web3js solana 工具关闭关联的令牌帐户
如何通过“@solana/web3.js”和“@solana/spl-token”调用 nft 程序?