0

I need to get the Metadata like name,symbol,image uri for a token with a mint address. what is the best way of doing it

2 Answers 2

0

You can try this way:

import * as anchor from "@project-serum/anchor";
import {
  Metadata,
  PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID,
} from "@metaplex-foundation/mpl-token-metadata";

describe("metaplex", () => {
  const RPC_URL = "https://api.devnet.solana.com";
  const walletKeypair = anchor.web3.Keypair.generate();

  const provider = new anchor.AnchorProvider(
    new anchor.web3.Connection(RPC_URL),
    new anchor.Wallet(walletKeypair),
    { commitment: "confirmed" }
  );

  it("metadata", async () => {
    const mint = new anchor.web3.PublicKey(
      "FSz6W7vznZH2kvkjqYESk35NozQn6eScqwNduHc46wL1"
    );

    const [metadataPDA] = anchor.web3.PublicKey.findProgramAddressSync(
      [
        Buffer.from("metadata"),
        TOKEN_METADATA_PROGRAM_ID.toBuffer(),
        mint.toBuffer(),
      ],
      TOKEN_METADATA_PROGRAM_ID
    );

    const accInfo = await provider.connection.getAccountInfo(metadataPDA);

    const metadata = Metadata.deserialize(accInfo.data, 0);

    console.log(metadata);
  });
});
0
0

If the token metadata was added using the Token Metadata Program, you can use the findByMint method from the Metaplex SDK, which returns an Nft object that contains all the relevant data for a specific token Mint.

Although its called the Nft model, it works for regular fungible tokens as well since metadata is associated with a token mint the same way.

  const metaplex = Metaplex.make(connection)

  const mintAddress = new PublicKey("TOKEN_MINT_HERE")

  const nft = await metaplex.nfts().findByMint({ mintAddress }).run()

  console.log(nft)

The metaplex docs are really well written with awesome diagrams, recommend looking through.

https://docs.metaplex.com/programs/token-metadata/overview

https://github.com/metaplex-foundation/js#findByMint

https://github.com/metaplex-foundation/js#the-nft-model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.