3

I already succeed create token using spl-token, now how to add metadata?

    const mint = Keypair.generate();
    const ata = getAssociatedTokenAddressSync(
      mint.publicKey,
      wallet.publicKey!
    );

    const tx = new Transaction().add(
      SystemProgram.createAccount({
        fromPubkey: wallet.publicKey!,
        newAccountPubkey: mint.publicKey,
        space: MINT_SIZE,
        lamports: await connection.getMinimumBalanceForRentExemption(MINT_SIZE),
        programId: TOKEN_PROGRAM_ID,
      }),
      createInitializeMintInstruction(
        mint.publicKey,
        Number(formData.decimals),
        wallet.publicKey!,
        null
      ),
      createAssociatedTokenAccountInstruction(
        wallet.publicKey!,
        ata,
        wallet.publicKey!,
        mint.publicKey
      ),
      createMintToCheckedInstruction(
        mint.publicKey,
        ata,
        wallet.publicKey!,
        Number(formData.supply) * 10 ** Number(formData.decimals),
        Number(formData.decimals)
      ),
    );

    const blockhash = (await connection.getLatestBlockhash()).blockhash;
    tx.recentBlockhash = blockhash;
    tx.feePayer = wallet.publicKey!;
    tx.sign(mint);

    const signedTx = awalt wallet.signTransaction(tx,connection);
    await wallet.sendTransaction(signedTx);

1
  • are you able to provide the full script including the imports? Thank you! Commented Oct 14 at 19:13

1 Answer 1

6

You can use npm i mpl-token-metadata version 2 with createCreateMetadataAccountV3Instruction, if you still want to continue your code

npm i @metaplex-foundation/mpl-token-metadata@2

base on your code:

    // define metadata program id
    const TOKEN_METADATA_PROGRAM_ID = new PublicKey(
    "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
    );

    const mint = Keypair.generate();
    const ata = getAssociatedTokenAddressSync(
      mint.publicKey,
      wallet.publicKey!
    );
    
    // Get metadataPDA
    const getMetadataPDA = PublicKey.findProgramAddressSync(
      [
        Buffer.from("metadata"),
        TOKEN_METADATA_PROGRAM_ID.toBuffer(),
        mint.publicKey.toBuffer(),
      ],
      TOKEN_METADATA_PROGRAM_ID
    );

    const metadataPDA = getMetadataPDA[0];

    const tx = new Transaction().add(
      SystemProgram.createAccount({
        fromPubkey: wallet.publicKey!,
        newAccountPubkey: mint.publicKey,
        space: MINT_SIZE,
        lamports: await connection.getMinimumBalanceForRentExemption(MINT_SIZE),
        programId: TOKEN_PROGRAM_ID,
      }),
      createInitializeMintInstruction(
        mint.publicKey,
        Number(formData.decimals),
        wallet.publicKey!,
        null
      ),
      createAssociatedTokenAccountInstruction(
        wallet.publicKey!,
        ata,
        wallet.publicKey!,
        mint.publicKey
      ),
      createMintToCheckedInstruction(
        mint.publicKey,
        ata,
        wallet.publicKey!,
        Number(formData.supply) * 10 ** Number(formData.decimals),
        Number(formData.decimals)
      ),
      // Instruction metadata here
      createCreateMetadataAccountV3Instruction(
        {
          metadata: metadataPDA,
          mint: mint.publicKey,
          mintAuthority: wallet.publicKey!,
          payer: wallet.publicKey!,
          updateAuthority: wallet.publicKey!,
        },
        {
          createMetadataAccountArgsV3: {
            collectionDetails: null,
            data: metadata,
            isMutable: !immutable,
          },
        }
      )
    );

    const blockhash = (await connection.getLatestBlockhash()).blockhash;
    tx.recentBlockhash = blockhash;
    tx.feePayer = wallet.publicKey!;
    tx.sign(mint);

    const signedTx = await wallet.signAndSendTransaction(tx);

If you want malicious warning to be immediately resolved, just use signAndSendTransaction, I think it's works properly. Good luck!

1
  • Thanks dude! Yeah it's works 100% Please rate his answer's guys, it's very helpful!
    – Yuhuuu
    Commented May 30 at 9:49

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.