import { PublicKey, TransactionInstruction } from "@solana/web3.js"; import type { PitProgram } from "../program.js"; import { PoolType } from "../types.js"; import { getMarketAddress } from "../accounts/pda.js"; /** * Parameters for creating a mark_extreme instruction */ export interface MarkExtremeParams { /** Week number of the market */ weekNumber: number; /** Pool type (High or Low) */ poolType: PoolType; /** New outcome to mark (1, 2, or 3 - must be higher than current) */ newOutcome: number; /** Keeper's public key (signer) - can be anyone */ keeper: PublicKey; /** Pyth price update account (PriceUpdateV2 from pyth-solana-receiver) */ priceUpdate: PublicKey; } /** * Create a mark_extreme instruction for updating the winning outcome * * This instruction is permissionless - anyone (typically a keeper bot) can call it. * The price is verified against Pyth oracle to ensure it matches the claimed outcome. * * For High Pool: * - Outcome 1: price >= strikes[0] * - Outcome 2: price >= strikes[1] * - Outcome 3: price >= strikes[2] * * For Low Pool: * - Outcome 1: price < strikes[2] * - Outcome 2: price < strikes[1] * - Outcome 3: price < strikes[0] * * @param program - Anchor Program instance * @param params - Mark extreme parameters * @returns Promise resolving to TransactionInstruction * * @example * ```typescript * import { createProgram, createMarkExtremeInstruction, PoolType } from "@pit-protocol/sdk"; * * const program = createProgram(provider); * * // 1. Get price update from Pyth * const priceUpdateData = await pythService.getPriceUpdateData(); * const priceUpdateAccount = await postPriceUpdate(connection, payer, priceUpdateData); * * // 2. Create instruction * const ix = await createMarkExtremeInstruction(program, { * weekNumber: 1, * poolType: PoolType.High, * newOutcome: 2, * keeper: keeperWallet.publicKey, * priceUpdate: priceUpdateAccount, * }); * * // 3. Send transaction * const tx = new Transaction().add(ix); * await sendAndConfirmTransaction(connection, tx, [keeperWallet]); * ``` */ export async function createMarkExtremeInstruction( program: PitProgram, params: MarkExtremeParams ): Promise { const { weekNumber, poolType, newOutcome, keeper, priceUpdate } = params; const marketState = getMarketAddress(weekNumber); // Convert PoolType to IDL format const poolTypeArg = (poolType === PoolType.High ? { high: {} } : { low: {} }) as | { high: Record } | { low: Record }; return await program.methods .markExtreme(poolTypeArg, newOutcome) .accountsPartial({ marketState, keeper, priceUpdate, }) .instruction(); }