1

I want to add metadata on solana nft.

export const createToken = async () => {
  let bs58 = require("bs58");
  const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
  const bs = bs58.decode(secretKey);
  const wallet = Keypair.fromSecretKey(bs); // get wallet keypair
  // TokenAccount
  const mint = await createMint(
    connection,
    wallet,
    wallet.publicKey,
    null,
    0, // if value is 0, means non-fungible token
    undefined,
    {},
    TOKEN_PROGRAM_ID,
  );
  console.log(mint.toBase58());
  // TokenMintAccount
  const associatedTokenAccount = await getOrCreateAssociatedTokenAccount(
    connection,
    wallet,
    mint,
    wallet.publicKey,
  );

  await mintTo(connection, wallet, mint, associatedTokenAccount.address, wallet, 1);

  let transaction = new Transaction().add(
    createSetAuthorityInstruction(mint, wallet.publicKey, AuthorityType.MintTokens, null),
  );

  const trans = await sendAndConfirmTransaction(connection, transaction, [wallet]);
  connectWithMetaplex(wallet, mint.toBase58());
};

export const connectWithMetaplex = async (wallet, tokenMintAccount) => {
  const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
  // const metaplex = Metaplex.make(connection).use(keypairIdentity(wallet)).use(bundlrStorage());
  const mx = Metaplex.make(connection);

  await mx.nfts().findByMint(new PublicKey(tokenMintAccount));
  await mx.nfts().uploadMetadata({
    name: "JERRY NFT",
    description: "JERRY'S NFT 1",
  });
};

I refer https://github.com/metaplex-foundation/js#uploadMetadata

I think there is no connection between metadata and my token address.

is there anyway make them connected?


export const createToken = async () => {
  let bs58 = require("bs58");
  const bs = bs58.decode(secretKey);
  const wallet = Keypair.fromSecretKey(bs); // get wallet keypair

  connectWithMetaplex(wallet);
};

export const connectWithMetaplex = async wallet => {
  const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
  const metaplex = Metaplex.make(connection).use(keypairIdentity(wallet));

  const { uri } = await metaplex
    .nfts()
    .uploadMetadata({
      name: "JERRY NFT",
      description: "JERRYS FIRST NFT",
    })
    .run();

  const { nft } = await metaplex
    .nfts()
    .create({
      uri: uri,
      name: "JERRY NFT",
      sellerFeeBasisPoints: 100,
    })
    .run();

  console.log(nft);
};

update code

1 Answer 1

3

Since you are using the metaplex js sdk already:

You should replace your whole createToken function with the create function from the sdk: https://github.com/metaplex-foundation/js#create

First run the upload, then create. It will automatically mint the token with the required accounts and add metadata to it.

4
  • thx ! you mean I delete createMint function from '@solana/spl-token' ? and I replaced it create function from '@metaplex-foundation/js' ?
    – Jerry
    Commented Jul 29, 2022 at 6:39
  • Delete the first 34 lines of your snippet. Use create from @metaplex-foundation/js instead, yes. Commented Jul 29, 2022 at 6:47
  • I tried what you say. it causes 'TypeError: Cannot read properties of undefined (reading 'toBuffer')'. I update my code
    – Jerry
    Commented Jul 29, 2022 at 6:51
  • The error occured when I call uploadMetadata function
    – Jerry
    Commented Jul 29, 2022 at 6:57

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.