Skip to main content
Became Hot Network Question
added 3034 characters in body
Source Link

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

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
Source Link

Unable to upload using metaplex uploadMetadata() or upload()

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)