import { TOKEN_PROGRAM_ID, MintLayout, Token, ASSOCIATED_TOKEN_PROGRAM_ID, } from '@solana/spl-token'; import { Connection, PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction, } from '@solana/web3.js'; import { SOLCIAL_SEED, USER_TOKEN_DECIMAL } from '../config'; import { deriveMetadataAccount, TOKEN_METADA_PROGRAM_ID, encodeTokenMeta, } from '../user-info/metadata'; const { createInitMintInstruction, getMinBalanceRentForExemptMint, createAssociatedTokenAccountInstruction, createMintToInstruction, createSetAuthorityInstruction, getAssociatedTokenAddress, } = Token; export async function getUserMint(user: PublicKey, tokenProgramId = TOKEN_PROGRAM_ID) { return await PublicKey.createWithSeed(user, SOLCIAL_SEED, tokenProgramId); } export async function mintUserTokenInstruction( connection: Connection, user: PublicKey, metaEnv = 'app', name: string | null = null, ticker: string | null = null, tokenProgramId: PublicKey = TOKEN_PROGRAM_ID, payer: PublicKey | null = null, ): Promise { const mintAccountAddress = await getUserMint(user, tokenProgramId); const balanceNeeded = await getMinBalanceRentForExemptMint(connection); const associatedTokenAccountAddress = await getAssociatedTokenAddress( ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, mintAccountAddress, user, ); if (payer == null) { payer = user; } const transaction = new Transaction(); transaction.add( // create user mint account SystemProgram.createAccountWithSeed({ basePubkey: user, fromPubkey: payer, lamports: balanceNeeded, newAccountPubkey: mintAccountAddress, programId: TOKEN_PROGRAM_ID, seed: SOLCIAL_SEED, space: MintLayout.span, }), createInitMintInstruction( TOKEN_PROGRAM_ID, mintAccountAddress, USER_TOKEN_DECIMAL, payer, null, ), createAssociatedTokenAccountInstruction( ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, mintAccountAddress, associatedTokenAccountAddress, user, payer, ), createMintToInstruction( TOKEN_PROGRAM_ID, mintAccountAddress, associatedTokenAccountAddress, payer, [], Number(BigInt(1_000_000) * BigInt(1e9)), ), await createMetadataInstruction(user, mintAccountAddress, user, metaEnv, name, ticker), createSetAuthorityInstruction( TOKEN_PROGRAM_ID, mintAccountAddress, null, 'MintTokens', payer, [], ), ); return transaction; } export async function createMetadataInstruction( user: PublicKey, mint: PublicKey, payer: PublicKey, metaEnv: string, name: string | null = null, ticker: string | null = null, ) { const metadata = await deriveMetadataAccount(mint); const mintAuthority = user; const updateAuthority = user; name = name || `SOLCIAL-${user.toBase58().slice(0, 5)}`; const symbol = ticker || `SLCL${user.toBase58().slice(0, 3)}`; const uri = `https://${metaEnv}.solcial.io/api/tokens/metadata/${user.toBase58()}`; 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; }