@epicentral/sos-sdk 0.2.1 → 0.2.3

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.
@@ -8,10 +8,8 @@
8
8
 
9
9
  export * from "./collateralPool";
10
10
  export * from "./config";
11
- export * from "./dualSourceContract";
12
11
  export * from "./escrowState";
13
12
  export * from "./lenderPosition";
14
- export * from "./liquidityRouter";
15
13
  export * from "./makerCollateralShare";
16
14
  export * from "./marketDataAccount";
17
15
  export * from "./optionAccount";
@@ -15,7 +15,6 @@ export * from "./claimTheta";
15
15
  export * from "./closeLongToPool";
16
16
  export * from "./closeOption";
17
17
  export * from "./createEscrowV2";
18
- export * from "./createLiquidityRouter";
19
18
  export * from "./depositCollateral";
20
19
  export * from "./depositToPosition";
21
20
  export * from "./initCollateralPool";
@@ -24,7 +23,6 @@ export * from "./initializeMarketData";
24
23
  export * from "./initOptionPool";
25
24
  export * from "./liquidateWriterPosition";
26
25
  export * from "./omlpCreateVault";
27
- export * from "./omlpTakeOfferWithFailover";
28
26
  export * from "./omlpUpdateMaxLeverage";
29
27
  export * from "./omlpUpdateProtocolFee";
30
28
  export * from "./omlpUpdateSupplyLimit";
@@ -92,7 +92,6 @@ export type OptionMintInstruction<
92
92
  TAccountEscrowAuthority extends string | AccountMeta<string> = string,
93
93
  TAccountEscrowTokenAccount extends string | AccountMeta<string> = string,
94
94
  TAccountPoolLoan extends string | AccountMeta<string> = string,
95
- TAccountLiquidityRouter extends string | AccountMeta<string> = string,
96
95
  TAccountMaker extends string | AccountMeta<string> = string,
97
96
  TAccountTokenProgram extends string | AccountMeta<string> =
98
97
  "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
@@ -178,9 +177,6 @@ export type OptionMintInstruction<
178
177
  TAccountPoolLoan extends string
179
178
  ? WritableAccount<TAccountPoolLoan>
180
179
  : TAccountPoolLoan,
181
- TAccountLiquidityRouter extends string
182
- ? WritableAccount<TAccountLiquidityRouter>
183
- : TAccountLiquidityRouter,
184
180
  TAccountMaker extends string
185
181
  ? WritableSignerAccount<TAccountMaker> &
186
182
  AccountSignerMeta<TAccountMaker>
@@ -298,7 +294,6 @@ export type OptionMintAsyncInput<
298
294
  TAccountEscrowAuthority extends string = string,
299
295
  TAccountEscrowTokenAccount extends string = string,
300
296
  TAccountPoolLoan extends string = string,
301
- TAccountLiquidityRouter extends string = string,
302
297
  TAccountMaker extends string = string,
303
298
  TAccountTokenProgram extends string = string,
304
299
  TAccountAssociatedTokenProgram extends string = string,
@@ -358,8 +353,6 @@ export type OptionMintAsyncInput<
358
353
  escrowTokenAccount?: Address<TAccountEscrowTokenAccount>;
359
354
  /** Pool loan account (optional - only required if borrowing) */
360
355
  poolLoan?: Address<TAccountPoolLoan>;
361
- /** Liquidity router (optional - only required if borrowing with SAP1/SAP2 failover) */
362
- liquidityRouter?: Address<TAccountLiquidityRouter>;
363
356
  maker: TransactionSigner<TAccountMaker>;
364
357
  tokenProgram?: Address<TAccountTokenProgram>;
365
358
  associatedTokenProgram?: Address<TAccountAssociatedTokenProgram>;
@@ -400,7 +393,6 @@ export async function getOptionMintInstructionAsync<
400
393
  TAccountEscrowAuthority extends string,
401
394
  TAccountEscrowTokenAccount extends string,
402
395
  TAccountPoolLoan extends string,
403
- TAccountLiquidityRouter extends string,
404
396
  TAccountMaker extends string,
405
397
  TAccountTokenProgram extends string,
406
398
  TAccountAssociatedTokenProgram extends string,
@@ -433,7 +425,6 @@ export async function getOptionMintInstructionAsync<
433
425
  TAccountEscrowAuthority,
434
426
  TAccountEscrowTokenAccount,
435
427
  TAccountPoolLoan,
436
- TAccountLiquidityRouter,
437
428
  TAccountMaker,
438
429
  TAccountTokenProgram,
439
430
  TAccountAssociatedTokenProgram,
@@ -468,7 +459,6 @@ export async function getOptionMintInstructionAsync<
468
459
  TAccountEscrowAuthority,
469
460
  TAccountEscrowTokenAccount,
470
461
  TAccountPoolLoan,
471
- TAccountLiquidityRouter,
472
462
  TAccountMaker,
473
463
  TAccountTokenProgram,
474
464
  TAccountAssociatedTokenProgram,
@@ -533,7 +523,6 @@ export async function getOptionMintInstructionAsync<
533
523
  isWritable: true,
534
524
  },
535
525
  poolLoan: { value: input.poolLoan ?? null, isWritable: true },
536
- liquidityRouter: { value: input.liquidityRouter ?? null, isWritable: true },
537
526
  maker: { value: input.maker ?? null, isWritable: true },
538
527
  tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
539
528
  associatedTokenProgram: {
@@ -795,7 +784,6 @@ export async function getOptionMintInstructionAsync<
795
784
  getAccountMeta(accounts.escrowAuthority),
796
785
  getAccountMeta(accounts.escrowTokenAccount),
797
786
  getAccountMeta(accounts.poolLoan),
798
- getAccountMeta(accounts.liquidityRouter),
799
787
  getAccountMeta(accounts.maker),
800
788
  getAccountMeta(accounts.tokenProgram),
801
789
  getAccountMeta(accounts.associatedTokenProgram),
@@ -832,7 +820,6 @@ export async function getOptionMintInstructionAsync<
832
820
  TAccountEscrowAuthority,
833
821
  TAccountEscrowTokenAccount,
834
822
  TAccountPoolLoan,
835
- TAccountLiquidityRouter,
836
823
  TAccountMaker,
837
824
  TAccountTokenProgram,
838
825
  TAccountAssociatedTokenProgram,
@@ -866,7 +853,6 @@ export type OptionMintInput<
866
853
  TAccountEscrowAuthority extends string = string,
867
854
  TAccountEscrowTokenAccount extends string = string,
868
855
  TAccountPoolLoan extends string = string,
869
- TAccountLiquidityRouter extends string = string,
870
856
  TAccountMaker extends string = string,
871
857
  TAccountTokenProgram extends string = string,
872
858
  TAccountAssociatedTokenProgram extends string = string,
@@ -926,8 +912,6 @@ export type OptionMintInput<
926
912
  escrowTokenAccount?: Address<TAccountEscrowTokenAccount>;
927
913
  /** Pool loan account (optional - only required if borrowing) */
928
914
  poolLoan?: Address<TAccountPoolLoan>;
929
- /** Liquidity router (optional - only required if borrowing with SAP1/SAP2 failover) */
930
- liquidityRouter?: Address<TAccountLiquidityRouter>;
931
915
  maker: TransactionSigner<TAccountMaker>;
932
916
  tokenProgram?: Address<TAccountTokenProgram>;
933
917
  associatedTokenProgram?: Address<TAccountAssociatedTokenProgram>;
@@ -968,7 +952,6 @@ export function getOptionMintInstruction<
968
952
  TAccountEscrowAuthority extends string,
969
953
  TAccountEscrowTokenAccount extends string,
970
954
  TAccountPoolLoan extends string,
971
- TAccountLiquidityRouter extends string,
972
955
  TAccountMaker extends string,
973
956
  TAccountTokenProgram extends string,
974
957
  TAccountAssociatedTokenProgram extends string,
@@ -1001,7 +984,6 @@ export function getOptionMintInstruction<
1001
984
  TAccountEscrowAuthority,
1002
985
  TAccountEscrowTokenAccount,
1003
986
  TAccountPoolLoan,
1004
- TAccountLiquidityRouter,
1005
987
  TAccountMaker,
1006
988
  TAccountTokenProgram,
1007
989
  TAccountAssociatedTokenProgram,
@@ -1035,7 +1017,6 @@ export function getOptionMintInstruction<
1035
1017
  TAccountEscrowAuthority,
1036
1018
  TAccountEscrowTokenAccount,
1037
1019
  TAccountPoolLoan,
1038
- TAccountLiquidityRouter,
1039
1020
  TAccountMaker,
1040
1021
  TAccountTokenProgram,
1041
1022
  TAccountAssociatedTokenProgram,
@@ -1099,7 +1080,6 @@ export function getOptionMintInstruction<
1099
1080
  isWritable: true,
1100
1081
  },
1101
1082
  poolLoan: { value: input.poolLoan ?? null, isWritable: true },
1102
- liquidityRouter: { value: input.liquidityRouter ?? null, isWritable: true },
1103
1083
  maker: { value: input.maker ?? null, isWritable: true },
1104
1084
  tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
1105
1085
  associatedTokenProgram: {
@@ -1169,7 +1149,6 @@ export function getOptionMintInstruction<
1169
1149
  getAccountMeta(accounts.escrowAuthority),
1170
1150
  getAccountMeta(accounts.escrowTokenAccount),
1171
1151
  getAccountMeta(accounts.poolLoan),
1172
- getAccountMeta(accounts.liquidityRouter),
1173
1152
  getAccountMeta(accounts.maker),
1174
1153
  getAccountMeta(accounts.tokenProgram),
1175
1154
  getAccountMeta(accounts.associatedTokenProgram),
@@ -1206,7 +1185,6 @@ export function getOptionMintInstruction<
1206
1185
  TAccountEscrowAuthority,
1207
1186
  TAccountEscrowTokenAccount,
1208
1187
  TAccountPoolLoan,
1209
- TAccountLiquidityRouter,
1210
1188
  TAccountMaker,
1211
1189
  TAccountTokenProgram,
1212
1190
  TAccountAssociatedTokenProgram,
@@ -1274,14 +1252,12 @@ export type ParsedOptionMintInstruction<
1274
1252
  escrowTokenAccount?: TAccountMetas[21] | undefined;
1275
1253
  /** Pool loan account (optional - only required if borrowing) */
1276
1254
  poolLoan?: TAccountMetas[22] | undefined;
1277
- /** Liquidity router (optional - only required if borrowing with SAP1/SAP2 failover) */
1278
- liquidityRouter?: TAccountMetas[23] | undefined;
1279
- maker: TAccountMetas[24];
1280
- tokenProgram: TAccountMetas[25];
1281
- associatedTokenProgram: TAccountMetas[26];
1282
- tokenMetadataProgram: TAccountMetas[27];
1283
- systemProgram: TAccountMetas[28];
1284
- rent: TAccountMetas[29];
1255
+ maker: TAccountMetas[23];
1256
+ tokenProgram: TAccountMetas[24];
1257
+ associatedTokenProgram: TAccountMetas[25];
1258
+ tokenMetadataProgram: TAccountMetas[26];
1259
+ systemProgram: TAccountMetas[27];
1260
+ rent: TAccountMetas[28];
1285
1261
  };
1286
1262
  data: OptionMintInstructionData;
1287
1263
  };
@@ -1294,7 +1270,7 @@ export function parseOptionMintInstruction<
1294
1270
  InstructionWithAccounts<TAccountMetas> &
1295
1271
  InstructionWithData<ReadonlyUint8Array>,
1296
1272
  ): ParsedOptionMintInstruction<TProgram, TAccountMetas> {
1297
- if (instruction.accounts.length < 30) {
1273
+ if (instruction.accounts.length < 29) {
1298
1274
  // TODO: Coded error.
1299
1275
  throw new Error("Not enough accounts");
1300
1276
  }
@@ -1336,7 +1312,6 @@ export function parseOptionMintInstruction<
1336
1312
  escrowAuthority: getNextOptionalAccount(),
1337
1313
  escrowTokenAccount: getNextOptionalAccount(),
1338
1314
  poolLoan: getNextOptionalAccount(),
1339
- liquidityRouter: getNextOptionalAccount(),
1340
1315
  maker: getNextAccount(),
1341
1316
  tokenProgram: getNextAccount(),
1342
1317
  associatedTokenProgram: getNextAccount(),
@@ -26,7 +26,6 @@ import {
26
26
  parseCloseLongToPoolInstruction,
27
27
  parseCloseOptionInstruction,
28
28
  parseCreateEscrowV2Instruction,
29
- parseCreateLiquidityRouterInstruction,
30
29
  parseDepositCollateralInstruction,
31
30
  parseDepositToPositionInstruction,
32
31
  parseInitCollateralPoolInstruction,
@@ -35,7 +34,6 @@ import {
35
34
  parseInitOptionPoolInstruction,
36
35
  parseLiquidateWriterPositionInstruction,
37
36
  parseOmlpCreateVaultInstruction,
38
- parseOmlpTakeOfferWithFailoverInstruction,
39
37
  parseOmlpUpdateMaxLeverageInstruction,
40
38
  parseOmlpUpdateProtocolFeeInstruction,
41
39
  parseOmlpUpdateSupplyLimitInstruction,
@@ -61,7 +59,6 @@ import {
61
59
  type ParsedCloseLongToPoolInstruction,
62
60
  type ParsedCloseOptionInstruction,
63
61
  type ParsedCreateEscrowV2Instruction,
64
- type ParsedCreateLiquidityRouterInstruction,
65
62
  type ParsedDepositCollateralInstruction,
66
63
  type ParsedDepositToPositionInstruction,
67
64
  type ParsedInitCollateralPoolInstruction,
@@ -70,7 +67,6 @@ import {
70
67
  type ParsedInitOptionPoolInstruction,
71
68
  type ParsedLiquidateWriterPositionInstruction,
72
69
  type ParsedOmlpCreateVaultInstruction,
73
- type ParsedOmlpTakeOfferWithFailoverInstruction,
74
70
  type ParsedOmlpUpdateMaxLeverageInstruction,
75
71
  type ParsedOmlpUpdateProtocolFeeInstruction,
76
72
  type ParsedOmlpUpdateSupplyLimitInstruction,
@@ -95,10 +91,8 @@ export const OPTION_PROGRAM_PROGRAM_ADDRESS =
95
91
  export enum OptionProgramAccount {
96
92
  CollateralPool,
97
93
  Config,
98
- DualSourceContract,
99
94
  EscrowState,
100
95
  LenderPosition,
101
- LiquidityRouter,
102
96
  MakerCollateralShare,
103
97
  MarketDataAccount,
104
98
  OptionAccount,
@@ -136,17 +130,6 @@ export function identifyOptionProgramAccount(
136
130
  ) {
137
131
  return OptionProgramAccount.Config;
138
132
  }
139
- if (
140
- containsBytes(
141
- data,
142
- fixEncoderSize(getBytesEncoder(), 8).encode(
143
- new Uint8Array([114, 105, 97, 181, 162, 170, 126, 215]),
144
- ),
145
- 0,
146
- )
147
- ) {
148
- return OptionProgramAccount.DualSourceContract;
149
- }
150
133
  if (
151
134
  containsBytes(
152
135
  data,
@@ -169,17 +152,6 @@ export function identifyOptionProgramAccount(
169
152
  ) {
170
153
  return OptionProgramAccount.LenderPosition;
171
154
  }
172
- if (
173
- containsBytes(
174
- data,
175
- fixEncoderSize(getBytesEncoder(), 8).encode(
176
- new Uint8Array([151, 96, 76, 12, 200, 0, 147, 111]),
177
- ),
178
- 0,
179
- )
180
- ) {
181
- return OptionProgramAccount.LiquidityRouter;
182
- }
183
155
  if (
184
156
  containsBytes(
185
157
  data,
@@ -294,7 +266,6 @@ export enum OptionProgramInstruction {
294
266
  CloseLongToPool,
295
267
  CloseOption,
296
268
  CreateEscrowV2,
297
- CreateLiquidityRouter,
298
269
  DepositCollateral,
299
270
  DepositToPosition,
300
271
  InitCollateralPool,
@@ -303,7 +274,6 @@ export enum OptionProgramInstruction {
303
274
  InitializeMarketData,
304
275
  LiquidateWriterPosition,
305
276
  OmlpCreateVault,
306
- OmlpTakeOfferWithFailover,
307
277
  OmlpUpdateMaxLeverage,
308
278
  OmlpUpdateProtocolFee,
309
279
  OmlpUpdateSupplyLimit,
@@ -425,17 +395,6 @@ export function identifyOptionProgramInstruction(
425
395
  ) {
426
396
  return OptionProgramInstruction.CreateEscrowV2;
427
397
  }
428
- if (
429
- containsBytes(
430
- data,
431
- fixEncoderSize(getBytesEncoder(), 8).encode(
432
- new Uint8Array([226, 122, 201, 80, 25, 49, 83, 84]),
433
- ),
434
- 0,
435
- )
436
- ) {
437
- return OptionProgramInstruction.CreateLiquidityRouter;
438
- }
439
398
  if (
440
399
  containsBytes(
441
400
  data,
@@ -524,17 +483,6 @@ export function identifyOptionProgramInstruction(
524
483
  ) {
525
484
  return OptionProgramInstruction.OmlpCreateVault;
526
485
  }
527
- if (
528
- containsBytes(
529
- data,
530
- fixEncoderSize(getBytesEncoder(), 8).encode(
531
- new Uint8Array([39, 155, 118, 226, 84, 159, 5, 76]),
532
- ),
533
- 0,
534
- )
535
- ) {
536
- return OptionProgramInstruction.OmlpTakeOfferWithFailover;
537
- }
538
486
  if (
539
487
  containsBytes(
540
488
  data,
@@ -746,9 +694,6 @@ export type ParsedOptionProgramInstruction<
746
694
  | ({
747
695
  instructionType: OptionProgramInstruction.CreateEscrowV2;
748
696
  } & ParsedCreateEscrowV2Instruction<TProgram>)
749
- | ({
750
- instructionType: OptionProgramInstruction.CreateLiquidityRouter;
751
- } & ParsedCreateLiquidityRouterInstruction<TProgram>)
752
697
  | ({
753
698
  instructionType: OptionProgramInstruction.DepositCollateral;
754
699
  } & ParsedDepositCollateralInstruction<TProgram>)
@@ -773,9 +718,6 @@ export type ParsedOptionProgramInstruction<
773
718
  | ({
774
719
  instructionType: OptionProgramInstruction.OmlpCreateVault;
775
720
  } & ParsedOmlpCreateVaultInstruction<TProgram>)
776
- | ({
777
- instructionType: OptionProgramInstruction.OmlpTakeOfferWithFailover;
778
- } & ParsedOmlpTakeOfferWithFailoverInstruction<TProgram>)
779
721
  | ({
780
722
  instructionType: OptionProgramInstruction.OmlpUpdateMaxLeverage;
781
723
  } & ParsedOmlpUpdateMaxLeverageInstruction<TProgram>)
@@ -893,13 +835,6 @@ export function parseOptionProgramInstruction<TProgram extends string>(
893
835
  ...parseCreateEscrowV2Instruction(instruction),
894
836
  };
895
837
  }
896
- case OptionProgramInstruction.CreateLiquidityRouter: {
897
- assertIsInstructionWithAccounts(instruction);
898
- return {
899
- instructionType: OptionProgramInstruction.CreateLiquidityRouter,
900
- ...parseCreateLiquidityRouterInstruction(instruction),
901
- };
902
- }
903
838
  case OptionProgramInstruction.DepositCollateral: {
904
839
  assertIsInstructionWithAccounts(instruction);
905
840
  return {
@@ -956,13 +891,6 @@ export function parseOptionProgramInstruction<TProgram extends string>(
956
891
  ...parseOmlpCreateVaultInstruction(instruction),
957
892
  };
958
893
  }
959
- case OptionProgramInstruction.OmlpTakeOfferWithFailover: {
960
- assertIsInstructionWithAccounts(instruction);
961
- return {
962
- instructionType: OptionProgramInstruction.OmlpTakeOfferWithFailover,
963
- ...parseOmlpTakeOfferWithFailoverInstruction(instruction),
964
- };
965
- }
966
894
  case OptionProgramInstruction.OmlpUpdateMaxLeverage: {
967
895
  assertIsInstructionWithAccounts(instruction);
968
896
  return {
@@ -6,13 +6,8 @@
6
6
  * @see https://github.com/codama-idl/codama
7
7
  */
8
8
 
9
- export * from "./borrowedFromSAP1";
10
- export * from "./borrowedFromSAP2";
11
- export * from "./failoverTriggered";
12
9
  export * from "./impliedVolatilityUpdated";
13
10
  export * from "./liquidationExecuted";
14
- export * from "./liquidityMetrics";
15
- export * from "./liquidityRouterCreated";
16
11
  export * from "./marketDataInitialized";
17
12
  export * from "./marketDataUpdated";
18
13
  export * from "./optionClosed";
@@ -27,6 +22,5 @@ export * from "./positionDeposited";
27
22
  export * from "./positionWithdrawn";
28
23
  export * from "./priceFeedMessage";
29
24
  export * from "./protocolFeeUpdated";
30
- export * from "./sap2Provider";
31
25
  export * from "./vaultCreated";
32
26
  export * from "./verificationLevel";
package/index.ts CHANGED
@@ -24,3 +24,11 @@ export * from "./short/pool";
24
24
 
25
25
  export * from "./omlp/builders";
26
26
  export * from "./omlp/service";
27
+
28
+ export {
29
+ getWrapSOLInstructions,
30
+ getUnwrapSOLInstructions,
31
+ getSyncNativeInstruction,
32
+ getCloseAccountInstruction,
33
+ NATIVE_MINT,
34
+ } from "./wsol/instructions";
package/omlp/service.ts CHANGED
@@ -1,3 +1,8 @@
1
+ /**
2
+ * OMLP (Option Maker Liquidity Pool) service – V2 pool-based API only.
3
+ * Exposes: depositToPosition, withdrawFromPosition, withdrawAllFromPosition, withdrawInterestFromPosition.
4
+ * Borrow/repay use short/pool (buildBorrowFromPool*, buildRepayPoolLoan*). Legacy offer-based instructions are not exposed.
5
+ */
1
6
  import { fetchLenderPosition, fetchVault } from "../accounts/fetchers";
2
7
  import type { KitRpc } from "../client/types";
3
8
  import {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@epicentral/sos-sdk",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "private": false,
5
5
  "description": "Solana Option Standard SDK. The frontend-first SDK for Native Options Trading on Solana. Created by Epicentral Labs.",
6
6
  "type": "module",
@@ -17,6 +17,7 @@
17
17
  "dependencies": {
18
18
  "@solana-program/address-lookup-table": "^0.11.0",
19
19
  "@solana-program/compute-budget": "^0.13.0",
20
+ "@solana-program/system": "^0.11.0",
20
21
  "@solana/kit": "^6.1.0",
21
22
  "bs58": "^6.0.0",
22
23
  "decimal.js": "^10.4.3"
package/short/builders.ts CHANGED
@@ -55,7 +55,6 @@ export interface BuildOptionMintParams {
55
55
  escrowAuthority?: AddressLike;
56
56
  escrowTokenAccount?: AddressLike;
57
57
  poolLoan?: AddressLike;
58
- liquidityRouter?: AddressLike;
59
58
  remainingAccounts?: RemainingAccountInput[];
60
59
  }
61
60
 
@@ -168,7 +167,6 @@ export async function buildOptionMintInstruction(
168
167
  ? toAddress(params.escrowTokenAccount)
169
168
  : undefined,
170
169
  poolLoan: params.poolLoan ? toAddress(params.poolLoan) : undefined,
171
- liquidityRouter: params.liquidityRouter ? toAddress(params.liquidityRouter) : undefined,
172
170
  maker: toAddress(params.maker) as any,
173
171
  optionType: params.optionType,
174
172
  strikePrice: params.strikePrice,
@@ -210,7 +208,6 @@ export interface BuildOptionMintTransactionWithDerivationParams {
210
208
  escrowAuthority?: AddressLike;
211
209
  escrowTokenAccount?: AddressLike;
212
210
  poolLoan?: AddressLike;
213
- liquidityRouter?: AddressLike;
214
211
  remainingAccounts?: RemainingAccountInput[];
215
212
  }
216
213
 
@@ -278,7 +275,6 @@ export async function buildOptionMintTransactionWithDerivation(
278
275
  escrowAuthority: params.escrowAuthority,
279
276
  escrowTokenAccount: params.escrowTokenAccount,
280
277
  poolLoan: params.poolLoan,
281
- liquidityRouter: params.liquidityRouter,
282
278
  remainingAccounts: params.remainingAccounts,
283
279
  });
284
280
  }
@@ -0,0 +1,221 @@
1
+ import {
2
+ AccountRole,
3
+ address,
4
+ isTransactionSigner,
5
+ type AccountMeta,
6
+ type Address,
7
+ type Instruction,
8
+ type TransactionSigner,
9
+ upgradeRoleToSigner,
10
+ } from "@solana/kit";
11
+ import { getTransferSolInstruction } from "@solana-program/system";
12
+ import { deriveAssociatedTokenAddress } from "../accounts/pdas";
13
+ import { toAddress } from "../client/program";
14
+ import type { AddressLike, KitRpc } from "../client/types";
15
+
16
+ /** Wrapped SOL mint address (WSOL). */
17
+ export const NATIVE_MINT = address("So11111111111111111111111111111111111111112");
18
+
19
+ const TOKEN_PROGRAM_ADDRESS = address("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
20
+ const ASSOCIATED_TOKEN_PROGRAM_ADDRESS = address(
21
+ "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
22
+ );
23
+ const SYSTEM_PROGRAM_ADDRESS = address("11111111111111111111111111111111");
24
+
25
+ /** SPL Token instruction discriminator: SyncNative. */
26
+ const SYNC_NATIVE_DISCRIMINATOR = 17;
27
+ /** SPL Token instruction discriminator: CloseAccount. */
28
+ const CLOSE_ACCOUNT_DISCRIMINATOR = 9;
29
+ /** Associated Token Program instruction discriminator: CreateIdempotent. */
30
+ const CREATE_ASSOCIATED_TOKEN_IDEMPOTENT_DISCRIMINATOR = 1;
31
+
32
+ /** SPL Token account data: amount field offset (u64 LE). */
33
+ const TOKEN_ACCOUNT_AMOUNT_OFFSET = 64;
34
+
35
+ function accountMeta(
36
+ addr: Address,
37
+ role: AccountRole,
38
+ signer?: TransactionSigner
39
+ ): AccountMeta<string> {
40
+ const base = { address: addr, role };
41
+ if (signer !== undefined) {
42
+ return Object.freeze({ ...base, role: upgradeRoleToSigner(role), signer }) as AccountMeta<string>;
43
+ }
44
+ return Object.freeze(base) as AccountMeta<string>;
45
+ }
46
+
47
+ /**
48
+ * Builds the SPL Token SyncNative instruction (for WSOL wrap). Syncs the native token
49
+ * account's amount with its lamport balance.
50
+ */
51
+ export function getSyncNativeInstruction(
52
+ account: AddressLike,
53
+ tokenProgram: AddressLike = TOKEN_PROGRAM_ADDRESS
54
+ ): Instruction<string> {
55
+ const programAddress = toAddress(tokenProgram);
56
+ return Object.freeze({
57
+ programAddress,
58
+ accounts: [
59
+ accountMeta(toAddress(account), AccountRole.WRITABLE),
60
+ ],
61
+ data: new Uint8Array([SYNC_NATIVE_DISCRIMINATOR]),
62
+ });
63
+ }
64
+
65
+ /**
66
+ * Builds the SPL Token CloseAccount instruction (for WSOL unwrap). Closes the token
67
+ * account and sends lamports to destination. Owner must sign the transaction.
68
+ */
69
+ export function getCloseAccountInstruction(
70
+ account: AddressLike,
71
+ destination: AddressLike,
72
+ owner: AddressLike | TransactionSigner<string>,
73
+ tokenProgram: AddressLike = TOKEN_PROGRAM_ADDRESS
74
+ ): Instruction<string> {
75
+ const programAddress = toAddress(tokenProgram);
76
+ const ownerAddress = toAddress(
77
+ typeof owner === "object" && owner !== null && "address" in owner
78
+ ? (owner as TransactionSigner<string>).address
79
+ : owner
80
+ );
81
+ const ownerSigner: TransactionSigner<string> | undefined =
82
+ typeof owner === "object" && owner !== null && "address" in owner && isTransactionSigner(owner)
83
+ ? owner
84
+ : undefined;
85
+ const ownerMeta: AccountMeta<string> = ownerSigner
86
+ ? Object.freeze({
87
+ address: ownerAddress,
88
+ role: upgradeRoleToSigner(AccountRole.READONLY),
89
+ signer: ownerSigner,
90
+ }) as AccountMeta<string>
91
+ : accountMeta(ownerAddress, AccountRole.READONLY);
92
+ return Object.freeze({
93
+ programAddress,
94
+ accounts: [
95
+ accountMeta(toAddress(account), AccountRole.WRITABLE),
96
+ accountMeta(toAddress(destination), AccountRole.WRITABLE),
97
+ ownerMeta,
98
+ ],
99
+ data: new Uint8Array([CLOSE_ACCOUNT_DISCRIMINATOR]),
100
+ });
101
+ }
102
+
103
+ /**
104
+ * Builds the Associated Token Program CreateIdempotent instruction. Safe to add
105
+ * even if the ATA already exists.
106
+ */
107
+ async function getCreateAssociatedTokenIdempotentInstruction(
108
+ payer: TransactionSigner<string>,
109
+ owner: AddressLike,
110
+ mint: AddressLike,
111
+ associatedToken: Address
112
+ ): Promise<Instruction<string>> {
113
+ const programAddress = ASSOCIATED_TOKEN_PROGRAM_ADDRESS;
114
+ return Object.freeze({
115
+ programAddress,
116
+ accounts: [
117
+ accountMeta(payer.address as Address, AccountRole.WRITABLE, payer),
118
+ accountMeta(associatedToken, AccountRole.WRITABLE),
119
+ accountMeta(toAddress(owner), AccountRole.READONLY),
120
+ accountMeta(toAddress(mint), AccountRole.READONLY),
121
+ accountMeta(SYSTEM_PROGRAM_ADDRESS, AccountRole.READONLY),
122
+ accountMeta(TOKEN_PROGRAM_ADDRESS, AccountRole.READONLY),
123
+ ],
124
+ data: new Uint8Array([CREATE_ASSOCIATED_TOKEN_IDEMPOTENT_DISCRIMINATOR]),
125
+ });
126
+ }
127
+
128
+ /**
129
+ * Returns instructions to wrap SOL into WSOL: create WSOL ATA (idempotent),
130
+ * transfer lamports to it, then sync native. Uses SDK's deriveAssociatedTokenAddress.
131
+ */
132
+ export async function getWrapSOLInstructions(params: {
133
+ payer: TransactionSigner<string>;
134
+ lamports: bigint;
135
+ owner?: AddressLike;
136
+ tokenProgram?: AddressLike;
137
+ wsolMint?: AddressLike;
138
+ }): Promise<Instruction<string>[]> {
139
+ const owner = params.owner ?? params.payer.address;
140
+ const wsolMint = params.wsolMint ?? NATIVE_MINT;
141
+ const tokenProgram = params.tokenProgram ?? TOKEN_PROGRAM_ADDRESS;
142
+
143
+ const wsolAta = await deriveAssociatedTokenAddress(owner, wsolMint);
144
+
145
+ const createAta = await getCreateAssociatedTokenIdempotentInstruction(
146
+ params.payer,
147
+ owner,
148
+ wsolMint,
149
+ wsolAta
150
+ );
151
+
152
+ const transfer = getTransferSolInstruction({
153
+ source: params.payer,
154
+ destination: wsolAta,
155
+ amount: params.lamports,
156
+ });
157
+
158
+ const syncNative = getSyncNativeInstruction(wsolAta, tokenProgram);
159
+
160
+ return [createAta, transfer, syncNative];
161
+ }
162
+
163
+ /**
164
+ * Reads token account amount from raw account data (SPL token account layout).
165
+ */
166
+ function decodeTokenAccountAmount(data: Uint8Array): bigint {
167
+ if (data.length < TOKEN_ACCOUNT_AMOUNT_OFFSET + 8) return BigInt(0);
168
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
169
+ return view.getBigUint64(TOKEN_ACCOUNT_AMOUNT_OFFSET, true);
170
+ }
171
+
172
+ /**
173
+ * Fetches WSOL ATA balance via RPC. Returns 0n if account missing or invalid.
174
+ */
175
+ async function fetchWsolAtaBalance(rpc: KitRpc, ata: Address): Promise<bigint> {
176
+ const response = await rpc.getAccountInfo(ata, { encoding: "base64" }).send();
177
+ const accountInfo = response.value;
178
+ if (!accountInfo) return BigInt(0);
179
+ const [b64] = accountInfo.data;
180
+ if (!b64) return BigInt(0);
181
+ const binary = atob(b64);
182
+ const data = new Uint8Array(binary.length);
183
+ for (let i = 0; i < binary.length; i++) data[i] = binary.charCodeAt(i);
184
+ return decodeTokenAccountAmount(data);
185
+ }
186
+
187
+ /**
188
+ * Returns the instruction to unwrap WSOL (close WSOL ATA and send SOL to destination),
189
+ * or null if the WSOL ATA has zero balance. Uses SDK's deriveAssociatedTokenAddress.
190
+ * Owner must be provided as a signer when the app signs the transaction.
191
+ */
192
+ export async function getUnwrapSOLInstructions(params: {
193
+ owner: AddressLike | TransactionSigner<string>;
194
+ rpc: KitRpc;
195
+ destination?: AddressLike;
196
+ tokenProgram?: AddressLike;
197
+ wsolMint?: AddressLike;
198
+ }): Promise<Instruction<string>[] | null> {
199
+ const destination = params.destination ?? (typeof params.owner === "object" && params.owner !== null && "address" in params.owner
200
+ ? (params.owner as TransactionSigner<string>).address
201
+ : toAddress(params.owner));
202
+ const wsolMint = params.wsolMint ?? NATIVE_MINT;
203
+ const tokenProgram = params.tokenProgram ?? TOKEN_PROGRAM_ADDRESS;
204
+
205
+ const ownerAddress = toAddress(
206
+ typeof params.owner === "object" && params.owner !== null && "address" in params.owner
207
+ ? (params.owner as TransactionSigner<string>).address
208
+ : params.owner
209
+ );
210
+ const wsolAta = await deriveAssociatedTokenAddress(ownerAddress, wsolMint);
211
+ const balance = await fetchWsolAtaBalance(params.rpc, wsolAta);
212
+ if (balance === BigInt(0)) return null;
213
+
214
+ const close = getCloseAccountInstruction(
215
+ wsolAta,
216
+ destination,
217
+ params.owner,
218
+ tokenProgram
219
+ );
220
+ return [close];
221
+ }