import { PublicKey, TransactionInstruction } from "@solana/web3.js"; import type { PitProgram } from "../program.js"; import { getMarketAddress, getRegistryAddress } from "../accounts/pda.js"; /** * Parameters for creating a settle_market instruction */ export interface SettleMarketParams { /** Week number of the market */ weekNumber: number; /** Settler's public key (signer) - can be anyone */ settler: PublicKey; } /** * Create a settle_market instruction to finalize both pools after market expiry * * This instruction is permissionless - anyone can call it after the market's end_timestamp. * It settles BOTH High and Low pools in a single instruction, locking in the winning_outcome * that was tracked via mark_extreme calls during the market period. * * Requirements: * - Both pools must be initialized * - Neither pool can already be settled * - Current timestamp must be > market.end_timestamp * * @param program - Anchor Program instance * @param params - Settle market parameters * @returns Promise resolving to TransactionInstruction * * @example * ```typescript * import { createProgram, createSettleMarketInstruction } from "@pit-protocol/sdk"; * * const program = createProgram(provider); * const ix = await createSettleMarketInstruction(program, { * weekNumber: 1, * settler: wallet.publicKey, * }); * * const tx = new Transaction().add(ix); * await sendAndConfirmTransaction(connection, tx, [wallet]); * ``` */ export async function createSettleMarketInstruction( program: PitProgram, params: SettleMarketParams ): Promise { const { weekNumber, settler } = params; const marketState = getMarketAddress(weekNumber); const registry = getRegistryAddress(); return await program.methods .settleMarket() .accountsPartial({ marketState, registry, settler, }) .instruction(); }