0

I'm trying to create a new SPL token using @solana/web3.js and @solana/spl-token at the end of the process I got this error Error occurred: Error: !signature. Even though I don't know if the weather code is correct or not.

export const getProvider = () => {
  if (typeof window?.solana !== "undefined" && window?.solana?.isPhantom) {
    return window.solana;
  } else {
    throw new Error("Phantom wallet is not available");
  }
};

export const createToken = async () => {
  try {
    console.log("Initializing connection to Solana network...");
    const connectionWallet = new web3.Connection("https://api.devnet.solana.com","confirmed");
    console.log("Connection to Solana network established.");

    console.log("Connecting to Phantom wallet...");
    const account = await window.solana.connect();
    const mintRequester = account?.publicKey;
    console.log("Connected to Phantom wallet.");

    // Connect to the wallet
    console.log("Fetching provider...");
    const provider = await getProvider();
    console.log("Provider fetched successfully.");

    // Get the payer's public key from the wallet
    const payerPublicKey = mintRequester;

    // Generate a new mint account
    console.log("Generating new mint account...");
    const mint = await web3.Keypair.generate();
    console.log("New mint account generated:", mint.publicKey.toString());

    // Construct the token create transaction
    console.log("Constructing token create transaction...");
    const instructions = [
      await splToken.createInitializeMintInstruction(
        mint.publicKey,
        0,
        payerPublicKey,
        null,
        splToken.TOKEN_PROGRAM_ID
      ),
    ];

    // Sign the transaction
    console.log("Signing the transaction...");
    const transaction = await new web3.Transaction().add(...instructions);
    const latestBlockhash = await connectionWallet.getLatestBlockhash();
    transaction.feePayer = payerPublicKey;
    transaction.recentBlockhash = latestBlockhash.blockhash;
    
    console.log("🚀 ~ createToken ~ connectionWallet:", connectionWallet)
    console.log("🚀 ~ createToken ~ connectionWallet:", provider.signTransaction.toString())
    const signedTransaction = await provider.signTransaction(transaction, [
      mint,
    ]);
    console.log("Transaction signed successfully:", signedTransaction);

    // Send the transaction to the network
    console.log("Sending the transaction to the network...");
    const signature = await connectionWallet.sendTransaction(
      signedTransaction,
      [mint]
    );
    console.log("Transaction sent successfully. Signature:", signature);

    // Return the new mint public key
    console.log("New mint public key:", mint.publicKey.toString());
    return mint.publicKey;
  } catch (error) {
    console.error("Error occurred:", error);
  }
};

1 Answer 1

0

The error is too vague to debug, but this likely won't work because the mint account isn't getting created before initialization. Try adding one more instruction before createInitializeMintInstruction:

import { getMinimumBalanceForRentExemptMint, MINT_SIZE, TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { Transaction, SystemProgram } from '@solana/web3.js';

    // all other setup code as before

    const mint = Keypair.generate();

    // Get required rent exemption
    const lamports = await getMinimumBalanceForRentExemptMint(connection);

    // Create transaction
    const transaction = new Transaction().add(
        SystemProgram.createAccount({
            fromPubkey: payer.publicKey,
            newAccountPubkey: mint.publicKey,
            space: MINT_SIZE,
            lamports,
            TOKEN_PROGRAM_ID,
        }),
        createInitializeMintInstruction(mint.publicKey, 0, payer, null, TOKEN_PROGRAM_ID)
    );
    // and all other code as before

Adapted from a larger example at https://github.com/solana-labs/solana-program-library/blob/master/token/js/examples/createMintAndTransferTokens.ts and mint creation at https://github.com/solana-labs/solana-program-library/blob/master/token/js/src/actions/createMint.ts

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.