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