@epicentral/sos-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.
Files changed (110) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +239 -0
  3. package/accounts/fetchers.ts +140 -0
  4. package/accounts/list.ts +152 -0
  5. package/accounts/pdas.ts +308 -0
  6. package/client/lookup-table.ts +32 -0
  7. package/client/program.ts +13 -0
  8. package/client/types.ts +8 -0
  9. package/generated/accounts/collateralPool.ts +217 -0
  10. package/generated/accounts/config.ts +156 -0
  11. package/generated/accounts/dualSourceContract.ts +235 -0
  12. package/generated/accounts/escrowState.ts +183 -0
  13. package/generated/accounts/index.ts +24 -0
  14. package/generated/accounts/lenderPosition.ts +211 -0
  15. package/generated/accounts/liquidityRouter.ts +223 -0
  16. package/generated/accounts/makerCollateralShare.ts +229 -0
  17. package/generated/accounts/makerPoolShare.ts +203 -0
  18. package/generated/accounts/marketDataAccount.ts +176 -0
  19. package/generated/accounts/optionAccount.ts +247 -0
  20. package/generated/accounts/optionPool.ts +253 -0
  21. package/generated/accounts/poolLoan.ts +220 -0
  22. package/generated/accounts/positionAccount.ts +187 -0
  23. package/generated/accounts/priceUpdateV2.ts +163 -0
  24. package/generated/accounts/vault.ts +304 -0
  25. package/generated/accounts/writerPosition.ts +297 -0
  26. package/generated/errors/index.ts +9 -0
  27. package/generated/errors/optionProgram.ts +392 -0
  28. package/generated/index.ts +13 -0
  29. package/generated/instructions/acceptAdmin.ts +230 -0
  30. package/generated/instructions/autoExerciseAllExpired.ts +523 -0
  31. package/generated/instructions/autoExerciseExpired.ts +623 -0
  32. package/generated/instructions/borrowFromPool.ts +554 -0
  33. package/generated/instructions/buyFromPool.ts +684 -0
  34. package/generated/instructions/claimPremium.ts +377 -0
  35. package/generated/instructions/closeLongToPool.ts +716 -0
  36. package/generated/instructions/closeOption.ts +235 -0
  37. package/generated/instructions/createEscrowV2.ts +518 -0
  38. package/generated/instructions/createLiquidityRouter.ts +361 -0
  39. package/generated/instructions/depositCollateral.ts +624 -0
  40. package/generated/instructions/depositToPool.ts +497 -0
  41. package/generated/instructions/depositToPosition.ts +429 -0
  42. package/generated/instructions/index.ts +45 -0
  43. package/generated/instructions/initCollateralPool.ts +513 -0
  44. package/generated/instructions/initConfig.ts +279 -0
  45. package/generated/instructions/initOptionPool.ts +587 -0
  46. package/generated/instructions/initializeMarketData.ts +359 -0
  47. package/generated/instructions/liquidateWriterPosition.ts +592 -0
  48. package/generated/instructions/omlpCreateVault.ts +547 -0
  49. package/generated/instructions/omlpTakeOfferWithFailover.ts +606 -0
  50. package/generated/instructions/omlpUpdateMaxLeverage.ts +304 -0
  51. package/generated/instructions/omlpUpdateProtocolFee.ts +304 -0
  52. package/generated/instructions/omlpUpdateSupplyLimit.ts +304 -0
  53. package/generated/instructions/optionExercise.ts +540 -0
  54. package/generated/instructions/optionMint.ts +1349 -0
  55. package/generated/instructions/optionValidate.ts +255 -0
  56. package/generated/instructions/repayPoolLoan.ts +499 -0
  57. package/generated/instructions/repayPoolLoanFromCollateral.ts +514 -0
  58. package/generated/instructions/settleMakerCollateral.ts +472 -0
  59. package/generated/instructions/syncWriterPosition.ts +206 -0
  60. package/generated/instructions/transferAdmin.ts +245 -0
  61. package/generated/instructions/unwindWriterUnsold.ts +668 -0
  62. package/generated/instructions/updateImpliedVolatility.ts +226 -0
  63. package/generated/instructions/updateMarketData.ts +315 -0
  64. package/generated/instructions/withdrawFromPool.ts +429 -0
  65. package/generated/instructions/withdrawFromPosition.ts +405 -0
  66. package/generated/instructions/writeToPool.ts +594 -0
  67. package/generated/programs/index.ts +9 -0
  68. package/generated/programs/optionProgram.ts +832 -0
  69. package/generated/shared/index.ts +164 -0
  70. package/generated/types/borrowedFromSAP1.ts +75 -0
  71. package/generated/types/borrowedFromSAP2.ts +83 -0
  72. package/generated/types/failoverTriggered.ts +85 -0
  73. package/generated/types/impliedVolatilityUpdated.ts +73 -0
  74. package/generated/types/index.ts +32 -0
  75. package/generated/types/liquidationExecuted.ts +73 -0
  76. package/generated/types/liquidityMetrics.ts +69 -0
  77. package/generated/types/liquidityRouterCreated.ts +79 -0
  78. package/generated/types/marketDataInitialized.ts +61 -0
  79. package/generated/types/marketDataUpdated.ts +69 -0
  80. package/generated/types/optionClosed.ts +56 -0
  81. package/generated/types/optionExercised.ts +62 -0
  82. package/generated/types/optionExpired.ts +49 -0
  83. package/generated/types/optionMinted.ts +78 -0
  84. package/generated/types/optionType.ts +38 -0
  85. package/generated/types/optionValidated.ts +82 -0
  86. package/generated/types/poolLoanCreated.ts +74 -0
  87. package/generated/types/poolLoanRepaid.ts +74 -0
  88. package/generated/types/positionDeposited.ts +73 -0
  89. package/generated/types/positionWithdrawn.ts +81 -0
  90. package/generated/types/priceFeedMessage.ts +117 -0
  91. package/generated/types/protocolFeeUpdated.ts +69 -0
  92. package/generated/types/sap2Provider.ts +38 -0
  93. package/generated/types/vaultCreated.ts +60 -0
  94. package/generated/types/verificationLevel.ts +95 -0
  95. package/index.ts +25 -0
  96. package/long/builders.ts +126 -0
  97. package/long/exercise.ts +49 -0
  98. package/long/quotes.ts +48 -0
  99. package/omlp/builders.ts +74 -0
  100. package/omlp/service.ts +94 -0
  101. package/package.json +22 -0
  102. package/shared/amounts.ts +32 -0
  103. package/shared/errors.ts +12 -0
  104. package/shared/remaining-accounts.ts +41 -0
  105. package/shared/transactions.ts +49 -0
  106. package/short/builders.ts +268 -0
  107. package/short/claim-premium.ts +37 -0
  108. package/short/close-option.ts +34 -0
  109. package/short/pool.ts +224 -0
  110. package/tsconfig.json +12 -0
@@ -0,0 +1,684 @@
1
+ /**
2
+ * This code was AUTOGENERATED using the Codama library.
3
+ * Please DO NOT EDIT THIS FILE, instead use visitors
4
+ * to add features, then rerun Codama to update it.
5
+ *
6
+ * @see https://github.com/codama-idl/codama
7
+ */
8
+
9
+ import {
10
+ combineCodec,
11
+ fixDecoderSize,
12
+ fixEncoderSize,
13
+ getAddressEncoder,
14
+ getBytesDecoder,
15
+ getBytesEncoder,
16
+ getProgramDerivedAddress,
17
+ getStructDecoder,
18
+ getStructEncoder,
19
+ getU64Decoder,
20
+ getU64Encoder,
21
+ transformEncoder,
22
+ type AccountMeta,
23
+ type AccountSignerMeta,
24
+ type Address,
25
+ type FixedSizeCodec,
26
+ type FixedSizeDecoder,
27
+ type FixedSizeEncoder,
28
+ type Instruction,
29
+ type InstructionWithAccounts,
30
+ type InstructionWithData,
31
+ type ReadonlyAccount,
32
+ type ReadonlyUint8Array,
33
+ type TransactionSigner,
34
+ type WritableAccount,
35
+ type WritableSignerAccount,
36
+ } from "@solana/kit";
37
+ import { OPTION_PROGRAM_PROGRAM_ADDRESS } from "../programs";
38
+ import {
39
+ expectAddress,
40
+ getAccountMetaFactory,
41
+ type ResolvedAccount,
42
+ } from "../shared";
43
+
44
+ export const BUY_FROM_POOL_DISCRIMINATOR = new Uint8Array([
45
+ 118, 108, 243, 198, 19, 184, 41, 246,
46
+ ]);
47
+
48
+ export function getBuyFromPoolDiscriminatorBytes() {
49
+ return fixEncoderSize(getBytesEncoder(), 8).encode(
50
+ BUY_FROM_POOL_DISCRIMINATOR,
51
+ );
52
+ }
53
+
54
+ export type BuyFromPoolInstruction<
55
+ TProgram extends string = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
56
+ TAccountOptionPool extends string | AccountMeta<string> = string,
57
+ TAccountOptionAccount extends string | AccountMeta<string> = string,
58
+ TAccountLongMint extends string | AccountMeta<string> = string,
59
+ TAccountUnderlyingMint extends string | AccountMeta<string> = string,
60
+ TAccountMarketData extends string | AccountMeta<string> = string,
61
+ TAccountPriceUpdate extends string | AccountMeta<string> = string,
62
+ TAccountBuyerPosition extends string | AccountMeta<string> = string,
63
+ TAccountBuyerOptionAccount extends string | AccountMeta<string> = string,
64
+ TAccountBuyerPaymentAccount extends string | AccountMeta<string> = string,
65
+ TAccountEscrowLongAccount extends string | AccountMeta<string> = string,
66
+ TAccountPremiumVault extends string | AccountMeta<string> = string,
67
+ TAccountBuyer extends string | AccountMeta<string> = string,
68
+ TAccountTokenProgram extends string | AccountMeta<string> =
69
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
70
+ TAccountAssociatedTokenProgram extends string | AccountMeta<string> =
71
+ "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
72
+ TAccountSystemProgram extends string | AccountMeta<string> =
73
+ "11111111111111111111111111111111",
74
+ TRemainingAccounts extends readonly AccountMeta<string>[] = [],
75
+ > = Instruction<TProgram> &
76
+ InstructionWithData<ReadonlyUint8Array> &
77
+ InstructionWithAccounts<
78
+ [
79
+ TAccountOptionPool extends string
80
+ ? WritableAccount<TAccountOptionPool>
81
+ : TAccountOptionPool,
82
+ TAccountOptionAccount extends string
83
+ ? WritableAccount<TAccountOptionAccount>
84
+ : TAccountOptionAccount,
85
+ TAccountLongMint extends string
86
+ ? ReadonlyAccount<TAccountLongMint>
87
+ : TAccountLongMint,
88
+ TAccountUnderlyingMint extends string
89
+ ? ReadonlyAccount<TAccountUnderlyingMint>
90
+ : TAccountUnderlyingMint,
91
+ TAccountMarketData extends string
92
+ ? ReadonlyAccount<TAccountMarketData>
93
+ : TAccountMarketData,
94
+ TAccountPriceUpdate extends string
95
+ ? ReadonlyAccount<TAccountPriceUpdate>
96
+ : TAccountPriceUpdate,
97
+ TAccountBuyerPosition extends string
98
+ ? WritableAccount<TAccountBuyerPosition>
99
+ : TAccountBuyerPosition,
100
+ TAccountBuyerOptionAccount extends string
101
+ ? WritableAccount<TAccountBuyerOptionAccount>
102
+ : TAccountBuyerOptionAccount,
103
+ TAccountBuyerPaymentAccount extends string
104
+ ? WritableAccount<TAccountBuyerPaymentAccount>
105
+ : TAccountBuyerPaymentAccount,
106
+ TAccountEscrowLongAccount extends string
107
+ ? WritableAccount<TAccountEscrowLongAccount>
108
+ : TAccountEscrowLongAccount,
109
+ TAccountPremiumVault extends string
110
+ ? WritableAccount<TAccountPremiumVault>
111
+ : TAccountPremiumVault,
112
+ TAccountBuyer extends string
113
+ ? WritableSignerAccount<TAccountBuyer> &
114
+ AccountSignerMeta<TAccountBuyer>
115
+ : TAccountBuyer,
116
+ TAccountTokenProgram extends string
117
+ ? ReadonlyAccount<TAccountTokenProgram>
118
+ : TAccountTokenProgram,
119
+ TAccountAssociatedTokenProgram extends string
120
+ ? ReadonlyAccount<TAccountAssociatedTokenProgram>
121
+ : TAccountAssociatedTokenProgram,
122
+ TAccountSystemProgram extends string
123
+ ? ReadonlyAccount<TAccountSystemProgram>
124
+ : TAccountSystemProgram,
125
+ ...TRemainingAccounts,
126
+ ]
127
+ >;
128
+
129
+ export type BuyFromPoolInstructionData = {
130
+ discriminator: ReadonlyUint8Array;
131
+ quantity: bigint;
132
+ premiumAmount: bigint;
133
+ };
134
+
135
+ export type BuyFromPoolInstructionDataArgs = {
136
+ quantity: number | bigint;
137
+ premiumAmount: number | bigint;
138
+ };
139
+
140
+ export function getBuyFromPoolInstructionDataEncoder(): FixedSizeEncoder<BuyFromPoolInstructionDataArgs> {
141
+ return transformEncoder(
142
+ getStructEncoder([
143
+ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
144
+ ["quantity", getU64Encoder()],
145
+ ["premiumAmount", getU64Encoder()],
146
+ ]),
147
+ (value) => ({ ...value, discriminator: BUY_FROM_POOL_DISCRIMINATOR }),
148
+ );
149
+ }
150
+
151
+ export function getBuyFromPoolInstructionDataDecoder(): FixedSizeDecoder<BuyFromPoolInstructionData> {
152
+ return getStructDecoder([
153
+ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
154
+ ["quantity", getU64Decoder()],
155
+ ["premiumAmount", getU64Decoder()],
156
+ ]);
157
+ }
158
+
159
+ export function getBuyFromPoolInstructionDataCodec(): FixedSizeCodec<
160
+ BuyFromPoolInstructionDataArgs,
161
+ BuyFromPoolInstructionData
162
+ > {
163
+ return combineCodec(
164
+ getBuyFromPoolInstructionDataEncoder(),
165
+ getBuyFromPoolInstructionDataDecoder(),
166
+ );
167
+ }
168
+
169
+ export type BuyFromPoolAsyncInput<
170
+ TAccountOptionPool extends string = string,
171
+ TAccountOptionAccount extends string = string,
172
+ TAccountLongMint extends string = string,
173
+ TAccountUnderlyingMint extends string = string,
174
+ TAccountMarketData extends string = string,
175
+ TAccountPriceUpdate extends string = string,
176
+ TAccountBuyerPosition extends string = string,
177
+ TAccountBuyerOptionAccount extends string = string,
178
+ TAccountBuyerPaymentAccount extends string = string,
179
+ TAccountEscrowLongAccount extends string = string,
180
+ TAccountPremiumVault extends string = string,
181
+ TAccountBuyer extends string = string,
182
+ TAccountTokenProgram extends string = string,
183
+ TAccountAssociatedTokenProgram extends string = string,
184
+ TAccountSystemProgram extends string = string,
185
+ > = {
186
+ /** The pool to buy from */
187
+ optionPool: Address<TAccountOptionPool>;
188
+ /**
189
+ * The option account (mut for state tracking updates)
190
+ * Must match option_pool.option_account to ensure correct buyer_position PDA
191
+ */
192
+ optionAccount: Address<TAccountOptionAccount>;
193
+ /** The LONG token mint */
194
+ longMint: Address<TAccountLongMint>;
195
+ /** Underlying token mint (for decimal handling) */
196
+ underlyingMint: Address<TAccountUnderlyingMint>;
197
+ /** Market data account (provides risk-free rate and pyth_feed_id) */
198
+ marketData: Address<TAccountMarketData>;
199
+ /** Pyth price update account for current underlying price */
200
+ priceUpdate: Address<TAccountPriceUpdate>;
201
+ /** Buyer's position account (created for tracking) */
202
+ buyerPosition?: Address<TAccountBuyerPosition>;
203
+ /** Buyer's LONG token account (receives LONG tokens) */
204
+ buyerOptionAccount?: Address<TAccountBuyerOptionAccount>;
205
+ /** Buyer's payment account (source of premium) */
206
+ buyerPaymentAccount: Address<TAccountBuyerPaymentAccount>;
207
+ /** Pool's LONG escrow (source of LONG tokens) */
208
+ escrowLongAccount: Address<TAccountEscrowLongAccount>;
209
+ /** Pool's premium vault (receives premium) */
210
+ premiumVault: Address<TAccountPremiumVault>;
211
+ buyer: TransactionSigner<TAccountBuyer>;
212
+ tokenProgram?: Address<TAccountTokenProgram>;
213
+ associatedTokenProgram?: Address<TAccountAssociatedTokenProgram>;
214
+ systemProgram?: Address<TAccountSystemProgram>;
215
+ quantity: BuyFromPoolInstructionDataArgs["quantity"];
216
+ premiumAmount: BuyFromPoolInstructionDataArgs["premiumAmount"];
217
+ };
218
+
219
+ export async function getBuyFromPoolInstructionAsync<
220
+ TAccountOptionPool extends string,
221
+ TAccountOptionAccount extends string,
222
+ TAccountLongMint extends string,
223
+ TAccountUnderlyingMint extends string,
224
+ TAccountMarketData extends string,
225
+ TAccountPriceUpdate extends string,
226
+ TAccountBuyerPosition extends string,
227
+ TAccountBuyerOptionAccount extends string,
228
+ TAccountBuyerPaymentAccount extends string,
229
+ TAccountEscrowLongAccount extends string,
230
+ TAccountPremiumVault extends string,
231
+ TAccountBuyer extends string,
232
+ TAccountTokenProgram extends string,
233
+ TAccountAssociatedTokenProgram extends string,
234
+ TAccountSystemProgram extends string,
235
+ TProgramAddress extends Address = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
236
+ >(
237
+ input: BuyFromPoolAsyncInput<
238
+ TAccountOptionPool,
239
+ TAccountOptionAccount,
240
+ TAccountLongMint,
241
+ TAccountUnderlyingMint,
242
+ TAccountMarketData,
243
+ TAccountPriceUpdate,
244
+ TAccountBuyerPosition,
245
+ TAccountBuyerOptionAccount,
246
+ TAccountBuyerPaymentAccount,
247
+ TAccountEscrowLongAccount,
248
+ TAccountPremiumVault,
249
+ TAccountBuyer,
250
+ TAccountTokenProgram,
251
+ TAccountAssociatedTokenProgram,
252
+ TAccountSystemProgram
253
+ >,
254
+ config?: { programAddress?: TProgramAddress },
255
+ ): Promise<
256
+ BuyFromPoolInstruction<
257
+ TProgramAddress,
258
+ TAccountOptionPool,
259
+ TAccountOptionAccount,
260
+ TAccountLongMint,
261
+ TAccountUnderlyingMint,
262
+ TAccountMarketData,
263
+ TAccountPriceUpdate,
264
+ TAccountBuyerPosition,
265
+ TAccountBuyerOptionAccount,
266
+ TAccountBuyerPaymentAccount,
267
+ TAccountEscrowLongAccount,
268
+ TAccountPremiumVault,
269
+ TAccountBuyer,
270
+ TAccountTokenProgram,
271
+ TAccountAssociatedTokenProgram,
272
+ TAccountSystemProgram
273
+ >
274
+ > {
275
+ // Program address.
276
+ const programAddress =
277
+ config?.programAddress ?? OPTION_PROGRAM_PROGRAM_ADDRESS;
278
+
279
+ // Original accounts.
280
+ const originalAccounts = {
281
+ optionPool: { value: input.optionPool ?? null, isWritable: true },
282
+ optionAccount: { value: input.optionAccount ?? null, isWritable: true },
283
+ longMint: { value: input.longMint ?? null, isWritable: false },
284
+ underlyingMint: { value: input.underlyingMint ?? null, isWritable: false },
285
+ marketData: { value: input.marketData ?? null, isWritable: false },
286
+ priceUpdate: { value: input.priceUpdate ?? null, isWritable: false },
287
+ buyerPosition: { value: input.buyerPosition ?? null, isWritable: true },
288
+ buyerOptionAccount: {
289
+ value: input.buyerOptionAccount ?? null,
290
+ isWritable: true,
291
+ },
292
+ buyerPaymentAccount: {
293
+ value: input.buyerPaymentAccount ?? null,
294
+ isWritable: true,
295
+ },
296
+ escrowLongAccount: {
297
+ value: input.escrowLongAccount ?? null,
298
+ isWritable: true,
299
+ },
300
+ premiumVault: { value: input.premiumVault ?? null, isWritable: true },
301
+ buyer: { value: input.buyer ?? null, isWritable: true },
302
+ tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
303
+ associatedTokenProgram: {
304
+ value: input.associatedTokenProgram ?? null,
305
+ isWritable: false,
306
+ },
307
+ systemProgram: { value: input.systemProgram ?? null, isWritable: false },
308
+ };
309
+ const accounts = originalAccounts as Record<
310
+ keyof typeof originalAccounts,
311
+ ResolvedAccount
312
+ >;
313
+
314
+ // Original args.
315
+ const args = { ...input };
316
+
317
+ // Resolve default values.
318
+ if (!accounts.buyerPosition.value) {
319
+ accounts.buyerPosition.value = await getProgramDerivedAddress({
320
+ programAddress,
321
+ seeds: [
322
+ getBytesEncoder().encode(
323
+ new Uint8Array([112, 111, 115, 105, 116, 105, 111, 110]),
324
+ ),
325
+ getAddressEncoder().encode(expectAddress(accounts.buyer.value)),
326
+ getAddressEncoder().encode(expectAddress(accounts.optionAccount.value)),
327
+ ],
328
+ });
329
+ }
330
+ if (!accounts.buyerOptionAccount.value) {
331
+ accounts.buyerOptionAccount.value = await getProgramDerivedAddress({
332
+ programAddress:
333
+ "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" as Address<"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL">,
334
+ seeds: [
335
+ getAddressEncoder().encode(expectAddress(accounts.buyer.value)),
336
+ getBytesEncoder().encode(
337
+ new Uint8Array([
338
+ 6, 221, 246, 225, 215, 101, 161, 147, 217, 203, 225, 70, 206, 235,
339
+ 121, 172, 28, 180, 133, 237, 95, 91, 55, 145, 58, 140, 245, 133,
340
+ 126, 255, 0, 169,
341
+ ]),
342
+ ),
343
+ getAddressEncoder().encode(expectAddress(accounts.longMint.value)),
344
+ ],
345
+ });
346
+ }
347
+ if (!accounts.tokenProgram.value) {
348
+ accounts.tokenProgram.value =
349
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
350
+ }
351
+ if (!accounts.associatedTokenProgram.value) {
352
+ accounts.associatedTokenProgram.value =
353
+ "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" as Address<"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL">;
354
+ }
355
+ if (!accounts.systemProgram.value) {
356
+ accounts.systemProgram.value =
357
+ "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
358
+ }
359
+
360
+ const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
361
+ return Object.freeze({
362
+ accounts: [
363
+ getAccountMeta(accounts.optionPool),
364
+ getAccountMeta(accounts.optionAccount),
365
+ getAccountMeta(accounts.longMint),
366
+ getAccountMeta(accounts.underlyingMint),
367
+ getAccountMeta(accounts.marketData),
368
+ getAccountMeta(accounts.priceUpdate),
369
+ getAccountMeta(accounts.buyerPosition),
370
+ getAccountMeta(accounts.buyerOptionAccount),
371
+ getAccountMeta(accounts.buyerPaymentAccount),
372
+ getAccountMeta(accounts.escrowLongAccount),
373
+ getAccountMeta(accounts.premiumVault),
374
+ getAccountMeta(accounts.buyer),
375
+ getAccountMeta(accounts.tokenProgram),
376
+ getAccountMeta(accounts.associatedTokenProgram),
377
+ getAccountMeta(accounts.systemProgram),
378
+ ],
379
+ data: getBuyFromPoolInstructionDataEncoder().encode(
380
+ args as BuyFromPoolInstructionDataArgs,
381
+ ),
382
+ programAddress,
383
+ } as BuyFromPoolInstruction<
384
+ TProgramAddress,
385
+ TAccountOptionPool,
386
+ TAccountOptionAccount,
387
+ TAccountLongMint,
388
+ TAccountUnderlyingMint,
389
+ TAccountMarketData,
390
+ TAccountPriceUpdate,
391
+ TAccountBuyerPosition,
392
+ TAccountBuyerOptionAccount,
393
+ TAccountBuyerPaymentAccount,
394
+ TAccountEscrowLongAccount,
395
+ TAccountPremiumVault,
396
+ TAccountBuyer,
397
+ TAccountTokenProgram,
398
+ TAccountAssociatedTokenProgram,
399
+ TAccountSystemProgram
400
+ >);
401
+ }
402
+
403
+ export type BuyFromPoolInput<
404
+ TAccountOptionPool extends string = string,
405
+ TAccountOptionAccount extends string = string,
406
+ TAccountLongMint extends string = string,
407
+ TAccountUnderlyingMint extends string = string,
408
+ TAccountMarketData extends string = string,
409
+ TAccountPriceUpdate extends string = string,
410
+ TAccountBuyerPosition extends string = string,
411
+ TAccountBuyerOptionAccount extends string = string,
412
+ TAccountBuyerPaymentAccount extends string = string,
413
+ TAccountEscrowLongAccount extends string = string,
414
+ TAccountPremiumVault extends string = string,
415
+ TAccountBuyer extends string = string,
416
+ TAccountTokenProgram extends string = string,
417
+ TAccountAssociatedTokenProgram extends string = string,
418
+ TAccountSystemProgram extends string = string,
419
+ > = {
420
+ /** The pool to buy from */
421
+ optionPool: Address<TAccountOptionPool>;
422
+ /**
423
+ * The option account (mut for state tracking updates)
424
+ * Must match option_pool.option_account to ensure correct buyer_position PDA
425
+ */
426
+ optionAccount: Address<TAccountOptionAccount>;
427
+ /** The LONG token mint */
428
+ longMint: Address<TAccountLongMint>;
429
+ /** Underlying token mint (for decimal handling) */
430
+ underlyingMint: Address<TAccountUnderlyingMint>;
431
+ /** Market data account (provides risk-free rate and pyth_feed_id) */
432
+ marketData: Address<TAccountMarketData>;
433
+ /** Pyth price update account for current underlying price */
434
+ priceUpdate: Address<TAccountPriceUpdate>;
435
+ /** Buyer's position account (created for tracking) */
436
+ buyerPosition: Address<TAccountBuyerPosition>;
437
+ /** Buyer's LONG token account (receives LONG tokens) */
438
+ buyerOptionAccount: Address<TAccountBuyerOptionAccount>;
439
+ /** Buyer's payment account (source of premium) */
440
+ buyerPaymentAccount: Address<TAccountBuyerPaymentAccount>;
441
+ /** Pool's LONG escrow (source of LONG tokens) */
442
+ escrowLongAccount: Address<TAccountEscrowLongAccount>;
443
+ /** Pool's premium vault (receives premium) */
444
+ premiumVault: Address<TAccountPremiumVault>;
445
+ buyer: TransactionSigner<TAccountBuyer>;
446
+ tokenProgram?: Address<TAccountTokenProgram>;
447
+ associatedTokenProgram?: Address<TAccountAssociatedTokenProgram>;
448
+ systemProgram?: Address<TAccountSystemProgram>;
449
+ quantity: BuyFromPoolInstructionDataArgs["quantity"];
450
+ premiumAmount: BuyFromPoolInstructionDataArgs["premiumAmount"];
451
+ };
452
+
453
+ export function getBuyFromPoolInstruction<
454
+ TAccountOptionPool extends string,
455
+ TAccountOptionAccount extends string,
456
+ TAccountLongMint extends string,
457
+ TAccountUnderlyingMint extends string,
458
+ TAccountMarketData extends string,
459
+ TAccountPriceUpdate extends string,
460
+ TAccountBuyerPosition extends string,
461
+ TAccountBuyerOptionAccount extends string,
462
+ TAccountBuyerPaymentAccount extends string,
463
+ TAccountEscrowLongAccount extends string,
464
+ TAccountPremiumVault extends string,
465
+ TAccountBuyer extends string,
466
+ TAccountTokenProgram extends string,
467
+ TAccountAssociatedTokenProgram extends string,
468
+ TAccountSystemProgram extends string,
469
+ TProgramAddress extends Address = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
470
+ >(
471
+ input: BuyFromPoolInput<
472
+ TAccountOptionPool,
473
+ TAccountOptionAccount,
474
+ TAccountLongMint,
475
+ TAccountUnderlyingMint,
476
+ TAccountMarketData,
477
+ TAccountPriceUpdate,
478
+ TAccountBuyerPosition,
479
+ TAccountBuyerOptionAccount,
480
+ TAccountBuyerPaymentAccount,
481
+ TAccountEscrowLongAccount,
482
+ TAccountPremiumVault,
483
+ TAccountBuyer,
484
+ TAccountTokenProgram,
485
+ TAccountAssociatedTokenProgram,
486
+ TAccountSystemProgram
487
+ >,
488
+ config?: { programAddress?: TProgramAddress },
489
+ ): BuyFromPoolInstruction<
490
+ TProgramAddress,
491
+ TAccountOptionPool,
492
+ TAccountOptionAccount,
493
+ TAccountLongMint,
494
+ TAccountUnderlyingMint,
495
+ TAccountMarketData,
496
+ TAccountPriceUpdate,
497
+ TAccountBuyerPosition,
498
+ TAccountBuyerOptionAccount,
499
+ TAccountBuyerPaymentAccount,
500
+ TAccountEscrowLongAccount,
501
+ TAccountPremiumVault,
502
+ TAccountBuyer,
503
+ TAccountTokenProgram,
504
+ TAccountAssociatedTokenProgram,
505
+ TAccountSystemProgram
506
+ > {
507
+ // Program address.
508
+ const programAddress =
509
+ config?.programAddress ?? OPTION_PROGRAM_PROGRAM_ADDRESS;
510
+
511
+ // Original accounts.
512
+ const originalAccounts = {
513
+ optionPool: { value: input.optionPool ?? null, isWritable: true },
514
+ optionAccount: { value: input.optionAccount ?? null, isWritable: true },
515
+ longMint: { value: input.longMint ?? null, isWritable: false },
516
+ underlyingMint: { value: input.underlyingMint ?? null, isWritable: false },
517
+ marketData: { value: input.marketData ?? null, isWritable: false },
518
+ priceUpdate: { value: input.priceUpdate ?? null, isWritable: false },
519
+ buyerPosition: { value: input.buyerPosition ?? null, isWritable: true },
520
+ buyerOptionAccount: {
521
+ value: input.buyerOptionAccount ?? null,
522
+ isWritable: true,
523
+ },
524
+ buyerPaymentAccount: {
525
+ value: input.buyerPaymentAccount ?? null,
526
+ isWritable: true,
527
+ },
528
+ escrowLongAccount: {
529
+ value: input.escrowLongAccount ?? null,
530
+ isWritable: true,
531
+ },
532
+ premiumVault: { value: input.premiumVault ?? null, isWritable: true },
533
+ buyer: { value: input.buyer ?? null, isWritable: true },
534
+ tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
535
+ associatedTokenProgram: {
536
+ value: input.associatedTokenProgram ?? null,
537
+ isWritable: false,
538
+ },
539
+ systemProgram: { value: input.systemProgram ?? null, isWritable: false },
540
+ };
541
+ const accounts = originalAccounts as Record<
542
+ keyof typeof originalAccounts,
543
+ ResolvedAccount
544
+ >;
545
+
546
+ // Original args.
547
+ const args = { ...input };
548
+
549
+ // Resolve default values.
550
+ if (!accounts.tokenProgram.value) {
551
+ accounts.tokenProgram.value =
552
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
553
+ }
554
+ if (!accounts.associatedTokenProgram.value) {
555
+ accounts.associatedTokenProgram.value =
556
+ "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" as Address<"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL">;
557
+ }
558
+ if (!accounts.systemProgram.value) {
559
+ accounts.systemProgram.value =
560
+ "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
561
+ }
562
+
563
+ const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
564
+ return Object.freeze({
565
+ accounts: [
566
+ getAccountMeta(accounts.optionPool),
567
+ getAccountMeta(accounts.optionAccount),
568
+ getAccountMeta(accounts.longMint),
569
+ getAccountMeta(accounts.underlyingMint),
570
+ getAccountMeta(accounts.marketData),
571
+ getAccountMeta(accounts.priceUpdate),
572
+ getAccountMeta(accounts.buyerPosition),
573
+ getAccountMeta(accounts.buyerOptionAccount),
574
+ getAccountMeta(accounts.buyerPaymentAccount),
575
+ getAccountMeta(accounts.escrowLongAccount),
576
+ getAccountMeta(accounts.premiumVault),
577
+ getAccountMeta(accounts.buyer),
578
+ getAccountMeta(accounts.tokenProgram),
579
+ getAccountMeta(accounts.associatedTokenProgram),
580
+ getAccountMeta(accounts.systemProgram),
581
+ ],
582
+ data: getBuyFromPoolInstructionDataEncoder().encode(
583
+ args as BuyFromPoolInstructionDataArgs,
584
+ ),
585
+ programAddress,
586
+ } as BuyFromPoolInstruction<
587
+ TProgramAddress,
588
+ TAccountOptionPool,
589
+ TAccountOptionAccount,
590
+ TAccountLongMint,
591
+ TAccountUnderlyingMint,
592
+ TAccountMarketData,
593
+ TAccountPriceUpdate,
594
+ TAccountBuyerPosition,
595
+ TAccountBuyerOptionAccount,
596
+ TAccountBuyerPaymentAccount,
597
+ TAccountEscrowLongAccount,
598
+ TAccountPremiumVault,
599
+ TAccountBuyer,
600
+ TAccountTokenProgram,
601
+ TAccountAssociatedTokenProgram,
602
+ TAccountSystemProgram
603
+ >);
604
+ }
605
+
606
+ export type ParsedBuyFromPoolInstruction<
607
+ TProgram extends string = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
608
+ TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
609
+ > = {
610
+ programAddress: Address<TProgram>;
611
+ accounts: {
612
+ /** The pool to buy from */
613
+ optionPool: TAccountMetas[0];
614
+ /**
615
+ * The option account (mut for state tracking updates)
616
+ * Must match option_pool.option_account to ensure correct buyer_position PDA
617
+ */
618
+ optionAccount: TAccountMetas[1];
619
+ /** The LONG token mint */
620
+ longMint: TAccountMetas[2];
621
+ /** Underlying token mint (for decimal handling) */
622
+ underlyingMint: TAccountMetas[3];
623
+ /** Market data account (provides risk-free rate and pyth_feed_id) */
624
+ marketData: TAccountMetas[4];
625
+ /** Pyth price update account for current underlying price */
626
+ priceUpdate: TAccountMetas[5];
627
+ /** Buyer's position account (created for tracking) */
628
+ buyerPosition: TAccountMetas[6];
629
+ /** Buyer's LONG token account (receives LONG tokens) */
630
+ buyerOptionAccount: TAccountMetas[7];
631
+ /** Buyer's payment account (source of premium) */
632
+ buyerPaymentAccount: TAccountMetas[8];
633
+ /** Pool's LONG escrow (source of LONG tokens) */
634
+ escrowLongAccount: TAccountMetas[9];
635
+ /** Pool's premium vault (receives premium) */
636
+ premiumVault: TAccountMetas[10];
637
+ buyer: TAccountMetas[11];
638
+ tokenProgram: TAccountMetas[12];
639
+ associatedTokenProgram: TAccountMetas[13];
640
+ systemProgram: TAccountMetas[14];
641
+ };
642
+ data: BuyFromPoolInstructionData;
643
+ };
644
+
645
+ export function parseBuyFromPoolInstruction<
646
+ TProgram extends string,
647
+ TAccountMetas extends readonly AccountMeta[],
648
+ >(
649
+ instruction: Instruction<TProgram> &
650
+ InstructionWithAccounts<TAccountMetas> &
651
+ InstructionWithData<ReadonlyUint8Array>,
652
+ ): ParsedBuyFromPoolInstruction<TProgram, TAccountMetas> {
653
+ if (instruction.accounts.length < 15) {
654
+ // TODO: Coded error.
655
+ throw new Error("Not enough accounts");
656
+ }
657
+ let accountIndex = 0;
658
+ const getNextAccount = () => {
659
+ const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
660
+ accountIndex += 1;
661
+ return accountMeta;
662
+ };
663
+ return {
664
+ programAddress: instruction.programAddress,
665
+ accounts: {
666
+ optionPool: getNextAccount(),
667
+ optionAccount: getNextAccount(),
668
+ longMint: getNextAccount(),
669
+ underlyingMint: getNextAccount(),
670
+ marketData: getNextAccount(),
671
+ priceUpdate: getNextAccount(),
672
+ buyerPosition: getNextAccount(),
673
+ buyerOptionAccount: getNextAccount(),
674
+ buyerPaymentAccount: getNextAccount(),
675
+ escrowLongAccount: getNextAccount(),
676
+ premiumVault: getNextAccount(),
677
+ buyer: getNextAccount(),
678
+ tokenProgram: getNextAccount(),
679
+ associatedTokenProgram: getNextAccount(),
680
+ systemProgram: getNextAccount(),
681
+ },
682
+ data: getBuyFromPoolInstructionDataDecoder().decode(instruction.data),
683
+ };
684
+ }