@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,606 @@
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
+ expectSome,
41
+ getAccountMetaFactory,
42
+ type ResolvedAccount,
43
+ } from "../shared";
44
+
45
+ export const OMLP_TAKE_OFFER_WITH_FAILOVER_DISCRIMINATOR = new Uint8Array([
46
+ 39, 155, 118, 226, 84, 159, 5, 76,
47
+ ]);
48
+
49
+ export function getOmlpTakeOfferWithFailoverDiscriminatorBytes() {
50
+ return fixEncoderSize(getBytesEncoder(), 8).encode(
51
+ OMLP_TAKE_OFFER_WITH_FAILOVER_DISCRIMINATOR,
52
+ );
53
+ }
54
+
55
+ export type OmlpTakeOfferWithFailoverInstruction<
56
+ TProgram extends string = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
57
+ TAccountDualContract extends string | AccountMeta<string> = string,
58
+ TAccountRouter extends string | AccountMeta<string> = string,
59
+ TAccountVault extends string | AccountMeta<string> = string,
60
+ TAccountVaultTokenAccount extends string | AccountMeta<string> = string,
61
+ TAccountEscrowAccount extends string | AccountMeta<string> = string,
62
+ TAccountEscrowAuthority extends string | AccountMeta<string> = string,
63
+ TAccountSap2Program extends string | AccountMeta<string> = string,
64
+ TAccountSap2LoanAccount extends string | AccountMeta<string> = string,
65
+ TAccountSap2OfferAccount extends string | AccountMeta<string> = string,
66
+ TAccountMaker extends string | AccountMeta<string> = string,
67
+ TAccountTokenProgram extends string | AccountMeta<string> =
68
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
69
+ TAccountSystemProgram extends string | AccountMeta<string> =
70
+ "11111111111111111111111111111111",
71
+ TRemainingAccounts extends readonly AccountMeta<string>[] = [],
72
+ > = Instruction<TProgram> &
73
+ InstructionWithData<ReadonlyUint8Array> &
74
+ InstructionWithAccounts<
75
+ [
76
+ TAccountDualContract extends string
77
+ ? WritableAccount<TAccountDualContract>
78
+ : TAccountDualContract,
79
+ TAccountRouter extends string
80
+ ? WritableAccount<TAccountRouter>
81
+ : TAccountRouter,
82
+ TAccountVault extends string
83
+ ? WritableAccount<TAccountVault>
84
+ : TAccountVault,
85
+ TAccountVaultTokenAccount extends string
86
+ ? WritableAccount<TAccountVaultTokenAccount>
87
+ : TAccountVaultTokenAccount,
88
+ TAccountEscrowAccount extends string
89
+ ? WritableAccount<TAccountEscrowAccount>
90
+ : TAccountEscrowAccount,
91
+ TAccountEscrowAuthority extends string
92
+ ? ReadonlyAccount<TAccountEscrowAuthority>
93
+ : TAccountEscrowAuthority,
94
+ TAccountSap2Program extends string
95
+ ? ReadonlyAccount<TAccountSap2Program>
96
+ : TAccountSap2Program,
97
+ TAccountSap2LoanAccount extends string
98
+ ? ReadonlyAccount<TAccountSap2LoanAccount>
99
+ : TAccountSap2LoanAccount,
100
+ TAccountSap2OfferAccount extends string
101
+ ? ReadonlyAccount<TAccountSap2OfferAccount>
102
+ : TAccountSap2OfferAccount,
103
+ TAccountMaker extends string
104
+ ? WritableSignerAccount<TAccountMaker> &
105
+ AccountSignerMeta<TAccountMaker>
106
+ : TAccountMaker,
107
+ TAccountTokenProgram extends string
108
+ ? ReadonlyAccount<TAccountTokenProgram>
109
+ : TAccountTokenProgram,
110
+ TAccountSystemProgram extends string
111
+ ? ReadonlyAccount<TAccountSystemProgram>
112
+ : TAccountSystemProgram,
113
+ ...TRemainingAccounts,
114
+ ]
115
+ >;
116
+
117
+ export type OmlpTakeOfferWithFailoverInstructionData = {
118
+ discriminator: ReadonlyUint8Array;
119
+ nonce: bigint;
120
+ borrowAmount: bigint;
121
+ collateralAmount: bigint;
122
+ };
123
+
124
+ export type OmlpTakeOfferWithFailoverInstructionDataArgs = {
125
+ nonce: number | bigint;
126
+ borrowAmount: number | bigint;
127
+ collateralAmount: number | bigint;
128
+ };
129
+
130
+ export function getOmlpTakeOfferWithFailoverInstructionDataEncoder(): FixedSizeEncoder<OmlpTakeOfferWithFailoverInstructionDataArgs> {
131
+ return transformEncoder(
132
+ getStructEncoder([
133
+ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
134
+ ["nonce", getU64Encoder()],
135
+ ["borrowAmount", getU64Encoder()],
136
+ ["collateralAmount", getU64Encoder()],
137
+ ]),
138
+ (value) => ({
139
+ ...value,
140
+ discriminator: OMLP_TAKE_OFFER_WITH_FAILOVER_DISCRIMINATOR,
141
+ }),
142
+ );
143
+ }
144
+
145
+ export function getOmlpTakeOfferWithFailoverInstructionDataDecoder(): FixedSizeDecoder<OmlpTakeOfferWithFailoverInstructionData> {
146
+ return getStructDecoder([
147
+ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
148
+ ["nonce", getU64Decoder()],
149
+ ["borrowAmount", getU64Decoder()],
150
+ ["collateralAmount", getU64Decoder()],
151
+ ]);
152
+ }
153
+
154
+ export function getOmlpTakeOfferWithFailoverInstructionDataCodec(): FixedSizeCodec<
155
+ OmlpTakeOfferWithFailoverInstructionDataArgs,
156
+ OmlpTakeOfferWithFailoverInstructionData
157
+ > {
158
+ return combineCodec(
159
+ getOmlpTakeOfferWithFailoverInstructionDataEncoder(),
160
+ getOmlpTakeOfferWithFailoverInstructionDataDecoder(),
161
+ );
162
+ }
163
+
164
+ export type OmlpTakeOfferWithFailoverAsyncInput<
165
+ TAccountDualContract extends string = string,
166
+ TAccountRouter extends string = string,
167
+ TAccountVault extends string = string,
168
+ TAccountVaultTokenAccount extends string = string,
169
+ TAccountEscrowAccount extends string = string,
170
+ TAccountEscrowAuthority extends string = string,
171
+ TAccountSap2Program extends string = string,
172
+ TAccountSap2LoanAccount extends string = string,
173
+ TAccountSap2OfferAccount extends string = string,
174
+ TAccountMaker extends string = string,
175
+ TAccountTokenProgram extends string = string,
176
+ TAccountSystemProgram extends string = string,
177
+ > = {
178
+ dualContract?: Address<TAccountDualContract>;
179
+ router?: Address<TAccountRouter>;
180
+ vault: Address<TAccountVault>;
181
+ /** Vault's token account (SAP1 source - pooled) */
182
+ vaultTokenAccount: Address<TAccountVaultTokenAccount>;
183
+ /**
184
+ * Escrow account that receives borrowed funds (NOT maker's wallet)
185
+ * Funds go directly to escrow to ensure they're used for collateral
186
+ * and can be repaid when the position is closed
187
+ */
188
+ escrowAccount: Address<TAccountEscrowAccount>;
189
+ escrowAuthority: Address<TAccountEscrowAuthority>;
190
+ /** SAP2 program - optional, used if SAP1 full */
191
+ sap2Program?: Address<TAccountSap2Program>;
192
+ sap2LoanAccount?: Address<TAccountSap2LoanAccount>;
193
+ sap2OfferAccount?: Address<TAccountSap2OfferAccount>;
194
+ maker: TransactionSigner<TAccountMaker>;
195
+ tokenProgram?: Address<TAccountTokenProgram>;
196
+ systemProgram?: Address<TAccountSystemProgram>;
197
+ nonce: OmlpTakeOfferWithFailoverInstructionDataArgs["nonce"];
198
+ borrowAmount: OmlpTakeOfferWithFailoverInstructionDataArgs["borrowAmount"];
199
+ collateralAmount: OmlpTakeOfferWithFailoverInstructionDataArgs["collateralAmount"];
200
+ };
201
+
202
+ export async function getOmlpTakeOfferWithFailoverInstructionAsync<
203
+ TAccountDualContract extends string,
204
+ TAccountRouter extends string,
205
+ TAccountVault extends string,
206
+ TAccountVaultTokenAccount extends string,
207
+ TAccountEscrowAccount extends string,
208
+ TAccountEscrowAuthority extends string,
209
+ TAccountSap2Program extends string,
210
+ TAccountSap2LoanAccount extends string,
211
+ TAccountSap2OfferAccount extends string,
212
+ TAccountMaker extends string,
213
+ TAccountTokenProgram extends string,
214
+ TAccountSystemProgram extends string,
215
+ TProgramAddress extends Address = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
216
+ >(
217
+ input: OmlpTakeOfferWithFailoverAsyncInput<
218
+ TAccountDualContract,
219
+ TAccountRouter,
220
+ TAccountVault,
221
+ TAccountVaultTokenAccount,
222
+ TAccountEscrowAccount,
223
+ TAccountEscrowAuthority,
224
+ TAccountSap2Program,
225
+ TAccountSap2LoanAccount,
226
+ TAccountSap2OfferAccount,
227
+ TAccountMaker,
228
+ TAccountTokenProgram,
229
+ TAccountSystemProgram
230
+ >,
231
+ config?: { programAddress?: TProgramAddress },
232
+ ): Promise<
233
+ OmlpTakeOfferWithFailoverInstruction<
234
+ TProgramAddress,
235
+ TAccountDualContract,
236
+ TAccountRouter,
237
+ TAccountVault,
238
+ TAccountVaultTokenAccount,
239
+ TAccountEscrowAccount,
240
+ TAccountEscrowAuthority,
241
+ TAccountSap2Program,
242
+ TAccountSap2LoanAccount,
243
+ TAccountSap2OfferAccount,
244
+ TAccountMaker,
245
+ TAccountTokenProgram,
246
+ TAccountSystemProgram
247
+ >
248
+ > {
249
+ // Program address.
250
+ const programAddress =
251
+ config?.programAddress ?? OPTION_PROGRAM_PROGRAM_ADDRESS;
252
+
253
+ // Original accounts.
254
+ const originalAccounts = {
255
+ dualContract: { value: input.dualContract ?? null, isWritable: true },
256
+ router: { value: input.router ?? null, isWritable: true },
257
+ vault: { value: input.vault ?? null, isWritable: true },
258
+ vaultTokenAccount: {
259
+ value: input.vaultTokenAccount ?? null,
260
+ isWritable: true,
261
+ },
262
+ escrowAccount: { value: input.escrowAccount ?? null, isWritable: true },
263
+ escrowAuthority: {
264
+ value: input.escrowAuthority ?? null,
265
+ isWritable: false,
266
+ },
267
+ sap2Program: { value: input.sap2Program ?? null, isWritable: false },
268
+ sap2LoanAccount: {
269
+ value: input.sap2LoanAccount ?? null,
270
+ isWritable: false,
271
+ },
272
+ sap2OfferAccount: {
273
+ value: input.sap2OfferAccount ?? null,
274
+ isWritable: false,
275
+ },
276
+ maker: { value: input.maker ?? null, isWritable: true },
277
+ tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
278
+ systemProgram: { value: input.systemProgram ?? null, isWritable: false },
279
+ };
280
+ const accounts = originalAccounts as Record<
281
+ keyof typeof originalAccounts,
282
+ ResolvedAccount
283
+ >;
284
+
285
+ // Original args.
286
+ const args = { ...input };
287
+
288
+ // Resolve default values.
289
+ if (!accounts.router.value) {
290
+ accounts.router.value = await getProgramDerivedAddress({
291
+ programAddress,
292
+ seeds: [
293
+ getBytesEncoder().encode(
294
+ new Uint8Array([
295
+ 108, 105, 113, 117, 105, 100, 105, 116, 121, 95, 114, 111, 117, 116,
296
+ 101, 114,
297
+ ]),
298
+ ),
299
+ getAddressEncoder().encode(expectAddress(accounts.vault.value)),
300
+ ],
301
+ });
302
+ }
303
+ if (!accounts.dualContract.value) {
304
+ accounts.dualContract.value = await getProgramDerivedAddress({
305
+ programAddress,
306
+ seeds: [
307
+ getBytesEncoder().encode(
308
+ new Uint8Array([
309
+ 100, 117, 97, 108, 95, 99, 111, 110, 116, 114, 97, 99, 116,
310
+ ]),
311
+ ),
312
+ getAddressEncoder().encode(expectAddress(accounts.maker.value)),
313
+ getAddressEncoder().encode(expectAddress(accounts.router.value)),
314
+ getU64Encoder().encode(expectSome(args.nonce)),
315
+ ],
316
+ });
317
+ }
318
+ if (!accounts.tokenProgram.value) {
319
+ accounts.tokenProgram.value =
320
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
321
+ }
322
+ if (!accounts.systemProgram.value) {
323
+ accounts.systemProgram.value =
324
+ "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
325
+ }
326
+
327
+ const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
328
+ return Object.freeze({
329
+ accounts: [
330
+ getAccountMeta(accounts.dualContract),
331
+ getAccountMeta(accounts.router),
332
+ getAccountMeta(accounts.vault),
333
+ getAccountMeta(accounts.vaultTokenAccount),
334
+ getAccountMeta(accounts.escrowAccount),
335
+ getAccountMeta(accounts.escrowAuthority),
336
+ getAccountMeta(accounts.sap2Program),
337
+ getAccountMeta(accounts.sap2LoanAccount),
338
+ getAccountMeta(accounts.sap2OfferAccount),
339
+ getAccountMeta(accounts.maker),
340
+ getAccountMeta(accounts.tokenProgram),
341
+ getAccountMeta(accounts.systemProgram),
342
+ ],
343
+ data: getOmlpTakeOfferWithFailoverInstructionDataEncoder().encode(
344
+ args as OmlpTakeOfferWithFailoverInstructionDataArgs,
345
+ ),
346
+ programAddress,
347
+ } as OmlpTakeOfferWithFailoverInstruction<
348
+ TProgramAddress,
349
+ TAccountDualContract,
350
+ TAccountRouter,
351
+ TAccountVault,
352
+ TAccountVaultTokenAccount,
353
+ TAccountEscrowAccount,
354
+ TAccountEscrowAuthority,
355
+ TAccountSap2Program,
356
+ TAccountSap2LoanAccount,
357
+ TAccountSap2OfferAccount,
358
+ TAccountMaker,
359
+ TAccountTokenProgram,
360
+ TAccountSystemProgram
361
+ >);
362
+ }
363
+
364
+ export type OmlpTakeOfferWithFailoverInput<
365
+ TAccountDualContract extends string = string,
366
+ TAccountRouter extends string = string,
367
+ TAccountVault extends string = string,
368
+ TAccountVaultTokenAccount extends string = string,
369
+ TAccountEscrowAccount extends string = string,
370
+ TAccountEscrowAuthority extends string = string,
371
+ TAccountSap2Program extends string = string,
372
+ TAccountSap2LoanAccount extends string = string,
373
+ TAccountSap2OfferAccount extends string = string,
374
+ TAccountMaker extends string = string,
375
+ TAccountTokenProgram extends string = string,
376
+ TAccountSystemProgram extends string = string,
377
+ > = {
378
+ dualContract: Address<TAccountDualContract>;
379
+ router: Address<TAccountRouter>;
380
+ vault: Address<TAccountVault>;
381
+ /** Vault's token account (SAP1 source - pooled) */
382
+ vaultTokenAccount: Address<TAccountVaultTokenAccount>;
383
+ /**
384
+ * Escrow account that receives borrowed funds (NOT maker's wallet)
385
+ * Funds go directly to escrow to ensure they're used for collateral
386
+ * and can be repaid when the position is closed
387
+ */
388
+ escrowAccount: Address<TAccountEscrowAccount>;
389
+ escrowAuthority: Address<TAccountEscrowAuthority>;
390
+ /** SAP2 program - optional, used if SAP1 full */
391
+ sap2Program?: Address<TAccountSap2Program>;
392
+ sap2LoanAccount?: Address<TAccountSap2LoanAccount>;
393
+ sap2OfferAccount?: Address<TAccountSap2OfferAccount>;
394
+ maker: TransactionSigner<TAccountMaker>;
395
+ tokenProgram?: Address<TAccountTokenProgram>;
396
+ systemProgram?: Address<TAccountSystemProgram>;
397
+ nonce: OmlpTakeOfferWithFailoverInstructionDataArgs["nonce"];
398
+ borrowAmount: OmlpTakeOfferWithFailoverInstructionDataArgs["borrowAmount"];
399
+ collateralAmount: OmlpTakeOfferWithFailoverInstructionDataArgs["collateralAmount"];
400
+ };
401
+
402
+ export function getOmlpTakeOfferWithFailoverInstruction<
403
+ TAccountDualContract extends string,
404
+ TAccountRouter extends string,
405
+ TAccountVault extends string,
406
+ TAccountVaultTokenAccount extends string,
407
+ TAccountEscrowAccount extends string,
408
+ TAccountEscrowAuthority extends string,
409
+ TAccountSap2Program extends string,
410
+ TAccountSap2LoanAccount extends string,
411
+ TAccountSap2OfferAccount extends string,
412
+ TAccountMaker extends string,
413
+ TAccountTokenProgram extends string,
414
+ TAccountSystemProgram extends string,
415
+ TProgramAddress extends Address = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
416
+ >(
417
+ input: OmlpTakeOfferWithFailoverInput<
418
+ TAccountDualContract,
419
+ TAccountRouter,
420
+ TAccountVault,
421
+ TAccountVaultTokenAccount,
422
+ TAccountEscrowAccount,
423
+ TAccountEscrowAuthority,
424
+ TAccountSap2Program,
425
+ TAccountSap2LoanAccount,
426
+ TAccountSap2OfferAccount,
427
+ TAccountMaker,
428
+ TAccountTokenProgram,
429
+ TAccountSystemProgram
430
+ >,
431
+ config?: { programAddress?: TProgramAddress },
432
+ ): OmlpTakeOfferWithFailoverInstruction<
433
+ TProgramAddress,
434
+ TAccountDualContract,
435
+ TAccountRouter,
436
+ TAccountVault,
437
+ TAccountVaultTokenAccount,
438
+ TAccountEscrowAccount,
439
+ TAccountEscrowAuthority,
440
+ TAccountSap2Program,
441
+ TAccountSap2LoanAccount,
442
+ TAccountSap2OfferAccount,
443
+ TAccountMaker,
444
+ TAccountTokenProgram,
445
+ TAccountSystemProgram
446
+ > {
447
+ // Program address.
448
+ const programAddress =
449
+ config?.programAddress ?? OPTION_PROGRAM_PROGRAM_ADDRESS;
450
+
451
+ // Original accounts.
452
+ const originalAccounts = {
453
+ dualContract: { value: input.dualContract ?? null, isWritable: true },
454
+ router: { value: input.router ?? null, isWritable: true },
455
+ vault: { value: input.vault ?? null, isWritable: true },
456
+ vaultTokenAccount: {
457
+ value: input.vaultTokenAccount ?? null,
458
+ isWritable: true,
459
+ },
460
+ escrowAccount: { value: input.escrowAccount ?? null, isWritable: true },
461
+ escrowAuthority: {
462
+ value: input.escrowAuthority ?? null,
463
+ isWritable: false,
464
+ },
465
+ sap2Program: { value: input.sap2Program ?? null, isWritable: false },
466
+ sap2LoanAccount: {
467
+ value: input.sap2LoanAccount ?? null,
468
+ isWritable: false,
469
+ },
470
+ sap2OfferAccount: {
471
+ value: input.sap2OfferAccount ?? null,
472
+ isWritable: false,
473
+ },
474
+ maker: { value: input.maker ?? null, isWritable: true },
475
+ tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
476
+ systemProgram: { value: input.systemProgram ?? null, isWritable: false },
477
+ };
478
+ const accounts = originalAccounts as Record<
479
+ keyof typeof originalAccounts,
480
+ ResolvedAccount
481
+ >;
482
+
483
+ // Original args.
484
+ const args = { ...input };
485
+
486
+ // Resolve default values.
487
+ if (!accounts.tokenProgram.value) {
488
+ accounts.tokenProgram.value =
489
+ "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
490
+ }
491
+ if (!accounts.systemProgram.value) {
492
+ accounts.systemProgram.value =
493
+ "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
494
+ }
495
+
496
+ const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
497
+ return Object.freeze({
498
+ accounts: [
499
+ getAccountMeta(accounts.dualContract),
500
+ getAccountMeta(accounts.router),
501
+ getAccountMeta(accounts.vault),
502
+ getAccountMeta(accounts.vaultTokenAccount),
503
+ getAccountMeta(accounts.escrowAccount),
504
+ getAccountMeta(accounts.escrowAuthority),
505
+ getAccountMeta(accounts.sap2Program),
506
+ getAccountMeta(accounts.sap2LoanAccount),
507
+ getAccountMeta(accounts.sap2OfferAccount),
508
+ getAccountMeta(accounts.maker),
509
+ getAccountMeta(accounts.tokenProgram),
510
+ getAccountMeta(accounts.systemProgram),
511
+ ],
512
+ data: getOmlpTakeOfferWithFailoverInstructionDataEncoder().encode(
513
+ args as OmlpTakeOfferWithFailoverInstructionDataArgs,
514
+ ),
515
+ programAddress,
516
+ } as OmlpTakeOfferWithFailoverInstruction<
517
+ TProgramAddress,
518
+ TAccountDualContract,
519
+ TAccountRouter,
520
+ TAccountVault,
521
+ TAccountVaultTokenAccount,
522
+ TAccountEscrowAccount,
523
+ TAccountEscrowAuthority,
524
+ TAccountSap2Program,
525
+ TAccountSap2LoanAccount,
526
+ TAccountSap2OfferAccount,
527
+ TAccountMaker,
528
+ TAccountTokenProgram,
529
+ TAccountSystemProgram
530
+ >);
531
+ }
532
+
533
+ export type ParsedOmlpTakeOfferWithFailoverInstruction<
534
+ TProgram extends string = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
535
+ TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
536
+ > = {
537
+ programAddress: Address<TProgram>;
538
+ accounts: {
539
+ dualContract: TAccountMetas[0];
540
+ router: TAccountMetas[1];
541
+ vault: TAccountMetas[2];
542
+ /** Vault's token account (SAP1 source - pooled) */
543
+ vaultTokenAccount: TAccountMetas[3];
544
+ /**
545
+ * Escrow account that receives borrowed funds (NOT maker's wallet)
546
+ * Funds go directly to escrow to ensure they're used for collateral
547
+ * and can be repaid when the position is closed
548
+ */
549
+ escrowAccount: TAccountMetas[4];
550
+ escrowAuthority: TAccountMetas[5];
551
+ /** SAP2 program - optional, used if SAP1 full */
552
+ sap2Program?: TAccountMetas[6] | undefined;
553
+ sap2LoanAccount?: TAccountMetas[7] | undefined;
554
+ sap2OfferAccount?: TAccountMetas[8] | undefined;
555
+ maker: TAccountMetas[9];
556
+ tokenProgram: TAccountMetas[10];
557
+ systemProgram: TAccountMetas[11];
558
+ };
559
+ data: OmlpTakeOfferWithFailoverInstructionData;
560
+ };
561
+
562
+ export function parseOmlpTakeOfferWithFailoverInstruction<
563
+ TProgram extends string,
564
+ TAccountMetas extends readonly AccountMeta[],
565
+ >(
566
+ instruction: Instruction<TProgram> &
567
+ InstructionWithAccounts<TAccountMetas> &
568
+ InstructionWithData<ReadonlyUint8Array>,
569
+ ): ParsedOmlpTakeOfferWithFailoverInstruction<TProgram, TAccountMetas> {
570
+ if (instruction.accounts.length < 12) {
571
+ // TODO: Coded error.
572
+ throw new Error("Not enough accounts");
573
+ }
574
+ let accountIndex = 0;
575
+ const getNextAccount = () => {
576
+ const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
577
+ accountIndex += 1;
578
+ return accountMeta;
579
+ };
580
+ const getNextOptionalAccount = () => {
581
+ const accountMeta = getNextAccount();
582
+ return accountMeta.address === OPTION_PROGRAM_PROGRAM_ADDRESS
583
+ ? undefined
584
+ : accountMeta;
585
+ };
586
+ return {
587
+ programAddress: instruction.programAddress,
588
+ accounts: {
589
+ dualContract: getNextAccount(),
590
+ router: getNextAccount(),
591
+ vault: getNextAccount(),
592
+ vaultTokenAccount: getNextAccount(),
593
+ escrowAccount: getNextAccount(),
594
+ escrowAuthority: getNextAccount(),
595
+ sap2Program: getNextOptionalAccount(),
596
+ sap2LoanAccount: getNextOptionalAccount(),
597
+ sap2OfferAccount: getNextOptionalAccount(),
598
+ maker: getNextAccount(),
599
+ tokenProgram: getNextAccount(),
600
+ systemProgram: getNextAccount(),
601
+ },
602
+ data: getOmlpTakeOfferWithFailoverInstructionDataDecoder().decode(
603
+ instruction.data,
604
+ ),
605
+ };
606
+ }