0

i am getting this error Error in create mint Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x1

import { useConnection, useWallet } from "@solana/wallet-adapter-react";
import * as token from "@solana/spl-token";
import * as web3 from "@solana/web3.js";
import { useState } from "react";

const CreateMint = () => {
  const { connection } = useConnection();
  const { publicKey, sendTransaction } = useWallet();
  const [minttoken, setMintToken] = useState<web3.PublicKey>();

  async function createMint() {
    if (!publicKey) return;
    const payer = web3.Keypair.generate();
    console.log("Payer:", payer.publicKey.toString());
    const lamports = await token.getMinimumBalanceForRentExemptMint(connection);
    const transaction = new web3.Transaction();
    const instruction = web3.SystemProgram.transfer({
      fromPubkey: publicKey,
      toPubkey: payer.publicKey,
      lamports,
    });
    transaction.add(instruction);
    const sign = await sendTransaction(transaction, connection);
    console.log("sol transfered from your wallet to payer account", sign);

    
       try {
        const Token = await token.createMint( //getting error in this line
            connection,
            payer,
            payer.publicKey,
            payer.publicKey,
            2,
          );
          console.log( `Token Mint: https://explorer.solana.com/address/${Token}?cluster=devnet`)
          setMintToken(Token);
       } catch (error) {
        console.log("Error in create mint", error);
       }
    }

  return (
    <div className=" w-fit mx-auto mt-4">
      {publicKey ? (
        <button className="bg-white text-black px-4 py-2 rounded-xl font-bold"
        onClick={createMint}>
          Create Mint
        </button>
      ) : null}
      <div>
        {minttoken ? (
          <div>
            <div className="text-xl">Mint Token Address:</div>
            <a
              className="text-blue-500"
              href={`https://explorer.solana.com/address/${minttoken}?cluster=devnet`}
              target="_blank"
            >
              {minttoken?.toString()}
            </a>
          </div>
        ) : null}
      </div>
    </div>
  );
};

export default CreateMint;

1 Answer 1

0

found my mistake i was only crediting the minimum amount needed to create token but forgot about the gas fee so added little more lamports to the amount to be credited and it worked

import { useConnection, useWallet } from "@solana/wallet-adapter-react";
import * as token from "@solana/spl-token";
import * as web3 from "@solana/web3.js";
import { useState } from "react";

const CreateMint = () => {
  const { connection } = useConnection();
  const { publicKey, sendTransaction } = useWallet();
  const [minttoken, setMintToken] = useState<web3.PublicKey>();

  async function createMint() {
    if (!publicKey) return;
    const payer = web3.Keypair.generate();
    console.log("Payer:", payer.publicKey.toString());
    const lamports = await token.getMinimumBalanceForRentExemptMint(connection)+10000;//here added some extra for gas fee
    const transaction = new web3.Transaction();
    const instruction = web3.SystemProgram.transfer({
      fromPubkey: publicKey,
      toPubkey: payer.publicKey,
      lamports,
    });
    transaction.add(instruction);
    const sign = await sendTransaction(transaction, connection);
    console.log("sol transfered from your wallet to payer account", sign);

    setTimeout(async()=>{ //added set timeout to just wait for the above transaction to settle
       try {
        const Token = await token.createMint(
            connection,
            payer,
            payer.publicKey,
            payer.publicKey,
            2,
          );
          console.log( `Token Mint: https://explorer.solana.com/address/${Token}?cluster=devnet`)
          setMintToken(Token);
       } catch (error) {
        console.log("Error in create mint", error);
       }
    }, 1000);
  }

  return (
    <div className=" w-fit mx-auto mt-4">
      {publicKey ? (
        <button className="bg-white text-black px-4 py-2 rounded-xl font-bold"
        onClick={createMint}>
          Create Mint
        </button>
      ) : null}
      <div>
        {minttoken ? (
          <div>
            <div className="text-xl">Mint Token Address:</div>
            <a
              className="text-blue-500"
              href={`https://explorer.solana.com/address/${minttoken}?cluster=devnet`}
              target="_blank"
            >
              {minttoken?.toString()}
            </a>
          </div>
        ) : null}
      </div>
    </div>
  );
};

export default CreateMint;

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.