import { PublicKey, TransactionInstruction } from "@solana/web3.js"; import { TOKEN_PROGRAM_ID } from "@solana/spl-token"; import type { PitProgram } from "../program.js"; import { TOKEN_2022_PROGRAM_ID } from "../constants.js"; import { getMarketAddress } from "../accounts/pda.js"; /** * Parameters for creating a withdraw_subsidy instruction */ interface WithdrawSubsidyParams { /** Week number of the market */ weekNumber: number; /** Operator's public key (must be market's operator) */ operator: PublicKey; /** Operator's wSOL account (receives remaining subsidy) */ operatorWsolAccount: PublicKey; /** wSOL vault (from MarketState.wsolVault) */ wsolVault: PublicKey; /** wSOL mint address */ wsolMint: PublicKey; /** * All winning token mints that need to be checked for outstanding supply. * Pass all 12 mint accounts (both pools: hitMints + missMints). */ winningMints: PublicKey[]; } /** * Create a withdraw_subsidy instruction for operator to withdraw remaining liquidity * * This instruction is operator-only and requires: * - Both pools to be settled * - Calculates outstanding winnings from unclaimed tokens * - Withdraws vault balance minus outstanding winnings * * @param program - Anchor Program instance * @param params - Withdraw subsidy parameters * @returns Promise resolving to TransactionInstruction * * @example * ```typescript * import { createProgram, createWithdrawSubsidyInstruction, WSOL_MINT } from "@pit-protocol/sdk"; * * const program = createProgram(provider); * * // Get all mint accounts for remaining accounts * const allMints = [ * ...market.highPool.hitMints, * ...market.highPool.missMints, * ...market.lowPool.hitMints, * ...market.lowPool.missMints, * ]; * * const ix = await createWithdrawSubsidyInstruction(program, { * weekNumber: 1, * operator: operatorKeypair.publicKey, * operatorWsolAccount: operatorWsolAta, * wsolVault: market.wsolVault, * wsolMint: WSOL_MINT, * winningMints: allMints, * }); * * const tx = new Transaction().add(ix); * await sendAndConfirmTransaction(connection, tx, [operatorKeypair]); * ``` */ export async function createWithdrawSubsidyInstruction( program: PitProgram, params: WithdrawSubsidyParams ): Promise { const { weekNumber, operator, operatorWsolAccount, wsolVault, wsolMint, winningMints } = params; const marketState = getMarketAddress(weekNumber); return await program.methods .withdrawSubsidy() .accountsPartial({ marketState, operator, operatorWsolAccount, wsolVault, wsolMint, tokenProgram: TOKEN_PROGRAM_ID, token2022Program: TOKEN_2022_PROGRAM_ID, }) .remainingAccounts( winningMints.map((mint) => ({ pubkey: mint, isSigner: false, isWritable: false, })) ) .instruction(); }