Getting errors when trying to mint NFT through command line

Issue with NFT Minting Script

I have successfully deployed my smart contract and linked it to my Alchemy provider. However, when I try to run my minting script, I keep getting errors.

Here’s my minting script:

require('dotenv').config();
const ALCHEMY_URL = process.env.ALCHEMY_URL;
const WALLET_ADDRESS = process.env.WALLET_ADDRESS;
const WALLET_PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY;

const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
const web3Instance = createAlchemyWeb3(ALCHEMY_URL);

const contractABI = require("../artifacts/contracts/TokenNFT.sol/TokenNFT.json");
const deployedAddress = "Contract_Address_Here";
const tokenContract = new web3Instance.eth.Contract(contractABI.abi, deployedAddress);

async function createNFT(metadataURI) {
  const txCount = await web3Instance.eth.getTransactionCount(WALLET_ADDRESS, 'latest');

  const transaction = {
    'from': WALLET_ADDRESS,
    'to': deployedAddress,
    'nonce': txCount,
    'gas': 500000,
    'data': tokenContract.methods.createNFT(WALLET_ADDRESS, metadataURI).encodeABI()
  };

  const signedTransaction = web3Instance.eth.accounts.signTransaction(transaction, WALLET_PRIVATE_KEY);
  signedTransaction.then((result) => {
    web3Instance.eth.sendSignedTransaction(result.rawTransaction, function(error, txHash) {
      if (!error) {
        console.log("Transaction hash: ", txHash, "\nCheck your transaction status on Alchemy!"); 
      } else {
        console.log("Error occurred:", error)
      }
    });
  }).catch((error) => {
    console.log("Transaction failed:", error);
  });
}

createNFT("https://gateway.pinata.cloud/ipfs/My_Metadata_Hash");

My environment variables:

ALCHEMY_URL = "https://eth-rinkeby.alchemyapi.io/v2/API_KEY"
WALLET_PRIVATE_KEY = "PRIVATE_KEY_HERE"
WALLET_ADDRESS = "PUBLIC_ADDRESS_HERE"

When I execute the script, I get some errors but I can’t figure out what’s causing them. Has anyone encountered similar issues with NFT minting scripts? What could be going wrong here?

Oh interesting! I’m curious about a few things - what error messages do you see when running the script? That’d help figure out what’s breaking.

Also, double-check your contract address is right and that createNFT function exists with those exact parameters. Sometimes the function name or signature isn’t what we expect.

One thing caught my eye - is your metadata URI accessible? Try visiting that Pinata link directly in your browser to make sure it returns valid JSON.

And did you verify your private key format? People sometimes include or exclude the ‘0x’ prefix, which causes signing issues.

What testnet are you using btw? Like creativePainter mentioned, some have been having issues lately.

Had the same issue recently - it was gas estimation. That hardcoded 500000 limit is probably either too low or way too high for your contract. Use web3Instance.eth.estimateGas() to get the actual gas needed.

Your signTransaction call isn’t being awaited properly either. The promise handling looks off. I’d wrap everything in try-catch and use async/await consistently.

Also check if your contract actually deployed successfully on the target network. Sometimes it looks like it worked but the contract didn’t initialize properly.

hey, ur code seems alright, but rinkeby is basically dead now. switch to goerli or sepolia and see if that helps! also, check if ur wallet’s got enough eth for gas on those testnets.