import { PublicKey, TransactionInstruction, SystemProgram, SYSVAR_RENT_PUBKEY, } from "@solana/web3.js"; import type { PitProgram } from "../program.js"; import { TOKEN_2022_PROGRAM_ID } from "../constants.js"; import { getMarketAddress, getMintAddress } from "../accounts/pda.js"; /** * Parameters for creating a create_market_mints instruction */ interface CreateMarketMintsParams { /** Week number for this market (must be valid u32) */ weekNumber: number; /** Operator's public key (must match hardcoded OPERATOR) */ operator: PublicKey; } /** * Create a create_market_mints instruction * * This instruction creates all 12 mint PDAs for a market. It must be called * BEFORE initialize_market for a given week number. * * The mints are PDA-based, derived from: * seeds = ["mint", week_number (4 bytes LE), pool_type (1 byte), strike_index (1 byte), is_hit (1 byte)] * * After calling this instruction, call initialize_market with the same week_number * and pass the mints as remaining_accounts (use getAllMintAddresses() to get them). * * @param program - Anchor Program instance * @param params - Create market mints parameters * @returns Promise resolving to TransactionInstruction * * @example * ```typescript * import { createProgram, createCreateMarketMintsInstruction, getAllMintAddresses } from "@pit-protocol/sdk"; * * const program = createProgram(provider); * * // Step 1: Create mints * const createMintsIx = await createCreateMarketMintsInstruction(program, { * weekNumber: 2604, * operator: operatorKeypair.publicKey, * }); * const tx1 = new Transaction().add(createMintsIx); * await sendAndConfirmTransaction(connection, tx1, [operatorKeypair]); * * // Step 2: Initialize market (pass mints as remaining_accounts) * const mintPdas = getAllMintAddresses(2604); * // ... create initialize_market instruction with mintPdas * ``` */ export async function createCreateMarketMintsInstruction( program: PitProgram, params: CreateMarketMintsParams ): Promise { const { weekNumber, operator } = params; // Derive all the required PDAs const marketAuthority = getMarketAddress(weekNumber); // Derive all 12 mint PDAs // High pool HIT mints (strikes 0, 1, 2) const highHitMint0 = getMintAddress(weekNumber, 0, 0, true); const highHitMint1 = getMintAddress(weekNumber, 0, 1, true); const highHitMint2 = getMintAddress(weekNumber, 0, 2, true); // High pool MISS mints (strikes 0, 1, 2) const highMissMint0 = getMintAddress(weekNumber, 0, 0, false); const highMissMint1 = getMintAddress(weekNumber, 0, 1, false); const highMissMint2 = getMintAddress(weekNumber, 0, 2, false); // Low pool HIT mints (strikes 0, 1, 2) const lowHitMint0 = getMintAddress(weekNumber, 1, 0, true); const lowHitMint1 = getMintAddress(weekNumber, 1, 1, true); const lowHitMint2 = getMintAddress(weekNumber, 1, 2, true); // Low pool MISS mints (strikes 0, 1, 2) const lowMissMint0 = getMintAddress(weekNumber, 1, 0, false); const lowMissMint1 = getMintAddress(weekNumber, 1, 1, false); const lowMissMint2 = getMintAddress(weekNumber, 1, 2, false); return await program.methods .createMarketMints(weekNumber) .accountsPartial({ operator, marketAuthority, highHitMint0, highHitMint1, highHitMint2, highMissMint0, highMissMint1, highMissMint2, lowHitMint0, lowHitMint1, lowHitMint2, lowMissMint0, lowMissMint1, lowMissMint2, systemProgram: SystemProgram.programId, token2022Program: TOKEN_2022_PROGRAM_ID, rent: SYSVAR_RENT_PUBKEY, }) .instruction(); }