Few things to check (Usually better to turn off preflight check to see the actual error from the block explorer)
- Check the
symbol
isn't too long
- Check the transaction is not too large (long URLs cause this)
- Ensure MetadataPDA is correct
- Ensure you set
recentBlockhash
& payer
params on the transaction
Data structure all looks correct from the code you have posted
Here is a full example of creating a NFT:
import {web3} from "@project-serum/anchor";
import {createCreateMasterEditionV3Instruction, createCreateMetadataAccountV3Instruction, PROGRAM_ID} from "@metaplex-foundation/mpl-token-metadata";
import {
createAssociatedTokenAccountInstruction,
createInitializeMintInstruction,
createMintToInstruction,
getAssociatedTokenAddressSync,
MINT_SIZE,
TOKEN_PROGRAM_ID
} from "@solana/spl-token";
async function getCreateNFTTransaction(owner: web3.PublicKey, connection: web3.Connection): web3.Transaction {
const nftMint = web3.Keypair.generate()
const tokenMint = nftMint.publicKey
const [metadataPDA] = web3.PublicKey.findProgramAddressSync([Buffer.from("metadata"), PROGRAM_ID.toBuffer(), tokenMint.toBuffer()], PROGRAM_ID)
const mintLamports = await connection.getMinimumBalanceForRentExemption(MINT_SIZE);
const ata = getAssociatedTokenAddressSync(tokenMint, owner, false, TOKEN_PROGRAM_ID)
const [nftMasterEdition] = web3.PublicKey.findProgramAddressSync([Buffer.from("metadata"), PROGRAM_ID.toBuffer(), nftMint.publicKey.toBuffer(), Buffer.from("edition")],
PROGRAM_ID)
const ON_CHAIN_METADATA = {
name: "{NAME}",
symbol: "{SYMBOL}",
uri: "{URI}",
sellerFeeBasisPoints: 0,
uses: null,
creators: [],
collection: null,
};
const txn = new web3.Transaction().add(
web3.SystemProgram.createAccount({
fromPubkey: owner,
newAccountPubkey: tokenMint,
space: MINT_SIZE,
lamports: mintLamports,
programId: TOKEN_PROGRAM_ID,
}),
);
txn.add(createInitializeMintInstruction(tokenMint, 0, owner, owner, TOKEN_PROGRAM_ID),
createCreateMetadataAccountV3Instruction({
metadata: metadataPDA,
mint: tokenMint,
mintAuthority: owner,
payer: owner,
updateAuthority: owner,
}, {
createMetadataAccountArgsV3:
{
data: ON_CHAIN_METADATA,
isMutable: false,
collectionDetails: null
}
}, PROGRAM_ID),
createAssociatedTokenAccountInstruction(owner, ata, owner, tokenMint, TOKEN_PROGRAM_ID),
)
txn.add(createMintToInstruction(tokenMint, ata, owner, 1n, [], TOKEN_PROGRAM_ID))
//Used to create a master collection
// txn.add(createSetAndVerifyCollectionInstruction({
// metadata: metadataPDA,
// collectionAuthority: owner,
// payer: owner,
// updateAuthority: owner,
// collectionMint: collectionMint,
// collection: collectionMetadata,
// collectionMasterEditionAccount: collectionMasterEdition,
// collectionAuthorityRecord: collectionAuthority,
// }))
txn.add(createCreateMasterEditionV3Instruction({
edition: nftMasterEdition,
mint: nftMint.publicKey,
updateAuthority: owner,
mintAuthority: owner,
payer: owner,
metadata: metadataPDA,
tokenProgram: TOKEN_PROGRAM_ID,
}, {createMasterEditionArgs: {maxSupply: 0}}))
const bhash = await connection.getLatestBlockhash("confirmed")
txn.feePayer = owner
txn.recentBlockhash = bhash.blockhash
return txn
}