Ood
Ood

Reputation: 1805

Add Meta Data To Solana Token with @solana/web3.js

I am trying to create an NFT using web3.js and spl-token.js.

However, i need to add meta data (like the name of the token or other attributes), otherwise it just shows up as "Unknown Token" in my wallet.

This is the relevant part of the code where I am minting the token:

let mint = await splToken.Token.createMint(
    connection,
    fromWallet,
    fromWallet.publicKey,
    null,
    0,
    splToken.TOKEN_PROGRAM_ID
);

Otherwise the code is similar to the answers on this question: I would like to mint a new token on solana. How can I do this using solana-web3.js?

There does not seem to be any documentation whatsoever, except for the structure of the meta data (which I found here: https://docs.phantom.app/integrating/tokens/on-chain-metadata).

If anyone could point me in the right direction with an example or documentation it would be really much appreciated. Thank you!

Upvotes: 13

Views: 6480

Answers (3)

Saidu
Saidu

Reputation: 159

To create a token with metadata you would need this package @solana/spl-token-metadata

And you can import the TokenMetadata from it

import { createInitializeInstruction, pack, TokenMetadata } from '@solana/spl-token-metadata';

const metadata: TokenMetadata = {
  mint: mint.publicKey,
  name: "Nata-Coin",
  symbol: "Nata-Coin",
  uri: "your-meta.json",
  additionalMetadata: [["description", "your description"]],
};

You can create and add your meta informations to the Transaction now like this

const mintTransaction = new Transaction().add(
  SystemProgram.createAccount({
    fromPubkey: payer.publicKey,
    newAccountPubkey: mint.publicKey,
    space: mintLen,
    lamports: mintLamports,
    programId: TOKEN_2022_PROGRAM_ID,
  }),

  createInitializeMetadataPointerInstruction(mint.publicKey, payer.publicKey, mint.publicKey, TOKEN_2022_PROGRAM_ID),
  createInitializeMintInstruction(mint.publicKey, decimals, payer.publicKey, null, TOKEN_2022_PROGRAM_ID),

  createInitializeInstruction({
    programId: TOKEN_2022_PROGRAM_ID,
    mint: mint.publicKey,
    metadata: metadata.mint,
    name: metadata.name,
    symbol: metadata.symbol,
    uri: metadata.uri,
    mintAuthority: payer.publicKey,
    updateAuthority: payer.publicKey,
  }),
);

You can look at this git repo for the full code SPL-Token with metadata

Upvotes: -1

olllejik
olllejik

Reputation: 1444

In order to add metadata to NFT you need to invoke this program spl_token_metadata::instruction::create_metadata_accounts. You can find the documentation here.


PS: The example above is in Rust. To do it in JavaScript is like that:

import { Metaplex, keypairIdentity } from "@metaplex-foundation/js";
const metaplex = new Metaplex(connection);
metaplex.use(keypairIdentity(keypair));
const mintNFTResponse = await metaplex.nfts().create({
  uri: "https://ffaaqinzhkt4ukhbohixfliubnvpjgyedi3f2iccrq4efh3s.arweave.net/KUAIIbk6p8oo4XHRcq0U__C2r0mwQaNl0gQow4Qp9yk",
  maxSupply: 1,
});

Like described here is another exemple.

Upvotes: 5

CK Chan
CK Chan

Reputation: 59

Create a metadata for an NFT token in Solana is quite complicated. It is because in Solana SPL-token account would not carry the Metadata. Instead, you have to create another account to carry such data. So, I suggest you to use Metaplex's Candy Machine to make your own NFT with Metadata. You may get more information from their github: https://github.com/metaplex-foundation/metaplex/

Upvotes: 0

Related Questions