@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,762 @@
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 CLOSE_LONG_TO_POOL_DISCRIMINATOR = new Uint8Array([
45
+ 72, 117, 94, 238, 111, 113, 243, 91,
46
+ ]);
47
+
48
+ export function getCloseLongToPoolDiscriminatorBytes() {
49
+ return fixEncoderSize(getBytesEncoder(), 8).encode(
50
+ CLOSE_LONG_TO_POOL_DISCRIMINATOR,
51
+ );
52
+ }
53
+
54
+ export type CloseLongToPoolInstruction<
55
+ TProgram extends string = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
56
+ TAccountOptionPool extends string | AccountMeta<string> = string,
57
+ TAccountOptionAccount extends string | AccountMeta<string> = string,
58
+ TAccountCollateralPool extends string | AccountMeta<string> = string,
59
+ TAccountUnderlyingMint extends string | AccountMeta<string> = string,
60
+ TAccountLongMint extends string | AccountMeta<string> = string,
61
+ TAccountEscrowLongAccount extends string | AccountMeta<string> = string,
62
+ TAccountPremiumVault extends string | AccountMeta<string> = string,
63
+ TAccountMarketData extends string | AccountMeta<string> = string,
64
+ TAccountSwitchboardQueue extends string | AccountMeta<string> = string,
65
+ TAccountSlotHashesSysvar extends string | AccountMeta<string> =
66
+ "SysvarS1otHashes111111111111111111111111111",
67
+ TAccountInstructionsSysvar extends string | AccountMeta<string> =
68
+ "Sysvar1nstructions1111111111111111111111111",
69
+ TAccountBuyerPosition extends string | AccountMeta<string> = string,
70
+ TAccountBuyerLongAccount extends string | AccountMeta<string> = string,
71
+ TAccountBuyerPayoutAccount extends string | AccountMeta<string> = string,
72
+ TAccountCollateralVault extends string | AccountMeta<string> = string,
73
+ TAccountBuyer extends string | AccountMeta<string> = string,
74
+ TAccountTokenProgram extends string | AccountMeta<string> =
75
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
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
+ TAccountCollateralPool extends string
90
+ ? WritableAccount<TAccountCollateralPool>
91
+ : TAccountCollateralPool,
92
+ TAccountUnderlyingMint extends string
93
+ ? ReadonlyAccount<TAccountUnderlyingMint>
94
+ : TAccountUnderlyingMint,
95
+ TAccountLongMint extends string
96
+ ? WritableAccount<TAccountLongMint>
97
+ : TAccountLongMint,
98
+ TAccountEscrowLongAccount extends string
99
+ ? WritableAccount<TAccountEscrowLongAccount>
100
+ : TAccountEscrowLongAccount,
101
+ TAccountPremiumVault extends string
102
+ ? WritableAccount<TAccountPremiumVault>
103
+ : TAccountPremiumVault,
104
+ TAccountMarketData extends string
105
+ ? ReadonlyAccount<TAccountMarketData>
106
+ : TAccountMarketData,
107
+ TAccountSwitchboardQueue extends string
108
+ ? ReadonlyAccount<TAccountSwitchboardQueue>
109
+ : TAccountSwitchboardQueue,
110
+ TAccountSlotHashesSysvar extends string
111
+ ? ReadonlyAccount<TAccountSlotHashesSysvar>
112
+ : TAccountSlotHashesSysvar,
113
+ TAccountInstructionsSysvar extends string
114
+ ? ReadonlyAccount<TAccountInstructionsSysvar>
115
+ : TAccountInstructionsSysvar,
116
+ TAccountBuyerPosition extends string
117
+ ? WritableAccount<TAccountBuyerPosition>
118
+ : TAccountBuyerPosition,
119
+ TAccountBuyerLongAccount extends string
120
+ ? WritableAccount<TAccountBuyerLongAccount>
121
+ : TAccountBuyerLongAccount,
122
+ TAccountBuyerPayoutAccount extends string
123
+ ? WritableAccount<TAccountBuyerPayoutAccount>
124
+ : TAccountBuyerPayoutAccount,
125
+ TAccountCollateralVault extends string
126
+ ? WritableAccount<TAccountCollateralVault>
127
+ : TAccountCollateralVault,
128
+ TAccountBuyer extends string
129
+ ? WritableSignerAccount<TAccountBuyer> &
130
+ AccountSignerMeta<TAccountBuyer>
131
+ : TAccountBuyer,
132
+ TAccountTokenProgram extends string
133
+ ? ReadonlyAccount<TAccountTokenProgram>
134
+ : TAccountTokenProgram,
135
+ TAccountSystemProgram extends string
136
+ ? ReadonlyAccount<TAccountSystemProgram>
137
+ : TAccountSystemProgram,
138
+ ...TRemainingAccounts,
139
+ ]
140
+ >;
141
+
142
+ export type CloseLongToPoolInstructionData = {
143
+ discriminator: ReadonlyUint8Array;
144
+ quantity: bigint;
145
+ minPayoutAmount: bigint;
146
+ };
147
+
148
+ export type CloseLongToPoolInstructionDataArgs = {
149
+ quantity: number | bigint;
150
+ minPayoutAmount: number | bigint;
151
+ };
152
+
153
+ export function getCloseLongToPoolInstructionDataEncoder(): FixedSizeEncoder<CloseLongToPoolInstructionDataArgs> {
154
+ return transformEncoder(
155
+ getStructEncoder([
156
+ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
157
+ ["quantity", getU64Encoder()],
158
+ ["minPayoutAmount", getU64Encoder()],
159
+ ]),
160
+ (value) => ({ ...value, discriminator: CLOSE_LONG_TO_POOL_DISCRIMINATOR }),
161
+ );
162
+ }
163
+
164
+ export function getCloseLongToPoolInstructionDataDecoder(): FixedSizeDecoder<CloseLongToPoolInstructionData> {
165
+ return getStructDecoder([
166
+ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
167
+ ["quantity", getU64Decoder()],
168
+ ["minPayoutAmount", getU64Decoder()],
169
+ ]);
170
+ }
171
+
172
+ export function getCloseLongToPoolInstructionDataCodec(): FixedSizeCodec<
173
+ CloseLongToPoolInstructionDataArgs,
174
+ CloseLongToPoolInstructionData
175
+ > {
176
+ return combineCodec(
177
+ getCloseLongToPoolInstructionDataEncoder(),
178
+ getCloseLongToPoolInstructionDataDecoder(),
179
+ );
180
+ }
181
+
182
+ export type CloseLongToPoolAsyncInput<
183
+ TAccountOptionPool extends string = string,
184
+ TAccountOptionAccount extends string = string,
185
+ TAccountCollateralPool extends string = string,
186
+ TAccountUnderlyingMint extends string = string,
187
+ TAccountLongMint extends string = string,
188
+ TAccountEscrowLongAccount extends string = string,
189
+ TAccountPremiumVault extends string = string,
190
+ TAccountMarketData extends string = string,
191
+ TAccountSwitchboardQueue extends string = string,
192
+ TAccountSlotHashesSysvar extends string = string,
193
+ TAccountInstructionsSysvar extends string = string,
194
+ TAccountBuyerPosition extends string = string,
195
+ TAccountBuyerLongAccount extends string = string,
196
+ TAccountBuyerPayoutAccount extends string = string,
197
+ TAccountCollateralVault extends string = string,
198
+ TAccountBuyer extends string = string,
199
+ TAccountTokenProgram extends string = string,
200
+ TAccountSystemProgram extends string = string,
201
+ > = {
202
+ /** The option pool */
203
+ optionPool: Address<TAccountOptionPool>;
204
+ /** The option account */
205
+ optionAccount: Address<TAccountOptionAccount>;
206
+ /** Collateral pool (source of payout) */
207
+ collateralPool?: Address<TAccountCollateralPool>;
208
+ /** Underlying token mint (for decimal handling) */
209
+ underlyingMint: Address<TAccountUnderlyingMint>;
210
+ /** LONG token mint (for transfer validation) */
211
+ longMint: Address<TAccountLongMint>;
212
+ /** Pool LONG escrow (destination when buyer closes back to pool) */
213
+ escrowLongAccount: Address<TAccountEscrowLongAccount>;
214
+ /** Premium vault (first source of buyer payout) */
215
+ premiumVault: Address<TAccountPremiumVault>;
216
+ /** Market data account (provides risk-free rate and switchboard_feed_id) */
217
+ marketData: Address<TAccountMarketData>;
218
+ switchboardQueue: Address<TAccountSwitchboardQueue>;
219
+ slotHashesSysvar?: Address<TAccountSlotHashesSysvar>;
220
+ instructionsSysvar?: Address<TAccountInstructionsSysvar>;
221
+ /** Buyer's position account */
222
+ buyerPosition?: Address<TAccountBuyerPosition>;
223
+ /** Buyer's LONG token account (source of tokens to return) */
224
+ buyerLongAccount: Address<TAccountBuyerLongAccount>;
225
+ /** Buyer's underlying token account (receives payout) */
226
+ buyerPayoutAccount: Address<TAccountBuyerPayoutAccount>;
227
+ /** Collateral vault (source of payout) */
228
+ collateralVault: Address<TAccountCollateralVault>;
229
+ buyer: TransactionSigner<TAccountBuyer>;
230
+ tokenProgram?: Address<TAccountTokenProgram>;
231
+ systemProgram?: Address<TAccountSystemProgram>;
232
+ quantity: CloseLongToPoolInstructionDataArgs["quantity"];
233
+ minPayoutAmount: CloseLongToPoolInstructionDataArgs["minPayoutAmount"];
234
+ };
235
+
236
+ export async function getCloseLongToPoolInstructionAsync<
237
+ TAccountOptionPool extends string,
238
+ TAccountOptionAccount extends string,
239
+ TAccountCollateralPool extends string,
240
+ TAccountUnderlyingMint extends string,
241
+ TAccountLongMint extends string,
242
+ TAccountEscrowLongAccount extends string,
243
+ TAccountPremiumVault extends string,
244
+ TAccountMarketData extends string,
245
+ TAccountSwitchboardQueue extends string,
246
+ TAccountSlotHashesSysvar extends string,
247
+ TAccountInstructionsSysvar extends string,
248
+ TAccountBuyerPosition extends string,
249
+ TAccountBuyerLongAccount extends string,
250
+ TAccountBuyerPayoutAccount extends string,
251
+ TAccountCollateralVault extends string,
252
+ TAccountBuyer extends string,
253
+ TAccountTokenProgram extends string,
254
+ TAccountSystemProgram extends string,
255
+ TProgramAddress extends Address = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
256
+ >(
257
+ input: CloseLongToPoolAsyncInput<
258
+ TAccountOptionPool,
259
+ TAccountOptionAccount,
260
+ TAccountCollateralPool,
261
+ TAccountUnderlyingMint,
262
+ TAccountLongMint,
263
+ TAccountEscrowLongAccount,
264
+ TAccountPremiumVault,
265
+ TAccountMarketData,
266
+ TAccountSwitchboardQueue,
267
+ TAccountSlotHashesSysvar,
268
+ TAccountInstructionsSysvar,
269
+ TAccountBuyerPosition,
270
+ TAccountBuyerLongAccount,
271
+ TAccountBuyerPayoutAccount,
272
+ TAccountCollateralVault,
273
+ TAccountBuyer,
274
+ TAccountTokenProgram,
275
+ TAccountSystemProgram
276
+ >,
277
+ config?: { programAddress?: TProgramAddress },
278
+ ): Promise<
279
+ CloseLongToPoolInstruction<
280
+ TProgramAddress,
281
+ TAccountOptionPool,
282
+ TAccountOptionAccount,
283
+ TAccountCollateralPool,
284
+ TAccountUnderlyingMint,
285
+ TAccountLongMint,
286
+ TAccountEscrowLongAccount,
287
+ TAccountPremiumVault,
288
+ TAccountMarketData,
289
+ TAccountSwitchboardQueue,
290
+ TAccountSlotHashesSysvar,
291
+ TAccountInstructionsSysvar,
292
+ TAccountBuyerPosition,
293
+ TAccountBuyerLongAccount,
294
+ TAccountBuyerPayoutAccount,
295
+ TAccountCollateralVault,
296
+ TAccountBuyer,
297
+ TAccountTokenProgram,
298
+ TAccountSystemProgram
299
+ >
300
+ > {
301
+ // Program address.
302
+ const programAddress =
303
+ config?.programAddress ?? OPTION_PROGRAM_PROGRAM_ADDRESS;
304
+
305
+ // Original accounts.
306
+ const originalAccounts = {
307
+ optionPool: { value: input.optionPool ?? null, isWritable: true },
308
+ optionAccount: { value: input.optionAccount ?? null, isWritable: true },
309
+ collateralPool: { value: input.collateralPool ?? null, isWritable: true },
310
+ underlyingMint: { value: input.underlyingMint ?? null, isWritable: false },
311
+ longMint: { value: input.longMint ?? null, isWritable: true },
312
+ escrowLongAccount: {
313
+ value: input.escrowLongAccount ?? null,
314
+ isWritable: true,
315
+ },
316
+ premiumVault: { value: input.premiumVault ?? null, isWritable: true },
317
+ marketData: { value: input.marketData ?? null, isWritable: false },
318
+ switchboardQueue: {
319
+ value: input.switchboardQueue ?? null,
320
+ isWritable: false,
321
+ },
322
+ slotHashesSysvar: {
323
+ value: input.slotHashesSysvar ?? null,
324
+ isWritable: false,
325
+ },
326
+ instructionsSysvar: {
327
+ value: input.instructionsSysvar ?? null,
328
+ isWritable: false,
329
+ },
330
+ buyerPosition: { value: input.buyerPosition ?? null, isWritable: true },
331
+ buyerLongAccount: {
332
+ value: input.buyerLongAccount ?? null,
333
+ isWritable: true,
334
+ },
335
+ buyerPayoutAccount: {
336
+ value: input.buyerPayoutAccount ?? null,
337
+ isWritable: true,
338
+ },
339
+ collateralVault: { value: input.collateralVault ?? null, isWritable: true },
340
+ buyer: { value: input.buyer ?? null, isWritable: true },
341
+ tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
342
+ systemProgram: { value: input.systemProgram ?? null, isWritable: false },
343
+ };
344
+ const accounts = originalAccounts as Record<
345
+ keyof typeof originalAccounts,
346
+ ResolvedAccount
347
+ >;
348
+
349
+ // Original args.
350
+ const args = { ...input };
351
+
352
+ // Resolve default values.
353
+ if (!accounts.collateralPool.value) {
354
+ accounts.collateralPool.value = await getProgramDerivedAddress({
355
+ programAddress,
356
+ seeds: [
357
+ getBytesEncoder().encode(
358
+ new Uint8Array([
359
+ 99, 111, 108, 108, 97, 116, 101, 114, 97, 108, 95, 112, 111, 111,
360
+ 108,
361
+ ]),
362
+ ),
363
+ getAddressEncoder().encode(expectAddress(accounts.optionAccount.value)),
364
+ ],
365
+ });
366
+ }
367
+ if (!accounts.slotHashesSysvar.value) {
368
+ accounts.slotHashesSysvar.value =
369
+ "SysvarS1otHashes111111111111111111111111111" as Address<"SysvarS1otHashes111111111111111111111111111">;
370
+ }
371
+ if (!accounts.instructionsSysvar.value) {
372
+ accounts.instructionsSysvar.value =
373
+ "Sysvar1nstructions1111111111111111111111111" as Address<"Sysvar1nstructions1111111111111111111111111">;
374
+ }
375
+ if (!accounts.buyerPosition.value) {
376
+ accounts.buyerPosition.value = await getProgramDerivedAddress({
377
+ programAddress,
378
+ seeds: [
379
+ getBytesEncoder().encode(
380
+ new Uint8Array([112, 111, 115, 105, 116, 105, 111, 110]),
381
+ ),
382
+ getAddressEncoder().encode(expectAddress(accounts.buyer.value)),
383
+ getAddressEncoder().encode(expectAddress(accounts.optionAccount.value)),
384
+ ],
385
+ });
386
+ }
387
+ if (!accounts.tokenProgram.value) {
388
+ accounts.tokenProgram.value =
389
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
390
+ }
391
+ if (!accounts.systemProgram.value) {
392
+ accounts.systemProgram.value =
393
+ "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
394
+ }
395
+
396
+ const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
397
+ return Object.freeze({
398
+ accounts: [
399
+ getAccountMeta(accounts.optionPool),
400
+ getAccountMeta(accounts.optionAccount),
401
+ getAccountMeta(accounts.collateralPool),
402
+ getAccountMeta(accounts.underlyingMint),
403
+ getAccountMeta(accounts.longMint),
404
+ getAccountMeta(accounts.escrowLongAccount),
405
+ getAccountMeta(accounts.premiumVault),
406
+ getAccountMeta(accounts.marketData),
407
+ getAccountMeta(accounts.switchboardQueue),
408
+ getAccountMeta(accounts.slotHashesSysvar),
409
+ getAccountMeta(accounts.instructionsSysvar),
410
+ getAccountMeta(accounts.buyerPosition),
411
+ getAccountMeta(accounts.buyerLongAccount),
412
+ getAccountMeta(accounts.buyerPayoutAccount),
413
+ getAccountMeta(accounts.collateralVault),
414
+ getAccountMeta(accounts.buyer),
415
+ getAccountMeta(accounts.tokenProgram),
416
+ getAccountMeta(accounts.systemProgram),
417
+ ],
418
+ data: getCloseLongToPoolInstructionDataEncoder().encode(
419
+ args as CloseLongToPoolInstructionDataArgs,
420
+ ),
421
+ programAddress,
422
+ } as CloseLongToPoolInstruction<
423
+ TProgramAddress,
424
+ TAccountOptionPool,
425
+ TAccountOptionAccount,
426
+ TAccountCollateralPool,
427
+ TAccountUnderlyingMint,
428
+ TAccountLongMint,
429
+ TAccountEscrowLongAccount,
430
+ TAccountPremiumVault,
431
+ TAccountMarketData,
432
+ TAccountSwitchboardQueue,
433
+ TAccountSlotHashesSysvar,
434
+ TAccountInstructionsSysvar,
435
+ TAccountBuyerPosition,
436
+ TAccountBuyerLongAccount,
437
+ TAccountBuyerPayoutAccount,
438
+ TAccountCollateralVault,
439
+ TAccountBuyer,
440
+ TAccountTokenProgram,
441
+ TAccountSystemProgram
442
+ >);
443
+ }
444
+
445
+ export type CloseLongToPoolInput<
446
+ TAccountOptionPool extends string = string,
447
+ TAccountOptionAccount extends string = string,
448
+ TAccountCollateralPool extends string = string,
449
+ TAccountUnderlyingMint extends string = string,
450
+ TAccountLongMint extends string = string,
451
+ TAccountEscrowLongAccount extends string = string,
452
+ TAccountPremiumVault extends string = string,
453
+ TAccountMarketData extends string = string,
454
+ TAccountSwitchboardQueue extends string = string,
455
+ TAccountSlotHashesSysvar extends string = string,
456
+ TAccountInstructionsSysvar extends string = string,
457
+ TAccountBuyerPosition extends string = string,
458
+ TAccountBuyerLongAccount extends string = string,
459
+ TAccountBuyerPayoutAccount extends string = string,
460
+ TAccountCollateralVault extends string = string,
461
+ TAccountBuyer extends string = string,
462
+ TAccountTokenProgram extends string = string,
463
+ TAccountSystemProgram extends string = string,
464
+ > = {
465
+ /** The option pool */
466
+ optionPool: Address<TAccountOptionPool>;
467
+ /** The option account */
468
+ optionAccount: Address<TAccountOptionAccount>;
469
+ /** Collateral pool (source of payout) */
470
+ collateralPool: Address<TAccountCollateralPool>;
471
+ /** Underlying token mint (for decimal handling) */
472
+ underlyingMint: Address<TAccountUnderlyingMint>;
473
+ /** LONG token mint (for transfer validation) */
474
+ longMint: Address<TAccountLongMint>;
475
+ /** Pool LONG escrow (destination when buyer closes back to pool) */
476
+ escrowLongAccount: Address<TAccountEscrowLongAccount>;
477
+ /** Premium vault (first source of buyer payout) */
478
+ premiumVault: Address<TAccountPremiumVault>;
479
+ /** Market data account (provides risk-free rate and switchboard_feed_id) */
480
+ marketData: Address<TAccountMarketData>;
481
+ switchboardQueue: Address<TAccountSwitchboardQueue>;
482
+ slotHashesSysvar?: Address<TAccountSlotHashesSysvar>;
483
+ instructionsSysvar?: Address<TAccountInstructionsSysvar>;
484
+ /** Buyer's position account */
485
+ buyerPosition: Address<TAccountBuyerPosition>;
486
+ /** Buyer's LONG token account (source of tokens to return) */
487
+ buyerLongAccount: Address<TAccountBuyerLongAccount>;
488
+ /** Buyer's underlying token account (receives payout) */
489
+ buyerPayoutAccount: Address<TAccountBuyerPayoutAccount>;
490
+ /** Collateral vault (source of payout) */
491
+ collateralVault: Address<TAccountCollateralVault>;
492
+ buyer: TransactionSigner<TAccountBuyer>;
493
+ tokenProgram?: Address<TAccountTokenProgram>;
494
+ systemProgram?: Address<TAccountSystemProgram>;
495
+ quantity: CloseLongToPoolInstructionDataArgs["quantity"];
496
+ minPayoutAmount: CloseLongToPoolInstructionDataArgs["minPayoutAmount"];
497
+ };
498
+
499
+ export function getCloseLongToPoolInstruction<
500
+ TAccountOptionPool extends string,
501
+ TAccountOptionAccount extends string,
502
+ TAccountCollateralPool extends string,
503
+ TAccountUnderlyingMint extends string,
504
+ TAccountLongMint extends string,
505
+ TAccountEscrowLongAccount extends string,
506
+ TAccountPremiumVault extends string,
507
+ TAccountMarketData extends string,
508
+ TAccountSwitchboardQueue extends string,
509
+ TAccountSlotHashesSysvar extends string,
510
+ TAccountInstructionsSysvar extends string,
511
+ TAccountBuyerPosition extends string,
512
+ TAccountBuyerLongAccount extends string,
513
+ TAccountBuyerPayoutAccount extends string,
514
+ TAccountCollateralVault extends string,
515
+ TAccountBuyer extends string,
516
+ TAccountTokenProgram extends string,
517
+ TAccountSystemProgram extends string,
518
+ TProgramAddress extends Address = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
519
+ >(
520
+ input: CloseLongToPoolInput<
521
+ TAccountOptionPool,
522
+ TAccountOptionAccount,
523
+ TAccountCollateralPool,
524
+ TAccountUnderlyingMint,
525
+ TAccountLongMint,
526
+ TAccountEscrowLongAccount,
527
+ TAccountPremiumVault,
528
+ TAccountMarketData,
529
+ TAccountSwitchboardQueue,
530
+ TAccountSlotHashesSysvar,
531
+ TAccountInstructionsSysvar,
532
+ TAccountBuyerPosition,
533
+ TAccountBuyerLongAccount,
534
+ TAccountBuyerPayoutAccount,
535
+ TAccountCollateralVault,
536
+ TAccountBuyer,
537
+ TAccountTokenProgram,
538
+ TAccountSystemProgram
539
+ >,
540
+ config?: { programAddress?: TProgramAddress },
541
+ ): CloseLongToPoolInstruction<
542
+ TProgramAddress,
543
+ TAccountOptionPool,
544
+ TAccountOptionAccount,
545
+ TAccountCollateralPool,
546
+ TAccountUnderlyingMint,
547
+ TAccountLongMint,
548
+ TAccountEscrowLongAccount,
549
+ TAccountPremiumVault,
550
+ TAccountMarketData,
551
+ TAccountSwitchboardQueue,
552
+ TAccountSlotHashesSysvar,
553
+ TAccountInstructionsSysvar,
554
+ TAccountBuyerPosition,
555
+ TAccountBuyerLongAccount,
556
+ TAccountBuyerPayoutAccount,
557
+ TAccountCollateralVault,
558
+ TAccountBuyer,
559
+ TAccountTokenProgram,
560
+ TAccountSystemProgram
561
+ > {
562
+ // Program address.
563
+ const programAddress =
564
+ config?.programAddress ?? OPTION_PROGRAM_PROGRAM_ADDRESS;
565
+
566
+ // Original accounts.
567
+ const originalAccounts = {
568
+ optionPool: { value: input.optionPool ?? null, isWritable: true },
569
+ optionAccount: { value: input.optionAccount ?? null, isWritable: true },
570
+ collateralPool: { value: input.collateralPool ?? null, isWritable: true },
571
+ underlyingMint: { value: input.underlyingMint ?? null, isWritable: false },
572
+ longMint: { value: input.longMint ?? null, isWritable: true },
573
+ escrowLongAccount: {
574
+ value: input.escrowLongAccount ?? null,
575
+ isWritable: true,
576
+ },
577
+ premiumVault: { value: input.premiumVault ?? null, isWritable: true },
578
+ marketData: { value: input.marketData ?? null, isWritable: false },
579
+ switchboardQueue: {
580
+ value: input.switchboardQueue ?? null,
581
+ isWritable: false,
582
+ },
583
+ slotHashesSysvar: {
584
+ value: input.slotHashesSysvar ?? null,
585
+ isWritable: false,
586
+ },
587
+ instructionsSysvar: {
588
+ value: input.instructionsSysvar ?? null,
589
+ isWritable: false,
590
+ },
591
+ buyerPosition: { value: input.buyerPosition ?? null, isWritable: true },
592
+ buyerLongAccount: {
593
+ value: input.buyerLongAccount ?? null,
594
+ isWritable: true,
595
+ },
596
+ buyerPayoutAccount: {
597
+ value: input.buyerPayoutAccount ?? null,
598
+ isWritable: true,
599
+ },
600
+ collateralVault: { value: input.collateralVault ?? null, isWritable: true },
601
+ buyer: { value: input.buyer ?? null, isWritable: true },
602
+ tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
603
+ systemProgram: { value: input.systemProgram ?? null, isWritable: false },
604
+ };
605
+ const accounts = originalAccounts as Record<
606
+ keyof typeof originalAccounts,
607
+ ResolvedAccount
608
+ >;
609
+
610
+ // Original args.
611
+ const args = { ...input };
612
+
613
+ // Resolve default values.
614
+ if (!accounts.slotHashesSysvar.value) {
615
+ accounts.slotHashesSysvar.value =
616
+ "SysvarS1otHashes111111111111111111111111111" as Address<"SysvarS1otHashes111111111111111111111111111">;
617
+ }
618
+ if (!accounts.instructionsSysvar.value) {
619
+ accounts.instructionsSysvar.value =
620
+ "Sysvar1nstructions1111111111111111111111111" as Address<"Sysvar1nstructions1111111111111111111111111">;
621
+ }
622
+ if (!accounts.tokenProgram.value) {
623
+ accounts.tokenProgram.value =
624
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
625
+ }
626
+ if (!accounts.systemProgram.value) {
627
+ accounts.systemProgram.value =
628
+ "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
629
+ }
630
+
631
+ const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
632
+ return Object.freeze({
633
+ accounts: [
634
+ getAccountMeta(accounts.optionPool),
635
+ getAccountMeta(accounts.optionAccount),
636
+ getAccountMeta(accounts.collateralPool),
637
+ getAccountMeta(accounts.underlyingMint),
638
+ getAccountMeta(accounts.longMint),
639
+ getAccountMeta(accounts.escrowLongAccount),
640
+ getAccountMeta(accounts.premiumVault),
641
+ getAccountMeta(accounts.marketData),
642
+ getAccountMeta(accounts.switchboardQueue),
643
+ getAccountMeta(accounts.slotHashesSysvar),
644
+ getAccountMeta(accounts.instructionsSysvar),
645
+ getAccountMeta(accounts.buyerPosition),
646
+ getAccountMeta(accounts.buyerLongAccount),
647
+ getAccountMeta(accounts.buyerPayoutAccount),
648
+ getAccountMeta(accounts.collateralVault),
649
+ getAccountMeta(accounts.buyer),
650
+ getAccountMeta(accounts.tokenProgram),
651
+ getAccountMeta(accounts.systemProgram),
652
+ ],
653
+ data: getCloseLongToPoolInstructionDataEncoder().encode(
654
+ args as CloseLongToPoolInstructionDataArgs,
655
+ ),
656
+ programAddress,
657
+ } as CloseLongToPoolInstruction<
658
+ TProgramAddress,
659
+ TAccountOptionPool,
660
+ TAccountOptionAccount,
661
+ TAccountCollateralPool,
662
+ TAccountUnderlyingMint,
663
+ TAccountLongMint,
664
+ TAccountEscrowLongAccount,
665
+ TAccountPremiumVault,
666
+ TAccountMarketData,
667
+ TAccountSwitchboardQueue,
668
+ TAccountSlotHashesSysvar,
669
+ TAccountInstructionsSysvar,
670
+ TAccountBuyerPosition,
671
+ TAccountBuyerLongAccount,
672
+ TAccountBuyerPayoutAccount,
673
+ TAccountCollateralVault,
674
+ TAccountBuyer,
675
+ TAccountTokenProgram,
676
+ TAccountSystemProgram
677
+ >);
678
+ }
679
+
680
+ export type ParsedCloseLongToPoolInstruction<
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 option pool */
687
+ optionPool: TAccountMetas[0];
688
+ /** The option account */
689
+ optionAccount: TAccountMetas[1];
690
+ /** Collateral pool (source of payout) */
691
+ collateralPool: TAccountMetas[2];
692
+ /** Underlying token mint (for decimal handling) */
693
+ underlyingMint: TAccountMetas[3];
694
+ /** LONG token mint (for transfer validation) */
695
+ longMint: TAccountMetas[4];
696
+ /** Pool LONG escrow (destination when buyer closes back to pool) */
697
+ escrowLongAccount: TAccountMetas[5];
698
+ /** Premium vault (first source of buyer payout) */
699
+ premiumVault: TAccountMetas[6];
700
+ /** Market data account (provides risk-free rate and switchboard_feed_id) */
701
+ marketData: TAccountMetas[7];
702
+ switchboardQueue: TAccountMetas[8];
703
+ slotHashesSysvar: TAccountMetas[9];
704
+ instructionsSysvar: TAccountMetas[10];
705
+ /** Buyer's position account */
706
+ buyerPosition: TAccountMetas[11];
707
+ /** Buyer's LONG token account (source of tokens to return) */
708
+ buyerLongAccount: TAccountMetas[12];
709
+ /** Buyer's underlying token account (receives payout) */
710
+ buyerPayoutAccount: TAccountMetas[13];
711
+ /** Collateral vault (source of payout) */
712
+ collateralVault: TAccountMetas[14];
713
+ buyer: TAccountMetas[15];
714
+ tokenProgram: TAccountMetas[16];
715
+ systemProgram: TAccountMetas[17];
716
+ };
717
+ data: CloseLongToPoolInstructionData;
718
+ };
719
+
720
+ export function parseCloseLongToPoolInstruction<
721
+ TProgram extends string,
722
+ TAccountMetas extends readonly AccountMeta[],
723
+ >(
724
+ instruction: Instruction<TProgram> &
725
+ InstructionWithAccounts<TAccountMetas> &
726
+ InstructionWithData<ReadonlyUint8Array>,
727
+ ): ParsedCloseLongToPoolInstruction<TProgram, TAccountMetas> {
728
+ if (instruction.accounts.length < 18) {
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
+ collateralPool: getNextAccount(),
744
+ underlyingMint: getNextAccount(),
745
+ longMint: getNextAccount(),
746
+ escrowLongAccount: getNextAccount(),
747
+ premiumVault: getNextAccount(),
748
+ marketData: getNextAccount(),
749
+ switchboardQueue: getNextAccount(),
750
+ slotHashesSysvar: getNextAccount(),
751
+ instructionsSysvar: getNextAccount(),
752
+ buyerPosition: getNextAccount(),
753
+ buyerLongAccount: getNextAccount(),
754
+ buyerPayoutAccount: getNextAccount(),
755
+ collateralVault: getNextAccount(),
756
+ buyer: getNextAccount(),
757
+ tokenProgram: getNextAccount(),
758
+ systemProgram: getNextAccount(),
759
+ },
760
+ data: getCloseLongToPoolInstructionDataDecoder().decode(instruction.data),
761
+ };
762
+ }