如何在 SOLANA 上使用 web3 js 从令牌地址获取元数据
How to get Metadata from a Token adress using web3 js on SOLANA
由于该方法,我得到了 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 个令牌的帐户的 return 值:
{
"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);
不确定这是否完全解决了您的问题,但我为钱包中的所有代币获取元数据的一种方法是使用 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, 251, 66, 44, 245, 255, 202, 46, 123, 104,
19, 136, 141, 79, 66, 188, 138, 134, 1, 0, 206, 203, 108, 109, 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bu9k4Yu6zUtBgJ4CYKo7whX3PyQDd12zpdjz3",
"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
}
}
]
在 Soubriquet 请求后,我将在此处添加实际工作代码。
Metaplex 文档并不总是最新的,所以不要相信它。
import * as metadata from "@metaplex-foundation/mpl-token-metadata";
const nftsmetadata:metadata.MetadataData[] = await metadata.Metadata.findDataByOwner(connection, publicKey);
这里publicKey为钱包公钥。它将 returns 钱包拥有的所有元数据。
这是返回值的JSON结构
https://docs.metaplex.com/token-metadata/Versions/v1.0.0/nft-standard
如果我能给你提示,请加入 Metaplex discord,在这里你会比查看文档更好地了解所有更新。
如果此代码已过时,我将尝试尽快更新此答案。
由于该方法,我得到了 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 个令牌的帐户的 return 值:
{
"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);
不确定这是否完全解决了您的问题,但我为钱包中的所有代币获取元数据的一种方法是使用 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, 251, 66, 44, 245, 255, 202, 46, 123, 104,
19, 136, 141, 79, 66, 188, 138, 134, 1, 0, 206, 203, 108, 109, 123,
133, 164, 214, 142, 166, 98, 20, 48, 35, 184, 204, 119, 234, 128, 56,
190, 152, 177, 103, 83, 12, 148, 233, 189, 70, 167, 39, 0, 100, 1, 1,
1, 254, 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, 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, 0, 0, 0
]
}
},
"data": {
"key": 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
}
}
]
在 Soubriquet 请求后,我将在此处添加实际工作代码。
Metaplex 文档并不总是最新的,所以不要相信它。
import * as metadata from "@metaplex-foundation/mpl-token-metadata";
const nftsmetadata:metadata.MetadataData[] = await metadata.Metadata.findDataByOwner(connection, publicKey);
这里publicKey为钱包公钥。它将 returns 钱包拥有的所有元数据。
这是返回值的JSON结构 https://docs.metaplex.com/token-metadata/Versions/v1.0.0/nft-standard
如果我能给你提示,请加入 Metaplex discord,在这里你会比查看文档更好地了解所有更新。
如果此代码已过时,我将尝试尽快更新此答案。