import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, MintLayout, Token, } from '@solana/spl-token'; import { PublicKey, Connection, Transaction, SystemProgram, SYSVAR_RENT_PUBKEY, TransactionInstruction, Keypair, } from '@solana/web3.js'; import { deriveMetadataAccount, encodeTokenMeta, TOKEN_METADA_PROGRAM_ID, } from '../user-info/metadata'; const { createInitMintInstruction, getMinBalanceRentForExemptMint, createAssociatedTokenAccountInstruction, createMintToInstruction, createSetAuthorityInstruction, getAssociatedTokenAddress, } = Token; export async function getMintNftAccount(user: PublicKey, symbol: string) { return await PublicKey.createWithSeed(user, symbol, TOKEN_PROGRAM_ID); } export async function mintUserNFTInstruction( connection: Connection, user: PublicKey, name: string, symbol: string, uri: string, ): Promise { const payer = user; const mintAccountAddress = await getMintNftAccount(user, symbol); const balanceNeeded = await getMinBalanceRentForExemptMint(connection); const associatedTokenAccountAddress = await getAssociatedTokenAddress( ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, mintAccountAddress, user, ); const transaction = new Transaction(); transaction.add( SystemProgram.createAccountWithSeed({ basePubkey: user, fromPubkey: payer, lamports: balanceNeeded, newAccountPubkey: mintAccountAddress, programId: TOKEN_PROGRAM_ID, seed: symbol, space: MintLayout.span, }), createInitMintInstruction(TOKEN_PROGRAM_ID, mintAccountAddress, 0, payer, null), createAssociatedTokenAccountInstruction( ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, mintAccountAddress, associatedTokenAccountAddress, user, payer, ), createMintToInstruction( TOKEN_PROGRAM_ID, mintAccountAddress, associatedTokenAccountAddress, payer, [], Number(1), ), await createNftMetadataInstruction(user, mintAccountAddress, user, name, symbol, uri), createSetAuthorityInstruction( TOKEN_PROGRAM_ID, mintAccountAddress, null, 'MintTokens', payer, [], ), ); return transaction; } export async function createNftMetadataInstruction( user: PublicKey, mint: PublicKey, payer: PublicKey, name: string, symbol: string, uri: string, ) { const metadata = await deriveMetadataAccount(mint); const mintAuthority = user; const updateAuthority = user; const keys = [ { pubkey: metadata, isWritable: true, isSigner: false }, { pubkey: mint, isWritable: false, isSigner: false }, { pubkey: mintAuthority, isWritable: false, isSigner: true }, { pubkey: payer, isWritable: true, isSigner: true }, { pubkey: updateAuthority, isWritable: false, isSigner: false }, { pubkey: SystemProgram.programId, isWritable: false, isSigner: false }, { pubkey: SYSVAR_RENT_PUBKEY, isWritable: false, isSigner: false }, ]; const instr = new TransactionInstruction({ keys, programId: TOKEN_METADA_PROGRAM_ID, data: Buffer.from(encodeTokenMeta(name, symbol, uri)), }); return instr; } export async function buildMintMetaDataJson(name, symbol, description, imageUri) { const metadata = { name, symbol, description, image: imageUri, external_url: 'https://solcial.io/', }; return metadata.toString(); }