简体   繁体   English

如何在 SOLANA 上使用 web3 js 从令牌地址获取元数据

[英]How to get Metadata from a Token adress using web3 js on SOLANA

I get a list of tokens own by a publicKey thanks to that method:由于该方法,我得到了 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));

return value for an account that own 1 token for example:例如,拥有 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"
}

Now that I have these informations, I would like to know how to get the metadata of that token现在我有了这些信息,我想知道如何获取该令牌的元数据

After Soubriquet request, I ll add here the actual working code.在 Soubriquet 请求之后,我将在此处添加实际工作代码。

Metaplex doc isnt always up to date, so don't trust it. Metaplex 文档并不总是最新的,所以不要相信它。

    const Metadata = require("@metaplex-foundation/mpl-token-metadata");
    
    async function getMetadataPDA(mint) 
        {
          const [publicKey] = await web3.PublicKey.findProgramAddress(
            [Buffer.from("metadata"), 
            Metadata.PROGRAM_ID.toBuffer(), 
            mint.toBuffer()],
            Metadata.PROGRAM_ID
          );
          return publicKey;
        }
    
    let pda = await getMetadataPDA(mint);
    let res = await Metadata.Metadata.fromAccountAddress(connection, account);

Consider that mint is a PublicKey, not a string.考虑 mint 是一个 PublicKey,而不是一个字符串。

Here is JSON structure of the returned values https://docs.metaplex.com/token-metadata/Versions/v1.0.0/nft-standard这是返回值的 JSON 结构https://docs.metaplex.com/token-metadata/Versions/v1.0.0/nft-standard

Unsure if this fully addresses your issue, but one way that I've fetched metadata for all tokens in a wallet is by using the metaplex library:不确定这是否完全解决了您的问题,但我为钱包中的所有代币获取元数据的一种方法是使用 metaplex 库:

import { programs } from "@metaplex/js"

const tokenMetadata = programs.metadata.Metadata.findByOwnerV2(connection, walletPublicKey);

console.log(JSON.stringify(tokenMetadata));

Printing the tokenMetadata for this test wallet looks like the following:打印此测试钱包的 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
    }
  }
]

Migrating OP's solution from the question to an answer:将 OP 的解决方案从问题迁移到答案:

As of 12.18.2021, please now use自 12.18.2021 起,请立即使用

import * as metadata from "@metaplex-foundation/mpl-token-metadata"; const nftsmetadata:metadata.MetadataData[] = await metadata.Metadata.findDataByOwner(connection, publicKey);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM