import * as anchor from "@project-serum/anchor"; import { createAssociatedTokenAccount, createMint, mintToChecked, } from "@solana/spl-token"; import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID, createCreateMetadataAccountV2Instruction, createCreateMasterEditionV3Instruction, } from "@metaplex-foundation/mpl-token-metadata"; export const createKeypair = async (provider: anchor.Provider) => { const keypair = new anchor.web3.Keypair(); const txn = await provider.connection.requestAirdrop( keypair.publicKey, 10 * anchor.web3.LAMPORTS_PER_SOL ); await provider.connection.confirmTransaction(txn); return keypair; }; const getTokenMetadata = async (tokenMint: anchor.web3.PublicKey) => { const [tokenMetadataAddress, bump] = await anchor.web3.PublicKey.findProgramAddress( [ Buffer.from("metadata"), TOKEN_METADATA_PROGRAM_ID.toBuffer(), tokenMint.toBuffer(), ], TOKEN_METADATA_PROGRAM_ID ); return tokenMetadataAddress; }; const getTokenEdition = async (tokenMint: anchor.web3.PublicKey) => { const [tokenMetadataAddress, bump] = await anchor.web3.PublicKey.findProgramAddress( [ Buffer.from("metadata"), TOKEN_METADATA_PROGRAM_ID.toBuffer(), tokenMint.toBuffer(), Buffer.from("edition"), ], TOKEN_METADATA_PROGRAM_ID ); return tokenMetadataAddress; }; export const mintNft = async ( provider: anchor.Provider, destination: anchor.web3.PublicKey ) => { const creator = await createKeypair(provider); const mint = await createMint( provider.connection, creator, creator.publicKey, null, 0 ); const tokenAccount = await createAssociatedTokenAccount( provider.connection, creator, mint, destination ); await mintToChecked( provider.connection, creator, mint, tokenAccount, creator.publicKey, 1, 0 ); const transaction = new anchor.web3.Transaction(); // Set Metadata const metadata = await getTokenMetadata(mint); transaction.add( createCreateMetadataAccountV2Instruction( { metadata, mint, mintAuthority: creator.publicKey, updateAuthority: creator.publicKey, payer: creator.publicKey, }, { createMetadataAccountArgsV2: { isMutable: false, data: { name: "Pretty Cool NFT", symbol: "PCN", sellerFeeBasisPoints: 10, uri: "https://pretty-cool-nft.xyz/metadata", creators: [ { address: creator.publicKey, share: 100, verified: true, }, ], collection: null, uses: null, }, }, } ) ); // Create master edition const edition = await getTokenEdition(mint); transaction.add( createCreateMasterEditionV3Instruction( { edition, mint, updateAuthority: creator.publicKey, mintAuthority: creator.publicKey, payer: creator.publicKey, metadata, }, { createMasterEditionArgs: { maxSupply: 0 } } ) ); // @ts-ignore await provider.sendAndConfirm(transaction, [creator]); return mint; };