1

When using either

metaplex.storage().upload(metaplexFile);
or
metaplex.nfts().uploadMetadata({})

I get an error

Error minting NFT: Error: failed to get recent blockhash: SolanaJSONRPCError: failed to get recent blockhash: Method not found
    at Connection.getRecentBlockhash (/home/****/Desktop/programs/addNFT/node_modules/@solana/web3.js/src/connection.ts:4680:13)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

this is my ts file

import { Connection, Keypair, PublicKey, clusterApiUrl } from "@solana/web3.js";
import { Metaplex, keypairIdentity, irysStorage, toMetaplexFile } from "@metaplex-foundation/js";
import fs from 'fs';
import bs58 from 'bs58';
import * as dotenv from 'dotenv';

dotenv.config();
const GAME_WALLET_PRIVATE_KEY = process.env.GAME_WALLET_PRIVATE_KEY;
const NETWORK = process.env.NETWORK

export async function mintNFTToPublicKey(newOwnerPublicKey: string, nftImagePath: string, nftName: string, nftDescription: string): Promise<boolean> {
  if(!GAME_WALLET_PRIVATE_KEY || !NETWORK) {
    console.log(GAME_WALLET_PRIVATE_KEY);
    console.log(NETWORK);
    console.log("Error in .env");
    return false;
  }
  const payerKeypair = Keypair.fromSecretKey(bs58.decode(GAME_WALLET_PRIVATE_KEY));
  const connection = new Connection(NETWORK);

  const metaplex = Metaplex.make(connection)
    .use(keypairIdentity(payerKeypair))
    .use(irysStorage({
      address: 'https://devnet.bundlr.network',
      providerUrl: 'https://api.devnet.solana.com',
      timeout: 60000,
    }));

  const imageBuffer = fs.readFileSync(nftImagePath);

  const fileName = nftName;
  const metaplexFile = toMetaplexFile(imageBuffer, fileName);

  try {
    console.log('Uploading image...');
    console.log("Error after this!!")
    const imageUri = await metaplex.storage().upload(metaplexFile);
    console.log("dfdf")
    console.log('Image uploaded successfully:', imageUri);

    console.log('Creating metadata...');
    const { uri: metadataUri } = await metaplex.nfts().uploadMetadata({
      name: nftName,
      description: nftDescription,
      image: imageUri,
      attributes: [
        { trait_type: "Created By", value: "NFTSS" }
      ],
      properties: {
        files: [{ uri: imageUri, type: "image/png" }]
      }
    });
    console.log('Metadata created successfully:', metadataUri);

    const newOwnerAddress = new PublicKey(newOwnerPublicKey);

    console.log('Minting NFT...');
    const { nft } = await metaplex.nfts().create({
      uri: metadataUri,
      name: nftName,
      sellerFeeBasisPoints: 500,
      symbol: "SLBNFT",
      tokenOwner: newOwnerAddress,
      creators: [{ address: payerKeypair.publicKey, share: 100 }],
      isMutable: true
    });

    console.log(`NFT minted successfully!`);
    console.log(`Owner: ${newOwnerPublicKey}`);
    console.log(`NFT Address: ${nft.address.toString()}`);
    console.log(`Metadata Address: ${nft.metadataAddress.toString()}`);
    return true;
  } catch (error) {
    console.error('Error minting NFT:', error);
    return false;
  }
}
enter code here
1
  • Were you able to solve this? I am getting the same error too.
    – Dwarum
    Commented Oct 11 at 9:54

1 Answer 1

2

You are using an old deprecated metaplex-foundation/js package. It may be that the package is calling a removed RPC method(a bunch of methods were removed in agave .2)

try using mpl-metadata with Umi

To upload the image

let filePath = "random-image.png";

const buffer = await fs.readFile(filePath);
let file = createGenericFile(buffer, filePath, {
  // chose the correct file MIME type https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
  contentType: "image/jpeg",
});
const [image] = await umi.uploader.upload([file]);

To upload the off-chain metadata JSON

const uri = await umi.uploader.uploadJson({
  name,
  description,
  image,
});

You can take a look at the NFTs with Metaplex lesson which goes into detail on how to do this:

https://github.com/solana-foundation/developer-content/blob/main/content/courses/tokens-and-nfts/nfts-with-metaplex.md

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.