2

Since @solana/spl-token-registry package is deprecated, I have the question: how to get token metadata by contract address?

  • decimals - can be obtained by getParsedAccountInfo method of @solana/web3.js library.
  • symbol - ?
  • token name -?

Is there any API to get symbol and token name?

For example the following token not present in the @solana/spl-token-registry https://solscan.io/token/hntyVP6YFm1Hg25TN9WGLqM12b8TQmcknKrdu1oxWux

Related issue: https://github.com/solana-labs/solana-program-library/issues/4144

1 Answer 1

5

Token Registry is an outdated way to retrieve token's metadata. The current standard is to use Metaplex's Token Metadata Program to add on-chain metadata for tokens.

First, you need to calculate the metadata address of the token, which can be done using @metaplex-foundation/js lib like this:

import { Metaplex } from '@metaplex-foundation/js';

const metaplex = Metaplex.make(connection);
const metadataPda = metaplex.nfts().pdas().metadata({ mint: mintAddress });

connection is a Connection instance from web3.js library and mintAddress is the public key of the token's mint.

Alternatively, you can also calculate pda directly using @solana/web3.js lib using seeds.

Once you have the PDA, fetch the token's metadata like this:

import { Metadata } from "@metaplex-foundation/mpl-token-metadata";

const account = await Metadata.fromAccountAddress(connection, metadataPda);

It will return Metadata class and you can get details about the token name, symbol, etc using data property on account: account.data.name.

More about Metadata:https://metaplex-foundation.github.io/metaplex-program-library/docs/token-metadata/classes/Metadata.html

1
  • Thank you. I've faced with that it is impossible to get metadata for some old tokens, for example 7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx
    – mahnunchik
    Commented May 15, 2023 at 10:01

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.