2

I am trying to create one Js script to mint 1 NFT to test new Metaplex function createNft on: https://developers.metaplex.com/token-metadata/getting-started/js

I am testing on Devnet. I am providing a test wallet with funds in Devnet. I am providing mint.js, package.json and output. node version: v18.15.0

Thanks in advance !

mint.js

import { Keypair, SystemProgram, TransactionInstruction, Transaction, Connection, PublicKey } from '@solana/web3.js';
import {
    generateSigner,
    signerIdentity,
    percentAmount,
    publicKey
} from "@metaplex-foundation/umi";
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import {
    createNft,
    mintV1,
    createV1,
    fetchDigitalAsset,
    TokenStandard,
    mplTokenMetadata
} from '@metaplex-foundation/mpl-token-metadata'


// *** Init

// Create connection to the network
const connection = new Connection('https://api.devnet.solana.com');
const TOKEN_PROGRAM_ID = new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');

// Read the keypair from byteArray
const secret = new Uint8Array([43, 6, 89, 198, 35, 211, 28, 143, 52, 141, 173, 50, 44, 40, 73, 73, 36, 59, 18, 43, 212, 76, 220, 75, 190, 66, 226, 200, 43, 4, 21, 38, 54, 153, 59, 206, 235, 98, 154, 46, 216, 138, 172, 203, 207, 78, 213, 240, 34, 53, 153, 15, 228, 27, 101, 192, 118, 125, 5, 73, 69, 166, 92, 230]);
const keypair = Keypair.fromSecretKey(secret);

// Create a keypair using the eddsa interface from the umi context
const umi = createUmi('https://api.devnet.solana.com').use(mplTokenMetadata());
const myKeypair = umi.eddsa.createKeypairFromSecretKey(secret);
const umiSigner = generateSigner(umi, myKeypair);
umi.use(signerIdentity(umiSigner));


// *** Generate a new mint account
let mintAccount = Keypair.generate();

let createMintAccountIx = SystemProgram.createAccount({
    fromPubkey: keypair.publicKey,
    newAccountPubkey: mintAccount.publicKey,
    lamports: await connection.getMinimumBalanceForRentExemption(500),
    space: 500,
    programId: TOKEN_PROGRAM_ID,
});

// Create a new transaction and add the create mint account instruction
let transaction = new Transaction().add(createMintAccountIx);
let signature = await connection.sendTransaction(transaction, [keypair, mintAccount]);
console.log(signature)

// Confirm the minting transaction
const mintConfirmation = await connection.confirmTransaction(signature);
console.log('transaction confirmation:', mintConfirmation);

// Add a delay to ensure the ledger is updated
console.log('Waiting for the ledger to update...');
await new Promise(resolve => setTimeout(resolve, 3000));

// Keep checking the mint account balance until it is positive
const payerBalance = await connection.getBalance(keypair.publicKey);
let mintAccountBalance = 0;
while (mintAccountBalance <= 0) {
    mintAccountBalance = await connection.getBalance(mintAccount.publicKey);
    console.log('Mint account balance:', mintAccountBalance);
    console.log('Payer balance:', payerBalance);
    if (mintAccountBalance <= 0) {
        await new Promise(resolve => setTimeout(resolve, 1000));
    }
}

// Create NFT using Metaplex https://developers.metaplex.com/token-metadata/getting-started/js
await createNft(umi, {
    mint: mintAccount.publicKey,
    name: 'My NFT',
    uri: 'https://example.com/my-nft.json',
    sellerFeeBasisPoints: percentAmount(5.5),
}).sendAndConfirm(umi)

package.json

{
  "type": "module",
  "dependencies": {
    "@metaplex-foundation/mpl-token-metadata": "^3.0.0",
    "@metaplex-foundation/umi": "^0.8.9",
    "@metaplex-foundation/umi-bundle-defaults": "^0.8.9",
    "@metaplex/js": "^4.12.0",
    "@solana/spl-token": "^0.3.8",
    "@solana/web3.js": "^1.78.5"
  }
}

output after executing: node mint.js

SouSwHsg97XLkckkXdYXeKMbJKJVJij8QLyGVs1uPYszN4NH92jG8rwxLf4oScGrVhXe3x3t222LBiLFnP21Yag
transaction confirmation: { context: { slot: 244902994 }, value: { err: null } }
Waiting for the ledger to update...
Mint account balance: 4370880
Payer balance: 3628931040
/mypath/node_modules/@solana/web3.js/lib/index.cjs.js:12412
              throw new SendTransactionError('failed to send transaction: ' + res.error.message, _logs2);
                    ^

SendTransactionError: failed to send transaction: Transaction simulation failed: Attempt to debit an account but found no record of a prior credit.
    at Connection._callee89$ (/mypath/node_modules/@solana/web3.js/lib/index.cjs.js:12412:21)
    at tryCatch (/mypath/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:45:16)
    at Generator.<anonymous> (/mypath/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:133:17)
    at Generator.next (/mypath/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:74:21)
    at asyncGeneratorStep (/mypath/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/mypath/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  logs: []
}

2 Answers 2

1

The error means that you have no funds. Probably being caused by umiSigner Try airdropping some when running the script or use mykeypair as your umi Identity.

Here is a shortened script that works on my end for minting an NFT using umi

import {secret} from './setup';
import { createNft, mplTokenMetadata } from '@metaplex-foundation/mpl-token-metadata';
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';

const umi = createUmi('https://api.devnet.solana.com');

const keypair = umi.eddsa.createKeypairFromSecretKey(uintSecret);

umi.use(keypairIdentity(keypair))
    .use(mplTokenMetadata())

// ? create a collection mint
const collectionMint = generateSigner(umi);
const createCollectionTx = async () => {
    await createNft(umi, {
        mint: collectionMint,
        name: `Kobeni`,
        symbol: 'KBN',
        uri: 'https://raw.githubusercontent.com/687c/solana-nft-native-client/main/metadata.json',
        sellerFeeBasisPoints: percentAmount(5.5), // 5.5%
        isCollection: false,
    }).sendAndConfirm(umi);
}
createCollectionTx()
    .then(res => res)
    .catch(err => console.error(err))


Here is my package.json

{
  "name": "test-umi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@metaplex-foundation/mpl-candy-machine": "^6.0.0",
    "@metaplex-foundation/mpl-token-metadata": "^3.0.0",
    "@metaplex-foundation/mpl-toolbox": "^0.9.1",
    "@metaplex-foundation/umi": "^0.8.9",
    "@metaplex-foundation/umi-bundle-defaults": "^0.8.9",
    "@metaplex-foundation/umi-program-repository": "^0.8.9",
    "@metaplex-foundation/umi-signer-wallet-adapters": "^0.8.9",
    "@solana/web3.js": "^1.78.5"
  }
}
12
  • Hi Jimmi, my wallet has funds as printed: Payer balance: 3628931040 I've tried many other ideas like generate and airdrop, but got same error. I also tried your code, but can't make it work with my secret, and a generated one too. I see that you're creating it from uintSecret, but i can't see this const declared. Can you update the code with a test secret to check if works correctly in my end ? All that I tried fails with: Error: provided secretKey is invalid or bad secret key size. I can't make this work: const keypair = umi.eddsa.createKeypairFromSecretKey(uintSecret);
    – Harlock
    Sep 18, 2023 at 11:56
  • yes, your myKeypair has balance in it, but umiSigner doesn't. This is where I think your error is coming from. Can you try it with the code I've shared.
    – Jimii
    Sep 18, 2023 at 12:10
  • Hi Jimmi, thanks for your help so far ! umiSigner is using the same wallet than signer, loaded from the secret bitArray. I tried your code with my wallet(secret) and failed with errors that i added to my last comment. I did manage to generate a new one in the code, fund it, and execute your mint code, but fails the same way: Generated public key: 7dhNv3Nxh8boYEMuZmr35h1ndqsUrCqGZ4S8gbG2Qckj Airdrop successful SendTransactionError: failed to send transaction: Transaction simulation failed: Attempt to debit an account but found no record of a prior credit. Will continue testing
    – Harlock
    Sep 18, 2023 at 14:05
  • 1
    Hi Jimmi. There should be a misunderstanding here because your code is incomplete, and also throw some errors. I can't make it work: You need to add this line: const uintSecret = Uint8Array.from(secret); | And in my side I have to modify the import due to (SyntaxError: Named export 'generateSigner' not found. The requested module '@metaplex-foundation/mpl-token-metadata' is a CommonJS module, which may not support all module.exports as named exports.) Can you share the full code you're running ? Thanks mate !
    – Harlock
    Sep 19, 2023 at 5:47
  • 1
    thanks Jimmi, very appreciate your help. I have used your NFT object to complete the solution in other post. If need anything you got a friend here !
    – Harlock
    Sep 19, 2023 at 8:42
1

After doing some tests, seems that the problem was some wrong library imports in my code. Some methods are used in different metaplex libraries, with different result. I took the Jimmi NFT object and merged a final and very simple working code that can be executed right away:

import { createNft, mplTokenMetadata } from '@metaplex-foundation/mpl-token-metadata';
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { percentAmount, generateSigner, keypairIdentity } from "@metaplex-foundation/umi";


// Wallet with funds in Devnet
const secret = [6, 75, 114, 41, 237, 21, 146, 239, 34, 199, 207, 63, 78, 73, 16, 187, 113, 38, 228, 73, 244, 253, 210, 84, 197, 26, 247, 50, 181, 61, 184, 221, 208, 236, 132, 116, 6, 84, 36, 102, 26, 230, 69, 156, 226, 121, 29, 12, 240, 33, 123, 145, 229, 194, 213, 51, 46, 233, 45, 36, 106, 221, 171, 221];
const uintSecret = Uint8Array.from(secret);

// Umi
const umi = createUmi('https://api.devnet.solana.com');
const keypair = umi.eddsa.createKeypairFromSecretKey(uintSecret);
umi.use(keypairIdentity(keypair)).use(mplTokenMetadata())

// Mint
const nftMint = generateSigner(umi);
await createNft(umi, {
    mint: nftMint,
    name: `Uminft`,
    symbol: 'UMI',
    uri: 'https://raw.githubusercontent.com/687c/solana-nft-native-client/main/metadata.json',
    sellerFeeBasisPoints: percentAmount(5),
    isCollection: false,
}).sendAndConfirm(umi);

// Show token address
console.log(nftMint.publicKey)

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.