pnp-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,583 @@
1
+ import { Connection, PublicKey, Commitment as Commitment$1, Transaction, VersionedTransaction, Keypair } from '@solana/web3.js';
2
+ import { z } from 'zod';
3
+ import * as anchor from '@coral-xyz/anchor';
4
+ import { Idl } from '@coral-xyz/anchor';
5
+ import { BN } from 'bn.js';
6
+
7
+ type Commitment = "processed" | "confirmed" | "finalized";
8
+ interface Logger {
9
+ debug?: (...args: any[]) => void;
10
+ info?: (...args: any[]) => void;
11
+ warn?: (...args: any[]) => void;
12
+ error?: (...args: any[]) => void;
13
+ }
14
+ interface SdkConfig {
15
+ rpcUrl: string;
16
+ commitment?: Commitment;
17
+ httpHeaders?: Record<string, string>;
18
+ priorityFeeMicroLamports?: number;
19
+ computeUnitLimit?: number;
20
+ computeUnitPriceMicroLamports?: number;
21
+ addressLookupTables?: unknown[];
22
+ logger?: Logger;
23
+ }
24
+ declare class SdkError extends Error {
25
+ code: string;
26
+ details?: unknown;
27
+ constructor(code: string, message: string, details?: unknown);
28
+ }
29
+ declare class ValidationError extends SdkError {
30
+ constructor(message: string, details?: unknown);
31
+ }
32
+ declare class TransportError extends SdkError {
33
+ constructor(message: string, details?: unknown);
34
+ }
35
+ declare class ProgramError extends SdkError {
36
+ programErrorCode?: number;
37
+ logs?: string[];
38
+ constructor(message: string, details?: unknown, programErrorCode?: number, logs?: string[]);
39
+ }
40
+ type PubkeyLike = string;
41
+ type U64Like = bigint | number | string;
42
+ type WinningTokenLike = "none" | "yes" | "no" | number | Record<string, unknown> | null;
43
+ interface MarketType {
44
+ id: U64Like;
45
+ resolved: boolean;
46
+ market_reserves: U64Like;
47
+ collateral_token: PubkeyLike;
48
+ winning_token_id: WinningTokenLike;
49
+ yes_token_mint: PubkeyLike;
50
+ no_token_mint: PubkeyLike;
51
+ yes_token_supply_minted: U64Like;
52
+ no_token_supply_minted: U64Like;
53
+ creator: PubkeyLike;
54
+ creator_fee_treasury: PubkeyLike;
55
+ question: string;
56
+ initial_liquidity: U64Like;
57
+ end_time: U64Like;
58
+ bump: number;
59
+ creation_time: U64Like;
60
+ resolvable: boolean;
61
+ force_resolve: boolean;
62
+ version: number;
63
+ }
64
+ interface MarketsResponse {
65
+ count: number;
66
+ data: Array<{
67
+ publicKey: string;
68
+ account: MarketType;
69
+ }>;
70
+ }
71
+ interface GlobalConfigType {
72
+ admin: PubkeyLike;
73
+ oracle_program: PubkeyLike;
74
+ fee: U64Like;
75
+ creator_fee: U64Like;
76
+ bump: number;
77
+ yes_global_metadata: string;
78
+ no_global_metadata: string;
79
+ collateral_token_mint: PubkeyLike;
80
+ global_id: U64Like;
81
+ min_liquidity: U64Like;
82
+ buffer_period: U64Like;
83
+ burn_fee: U64Like;
84
+ trading_paused: boolean;
85
+ }
86
+ interface SettlementCriteria {
87
+ category: string;
88
+ reasoning: string;
89
+ resolvable: boolean;
90
+ resolution_sources: string[];
91
+ settlement_criteria: string;
92
+ suggested_improvements: string;
93
+ }
94
+ interface SettlementData {
95
+ answer: string;
96
+ reasoning: string;
97
+ resolved?: boolean;
98
+ winning_token_id?: string;
99
+ resolution_time?: string;
100
+ settlement_description?: string;
101
+ resolution_source?: string;
102
+ }
103
+
104
+ declare class Client {
105
+ readonly connection: Connection;
106
+ readonly programId: PublicKey;
107
+ readonly commitment: Commitment$1;
108
+ readonly logger: Logger;
109
+ constructor(cfg: SdkConfig);
110
+ fail(code: string, message: string, details?: unknown): TransportError;
111
+ }
112
+
113
+ type AccountsMap = Record<string, PublicKey>;
114
+
115
+ type CreateMarketArgs = {
116
+ question: string;
117
+ initial_liquidity: bigint;
118
+ end_time: bigint;
119
+ creator?: PublicKey;
120
+ };
121
+ type MintDecisionTokensArgs = {
122
+ amount: bigint;
123
+ buy_yes_token: boolean;
124
+ minimum_out: bigint;
125
+ };
126
+ type BurnDecisionTokensArgs = {
127
+ amount: bigint;
128
+ burn_yes_token: boolean;
129
+ };
130
+
131
+ interface SignerLike {
132
+ publicKey: PublicKey;
133
+ signTransaction(tx: Transaction | VersionedTransaction): Promise<Transaction | VersionedTransaction>;
134
+ signAllTransactions?(txs: (Transaction | VersionedTransaction)[]): Promise<(Transaction | VersionedTransaction)[]>;
135
+ }
136
+
137
+ declare const CreateMarketSchema: z.ZodObject<{
138
+ baseMint: z.ZodOptional<z.ZodType<PublicKey, z.ZodTypeDef, PublicKey>>;
139
+ quoteMint: z.ZodOptional<z.ZodType<PublicKey, z.ZodTypeDef, PublicKey>>;
140
+ question: z.ZodOptional<z.ZodString>;
141
+ initialLiquidity: z.ZodOptional<z.ZodBigInt>;
142
+ endTime: z.ZodOptional<z.ZodBigInt>;
143
+ tickSize: z.ZodOptional<z.ZodBigInt>;
144
+ lotSize: z.ZodOptional<z.ZodBigInt>;
145
+ }, "strip", z.ZodTypeAny, {
146
+ question?: string | undefined;
147
+ initialLiquidity?: bigint | undefined;
148
+ endTime?: bigint | undefined;
149
+ baseMint?: PublicKey | undefined;
150
+ quoteMint?: PublicKey | undefined;
151
+ tickSize?: bigint | undefined;
152
+ lotSize?: bigint | undefined;
153
+ }, {
154
+ question?: string | undefined;
155
+ initialLiquidity?: bigint | undefined;
156
+ endTime?: bigint | undefined;
157
+ baseMint?: PublicKey | undefined;
158
+ quoteMint?: PublicKey | undefined;
159
+ tickSize?: bigint | undefined;
160
+ lotSize?: bigint | undefined;
161
+ }>;
162
+ declare class MarketModule {
163
+ private readonly client;
164
+ private readonly signer;
165
+ constructor(client: Client, signer: SignerLike);
166
+ createMarket(params: z.infer<typeof CreateMarketSchema>): Promise<{
167
+ signature: string | undefined;
168
+ market: PublicKey;
169
+ }>;
170
+ createMarketIdl(accounts: AccountsMap, args: CreateMarketArgs): Promise<{
171
+ signature: string | undefined;
172
+ }>;
173
+ createMarketDerived(params: {
174
+ question: string;
175
+ initialLiquidity: bigint;
176
+ endTime: bigint;
177
+ collateralMint?: PublicKey;
178
+ }): Promise<{
179
+ signature: string | undefined;
180
+ market: PublicKey;
181
+ }>;
182
+ fetchGlobalConfig(): Promise<{
183
+ publicKey: PublicKey;
184
+ account: any;
185
+ }>;
186
+ fetchMarket(market: PublicKey): Promise<{
187
+ publicKey: PublicKey;
188
+ account: any;
189
+ }>;
190
+ }
191
+
192
+ declare const TradeSchema: z.ZodObject<{
193
+ market: z.ZodType<PublicKey, z.ZodTypeDef, PublicKey>;
194
+ side: z.ZodUnion<[z.ZodLiteral<"buy">, z.ZodLiteral<"sell">]>;
195
+ price: z.ZodBigInt;
196
+ size: z.ZodBigInt;
197
+ }, "strip", z.ZodTypeAny, {
198
+ market: PublicKey;
199
+ side: "buy" | "sell";
200
+ price: bigint;
201
+ size: bigint;
202
+ }, {
203
+ market: PublicKey;
204
+ side: "buy" | "sell";
205
+ price: bigint;
206
+ size: bigint;
207
+ }>;
208
+ declare class TradingModule {
209
+ private readonly client;
210
+ private readonly signer;
211
+ constructor(client: Client, signer: SignerLike);
212
+ trade(params: z.infer<typeof TradeSchema>): Promise<{
213
+ signature: string | undefined;
214
+ }>;
215
+ mintDecisionTokens(accounts: AccountsMap, args: MintDecisionTokensArgs): Promise<{
216
+ signature: string | undefined;
217
+ }>;
218
+ burnDecisionTokens(accounts: AccountsMap, args: BurnDecisionTokensArgs): Promise<{
219
+ signature: string | undefined;
220
+ }>;
221
+ mintDecisionTokensDerived(params: {
222
+ market: PublicKey;
223
+ amount: bigint;
224
+ buyYesToken: boolean;
225
+ minimumOut: bigint;
226
+ }): Promise<{
227
+ signature: string | undefined;
228
+ }>;
229
+ burnDecisionTokensDerived(params: {
230
+ market: PublicKey;
231
+ amount: bigint;
232
+ burnYesToken: boolean;
233
+ }): Promise<{
234
+ signature: string | undefined;
235
+ }>;
236
+ getMarketInfo(market: PublicKey): Promise<{
237
+ address: PublicKey;
238
+ question: any;
239
+ id: any;
240
+ creator: PublicKey;
241
+ initialLiquidity: any;
242
+ marketReserves: any;
243
+ endTime: any;
244
+ resolvable: any;
245
+ resolved: any;
246
+ winningTokenId: any;
247
+ yesTokenMint: PublicKey;
248
+ noTokenMint: PublicKey;
249
+ collateralToken: PublicKey;
250
+ yesTokenSupplyMinted: any;
251
+ noTokenSupplyMinted: any;
252
+ }>;
253
+ getBalances(market: PublicKey): Promise<{
254
+ collateral: {
255
+ account: PublicKey;
256
+ amount: any;
257
+ uiAmount: any;
258
+ uiAmountString: any;
259
+ };
260
+ yes: {
261
+ account: PublicKey;
262
+ amount: any;
263
+ uiAmount: any;
264
+ uiAmountString: any;
265
+ };
266
+ no: {
267
+ account: PublicKey;
268
+ amount: any;
269
+ uiAmount: any;
270
+ uiAmountString: any;
271
+ };
272
+ }>;
273
+ getPrices(market: PublicKey): Promise<{
274
+ yesMinted: number;
275
+ noMinted: number;
276
+ totalMinted: number;
277
+ yesShare: number;
278
+ noShare: number;
279
+ }>;
280
+ buyTokensUsdc(params: {
281
+ market: PublicKey;
282
+ buyYesToken: boolean;
283
+ amountUsdc: number;
284
+ minimumOut?: bigint;
285
+ }): Promise<{
286
+ signature: string;
287
+ usdcSpent: number;
288
+ tokensReceived: number;
289
+ before: {
290
+ collateral: {
291
+ account: PublicKey;
292
+ amount: any;
293
+ uiAmount: any;
294
+ uiAmountString: any;
295
+ };
296
+ yes: {
297
+ account: PublicKey;
298
+ amount: any;
299
+ uiAmount: any;
300
+ uiAmountString: any;
301
+ };
302
+ no: {
303
+ account: PublicKey;
304
+ amount: any;
305
+ uiAmount: any;
306
+ uiAmountString: any;
307
+ };
308
+ };
309
+ after: {
310
+ collateral: {
311
+ account: PublicKey;
312
+ amount: any;
313
+ uiAmount: any;
314
+ uiAmountString: any;
315
+ };
316
+ yes: {
317
+ account: PublicKey;
318
+ amount: any;
319
+ uiAmount: any;
320
+ uiAmountString: any;
321
+ };
322
+ no: {
323
+ account: PublicKey;
324
+ amount: any;
325
+ uiAmount: any;
326
+ uiAmountString: any;
327
+ };
328
+ };
329
+ }>;
330
+ sellTokensBase(params: {
331
+ market: PublicKey;
332
+ burnYesToken: boolean;
333
+ amountBaseUnits: bigint;
334
+ }): Promise<{
335
+ signature: string;
336
+ tokensSold: number;
337
+ usdcReceived: number;
338
+ before: {
339
+ collateral: {
340
+ account: PublicKey;
341
+ amount: any;
342
+ uiAmount: any;
343
+ uiAmountString: any;
344
+ };
345
+ yes: {
346
+ account: PublicKey;
347
+ amount: any;
348
+ uiAmount: any;
349
+ uiAmountString: any;
350
+ };
351
+ no: {
352
+ account: PublicKey;
353
+ amount: any;
354
+ uiAmount: any;
355
+ uiAmountString: any;
356
+ };
357
+ };
358
+ after: {
359
+ collateral: {
360
+ account: PublicKey;
361
+ amount: any;
362
+ uiAmount: any;
363
+ uiAmountString: any;
364
+ };
365
+ yes: {
366
+ account: PublicKey;
367
+ amount: any;
368
+ uiAmount: any;
369
+ uiAmountString: any;
370
+ };
371
+ no: {
372
+ account: PublicKey;
373
+ amount: any;
374
+ uiAmount: any;
375
+ uiAmountString: any;
376
+ };
377
+ };
378
+ }>;
379
+ redeemPosition(market: PublicKey): Promise<void>;
380
+ }
381
+
382
+ declare const RedeemSchema: z.ZodObject<{
383
+ market: z.ZodType<PublicKey, z.ZodTypeDef, PublicKey>;
384
+ position: z.ZodType<PublicKey, z.ZodTypeDef, PublicKey>;
385
+ amount: z.ZodBigInt;
386
+ to: z.ZodOptional<z.ZodType<PublicKey, z.ZodTypeDef, PublicKey>>;
387
+ }, "strip", z.ZodTypeAny, {
388
+ market: PublicKey;
389
+ amount: bigint;
390
+ position: PublicKey;
391
+ to?: PublicKey | undefined;
392
+ }, {
393
+ market: PublicKey;
394
+ amount: bigint;
395
+ position: PublicKey;
396
+ to?: PublicKey | undefined;
397
+ }>;
398
+ declare class RedemptionModule {
399
+ private readonly client;
400
+ private readonly signer;
401
+ constructor(client: Client, signer: SignerLike);
402
+ redeemPosition(params: z.infer<typeof RedeemSchema>): Promise<{
403
+ signature: string | undefined;
404
+ }>;
405
+ claimCreatorFee(accounts: AccountsMap): Promise<{
406
+ signature: string | undefined;
407
+ }>;
408
+ creatorRefund(accounts: AccountsMap): Promise<{
409
+ signature: string | undefined;
410
+ }>;
411
+ creatorRefundV2(accounts: AccountsMap): Promise<{
412
+ signature: string | undefined;
413
+ }>;
414
+ /**
415
+ * Redeem a winning position from a resolved market
416
+ * @param accounts - Account mapping for the redemption instruction
417
+ * @returns Transaction signature
418
+ */
419
+ redeemPositionV2(accounts: AccountsMap): Promise<{
420
+ signature: string | undefined;
421
+ }>;
422
+ }
423
+
424
+ /**
425
+ * Provides a wrapper for Anchor program interactions
426
+ * Following the same pattern as the JavaScript example
427
+ */
428
+ declare class AnchorClient {
429
+ readonly connection: Connection;
430
+ readonly signer: Keypair;
431
+ readonly idlOverride?: Idl | undefined;
432
+ readonly provider: anchor.AnchorProvider;
433
+ readonly wallet: anchor.Wallet;
434
+ readonly program: anchor.Program;
435
+ constructor(connection: Connection, signer: Keypair, idlOverride?: Idl | undefined);
436
+ /**
437
+ * Convert a JavaScript BigInt or number to Anchor BN
438
+ */
439
+ static toBN(value: bigint | number): typeof BN.prototype;
440
+ /**
441
+ * Returns the IDL for the PNP program
442
+ */
443
+ static getIdl(): Promise<Idl>;
444
+ static fromIdl(connection: Connection, signer: Keypair, idl: Idl): AnchorClient;
445
+ /**
446
+ * Get the wallet's public key
447
+ */
448
+ get walletPublicKey(): PublicKey;
449
+ }
450
+
451
+ /**
452
+ * Market module that uses direct Anchor program calls
453
+ */
454
+ declare class AnchorMarketModule {
455
+ private anchorClient;
456
+ constructor(anchorClient: AnchorClient);
457
+ /**
458
+ * Create a new prediction market using direct Anchor program calls
459
+ * Following the same structure as the working JavaScript example
460
+ */
461
+ createMarket(params: {
462
+ question: string;
463
+ initialLiquidity: bigint;
464
+ endTime: bigint;
465
+ collateralTokenMint: PublicKey;
466
+ creator?: PublicKey;
467
+ }): Promise<{
468
+ signature: any;
469
+ market: PublicKey;
470
+ marketDetails: any;
471
+ } | {
472
+ signature: any;
473
+ market: PublicKey;
474
+ marketDetails?: undefined;
475
+ }>;
476
+ }
477
+
478
+ declare class PNPClient {
479
+ readonly client: Client;
480
+ readonly signer?: SignerLike;
481
+ readonly market?: MarketModule;
482
+ readonly trading?: TradingModule;
483
+ readonly redemption?: RedemptionModule;
484
+ readonly anchorMarket?: AnchorMarketModule;
485
+ readonly anchorClient?: AnchorClient;
486
+ constructor(rpcUrl: string, privateKey?: Uint8Array | string | {
487
+ secretKey: Uint8Array;
488
+ });
489
+ /**
490
+ * Redeems a winning position in a resolved market
491
+ * @param market - The public key of the market where the position was created
492
+ * @param options - Optional parameters for customizing the redemption
493
+ * @returns Transaction signature
494
+ */
495
+ redeemPosition(market: PublicKey, options?: {
496
+ admin?: PublicKey;
497
+ marketCreator?: PublicKey;
498
+ creatorFeeTreasury?: PublicKey;
499
+ }): Promise<{
500
+ signature: string | undefined;
501
+ }>;
502
+ fetchGlobalConfig(): Promise<{
503
+ publicKey: PublicKey;
504
+ account: GlobalConfigType;
505
+ }>;
506
+ fetchMarket(market: PublicKey): Promise<{
507
+ publicKey: PublicKey;
508
+ account: MarketType;
509
+ }>;
510
+ fetchMarkets(): Promise<MarketsResponse>;
511
+ /**
512
+ * Fetch settlement criteria for a market from the proxy server
513
+ * @param market - Market public key (as string or PublicKey)
514
+ * @param baseUrl - Optional base URL for the proxy server
515
+ * @returns Settlement criteria data
516
+ */
517
+ fetchSettlementCriteria(market: string | PublicKey, baseUrl?: string): Promise<SettlementCriteria>;
518
+ /**
519
+ * Get settlement criteria for a market from the proxy server (alias for fetchSettlementCriteria)
520
+ * @param market - Market public key (as string or PublicKey)
521
+ * @param baseUrl - Optional base URL for the proxy server
522
+ * @returns Settlement criteria data
523
+ */
524
+ getSettlementCriteria(market: string | PublicKey, baseUrl?: string, options?: {
525
+ retryDelayMs?: number;
526
+ maxRetryTimeMs?: number;
527
+ }): Promise<SettlementCriteria>;
528
+ /**
529
+ * Fetch settlement data for a market from the proxy server
530
+ * @param market - Market public key (as string or PublicKey)
531
+ * @param baseUrl - Optional base URL for the proxy server
532
+ * @returns Settlement data
533
+ */
534
+ fetchSettlementData(market: string | PublicKey, baseUrl?: string): Promise<SettlementData>;
535
+ /**
536
+ * Get settlement data for a market from the proxy server (alias for fetchSettlementData)
537
+ * @param market - Market public key (as string or PublicKey)
538
+ * @param baseUrl - Optional base URL for the proxy server
539
+ * @returns Settlement data
540
+ */
541
+ getSettlementData(market: string | PublicKey, baseUrl?: string): Promise<SettlementData>;
542
+ /**
543
+ * Fetch market addresses from the proxy server
544
+ * @param baseUrl - Optional base URL for the proxy server
545
+ * @returns Array of market addresses
546
+ */
547
+ fetchMarketAddresses(baseUrl?: string): Promise<string[]>;
548
+ /**
549
+ * Claim creator refund for a market.
550
+ * Behavior parity with scripts/claimCreatorRefund.ts:
551
+ * - Eligible if market is not resolvable (proxy or on-chain flag) AND signer is the creator
552
+ * - Mixed token programs => use creator_refund_v2 with accountsPartial (yes/no token mints null)
553
+ * - Uniform token programs => try creator_refund_v2 then fallback to v1
554
+ */
555
+ claimMarketRefund(market: PublicKey): Promise<{
556
+ signature: string;
557
+ }>;
558
+ }
559
+
560
+ declare function deriveMarketPda(baseMint: PublicKey, quoteMint: PublicKey, programId: PublicKey): [PublicKey, number];
561
+ declare function derivePositionPda(market: PublicKey, owner: PublicKey, programId: PublicKey): [PublicKey, number];
562
+ declare function deriveGlobalConfigPda(programId: PublicKey): [PublicKey, number];
563
+ declare function deriveCreatorFeeTreasuryPda(creator: PublicKey, programId: PublicKey): [PublicKey, number];
564
+ declare function deriveAta(owner: PublicKey, mint: PublicKey, allowOwnerOffCurve?: boolean, tokenProgramId?: PublicKey, ataProgramId?: PublicKey): PublicKey;
565
+ declare function deriveMarketPdaFromMints(yesMint: PublicKey, noMint: PublicKey, programId: PublicKey): [PublicKey, number];
566
+ declare function deriveYesTokenMint(globalId: bigint, programId: PublicKey): [PublicKey, number];
567
+ declare function deriveNoTokenMint(globalId: bigint, programId: PublicKey): [PublicKey, number];
568
+ declare function deriveTokenMetadataPda(mint: PublicKey, metadataProgramId: PublicKey): [PublicKey, number];
569
+
570
+ declare const pdas_deriveAta: typeof deriveAta;
571
+ declare const pdas_deriveCreatorFeeTreasuryPda: typeof deriveCreatorFeeTreasuryPda;
572
+ declare const pdas_deriveGlobalConfigPda: typeof deriveGlobalConfigPda;
573
+ declare const pdas_deriveMarketPda: typeof deriveMarketPda;
574
+ declare const pdas_deriveMarketPdaFromMints: typeof deriveMarketPdaFromMints;
575
+ declare const pdas_deriveNoTokenMint: typeof deriveNoTokenMint;
576
+ declare const pdas_derivePositionPda: typeof derivePositionPda;
577
+ declare const pdas_deriveTokenMetadataPda: typeof deriveTokenMetadataPda;
578
+ declare const pdas_deriveYesTokenMint: typeof deriveYesTokenMint;
579
+ declare namespace pdas {
580
+ export { pdas_deriveAta as deriveAta, pdas_deriveCreatorFeeTreasuryPda as deriveCreatorFeeTreasuryPda, pdas_deriveGlobalConfigPda as deriveGlobalConfigPda, pdas_deriveMarketPda as deriveMarketPda, pdas_deriveMarketPdaFromMints as deriveMarketPdaFromMints, pdas_deriveNoTokenMint as deriveNoTokenMint, pdas_derivePositionPda as derivePositionPda, pdas_deriveTokenMetadataPda as deriveTokenMetadataPda, pdas_deriveYesTokenMint as deriveYesTokenMint };
581
+ }
582
+
583
+ export { Client, type Commitment, type GlobalConfigType, type Logger, MarketModule, type MarketType, type MarketsResponse, PNPClient, ProgramError, type PubkeyLike, RedemptionModule, type SdkConfig, SdkError, type SettlementCriteria, type SettlementData, TradingModule, TransportError, type U64Like, ValidationError, type WinningTokenLike, pdas };