Getting errors when trying to mint NFT through command line

My Setup

I have successfully deployed my smart contract and linked it with my Alchemy provider. Everything seems to be configured properly but I’m running into issues when executing the minting process.

Code Files

create-nft.js

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 nftArtifact = require("../artifacts/contracts/TokenNFT.sol/TokenNFT.json");
const deployedAddress = "Contract_Address_Here";
const tokenContract = new web3Instance.eth.Contract(nftArtifact.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");

Environment variables (.env)

ALCHEMY_URL = "https://eth-rinkeby.alchemyapi.io/v2/YOUR_KEY"
WALLET_PRIVATE_KEY = "YOUR_PRIVATE_KEY"
WALLET_ADDRESS = "YOUR_WALLET_ADDRESS"

The Problem

When I run the minting script, I encounter errors in the terminal. The contract deployment worked fine, but the actual NFT creation process fails. Has anyone experienced similar issues with NFT minting? What could be causing these errors during execution?

Check your wallet’s got enough ETH for gas - that’s usually what breaks it. Also make sure you swapped out that “Contract_Address_Here” placeholder with the real address. Been there lol. Try throwing an await before signTransaction too, helps with the async stuff.

I ran into the same problems when I started minting NFTs via command line. Your code’s using the old Alchemy Web3 library - that’s been deprecated. Switch to the newer Alchemy SDK or just use Web3.js with ethers.js instead. You’ll get better compatibility. That hardcoded 500000 gas limit might be your problem too. It’s not enough for all networks or complex contracts. Use web3Instance.eth.estimateGas() to get the actual gas needed before sending the transaction. Double-check your contract address and make sure the createNFT function matches what you’re passing it. I’ve seen scripts fail because the function name or parameters don’t line up with what’s in the contract. Pull up your contract’s ABI and verify everything matches.

Hmm, interesting - what exact error messages are you seeing? That’d help narrow this down a lot.

Also noticed you’re using Rinkeby in your env file. Just wanted to check - is Rinkeby still working for you? It got deprecated a while back and most people moved to Goerli or Sepolia for testing. Might be worth trying a different testnet.

One thing that caught my eye - you’re missing the gasPrice field in your transaction structure. This can cause issues depending on the network. Nodes sometimes reject transactions without proper gas pricing.

I’m curious about your TokenNFT contract too - does it actually have a createNFT function that takes those exact parameters? Sometimes tutorial function names don’t match what people actually deploy.

Could you share the exact error you’re getting? Is it failing during signing, sending, or execution on chain?