@epicentral/sos-sdk 0.9.0-beta

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