Error when attempting to mint SPL token on Solana

402 views Asked by At

Getting the following error every time script is run:

TransactionExpiredBlockheightExceededError: Signature [SIGNATURE] has expired: block height exceeded.

Script:

import { percentAmount, generateSigner, signerIdentity, createSignerFromKeypair } from '@metaplex-foundation/umi';
import { TokenStandard, createAndMint } from '@metaplex-foundation/mpl-token-metadata';
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { mplCandyMachine } from "@metaplex-foundation/mpl-candy-machine";
import  "@solana/web3.js";
import secret from './guideSecret.json';

const umi = createUmi('[QuickNode Endpoint URL]'); //Replace with your QuickNode RPC Endpoint

const userWallet = umi.eddsa.createKeypairFromSecretKey(new Uint8Array(secret));
const userWalletSigner = createSignerFromKeypair(umi, userWallet);

const metadata = {
    name: "Token Name",
    symbol: "TOKEN",
    uri: "[URI]",
};

const mint = generateSigner(umi);
umi.use(signerIdentity(userWalletSigner));
umi.use(mplCandyMachine());

void async function() {
  try {
    await createAndMint(umi, {
      mint,
      authority: umi.identity,
      name: metadata.name,
      symbol: metadata.symbol,
      uri: metadata.uri,
      sellerFeeBasisPoints: percentAmount(0),
      decimals: 8,
      amount: 1000000,
      tokenOwner: userWallet.publicKey,
      tokenStandard: TokenStandard.Fungible,
      }).sendAndConfirm(umi);
      console.log("Successfully minted 1 million tokens (", mint.publicKey, ")");
  } catch (error) {
    console.error(error);
  }
}();

Unfortunately, the error is not verbose and I am stumped as to what is missing/incorrect. As far as I can tell, this is using the latest Solana libraries to create and mint the tokens.

1

There are 1 answers

6
user130198 On

You need to sign the transaction before sending it. Try out following code:

const createAndMintBuild = createAndMint(umi, {
    mint,
    authority: umi.identity,
    name: metadata.name,
    symbol: metadata.symbol,
    uri: metadata.uri,
    sellerFeeBasisPoints: percentAmount(0),
    decimals: 8,
    amount: 1000000000_00000000,
    tokenOwner: userWallet.publicKey,
    tokenStandard: TokenStandard.Fungible,
    });
  
  let builder = transactionBuilder()
  .add(createAndMintBuild)
  ;

  builder.setFeePayer(userWalletSigner);
  builder.buildAndSign(umi);
  
  const confirmResult = await builder.sendAndConfirm(umi).then(() => {
    console.log("Successfully minted 1 billion tokens (", mint.publicKey, ")"); });

}