/** * Anchor Program Management * * This module provides utilities for creating and managing the Anchor Program * instance used to interact with the PIT protocol. */ import { Program, AnchorProvider } from "@coral-xyz/anchor"; import { Connection, Keypair, Transaction, VersionedTransaction } from "@solana/web3.js"; import { IDL_DATA } from "./idl/pit.generated.js"; import type { Pit } from "./idl/pit.js"; // Use inlined constant directly const IDL = IDL_DATA; /** * The typed Anchor Program instance for PIT protocol */ export type PitProgram = Program; /** * Re-export IDL for consumers who need it */ export { IDL }; export type { Pit }; /** * Create a PIT Program instance with the given AnchorProvider * * Use this when you have a wallet connected (for signing transactions). * * @param provider - AnchorProvider with wallet and connection * @returns Typed Anchor Program instance * * @example * ```typescript * import { AnchorProvider } from "@coral-xyz/anchor"; * import { createProgram } from "@pit-protocol/sdk"; * * const provider = new AnchorProvider(connection, wallet, { commitment: "confirmed" }); * const program = createProgram(provider); * * // Now use program.methods.* to build instructions * const ix = await program.methods * .trade(poolType, strikeIndex, isHit, amount, isBuy) * .accountsPartial({ ... }) * .instruction(); * ``` */ export function createProgram(provider: AnchorProvider): PitProgram { return new Program(IDL as Pit, provider); } /** * Create a read-only PIT Program instance * * Use this when you don't need to sign transactions (e.g., fetching accounts). * This creates a dummy wallet that cannot sign. * * @param connection - Solana connection * @returns Typed Anchor Program instance (read-only) * * @example * ```typescript * import { Connection } from "@solana/web3.js"; * import { createReadOnlyProgram, getMarketAddress } from "@pit-protocol/sdk"; * * const connection = new Connection("https://api.mainnet-beta.solana.com"); * const program = createReadOnlyProgram(connection); * * // Fetch market state * const marketPda = getMarketAddress(weekNumber); * const market = await program.account.marketState.fetch(marketPda); * ``` */ export function createReadOnlyProgram(connection: Connection): PitProgram { // Create a dummy keypair for read-only operations const dummyKeypair = Keypair.generate(); const provider = new AnchorProvider( connection, { publicKey: dummyKeypair.publicKey, signTransaction: async () => { throw new Error("Read-only program cannot sign transactions"); }, signAllTransactions: async () => { throw new Error("Read-only program cannot sign transactions"); }, }, { commitment: "confirmed" } ); return new Program(IDL as Pit, provider); } /** * Create a PIT Program instance with a keypair signer * * Use this for backend services (like keeper bots) that sign with a keypair. * * @param connection - Solana connection * @param signer - Keypair for signing transactions * @returns Typed Anchor Program instance * * @example * ```typescript * import { Connection, Keypair } from "@solana/web3.js"; * import { createProgramWithKeypair } from "@pit-protocol/sdk"; * * const connection = new Connection(rpcUrl, "confirmed"); * const signer = Keypair.fromSecretKey(secretKey); * const program = createProgramWithKeypair(connection, signer); * * // Build and send transactions * const tx = await program.methods * .markExtreme(poolType, newOutcome) * .accountsPartial({ ... }) * .transaction(); * ``` */ export function createProgramWithKeypair(connection: Connection, signer: Keypair): PitProgram { const provider = new AnchorProvider( connection, { publicKey: signer.publicKey, signTransaction: async (tx: T): Promise => { if (tx instanceof Transaction) { tx.partialSign(signer); } // VersionedTransaction doesn't support partialSign with Keypair directly return tx; }, signAllTransactions: async ( txs: T[] ): Promise => { txs.forEach((tx) => { if (tx instanceof Transaction) { tx.partialSign(signer); } }); return txs; }, }, { commitment: "confirmed" } ); return new Program(IDL as Pit, provider); }