@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,540 @@
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
+ transformEncoder,
20
+ type AccountMeta,
21
+ type AccountSignerMeta,
22
+ type Address,
23
+ type FixedSizeCodec,
24
+ type FixedSizeDecoder,
25
+ type FixedSizeEncoder,
26
+ type Instruction,
27
+ type InstructionWithAccounts,
28
+ type InstructionWithData,
29
+ type ReadonlyAccount,
30
+ type ReadonlyUint8Array,
31
+ type TransactionSigner,
32
+ type WritableAccount,
33
+ type WritableSignerAccount,
34
+ } from "@solana/kit";
35
+ import { OPTION_PROGRAM_PROGRAM_ADDRESS } from "../programs";
36
+ import {
37
+ expectAddress,
38
+ getAccountMetaFactory,
39
+ type ResolvedAccount,
40
+ } from "../shared";
41
+
42
+ export const OPTION_EXERCISE_DISCRIMINATOR = new Uint8Array([
43
+ 43, 86, 237, 78, 235, 74, 131, 206,
44
+ ]);
45
+
46
+ export function getOptionExerciseDiscriminatorBytes() {
47
+ return fixEncoderSize(getBytesEncoder(), 8).encode(
48
+ OPTION_EXERCISE_DISCRIMINATOR,
49
+ );
50
+ }
51
+
52
+ export type OptionExerciseInstruction<
53
+ TProgram extends string = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
54
+ TAccountOptionAccount extends string | AccountMeta<string> = string,
55
+ TAccountPositionAccount extends string | AccountMeta<string> = string,
56
+ TAccountMarketData extends string | AccountMeta<string> = string,
57
+ TAccountUnderlyingMint extends string | AccountMeta<string> = string,
58
+ TAccountPriceUpdate extends string | AccountMeta<string> = string,
59
+ TAccountBuyerPaymentAccount extends string | AccountMeta<string> = string,
60
+ TAccountMakerCollateralAccount extends string | AccountMeta<string> = string,
61
+ TAccountEscrowState extends string | AccountMeta<string> = string,
62
+ TAccountEscrowTokenAccount extends string | AccountMeta<string> = string,
63
+ TAccountEscrowAuthority extends string | AccountMeta<string> = string,
64
+ TAccountBuyer extends string | AccountMeta<string> = string,
65
+ TAccountTokenProgram extends string | AccountMeta<string> =
66
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
67
+ TRemainingAccounts extends readonly AccountMeta<string>[] = [],
68
+ > = Instruction<TProgram> &
69
+ InstructionWithData<ReadonlyUint8Array> &
70
+ InstructionWithAccounts<
71
+ [
72
+ TAccountOptionAccount extends string
73
+ ? WritableAccount<TAccountOptionAccount>
74
+ : TAccountOptionAccount,
75
+ TAccountPositionAccount extends string
76
+ ? WritableAccount<TAccountPositionAccount>
77
+ : TAccountPositionAccount,
78
+ TAccountMarketData extends string
79
+ ? ReadonlyAccount<TAccountMarketData>
80
+ : TAccountMarketData,
81
+ TAccountUnderlyingMint extends string
82
+ ? ReadonlyAccount<TAccountUnderlyingMint>
83
+ : TAccountUnderlyingMint,
84
+ TAccountPriceUpdate extends string
85
+ ? ReadonlyAccount<TAccountPriceUpdate>
86
+ : TAccountPriceUpdate,
87
+ TAccountBuyerPaymentAccount extends string
88
+ ? WritableAccount<TAccountBuyerPaymentAccount>
89
+ : TAccountBuyerPaymentAccount,
90
+ TAccountMakerCollateralAccount extends string
91
+ ? WritableAccount<TAccountMakerCollateralAccount>
92
+ : TAccountMakerCollateralAccount,
93
+ TAccountEscrowState extends string
94
+ ? ReadonlyAccount<TAccountEscrowState>
95
+ : TAccountEscrowState,
96
+ TAccountEscrowTokenAccount extends string
97
+ ? WritableAccount<TAccountEscrowTokenAccount>
98
+ : TAccountEscrowTokenAccount,
99
+ TAccountEscrowAuthority extends string
100
+ ? ReadonlyAccount<TAccountEscrowAuthority>
101
+ : TAccountEscrowAuthority,
102
+ TAccountBuyer extends string
103
+ ? WritableSignerAccount<TAccountBuyer> &
104
+ AccountSignerMeta<TAccountBuyer>
105
+ : TAccountBuyer,
106
+ TAccountTokenProgram extends string
107
+ ? ReadonlyAccount<TAccountTokenProgram>
108
+ : TAccountTokenProgram,
109
+ ...TRemainingAccounts,
110
+ ]
111
+ >;
112
+
113
+ export type OptionExerciseInstructionData = {
114
+ discriminator: ReadonlyUint8Array;
115
+ };
116
+
117
+ export type OptionExerciseInstructionDataArgs = {};
118
+
119
+ export function getOptionExerciseInstructionDataEncoder(): FixedSizeEncoder<OptionExerciseInstructionDataArgs> {
120
+ return transformEncoder(
121
+ getStructEncoder([["discriminator", fixEncoderSize(getBytesEncoder(), 8)]]),
122
+ (value) => ({ ...value, discriminator: OPTION_EXERCISE_DISCRIMINATOR }),
123
+ );
124
+ }
125
+
126
+ export function getOptionExerciseInstructionDataDecoder(): FixedSizeDecoder<OptionExerciseInstructionData> {
127
+ return getStructDecoder([
128
+ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
129
+ ]);
130
+ }
131
+
132
+ export function getOptionExerciseInstructionDataCodec(): FixedSizeCodec<
133
+ OptionExerciseInstructionDataArgs,
134
+ OptionExerciseInstructionData
135
+ > {
136
+ return combineCodec(
137
+ getOptionExerciseInstructionDataEncoder(),
138
+ getOptionExerciseInstructionDataDecoder(),
139
+ );
140
+ }
141
+
142
+ export type OptionExerciseAsyncInput<
143
+ TAccountOptionAccount extends string = string,
144
+ TAccountPositionAccount extends string = string,
145
+ TAccountMarketData extends string = string,
146
+ TAccountUnderlyingMint extends string = string,
147
+ TAccountPriceUpdate extends string = string,
148
+ TAccountBuyerPaymentAccount extends string = string,
149
+ TAccountMakerCollateralAccount extends string = string,
150
+ TAccountEscrowState extends string = string,
151
+ TAccountEscrowTokenAccount extends string = string,
152
+ TAccountEscrowAuthority extends string = string,
153
+ TAccountBuyer extends string = string,
154
+ TAccountTokenProgram extends string = string,
155
+ > = {
156
+ optionAccount: Address<TAccountOptionAccount>;
157
+ positionAccount: Address<TAccountPositionAccount>;
158
+ /** Market data for risk-free rate (used in Black-Scholes premium calculation) */
159
+ marketData: Address<TAccountMarketData>;
160
+ /** Underlying token mint (for dynamic decimal handling - supports any SPL token) */
161
+ underlyingMint: Address<TAccountUnderlyingMint>;
162
+ /** Pyth price update account (ownership validated by Anchor) */
163
+ priceUpdate: Address<TAccountPriceUpdate>;
164
+ buyerPaymentAccount: Address<TAccountBuyerPaymentAccount>;
165
+ makerCollateralAccount: Address<TAccountMakerCollateralAccount>;
166
+ /**
167
+ * Escrow state PDA - validates maker/mint binding
168
+ * Validates escrow_state.maker matches option_account.authority (the option's maker)
169
+ */
170
+ escrowState: Address<TAccountEscrowState>;
171
+ /** Escrow token account holding collateral */
172
+ escrowTokenAccount: Address<TAccountEscrowTokenAccount>;
173
+ escrowAuthority?: Address<TAccountEscrowAuthority>;
174
+ buyer: TransactionSigner<TAccountBuyer>;
175
+ tokenProgram?: Address<TAccountTokenProgram>;
176
+ };
177
+
178
+ export async function getOptionExerciseInstructionAsync<
179
+ TAccountOptionAccount extends string,
180
+ TAccountPositionAccount extends string,
181
+ TAccountMarketData extends string,
182
+ TAccountUnderlyingMint extends string,
183
+ TAccountPriceUpdate extends string,
184
+ TAccountBuyerPaymentAccount extends string,
185
+ TAccountMakerCollateralAccount extends string,
186
+ TAccountEscrowState extends string,
187
+ TAccountEscrowTokenAccount extends string,
188
+ TAccountEscrowAuthority extends string,
189
+ TAccountBuyer extends string,
190
+ TAccountTokenProgram extends string,
191
+ TProgramAddress extends Address = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
192
+ >(
193
+ input: OptionExerciseAsyncInput<
194
+ TAccountOptionAccount,
195
+ TAccountPositionAccount,
196
+ TAccountMarketData,
197
+ TAccountUnderlyingMint,
198
+ TAccountPriceUpdate,
199
+ TAccountBuyerPaymentAccount,
200
+ TAccountMakerCollateralAccount,
201
+ TAccountEscrowState,
202
+ TAccountEscrowTokenAccount,
203
+ TAccountEscrowAuthority,
204
+ TAccountBuyer,
205
+ TAccountTokenProgram
206
+ >,
207
+ config?: { programAddress?: TProgramAddress },
208
+ ): Promise<
209
+ OptionExerciseInstruction<
210
+ TProgramAddress,
211
+ TAccountOptionAccount,
212
+ TAccountPositionAccount,
213
+ TAccountMarketData,
214
+ TAccountUnderlyingMint,
215
+ TAccountPriceUpdate,
216
+ TAccountBuyerPaymentAccount,
217
+ TAccountMakerCollateralAccount,
218
+ TAccountEscrowState,
219
+ TAccountEscrowTokenAccount,
220
+ TAccountEscrowAuthority,
221
+ TAccountBuyer,
222
+ TAccountTokenProgram
223
+ >
224
+ > {
225
+ // Program address.
226
+ const programAddress =
227
+ config?.programAddress ?? OPTION_PROGRAM_PROGRAM_ADDRESS;
228
+
229
+ // Original accounts.
230
+ const originalAccounts = {
231
+ optionAccount: { value: input.optionAccount ?? null, isWritable: true },
232
+ positionAccount: { value: input.positionAccount ?? null, isWritable: true },
233
+ marketData: { value: input.marketData ?? null, isWritable: false },
234
+ underlyingMint: { value: input.underlyingMint ?? null, isWritable: false },
235
+ priceUpdate: { value: input.priceUpdate ?? null, isWritable: false },
236
+ buyerPaymentAccount: {
237
+ value: input.buyerPaymentAccount ?? null,
238
+ isWritable: true,
239
+ },
240
+ makerCollateralAccount: {
241
+ value: input.makerCollateralAccount ?? null,
242
+ isWritable: true,
243
+ },
244
+ escrowState: { value: input.escrowState ?? null, isWritable: false },
245
+ escrowTokenAccount: {
246
+ value: input.escrowTokenAccount ?? null,
247
+ isWritable: true,
248
+ },
249
+ escrowAuthority: {
250
+ value: input.escrowAuthority ?? null,
251
+ isWritable: false,
252
+ },
253
+ buyer: { value: input.buyer ?? null, isWritable: true },
254
+ tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
255
+ };
256
+ const accounts = originalAccounts as Record<
257
+ keyof typeof originalAccounts,
258
+ ResolvedAccount
259
+ >;
260
+
261
+ // Resolve default values.
262
+ if (!accounts.escrowAuthority.value) {
263
+ accounts.escrowAuthority.value = await getProgramDerivedAddress({
264
+ programAddress,
265
+ seeds: [
266
+ getBytesEncoder().encode(
267
+ new Uint8Array([
268
+ 101, 115, 99, 114, 111, 119, 95, 97, 117, 116, 104, 111, 114, 105,
269
+ 116, 121, 95, 118, 50,
270
+ ]),
271
+ ),
272
+ getAddressEncoder().encode(expectAddress(accounts.escrowState.value)),
273
+ ],
274
+ });
275
+ }
276
+ if (!accounts.tokenProgram.value) {
277
+ accounts.tokenProgram.value =
278
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
279
+ }
280
+
281
+ const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
282
+ return Object.freeze({
283
+ accounts: [
284
+ getAccountMeta(accounts.optionAccount),
285
+ getAccountMeta(accounts.positionAccount),
286
+ getAccountMeta(accounts.marketData),
287
+ getAccountMeta(accounts.underlyingMint),
288
+ getAccountMeta(accounts.priceUpdate),
289
+ getAccountMeta(accounts.buyerPaymentAccount),
290
+ getAccountMeta(accounts.makerCollateralAccount),
291
+ getAccountMeta(accounts.escrowState),
292
+ getAccountMeta(accounts.escrowTokenAccount),
293
+ getAccountMeta(accounts.escrowAuthority),
294
+ getAccountMeta(accounts.buyer),
295
+ getAccountMeta(accounts.tokenProgram),
296
+ ],
297
+ data: getOptionExerciseInstructionDataEncoder().encode({}),
298
+ programAddress,
299
+ } as OptionExerciseInstruction<
300
+ TProgramAddress,
301
+ TAccountOptionAccount,
302
+ TAccountPositionAccount,
303
+ TAccountMarketData,
304
+ TAccountUnderlyingMint,
305
+ TAccountPriceUpdate,
306
+ TAccountBuyerPaymentAccount,
307
+ TAccountMakerCollateralAccount,
308
+ TAccountEscrowState,
309
+ TAccountEscrowTokenAccount,
310
+ TAccountEscrowAuthority,
311
+ TAccountBuyer,
312
+ TAccountTokenProgram
313
+ >);
314
+ }
315
+
316
+ export type OptionExerciseInput<
317
+ TAccountOptionAccount extends string = string,
318
+ TAccountPositionAccount extends string = string,
319
+ TAccountMarketData extends string = string,
320
+ TAccountUnderlyingMint extends string = string,
321
+ TAccountPriceUpdate extends string = string,
322
+ TAccountBuyerPaymentAccount extends string = string,
323
+ TAccountMakerCollateralAccount extends string = string,
324
+ TAccountEscrowState extends string = string,
325
+ TAccountEscrowTokenAccount extends string = string,
326
+ TAccountEscrowAuthority extends string = string,
327
+ TAccountBuyer extends string = string,
328
+ TAccountTokenProgram extends string = string,
329
+ > = {
330
+ optionAccount: Address<TAccountOptionAccount>;
331
+ positionAccount: Address<TAccountPositionAccount>;
332
+ /** Market data for risk-free rate (used in Black-Scholes premium calculation) */
333
+ marketData: Address<TAccountMarketData>;
334
+ /** Underlying token mint (for dynamic decimal handling - supports any SPL token) */
335
+ underlyingMint: Address<TAccountUnderlyingMint>;
336
+ /** Pyth price update account (ownership validated by Anchor) */
337
+ priceUpdate: Address<TAccountPriceUpdate>;
338
+ buyerPaymentAccount: Address<TAccountBuyerPaymentAccount>;
339
+ makerCollateralAccount: Address<TAccountMakerCollateralAccount>;
340
+ /**
341
+ * Escrow state PDA - validates maker/mint binding
342
+ * Validates escrow_state.maker matches option_account.authority (the option's maker)
343
+ */
344
+ escrowState: Address<TAccountEscrowState>;
345
+ /** Escrow token account holding collateral */
346
+ escrowTokenAccount: Address<TAccountEscrowTokenAccount>;
347
+ escrowAuthority: Address<TAccountEscrowAuthority>;
348
+ buyer: TransactionSigner<TAccountBuyer>;
349
+ tokenProgram?: Address<TAccountTokenProgram>;
350
+ };
351
+
352
+ export function getOptionExerciseInstruction<
353
+ TAccountOptionAccount extends string,
354
+ TAccountPositionAccount extends string,
355
+ TAccountMarketData extends string,
356
+ TAccountUnderlyingMint extends string,
357
+ TAccountPriceUpdate extends string,
358
+ TAccountBuyerPaymentAccount extends string,
359
+ TAccountMakerCollateralAccount extends string,
360
+ TAccountEscrowState extends string,
361
+ TAccountEscrowTokenAccount extends string,
362
+ TAccountEscrowAuthority extends string,
363
+ TAccountBuyer extends string,
364
+ TAccountTokenProgram extends string,
365
+ TProgramAddress extends Address = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
366
+ >(
367
+ input: OptionExerciseInput<
368
+ TAccountOptionAccount,
369
+ TAccountPositionAccount,
370
+ TAccountMarketData,
371
+ TAccountUnderlyingMint,
372
+ TAccountPriceUpdate,
373
+ TAccountBuyerPaymentAccount,
374
+ TAccountMakerCollateralAccount,
375
+ TAccountEscrowState,
376
+ TAccountEscrowTokenAccount,
377
+ TAccountEscrowAuthority,
378
+ TAccountBuyer,
379
+ TAccountTokenProgram
380
+ >,
381
+ config?: { programAddress?: TProgramAddress },
382
+ ): OptionExerciseInstruction<
383
+ TProgramAddress,
384
+ TAccountOptionAccount,
385
+ TAccountPositionAccount,
386
+ TAccountMarketData,
387
+ TAccountUnderlyingMint,
388
+ TAccountPriceUpdate,
389
+ TAccountBuyerPaymentAccount,
390
+ TAccountMakerCollateralAccount,
391
+ TAccountEscrowState,
392
+ TAccountEscrowTokenAccount,
393
+ TAccountEscrowAuthority,
394
+ TAccountBuyer,
395
+ TAccountTokenProgram
396
+ > {
397
+ // Program address.
398
+ const programAddress =
399
+ config?.programAddress ?? OPTION_PROGRAM_PROGRAM_ADDRESS;
400
+
401
+ // Original accounts.
402
+ const originalAccounts = {
403
+ optionAccount: { value: input.optionAccount ?? null, isWritable: true },
404
+ positionAccount: { value: input.positionAccount ?? null, isWritable: true },
405
+ marketData: { value: input.marketData ?? null, isWritable: false },
406
+ underlyingMint: { value: input.underlyingMint ?? null, isWritable: false },
407
+ priceUpdate: { value: input.priceUpdate ?? null, isWritable: false },
408
+ buyerPaymentAccount: {
409
+ value: input.buyerPaymentAccount ?? null,
410
+ isWritable: true,
411
+ },
412
+ makerCollateralAccount: {
413
+ value: input.makerCollateralAccount ?? null,
414
+ isWritable: true,
415
+ },
416
+ escrowState: { value: input.escrowState ?? null, isWritable: false },
417
+ escrowTokenAccount: {
418
+ value: input.escrowTokenAccount ?? null,
419
+ isWritable: true,
420
+ },
421
+ escrowAuthority: {
422
+ value: input.escrowAuthority ?? null,
423
+ isWritable: false,
424
+ },
425
+ buyer: { value: input.buyer ?? null, isWritable: true },
426
+ tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
427
+ };
428
+ const accounts = originalAccounts as Record<
429
+ keyof typeof originalAccounts,
430
+ ResolvedAccount
431
+ >;
432
+
433
+ // Resolve default values.
434
+ if (!accounts.tokenProgram.value) {
435
+ accounts.tokenProgram.value =
436
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
437
+ }
438
+
439
+ const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
440
+ return Object.freeze({
441
+ accounts: [
442
+ getAccountMeta(accounts.optionAccount),
443
+ getAccountMeta(accounts.positionAccount),
444
+ getAccountMeta(accounts.marketData),
445
+ getAccountMeta(accounts.underlyingMint),
446
+ getAccountMeta(accounts.priceUpdate),
447
+ getAccountMeta(accounts.buyerPaymentAccount),
448
+ getAccountMeta(accounts.makerCollateralAccount),
449
+ getAccountMeta(accounts.escrowState),
450
+ getAccountMeta(accounts.escrowTokenAccount),
451
+ getAccountMeta(accounts.escrowAuthority),
452
+ getAccountMeta(accounts.buyer),
453
+ getAccountMeta(accounts.tokenProgram),
454
+ ],
455
+ data: getOptionExerciseInstructionDataEncoder().encode({}),
456
+ programAddress,
457
+ } as OptionExerciseInstruction<
458
+ TProgramAddress,
459
+ TAccountOptionAccount,
460
+ TAccountPositionAccount,
461
+ TAccountMarketData,
462
+ TAccountUnderlyingMint,
463
+ TAccountPriceUpdate,
464
+ TAccountBuyerPaymentAccount,
465
+ TAccountMakerCollateralAccount,
466
+ TAccountEscrowState,
467
+ TAccountEscrowTokenAccount,
468
+ TAccountEscrowAuthority,
469
+ TAccountBuyer,
470
+ TAccountTokenProgram
471
+ >);
472
+ }
473
+
474
+ export type ParsedOptionExerciseInstruction<
475
+ TProgram extends string = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
476
+ TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
477
+ > = {
478
+ programAddress: Address<TProgram>;
479
+ accounts: {
480
+ optionAccount: TAccountMetas[0];
481
+ positionAccount: TAccountMetas[1];
482
+ /** Market data for risk-free rate (used in Black-Scholes premium calculation) */
483
+ marketData: TAccountMetas[2];
484
+ /** Underlying token mint (for dynamic decimal handling - supports any SPL token) */
485
+ underlyingMint: TAccountMetas[3];
486
+ /** Pyth price update account (ownership validated by Anchor) */
487
+ priceUpdate: TAccountMetas[4];
488
+ buyerPaymentAccount: TAccountMetas[5];
489
+ makerCollateralAccount: TAccountMetas[6];
490
+ /**
491
+ * Escrow state PDA - validates maker/mint binding
492
+ * Validates escrow_state.maker matches option_account.authority (the option's maker)
493
+ */
494
+ escrowState: TAccountMetas[7];
495
+ /** Escrow token account holding collateral */
496
+ escrowTokenAccount: TAccountMetas[8];
497
+ escrowAuthority: TAccountMetas[9];
498
+ buyer: TAccountMetas[10];
499
+ tokenProgram: TAccountMetas[11];
500
+ };
501
+ data: OptionExerciseInstructionData;
502
+ };
503
+
504
+ export function parseOptionExerciseInstruction<
505
+ TProgram extends string,
506
+ TAccountMetas extends readonly AccountMeta[],
507
+ >(
508
+ instruction: Instruction<TProgram> &
509
+ InstructionWithAccounts<TAccountMetas> &
510
+ InstructionWithData<ReadonlyUint8Array>,
511
+ ): ParsedOptionExerciseInstruction<TProgram, TAccountMetas> {
512
+ if (instruction.accounts.length < 12) {
513
+ // TODO: Coded error.
514
+ throw new Error("Not enough accounts");
515
+ }
516
+ let accountIndex = 0;
517
+ const getNextAccount = () => {
518
+ const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
519
+ accountIndex += 1;
520
+ return accountMeta;
521
+ };
522
+ return {
523
+ programAddress: instruction.programAddress,
524
+ accounts: {
525
+ optionAccount: getNextAccount(),
526
+ positionAccount: getNextAccount(),
527
+ marketData: getNextAccount(),
528
+ underlyingMint: getNextAccount(),
529
+ priceUpdate: getNextAccount(),
530
+ buyerPaymentAccount: getNextAccount(),
531
+ makerCollateralAccount: getNextAccount(),
532
+ escrowState: getNextAccount(),
533
+ escrowTokenAccount: getNextAccount(),
534
+ escrowAuthority: getNextAccount(),
535
+ buyer: getNextAccount(),
536
+ tokenProgram: getNextAccount(),
537
+ },
538
+ data: getOptionExerciseInstructionDataDecoder().decode(instruction.data),
539
+ };
540
+ }