@meteora-ag/zap-sdk 1.3.2 → 2.0.0-rc.1

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.
package/dist/index.js CHANGED
@@ -75,12 +75,14 @@ __export(index_exports, {
75
75
  DAMM_V2_PROGRAM_ID: () => DAMM_V2_PROGRAM_ID,
76
76
  DAMM_V2_SWAP_DISCRIMINATOR: () => DAMM_V2_SWAP_DISCRIMINATOR,
77
77
  DEFAULT_JUPITER_API_URL: () => DEFAULT_JUPITER_API_URL,
78
+ DEFAULT_JUPITER_API_VERSION: () => DEFAULT_JUPITER_API_VERSION,
78
79
  DLMM_PROGRAM_ID: () => DLMM_PROGRAM_ID,
79
80
  DLMM_SWAP_DISCRIMINATOR: () => DLMM_SWAP_DISCRIMINATOR,
80
81
  DlmmDirectSwapQuoteRoute: () => DlmmDirectSwapQuoteRoute,
81
82
  DlmmSingleSided: () => DlmmSingleSided,
82
83
  DlmmSwapType: () => DlmmSwapType,
83
84
  JUP_V6_PROGRAM_ID: () => JUP_V6_PROGRAM_ID,
85
+ JupiterApiVersion: () => JupiterApiVersion,
84
86
  MEMO_PROGRAM_ID: () => MEMO_PROGRAM_ID,
85
87
  SwapExternalType: () => SwapExternalType,
86
88
  ZAP_PROGRAM_ID: () => ZAP_PROGRAM_ID,
@@ -125,21 +127,30 @@ module.exports = __toCommonJS(index_exports);
125
127
  // src/zap.ts
126
128
  var import_web38 = require("@solana/web3.js");
127
129
  var import_invariant2 = __toESM(require("invariant"));
128
- var import_anchor = require("@coral-xyz/anchor");
130
+ var import_core = require("@anchor-lang/core");
129
131
 
130
132
  // src/idl/zap/idl.json
131
133
  var idl_default = {
132
134
  address: "zapvX9M3uf5pvy4wRPAbQgdQsM1xmuiFnkfHKPvwMiz",
133
135
  metadata: {
134
136
  name: "zap",
135
- version: "0.2.0",
137
+ version: "0.2.3",
136
138
  spec: "0.1.0",
137
139
  description: "Created with Anchor"
138
140
  },
139
141
  instructions: [
140
142
  {
141
143
  name: "close_ledger_account",
142
- discriminator: [189, 122, 172, 13, 122, 54, 54, 51],
144
+ discriminator: [
145
+ 189,
146
+ 122,
147
+ 172,
148
+ 13,
149
+ 122,
150
+ 54,
151
+ 54,
152
+ 51
153
+ ],
143
154
  accounts: [
144
155
  {
145
156
  name: "ledger",
@@ -148,7 +159,9 @@ var idl_default = {
148
159
  {
149
160
  name: "owner",
150
161
  signer: true,
151
- relations: ["ledger"]
162
+ relations: [
163
+ "ledger"
164
+ ]
152
165
  },
153
166
  {
154
167
  name: "rent_receiver",
@@ -160,7 +173,16 @@ var idl_default = {
160
173
  },
161
174
  {
162
175
  name: "initialize_ledger_account",
163
- discriminator: [120, 69, 30, 74, 76, 242, 153, 162],
176
+ discriminator: [
177
+ 120,
178
+ 69,
179
+ 30,
180
+ 74,
181
+ 76,
182
+ 242,
183
+ 153,
184
+ 162
185
+ ],
164
186
  accounts: [
165
187
  {
166
188
  name: "ledger",
@@ -169,7 +191,19 @@ var idl_default = {
169
191
  seeds: [
170
192
  {
171
193
  kind: "const",
172
- value: [117, 115, 101, 114, 95, 108, 101, 100, 103, 101, 114]
194
+ value: [
195
+ 117,
196
+ 115,
197
+ 101,
198
+ 114,
199
+ 95,
200
+ 108,
201
+ 101,
202
+ 100,
203
+ 103,
204
+ 101,
205
+ 114
206
+ ]
173
207
  },
174
208
  {
175
209
  kind: "account",
@@ -196,7 +230,16 @@ var idl_default = {
196
230
  },
197
231
  {
198
232
  name: "set_ledger_balance",
199
- discriminator: [131, 49, 240, 17, 228, 248, 156, 54],
233
+ discriminator: [
234
+ 131,
235
+ 49,
236
+ 240,
237
+ 17,
238
+ 228,
239
+ 248,
240
+ 156,
241
+ 54
242
+ ],
200
243
  accounts: [
201
244
  {
202
245
  name: "ledger",
@@ -205,7 +248,9 @@ var idl_default = {
205
248
  {
206
249
  name: "owner",
207
250
  signer: true,
208
- relations: ["ledger"]
251
+ relations: [
252
+ "ledger"
253
+ ]
209
254
  }
210
255
  ],
211
256
  args: [
@@ -221,7 +266,16 @@ var idl_default = {
221
266
  },
222
267
  {
223
268
  name: "update_ledger_balance_after_swap",
224
- discriminator: [59, 206, 173, 232, 94, 57, 174, 202],
269
+ discriminator: [
270
+ 59,
271
+ 206,
272
+ 173,
273
+ 232,
274
+ 94,
275
+ 57,
276
+ 174,
277
+ 202
278
+ ],
225
279
  accounts: [
226
280
  {
227
281
  name: "ledger",
@@ -233,7 +287,9 @@ var idl_default = {
233
287
  {
234
288
  name: "owner",
235
289
  signer: true,
236
- relations: ["ledger"]
290
+ relations: [
291
+ "ledger"
292
+ ]
237
293
  }
238
294
  ],
239
295
  args: [
@@ -253,7 +309,16 @@ var idl_default = {
253
309
  },
254
310
  {
255
311
  name: "zap_in_damm_v2",
256
- discriminator: [243, 243, 119, 52, 199, 44, 154, 186],
312
+ discriminator: [
313
+ 243,
314
+ 243,
315
+ 119,
316
+ 52,
317
+ 199,
318
+ 44,
319
+ 154,
320
+ 186
321
+ ],
257
322
  accounts: [
258
323
  {
259
324
  name: "ledger",
@@ -297,9 +362,13 @@ var idl_default = {
297
362
  },
298
363
  {
299
364
  name: "owner",
300
- docs: ["owner of position"],
365
+ docs: [
366
+ "owner of position"
367
+ ],
301
368
  signer: true,
302
- relations: ["ledger"]
369
+ relations: [
370
+ "ledger"
371
+ ]
303
372
  },
304
373
  {
305
374
  name: "token_a_program"
@@ -328,7 +397,16 @@ var idl_default = {
328
397
  },
329
398
  {
330
399
  name: "zap_in_dlmm_for_initialized_position",
331
- discriminator: [184, 71, 198, 231, 129, 110, 193, 67],
400
+ discriminator: [
401
+ 184,
402
+ 71,
403
+ 198,
404
+ 231,
405
+ 129,
406
+ 110,
407
+ 193,
408
+ 67
409
+ ],
332
410
  accounts: [
333
411
  {
334
412
  name: "ledger",
@@ -336,7 +414,9 @@ var idl_default = {
336
414
  },
337
415
  {
338
416
  name: "lb_pair",
339
- docs: ["lb pair"],
417
+ docs: [
418
+ "lb pair"
419
+ ],
340
420
  writable: true
341
421
  },
342
422
  {
@@ -376,9 +456,13 @@ var idl_default = {
376
456
  },
377
457
  {
378
458
  name: "owner",
379
- docs: ["owner of position"],
459
+ docs: [
460
+ "owner of position"
461
+ ],
380
462
  signer: true,
381
- relations: ["ledger"]
463
+ relations: [
464
+ "ledger"
465
+ ]
382
466
  },
383
467
  {
384
468
  name: "rent_payer",
@@ -442,7 +526,16 @@ var idl_default = {
442
526
  },
443
527
  {
444
528
  name: "zap_in_dlmm_for_uninitialized_position",
445
- discriminator: [59, 220, 182, 27, 254, 253, 2, 232],
529
+ discriminator: [
530
+ 59,
531
+ 220,
532
+ 182,
533
+ 27,
534
+ 254,
535
+ 253,
536
+ 2,
537
+ 232
538
+ ],
446
539
  accounts: [
447
540
  {
448
541
  name: "ledger",
@@ -450,7 +543,9 @@ var idl_default = {
450
543
  },
451
544
  {
452
545
  name: "lb_pair",
453
- docs: ["lb pair"],
546
+ docs: [
547
+ "lb pair"
548
+ ],
454
549
  writable: true
455
550
  },
456
551
  {
@@ -495,9 +590,13 @@ var idl_default = {
495
590
  },
496
591
  {
497
592
  name: "owner",
498
- docs: ["owner of position"],
593
+ docs: [
594
+ "owner of position"
595
+ ],
499
596
  signer: true,
500
- relations: ["ledger"]
597
+ relations: [
598
+ "ledger"
599
+ ]
501
600
  },
502
601
  {
503
602
  name: "rent_payer",
@@ -561,7 +660,16 @@ var idl_default = {
561
660
  },
562
661
  {
563
662
  name: "zap_out",
564
- discriminator: [155, 108, 185, 112, 104, 210, 161, 64],
663
+ discriminator: [
664
+ 155,
665
+ 108,
666
+ 185,
667
+ 112,
668
+ 104,
669
+ 210,
670
+ 161,
671
+ 64
672
+ ],
565
673
  accounts: [
566
674
  {
567
675
  name: "user_token_in_account",
@@ -584,17 +692,18 @@ var idl_default = {
584
692
  }
585
693
  ],
586
694
  accounts: [
587
- {
588
- name: "LbPair",
589
- discriminator: [33, 11, 49, 98, 181, 101, 177, 13]
590
- },
591
- {
592
- name: "Pool",
593
- discriminator: [241, 154, 109, 4, 17, 177, 109, 188]
594
- },
595
695
  {
596
696
  name: "UserLedger",
597
- discriminator: [185, 84, 101, 128, 8, 6, 160, 83]
697
+ discriminator: [
698
+ 185,
699
+ 84,
700
+ 101,
701
+ 128,
702
+ 8,
703
+ 6,
704
+ 160,
705
+ 83
706
+ ]
598
707
  }
599
708
  ],
600
709
  errors: [
@@ -661,7 +770,9 @@ var idl_default = {
661
770
  },
662
771
  {
663
772
  name: "TransferHookMultiReward",
664
- fields: ["u8"]
773
+ fields: [
774
+ "u8"
775
+ ]
665
776
  },
666
777
  {
667
778
  name: "TransferHookReferral"
@@ -670,7 +781,7 @@ var idl_default = {
670
781
  }
671
782
  },
672
783
  {
673
- name: "BaseFeeStruct",
784
+ name: "BaseFeeInfo",
674
785
  serialization: "bytemuck",
675
786
  repr: {
676
787
  kind: "c"
@@ -679,33 +790,34 @@ var idl_default = {
679
790
  kind: "struct",
680
791
  fields: [
681
792
  {
682
- name: "cliff_fee_numerator",
683
- type: "u64"
684
- },
685
- {
686
- name: "base_fee_mode",
687
- type: "u8"
688
- },
689
- {
690
- name: "padding_0",
793
+ name: "data",
691
794
  type: {
692
- array: ["u8", 5]
795
+ array: [
796
+ "u8",
797
+ 32
798
+ ]
693
799
  }
694
- },
695
- {
696
- name: "first_factor",
697
- type: "u16"
698
- },
800
+ }
801
+ ]
802
+ }
803
+ },
804
+ {
805
+ name: "BaseFeeStruct",
806
+ serialization: "bytemuck",
807
+ repr: {
808
+ kind: "c"
809
+ },
810
+ type: {
811
+ kind: "struct",
812
+ fields: [
699
813
  {
700
- name: "second_factor",
814
+ name: "base_fee_info",
701
815
  type: {
702
- array: ["u8", 8]
816
+ defined: {
817
+ name: "BaseFeeInfo"
818
+ }
703
819
  }
704
820
  },
705
- {
706
- name: "third_factor",
707
- type: "u64"
708
- },
709
821
  {
710
822
  name: "padding_1",
711
823
  type: "u64"
@@ -729,7 +841,10 @@ var idl_default = {
729
841
  {
730
842
  name: "padding",
731
843
  type: {
732
- array: ["u8", 7]
844
+ array: [
845
+ "u8",
846
+ 7
847
+ ]
733
848
  }
734
849
  },
735
850
  {
@@ -807,13 +922,19 @@ var idl_default = {
807
922
  {
808
923
  name: "bump_seed",
809
924
  type: {
810
- array: ["u8", 1]
925
+ array: [
926
+ "u8",
927
+ 1
928
+ ]
811
929
  }
812
930
  },
813
931
  {
814
932
  name: "bin_step_seed",
815
933
  type: {
816
- array: ["u8", 2]
934
+ array: [
935
+ "u8",
936
+ 2
937
+ ]
817
938
  }
818
939
  },
819
940
  {
@@ -839,7 +960,10 @@ var idl_default = {
839
960
  {
840
961
  name: "base_factor_seed",
841
962
  type: {
842
- array: ["u8", 2]
963
+ array: [
964
+ "u8",
965
+ 2
966
+ ]
843
967
  }
844
968
  },
845
969
  {
@@ -877,7 +1001,10 @@ var idl_default = {
877
1001
  {
878
1002
  name: "_padding_1",
879
1003
  type: {
880
- array: ["u8", 32]
1004
+ array: [
1005
+ "u8",
1006
+ 32
1007
+ ]
881
1008
  }
882
1009
  },
883
1010
  {
@@ -900,7 +1027,10 @@ var idl_default = {
900
1027
  {
901
1028
  name: "bin_array_bitmap",
902
1029
  type: {
903
- array: ["u64", 16]
1030
+ array: [
1031
+ "u64",
1032
+ 16
1033
+ ]
904
1034
  }
905
1035
  },
906
1036
  {
@@ -910,7 +1040,10 @@ var idl_default = {
910
1040
  {
911
1041
  name: "_padding_2",
912
1042
  type: {
913
- array: ["u8", 32]
1043
+ array: [
1044
+ "u8",
1045
+ 32
1046
+ ]
914
1047
  }
915
1048
  },
916
1049
  {
@@ -932,7 +1065,10 @@ var idl_default = {
932
1065
  {
933
1066
  name: "_padding_3",
934
1067
  type: {
935
- array: ["u8", 8]
1068
+ array: [
1069
+ "u8",
1070
+ 8
1071
+ ]
936
1072
  }
937
1073
  },
938
1074
  {
@@ -951,10 +1087,17 @@ var idl_default = {
951
1087
  name: "token_mint_y_program_flag",
952
1088
  type: "u8"
953
1089
  },
1090
+ {
1091
+ name: "version",
1092
+ type: "u8"
1093
+ },
954
1094
  {
955
1095
  name: "_reserved",
956
1096
  type: {
957
- array: ["u8", 22]
1097
+ array: [
1098
+ "u8",
1099
+ 21
1100
+ ]
958
1101
  }
959
1102
  }
960
1103
  ]
@@ -971,7 +1114,9 @@ var idl_default = {
971
1114
  fields: [
972
1115
  {
973
1116
  name: "pool_fees",
974
- docs: ["Pool fee"],
1117
+ docs: [
1118
+ "Pool fee"
1119
+ ],
975
1120
  type: {
976
1121
  defined: {
977
1122
  name: "PoolFeesStruct"
@@ -980,22 +1125,30 @@ var idl_default = {
980
1125
  },
981
1126
  {
982
1127
  name: "token_a_mint",
983
- docs: ["token a mint"],
1128
+ docs: [
1129
+ "token a mint"
1130
+ ],
984
1131
  type: "pubkey"
985
1132
  },
986
1133
  {
987
1134
  name: "token_b_mint",
988
- docs: ["token b mint"],
1135
+ docs: [
1136
+ "token b mint"
1137
+ ],
989
1138
  type: "pubkey"
990
1139
  },
991
1140
  {
992
1141
  name: "token_a_vault",
993
- docs: ["token a vault"],
1142
+ docs: [
1143
+ "token a vault"
1144
+ ],
994
1145
  type: "pubkey"
995
1146
  },
996
1147
  {
997
1148
  name: "token_b_vault",
998
- docs: ["token b vault"],
1149
+ docs: [
1150
+ "token b vault"
1151
+ ],
999
1152
  type: "pubkey"
1000
1153
  },
1001
1154
  {
@@ -1006,17 +1159,26 @@ var idl_default = {
1006
1159
  type: "pubkey"
1007
1160
  },
1008
1161
  {
1009
- name: "partner",
1010
- docs: ["partner"],
1011
- type: "pubkey"
1162
+ name: "padding_0",
1163
+ docs: [
1164
+ "padding, previously partner pubkey, be careful when using this field"
1165
+ ],
1166
+ type: {
1167
+ array: [
1168
+ "u8",
1169
+ 32
1170
+ ]
1171
+ }
1012
1172
  },
1013
1173
  {
1014
1174
  name: "liquidity",
1015
- docs: ["liquidity share"],
1175
+ docs: [
1176
+ "liquidity share"
1177
+ ],
1016
1178
  type: "u128"
1017
1179
  },
1018
1180
  {
1019
- name: "_padding",
1181
+ name: "padding_1",
1020
1182
  docs: [
1021
1183
  "padding, previous reserve amount, be careful to use that field"
1022
1184
  ],
@@ -1024,100 +1186,128 @@ var idl_default = {
1024
1186
  },
1025
1187
  {
1026
1188
  name: "protocol_a_fee",
1027
- docs: ["protocol a fee"],
1189
+ docs: [
1190
+ "protocol a fee"
1191
+ ],
1028
1192
  type: "u64"
1029
1193
  },
1030
1194
  {
1031
1195
  name: "protocol_b_fee",
1032
- docs: ["protocol b fee"],
1033
- type: "u64"
1034
- },
1035
- {
1036
- name: "partner_a_fee",
1037
- docs: ["partner a fee"],
1196
+ docs: [
1197
+ "protocol b fee"
1198
+ ],
1038
1199
  type: "u64"
1039
1200
  },
1040
1201
  {
1041
- name: "partner_b_fee",
1042
- docs: ["partner b fee"],
1043
- type: "u64"
1202
+ name: "padding_2",
1203
+ type: "u128"
1044
1204
  },
1045
1205
  {
1046
1206
  name: "sqrt_min_price",
1047
- docs: ["min price"],
1207
+ docs: [
1208
+ "min price"
1209
+ ],
1048
1210
  type: "u128"
1049
1211
  },
1050
1212
  {
1051
1213
  name: "sqrt_max_price",
1052
- docs: ["max price"],
1214
+ docs: [
1215
+ "max price"
1216
+ ],
1053
1217
  type: "u128"
1054
1218
  },
1055
1219
  {
1056
1220
  name: "sqrt_price",
1057
- docs: ["current price"],
1221
+ docs: [
1222
+ "current price"
1223
+ ],
1058
1224
  type: "u128"
1059
1225
  },
1060
1226
  {
1061
1227
  name: "activation_point",
1062
- docs: ["Activation point, can be slot or timestamp"],
1228
+ docs: [
1229
+ "Activation point, can be slot or timestamp"
1230
+ ],
1063
1231
  type: "u64"
1064
1232
  },
1065
1233
  {
1066
1234
  name: "activation_type",
1067
- docs: ["Activation type, 0 means by slot, 1 means by timestamp"],
1235
+ docs: [
1236
+ "Activation type, 0 means by slot, 1 means by timestamp"
1237
+ ],
1068
1238
  type: "u8"
1069
1239
  },
1070
1240
  {
1071
1241
  name: "pool_status",
1072
- docs: ["pool status, 0: enable, 1 disable"],
1242
+ docs: [
1243
+ "pool status, 0: enable, 1 disable"
1244
+ ],
1073
1245
  type: "u8"
1074
1246
  },
1075
1247
  {
1076
1248
  name: "token_a_flag",
1077
- docs: ["token a flag"],
1249
+ docs: [
1250
+ "token a flag"
1251
+ ],
1078
1252
  type: "u8"
1079
1253
  },
1080
1254
  {
1081
1255
  name: "token_b_flag",
1082
- docs: ["token b flag"],
1256
+ docs: [
1257
+ "token b flag"
1258
+ ],
1083
1259
  type: "u8"
1084
1260
  },
1085
1261
  {
1086
1262
  name: "collect_fee_mode",
1087
1263
  docs: [
1088
- "0 is collect fee in both token, 1 only collect fee in token a, 2 only collect fee in token b"
1264
+ "0 is collect fee in both token, 1 only collect fee only in token b"
1089
1265
  ],
1090
1266
  type: "u8"
1091
1267
  },
1092
1268
  {
1093
1269
  name: "pool_type",
1094
- docs: ["pool type"],
1270
+ docs: [
1271
+ "pool type"
1272
+ ],
1095
1273
  type: "u8"
1096
1274
  },
1097
1275
  {
1098
- name: "version",
1276
+ name: "fee_version",
1099
1277
  docs: [
1100
- "pool version, 0: max_fee is still capped at 50%, 1: max_fee is capped at 99%"
1278
+ "pool fee version, 0: max_fee is still capped at 50%, 1: max_fee is capped at 99%"
1101
1279
  ],
1102
1280
  type: "u8"
1103
1281
  },
1104
1282
  {
1105
- name: "_padding_0",
1106
- docs: ["padding"],
1283
+ name: "padding_3",
1284
+ docs: [
1285
+ "padding"
1286
+ ],
1107
1287
  type: "u8"
1108
1288
  },
1109
1289
  {
1110
1290
  name: "fee_a_per_liquidity",
1111
- docs: ["cumulative"],
1291
+ docs: [
1292
+ "cumulative"
1293
+ ],
1112
1294
  type: {
1113
- array: ["u8", 32]
1295
+ array: [
1296
+ "u8",
1297
+ 32
1298
+ ]
1114
1299
  }
1115
1300
  },
1116
1301
  {
1117
1302
  name: "fee_b_per_liquidity",
1118
- docs: ["cumulative"],
1303
+ docs: [
1304
+ "cumulative"
1305
+ ],
1119
1306
  type: {
1120
- array: ["u8", 32]
1307
+ array: [
1308
+ "u8",
1309
+ 32
1310
+ ]
1121
1311
  }
1122
1312
  },
1123
1313
  {
@@ -1126,7 +1316,9 @@ var idl_default = {
1126
1316
  },
1127
1317
  {
1128
1318
  name: "metrics",
1129
- docs: ["metrics"],
1319
+ docs: [
1320
+ "metrics"
1321
+ ],
1130
1322
  type: {
1131
1323
  defined: {
1132
1324
  name: "PoolMetrics"
@@ -1135,19 +1327,61 @@ var idl_default = {
1135
1327
  },
1136
1328
  {
1137
1329
  name: "creator",
1138
- docs: ["pool creator"],
1330
+ docs: [
1331
+ "pool creator"
1332
+ ],
1139
1333
  type: "pubkey"
1140
1334
  },
1141
1335
  {
1142
- name: "_padding_1",
1143
- docs: ["Padding for further use"],
1336
+ name: "token_a_amount",
1337
+ docs: [
1338
+ "token a amount"
1339
+ ],
1340
+ type: "u64"
1341
+ },
1342
+ {
1343
+ name: "token_b_amount",
1344
+ docs: [
1345
+ "token b amount"
1346
+ ],
1347
+ type: "u64"
1348
+ },
1349
+ {
1350
+ name: "layout_version",
1351
+ docs: [
1352
+ "layout version: version 0: haven't track token_a_amount and token_b_amount, version 1: track token_a_amount and token_b_amount"
1353
+ ],
1354
+ type: "u8"
1355
+ },
1356
+ {
1357
+ name: "padding_4",
1358
+ docs: [
1359
+ "Padding for further use"
1360
+ ],
1361
+ type: {
1362
+ array: [
1363
+ "u8",
1364
+ 7
1365
+ ]
1366
+ }
1367
+ },
1368
+ {
1369
+ name: "padding_5",
1370
+ docs: [
1371
+ "Padding for further use"
1372
+ ],
1144
1373
  type: {
1145
- array: ["u64", 6]
1374
+ array: [
1375
+ "u64",
1376
+ 3
1377
+ ]
1146
1378
  }
1147
1379
  },
1148
1380
  {
1149
1381
  name: "reward_infos",
1150
- docs: ["Farming reward information"],
1382
+ docs: [
1383
+ "Farming reward information"
1384
+ ],
1151
1385
  type: {
1152
1386
  array: [
1153
1387
  {
@@ -1168,8 +1402,7 @@ var idl_default = {
1168
1402
  "Information regarding fee charges",
1169
1403
  "trading_fee = amount * trade_fee_numerator / denominator",
1170
1404
  "protocol_fee = trading_fee * protocol_fee_percentage / 100",
1171
- "referral_fee = protocol_fee * referral_percentage / 100",
1172
- "partner_fee = (protocol_fee - referral_fee) * partner_fee_percentage / denominator"
1405
+ "referral_fee = protocol_fee * referral_percentage / 100"
1173
1406
  ],
1174
1407
  serialization: "bytemuck",
1175
1408
  repr: {
@@ -1202,25 +1435,43 @@ var idl_default = {
1202
1435
  type: "u8"
1203
1436
  },
1204
1437
  {
1205
- name: "partner_fee_percent",
1206
- docs: ["partner fee"],
1438
+ name: "padding_0",
1439
+ docs: [
1440
+ "padding for future use"
1441
+ ],
1207
1442
  type: "u8"
1208
1443
  },
1209
1444
  {
1210
1445
  name: "referral_fee_percent",
1211
- docs: ["referral fee"],
1446
+ docs: [
1447
+ "referral fee"
1448
+ ],
1212
1449
  type: "u8"
1213
1450
  },
1214
1451
  {
1215
- name: "padding_0",
1216
- docs: ["padding"],
1452
+ name: "padding_1",
1453
+ docs: [
1454
+ "padding"
1455
+ ],
1217
1456
  type: {
1218
- array: ["u8", 5]
1457
+ array: [
1458
+ "u8",
1459
+ 3
1460
+ ]
1219
1461
  }
1220
1462
  },
1463
+ {
1464
+ name: "compounding_fee_bps",
1465
+ docs: [
1466
+ "compounding fee bps, only non-zero in CollectFeeMode::Compounding"
1467
+ ],
1468
+ type: "u16"
1469
+ },
1221
1470
  {
1222
1471
  name: "dynamic_fee",
1223
- docs: ["dynamic fee"],
1472
+ docs: [
1473
+ "dynamic fee"
1474
+ ],
1224
1475
  type: {
1225
1476
  defined: {
1226
1477
  name: "DynamicFeeStruct"
@@ -1228,11 +1479,8 @@ var idl_default = {
1228
1479
  }
1229
1480
  },
1230
1481
  {
1231
- name: "padding_1",
1232
- docs: ["padding"],
1233
- type: {
1234
- array: ["u64", 2]
1235
- }
1482
+ name: "init_sqrt_price",
1483
+ type: "u128"
1236
1484
  }
1237
1485
  ]
1238
1486
  }
@@ -1263,12 +1511,13 @@ var idl_default = {
1263
1511
  type: "u64"
1264
1512
  },
1265
1513
  {
1266
- name: "total_partner_a_fee",
1267
- type: "u64"
1268
- },
1269
- {
1270
- name: "total_partner_b_fee",
1271
- type: "u64"
1514
+ name: "padding_0",
1515
+ type: {
1516
+ array: [
1517
+ "u64",
1518
+ 2
1519
+ ]
1520
+ }
1272
1521
  },
1273
1522
  {
1274
1523
  name: "total_position",
@@ -1341,7 +1590,9 @@ var idl_default = {
1341
1590
  },
1342
1591
  {
1343
1592
  name: "StaticParameters",
1344
- docs: ["Parameter that set by the protocol"],
1593
+ docs: [
1594
+ "Parameter that set by the protocol"
1595
+ ],
1345
1596
  serialization: "bytemuck",
1346
1597
  repr: {
1347
1598
  kind: "c"
@@ -1389,10 +1640,21 @@ var idl_default = {
1389
1640
  name: "base_fee_power_factor",
1390
1641
  type: "u8"
1391
1642
  },
1643
+ {
1644
+ name: "function_type",
1645
+ type: "u8"
1646
+ },
1647
+ {
1648
+ name: "collect_fee_mode",
1649
+ type: "u8"
1650
+ },
1392
1651
  {
1393
1652
  name: "_padding",
1394
1653
  type: {
1395
- array: ["u8", 5]
1654
+ array: [
1655
+ "u8",
1656
+ 3
1657
+ ]
1396
1658
  }
1397
1659
  }
1398
1660
  ]
@@ -1441,7 +1703,9 @@ var idl_default = {
1441
1703
  },
1442
1704
  {
1443
1705
  name: "VariableParameters",
1444
- docs: ["Parameters that changes based on dynamic of the market"],
1706
+ docs: [
1707
+ "Parameters that changes based on dynamic of the market"
1708
+ ],
1445
1709
  serialization: "bytemuck",
1446
1710
  repr: {
1447
1711
  kind: "c"
@@ -1464,7 +1728,10 @@ var idl_default = {
1464
1728
  {
1465
1729
  name: "_padding",
1466
1730
  type: {
1467
- array: ["u8", 4]
1731
+ array: [
1732
+ "u8",
1733
+ 4
1734
+ ]
1468
1735
  }
1469
1736
  },
1470
1737
  {
@@ -1474,7 +1741,10 @@ var idl_default = {
1474
1741
  {
1475
1742
  name: "_padding_1",
1476
1743
  type: {
1477
- array: ["u8", 8]
1744
+ array: [
1745
+ "u8",
1746
+ 8
1747
+ ]
1478
1748
  }
1479
1749
  }
1480
1750
  ]
@@ -1522,70 +1792,96 @@ var idl_default = {
1522
1792
  fields: [
1523
1793
  {
1524
1794
  name: "initialized",
1525
- docs: ["Indicates if the reward has been initialized"],
1795
+ docs: [
1796
+ "Indicates if the reward has been initialized"
1797
+ ],
1526
1798
  type: "u8"
1527
1799
  },
1528
1800
  {
1529
1801
  name: "reward_token_flag",
1530
- docs: ["reward token flag"],
1802
+ docs: [
1803
+ "reward token flag"
1804
+ ],
1531
1805
  type: "u8"
1532
1806
  },
1533
1807
  {
1534
1808
  name: "_padding_0",
1535
- docs: ["padding"],
1809
+ docs: [
1810
+ "padding"
1811
+ ],
1536
1812
  type: {
1537
- array: ["u8", 6]
1813
+ array: [
1814
+ "u8",
1815
+ 6
1816
+ ]
1538
1817
  }
1539
1818
  },
1540
1819
  {
1541
- name: "_padding_1",
1820
+ name: "dead_liquidity_reward_checkpoint",
1542
1821
  docs: [
1543
- "Padding to ensure `reward_rate: u128` is 16-byte aligned"
1822
+ "Cumulative dead-liquidity reward (Compounding Pool only)"
1544
1823
  ],
1545
- type: {
1546
- array: ["u8", 8]
1547
- }
1824
+ type: "u64"
1548
1825
  },
1549
1826
  {
1550
1827
  name: "mint",
1551
- docs: ["Reward token mint."],
1828
+ docs: [
1829
+ "Reward token mint."
1830
+ ],
1552
1831
  type: "pubkey"
1553
1832
  },
1554
1833
  {
1555
1834
  name: "vault",
1556
- docs: ["Reward vault token account."],
1835
+ docs: [
1836
+ "Reward vault token account."
1837
+ ],
1557
1838
  type: "pubkey"
1558
1839
  },
1559
1840
  {
1560
1841
  name: "funder",
1561
- docs: ["Authority account that allows to fund rewards"],
1842
+ docs: [
1843
+ "Authority account that allows to fund rewards"
1844
+ ],
1562
1845
  type: "pubkey"
1563
1846
  },
1564
1847
  {
1565
1848
  name: "reward_duration",
1566
- docs: ["reward duration"],
1849
+ docs: [
1850
+ "reward duration"
1851
+ ],
1567
1852
  type: "u64"
1568
1853
  },
1569
1854
  {
1570
1855
  name: "reward_duration_end",
1571
- docs: ["reward duration end"],
1856
+ docs: [
1857
+ "reward duration end"
1858
+ ],
1572
1859
  type: "u64"
1573
1860
  },
1574
1861
  {
1575
1862
  name: "reward_rate",
1576
- docs: ["reward rate"],
1863
+ docs: [
1864
+ "reward rate"
1865
+ ],
1577
1866
  type: "u128"
1578
1867
  },
1579
1868
  {
1580
1869
  name: "reward_per_token_stored",
1581
- docs: ["Reward per token stored"],
1870
+ docs: [
1871
+ "Reward per token stored"
1872
+ ],
1582
1873
  type: {
1583
- array: ["u8", 32]
1874
+ array: [
1875
+ "u8",
1876
+ 32
1877
+ ]
1584
1878
  }
1585
1879
  },
1586
1880
  {
1587
1881
  name: "last_update_time",
1588
- docs: ["The last time reward states were updated."],
1882
+ docs: [
1883
+ "The last time reward states were updated."
1884
+ ],
1589
1885
  type: "u64"
1590
1886
  },
1591
1887
  {
@@ -1648,44 +1944,6 @@ var idl_default = {
1648
1944
  }
1649
1945
  ],
1650
1946
  constants: [
1651
- {
1652
- name: "DAMM_V2",
1653
- type: "pubkey",
1654
- value: "cpamdpZCGKUy5JxQXB4dcpGPiikHawvSWAd6mEn1sGG"
1655
- },
1656
- {
1657
- name: "DAMM_V2_SWAP_DISC",
1658
- type: {
1659
- array: ["u8", 8]
1660
- },
1661
- value: "[248, 198, 158, 145, 225, 117, 135, 200]"
1662
- },
1663
- {
1664
- name: "DLMM_SWAP2_DISC",
1665
- type: {
1666
- array: ["u8", 8]
1667
- },
1668
- value: "[65, 75, 63, 76, 235, 91, 91, 136]"
1669
- },
1670
- {
1671
- name: "JUP_V6",
1672
- type: "pubkey",
1673
- value: "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4"
1674
- },
1675
- {
1676
- name: "JUP_V6_ROUTE_DISC",
1677
- type: {
1678
- array: ["u8", 8]
1679
- },
1680
- value: "[229, 23, 203, 151, 122, 227, 173, 42]"
1681
- },
1682
- {
1683
- name: "JUP_V6_SHARED_ACCOUNT_ROUTE_DISC",
1684
- type: {
1685
- array: ["u8", 8]
1686
- },
1687
- value: "[193, 32, 155, 51, 65, 214, 156, 129]"
1688
- },
1689
1947
  {
1690
1948
  name: "MAX_BASIS_POINT",
1691
1949
  type: "u16",
@@ -1700,6 +1958,11 @@ var idl_default = {
1700
1958
  };
1701
1959
 
1702
1960
  // src/types.ts
1961
+ var JupiterApiVersion = /* @__PURE__ */ ((JupiterApiVersion3) => {
1962
+ JupiterApiVersion3["V1"] = "v1";
1963
+ JupiterApiVersion3["V2"] = "v2";
1964
+ return JupiterApiVersion3;
1965
+ })(JupiterApiVersion || {});
1703
1966
  var ZapInDammV2PoolSwapRoute = /* @__PURE__ */ ((ZapInDammV2PoolSwapRoute2) => {
1704
1967
  ZapInDammV2PoolSwapRoute2["Jupiter"] = "jupiter";
1705
1968
  ZapInDammV2PoolSwapRoute2["DammV2"] = "dammV2";
@@ -1729,7 +1992,7 @@ var DlmmSingleSided = /* @__PURE__ */ ((DlmmSingleSided2) => {
1729
1992
  })(DlmmSingleSided || {});
1730
1993
 
1731
1994
  // src/helpers/jupiter.ts
1732
- var import_web32 = require("@solana/web3.js");
1995
+ var import_web33 = require("@solana/web3.js");
1733
1996
 
1734
1997
  // src/constants.ts
1735
1998
  var import_web3 = require("@solana/web3.js");
@@ -1770,6 +2033,68 @@ var AccountsType = {
1770
2033
  var DLMM_SWAP_DISCRIMINATOR = [65, 75, 63, 76, 235, 91, 91, 136];
1771
2034
  var AMOUNT_IN_DLMM_OFFSET = 8;
1772
2035
  var AMOUNT_IN_JUP_V6_REVERSE_OFFSET = 19;
2036
+ var JUP_V6_ROUTE_DISCRIMINATOR = [
2037
+ 229,
2038
+ 23,
2039
+ 203,
2040
+ 151,
2041
+ 122,
2042
+ 227,
2043
+ 173,
2044
+ 42
2045
+ ];
2046
+ var JUP_V6_SHARED_ACCOUNTS_ROUTE_DISCRIMINATOR = [
2047
+ 193,
2048
+ 32,
2049
+ 155,
2050
+ 51,
2051
+ 65,
2052
+ 214,
2053
+ 156,
2054
+ 129
2055
+ ];
2056
+ var JUP_V6_ROUTE_V2_DISCRIMINATOR = [
2057
+ 187,
2058
+ 100,
2059
+ 250,
2060
+ 204,
2061
+ 49,
2062
+ 196,
2063
+ 175,
2064
+ 20
2065
+ ];
2066
+ var JUP_V6_SHARED_ACCOUNTS_ROUTE_V2_DISCRIMINATOR = [
2067
+ 209,
2068
+ 152,
2069
+ 83,
2070
+ 147,
2071
+ 124,
2072
+ 254,
2073
+ 216,
2074
+ 233
2075
+ ];
2076
+ var JUPITER_INSTRUCTION_LAYOUTS = {
2077
+ // route
2078
+ [Buffer.from(JUP_V6_ROUTE_DISCRIMINATOR).toString("hex")]: {
2079
+ amountInOffset: (dataLength) => dataLength - AMOUNT_IN_JUP_V6_REVERSE_OFFSET,
2080
+ userTransferAuthorityIndex: 1
2081
+ },
2082
+ // shared_accounts_route
2083
+ [Buffer.from(JUP_V6_SHARED_ACCOUNTS_ROUTE_DISCRIMINATOR).toString("hex")]: {
2084
+ amountInOffset: (dataLength) => dataLength - AMOUNT_IN_JUP_V6_REVERSE_OFFSET,
2085
+ userTransferAuthorityIndex: 2
2086
+ },
2087
+ // route_v2
2088
+ [Buffer.from(JUP_V6_ROUTE_V2_DISCRIMINATOR).toString("hex")]: {
2089
+ amountInOffset: () => 8,
2090
+ userTransferAuthorityIndex: 0
2091
+ },
2092
+ // shared_accounts_route_v2
2093
+ [Buffer.from(JUP_V6_SHARED_ACCOUNTS_ROUTE_V2_DISCRIMINATOR).toString("hex")]: {
2094
+ amountInOffset: () => 9,
2095
+ userTransferAuthorityIndex: 1
2096
+ }
2097
+ };
1773
2098
  var AMOUNT_IN_DAMM_V2_OFFSET = 8;
1774
2099
  var DAMM_V2_SWAP_DISCRIMINATOR = [
1775
2100
  248,
@@ -1782,42 +2107,214 @@ var DAMM_V2_SWAP_DISCRIMINATOR = [
1782
2107
  200
1783
2108
  ];
1784
2109
  var DEFAULT_JUPITER_API_URL = "https://api.jup.ag";
2110
+ var DEFAULT_JUPITER_API_VERSION = "v2" /* V2 */;
2111
+
2112
+ // src/helpers/token.ts
2113
+ var import_spl_token = require("@solana/spl-token");
2114
+ var import_web32 = require("@solana/web3.js");
2115
+ var getOrCreateATAInstruction = (_connection, tokenMint, owner, payer, allowOwnerOffCurve = true, tokenProgram) => __async(null, null, function* () {
2116
+ const toAccount = (0, import_spl_token.getAssociatedTokenAddressSync)(
2117
+ tokenMint,
2118
+ owner,
2119
+ allowOwnerOffCurve,
2120
+ tokenProgram
2121
+ );
2122
+ const ix = (0, import_spl_token.createAssociatedTokenAccountIdempotentInstruction)(
2123
+ payer,
2124
+ toAccount,
2125
+ owner,
2126
+ tokenMint,
2127
+ tokenProgram
2128
+ );
2129
+ return { ataPubkey: toAccount, ix };
2130
+ });
2131
+ function unwrapSOLInstruction(owner, receiver, allowOwnerOffCurve = true) {
2132
+ const wSolATAAccount = (0, import_spl_token.getAssociatedTokenAddressSync)(
2133
+ import_spl_token.NATIVE_MINT,
2134
+ owner,
2135
+ allowOwnerOffCurve
2136
+ );
2137
+ if (wSolATAAccount) {
2138
+ const closedWrappedSolInstruction = (0, import_spl_token.createCloseAccountInstruction)(
2139
+ wSolATAAccount,
2140
+ receiver,
2141
+ owner,
2142
+ [],
2143
+ import_spl_token.TOKEN_PROGRAM_ID
2144
+ );
2145
+ return closedWrappedSolInstruction;
2146
+ }
2147
+ return null;
2148
+ }
2149
+ function getTokenAccountBalance(connection, tokenAccount) {
2150
+ return __async(this, null, function* () {
2151
+ let balance;
2152
+ try {
2153
+ balance = (yield connection.getTokenAccountBalance(tokenAccount)).value.amount;
2154
+ } catch (e) {
2155
+ balance = "0";
2156
+ }
2157
+ return balance;
2158
+ });
2159
+ }
2160
+ function wrapSOLInstruction(from, to, amount, tokenProgram = import_spl_token.TOKEN_PROGRAM_ID) {
2161
+ return [
2162
+ import_web32.SystemProgram.transfer({
2163
+ fromPubkey: from,
2164
+ toPubkey: to,
2165
+ lamports: amount
2166
+ }),
2167
+ new import_web32.TransactionInstruction({
2168
+ keys: [
2169
+ {
2170
+ pubkey: to,
2171
+ isSigner: false,
2172
+ isWritable: true
2173
+ }
2174
+ ],
2175
+ data: Buffer.from(new Uint8Array([17])),
2176
+ programId: tokenProgram
2177
+ })
2178
+ ];
2179
+ }
2180
+ var CLOSE_ACCOUNT_DISCRIMINATOR = 9;
2181
+ function filterOutCloseSplTokenAccountInstructions(instructions) {
2182
+ return instructions.filter((ix) => {
2183
+ if (ix.programId.equals(import_spl_token.TOKEN_PROGRAM_ID)) {
2184
+ return ix.data[0] !== CLOSE_ACCOUNT_DISCRIMINATOR;
2185
+ }
2186
+ return true;
2187
+ });
2188
+ }
2189
+ function getCreateAtaInstructions(user, tokens) {
2190
+ const tokenAccounts = tokens.map(
2191
+ ({ mint, tokenProgram }) => (0, import_spl_token.getAssociatedTokenAddressSync)(mint, user, true, tokenProgram)
2192
+ );
2193
+ const instructions = tokens.map(
2194
+ ({ mint, tokenProgram }, i) => (0, import_spl_token.createAssociatedTokenAccountIdempotentInstruction)(
2195
+ user,
2196
+ tokenAccounts[i],
2197
+ user,
2198
+ mint,
2199
+ tokenProgram
2200
+ )
2201
+ );
2202
+ return { tokenAccounts, instructions };
2203
+ }
2204
+ function isCreateAtaInstruction(ix) {
2205
+ return ix.programId.equals(import_spl_token.ASSOCIATED_TOKEN_PROGRAM_ID) && (ix.data.length === 0 || ix.data[0] === 0 || ix.data[0] === 1);
2206
+ }
2207
+ function dedupeCreateAtaInstructions(instructions) {
2208
+ const createdAccounts = /* @__PURE__ */ new Set();
2209
+ return instructions.filter((ix) => {
2210
+ if (!isCreateAtaInstruction(ix)) {
2211
+ return true;
2212
+ }
2213
+ const account = ix.keys[1].pubkey.toBase58();
2214
+ if (createdAccounts.has(account)) {
2215
+ return false;
2216
+ }
2217
+ createdAccounts.add(account);
2218
+ return true;
2219
+ });
2220
+ }
1785
2221
 
1786
2222
  // src/helpers/jupiter.ts
1787
- function getJupiterQuote(_0, _1, _2, _3, _4) {
1788
- return __async(this, arguments, function* (inputMint, outputMint, amount, maxAccounts, slippageBps, dynamicSlippage = false, onlyDirectRoutes, restrictIntermediateTokens, forJitoBundle = true, config = {}) {
1789
- const params = new URLSearchParams({
1790
- inputMint: inputMint.toString(),
1791
- outputMint: outputMint.toString(),
1792
- amount: amount.toString(),
1793
- slippageBps: slippageBps.toString(),
1794
- maxAccounts: maxAccounts.toString(),
1795
- onlyDirectRoutes: onlyDirectRoutes.toString(),
1796
- restrictIntermediateTokens: restrictIntermediateTokens.toString(),
1797
- dynamicSlippage: dynamicSlippage.toString(),
1798
- forJitoBundle: forJitoBundle.toString()
1799
- });
1800
- const url = `${config.jupiterApiUrl || DEFAULT_JUPITER_API_URL}/swap/v1/quote?${params.toString()}`;
2223
+ function resolveJupiterConfig(config) {
2224
+ var _a;
2225
+ return {
2226
+ url: (config == null ? void 0 : config.jupiterApiUrl) || DEFAULT_JUPITER_API_URL,
2227
+ apiKey: (config == null ? void 0 : config.jupiterApiKey) || "",
2228
+ version: (_a = config == null ? void 0 : config.jupiterApiVersion) != null ? _a : DEFAULT_JUPITER_API_VERSION
2229
+ };
2230
+ }
2231
+ function getJupiterInstructionLayout(data) {
2232
+ const discriminator = data.subarray(0, 8).toString("hex");
2233
+ const layout = JUPITER_INSTRUCTION_LAYOUTS[discriminator];
2234
+ if (!layout) {
2235
+ throw new Error(
2236
+ `Unsupported Jupiter instruction discriminator ${discriminator}`
2237
+ );
2238
+ }
2239
+ return layout;
2240
+ }
2241
+ function isJupiterBuildResponse(quoteResponse) {
2242
+ return quoteResponse.swapInstruction !== void 0;
2243
+ }
2244
+ function isJupiterSwapInstructionForUser(swapInstruction, user) {
2245
+ const layout = getJupiterInstructionLayout(
2246
+ Buffer.from(swapInstruction.data, "base64")
2247
+ );
2248
+ const authority = swapInstruction.accounts[layout.userTransferAuthorityIndex];
2249
+ return authority !== void 0 && new import_web33.PublicKey(authority.pubkey).equals(user);
2250
+ }
2251
+ function jupiterHeaders(apiKey, json) {
2252
+ return __spreadValues(__spreadValues({
2253
+ Accept: "application/json"
2254
+ }, json ? { "Content-Type": "application/json" } : {}), apiKey ? { "x-api-key": apiKey } : {});
2255
+ }
2256
+ function toSearchParams(params) {
2257
+ var _a;
2258
+ const searchParams = new URLSearchParams();
2259
+ for (const [name, value] of Object.entries((_a = params.queryParams) != null ? _a : {})) {
2260
+ searchParams.set(name, value.toString());
2261
+ }
2262
+ searchParams.set("inputMint", params.inputMint.toString());
2263
+ searchParams.set("outputMint", params.outputMint.toString());
2264
+ searchParams.set("amount", params.amount.toString());
2265
+ return searchParams;
2266
+ }
2267
+ function fetchJupiterQuoteV1(params, config) {
2268
+ return __async(this, null, function* () {
2269
+ const { url: baseUrl, apiKey } = resolveJupiterConfig(config);
2270
+ const url = `${baseUrl}/swap/v1/quote?${toSearchParams(params).toString()}`;
1801
2271
  try {
1802
2272
  const response = yield fetch(url, {
1803
2273
  method: "GET",
1804
- headers: __spreadValues({
1805
- Accept: "application/json"
1806
- }, config.jupiterApiKey ? { "x-api-key": config.jupiterApiKey } : {})
2274
+ headers: jupiterHeaders(apiKey, false)
1807
2275
  });
1808
2276
  if (!response.ok) {
1809
2277
  return null;
1810
2278
  }
1811
- const result = yield response.json();
1812
- return result;
2279
+ return yield response.json();
1813
2280
  } catch (error) {
1814
2281
  return null;
1815
2282
  }
1816
2283
  });
1817
2284
  }
1818
- function getJupiterSwapInstruction(_0, _1) {
1819
- return __async(this, arguments, function* (userPublicKey, quoteResponse, config = {}) {
1820
- const url = `${config.jupiterApiUrl || DEFAULT_JUPITER_API_URL}/swap/v1/swap-instructions`;
2285
+ function fetchJupiterBuildV2(params, config) {
2286
+ return __async(this, null, function* () {
2287
+ const { url: baseUrl, apiKey } = resolveJupiterConfig(config);
2288
+ const searchParams = toSearchParams(params);
2289
+ searchParams.set("taker", params.user.toString());
2290
+ const url = `${baseUrl}/swap/v2/build?${searchParams.toString()}`;
2291
+ try {
2292
+ const response = yield fetch(url, {
2293
+ method: "GET",
2294
+ headers: jupiterHeaders(apiKey, false)
2295
+ });
2296
+ if (!response.ok) {
2297
+ return null;
2298
+ }
2299
+ return yield response.json();
2300
+ } catch (error) {
2301
+ return null;
2302
+ }
2303
+ });
2304
+ }
2305
+ function getJupiterQuote(params, config) {
2306
+ return __async(this, null, function* () {
2307
+ const { version } = resolveJupiterConfig(config);
2308
+ if (version === "v1" /* V1 */) {
2309
+ return fetchJupiterQuoteV1(params, config);
2310
+ }
2311
+ return fetchJupiterBuildV2(params, config);
2312
+ });
2313
+ }
2314
+ function fetchJupiterSwapInstructionV1(userPublicKey, quoteResponse, config) {
2315
+ return __async(this, null, function* () {
2316
+ const { url: baseUrl, apiKey } = resolveJupiterConfig(config);
2317
+ const url = `${baseUrl}/swap/v1/swap-instructions`;
1821
2318
  const requestBody = {
1822
2319
  userPublicKey: userPublicKey.toString(),
1823
2320
  quoteResponse
@@ -1826,10 +2323,7 @@ function getJupiterSwapInstruction(_0, _1) {
1826
2323
  try {
1827
2324
  response = yield fetch(url, {
1828
2325
  method: "POST",
1829
- headers: __spreadValues({
1830
- "Content-Type": "application/json",
1831
- Accept: "application/json"
1832
- }, config.jupiterApiKey ? { "x-api-key": config.jupiterApiKey } : {}),
2326
+ headers: jupiterHeaders(apiKey, true),
1833
2327
  body: JSON.stringify(requestBody)
1834
2328
  });
1835
2329
  if (!response.ok) {
@@ -1841,22 +2335,124 @@ function getJupiterSwapInstruction(_0, _1) {
1841
2335
  } catch (error) {
1842
2336
  throw new Error(`Jupiter swap instruction failed to fetch`);
1843
2337
  }
1844
- const result = yield response.json();
1845
- return result;
2338
+ return yield response.json();
1846
2339
  });
1847
2340
  }
1848
- function buildJupiterSwapTransaction(_0, _1, _2, _3, _4, _5, _6) {
1849
- return __async(this, arguments, function* (user, inputMint, outputMint, amount, maxAccounts, slippageBps, jupiterQuoteResponse, config = {}) {
2341
+ function getJupiterSwapInstruction(userPublicKey, quoteResponse, config) {
2342
+ return __async(this, null, function* () {
2343
+ const { version } = resolveJupiterConfig(config);
2344
+ if (version === "v1" /* V1 */) {
2345
+ return fetchJupiterSwapInstructionV1(userPublicKey, quoteResponse, config);
2346
+ }
2347
+ if (!isJupiterBuildResponse(quoteResponse)) {
2348
+ throw new Error(
2349
+ "Jupiter quote has no swap instructions, get the quote with getJupiterQuote under Jupiter API v2"
2350
+ );
2351
+ }
2352
+ if (!isJupiterSwapInstructionForUser(
2353
+ quoteResponse.swapInstruction,
2354
+ userPublicKey
2355
+ )) {
2356
+ throw new Error(
2357
+ `Jupiter quote swap instructions are not built for ${userPublicKey.toBase58()}`
2358
+ );
2359
+ }
2360
+ return quoteResponse;
2361
+ });
2362
+ }
2363
+ function isQuoteMatchesSwap(quoteResponse, user, amount) {
2364
+ if (quoteResponse.inAmount !== amount.toString()) {
2365
+ return false;
2366
+ }
2367
+ if (isJupiterBuildResponse(quoteResponse)) {
2368
+ return isJupiterSwapInstructionForUser(quoteResponse.swapInstruction, user);
2369
+ }
2370
+ return true;
2371
+ }
2372
+ var ACTIVE_LOOKUP_TABLE_DEACTIVATION_SLOT = BigInt("0xffffffffffffffff");
2373
+ function toAddressLookupTableAccounts(addressesByLookupTableAddress) {
2374
+ return Object.entries(addressesByLookupTableAddress != null ? addressesByLookupTableAddress : {}).map(
2375
+ ([key, addresses]) => new import_web33.AddressLookupTableAccount({
2376
+ key: new import_web33.PublicKey(key),
2377
+ state: {
2378
+ addresses: addresses.map((address) => new import_web33.PublicKey(address)),
2379
+ deactivationSlot: ACTIVE_LOOKUP_TABLE_DEACTIVATION_SLOT,
2380
+ lastExtendedSlot: 0,
2381
+ lastExtendedSlotStartIndex: 0
2382
+ }
2383
+ })
2384
+ );
2385
+ }
2386
+ function fetchAddressLookupTableAccounts(connection, addresses) {
2387
+ return __async(this, null, function* () {
2388
+ if (addresses.length === 0) {
2389
+ return [];
2390
+ }
2391
+ const keys = addresses.map((address) => new import_web33.PublicKey(address));
2392
+ const accounts = yield connection.getMultipleAccountsInfo(keys);
2393
+ return keys.map((key, i) => {
2394
+ const account = accounts[i];
2395
+ if (!account || !account.owner.equals(import_web33.AddressLookupTableProgram.programId)) {
2396
+ throw new Error(`Address lookup table not found: ${key.toBase58()}`);
2397
+ }
2398
+ return new import_web33.AddressLookupTableAccount({
2399
+ key,
2400
+ state: import_web33.AddressLookupTableAccount.deserialize(account.data)
2401
+ });
2402
+ });
2403
+ });
2404
+ }
2405
+ function mergeAddressLookupTableAccounts(...lists) {
2406
+ const merged = /* @__PURE__ */ new Map();
2407
+ for (const table of lists.flat()) {
2408
+ const key = table.key.toBase58();
2409
+ if (!merged.has(key)) {
2410
+ merged.set(key, table);
2411
+ }
2412
+ }
2413
+ return [...merged.values()];
2414
+ }
2415
+ function getJupiterAddressLookupTableAccounts(connection, response) {
2416
+ return __async(this, null, function* () {
2417
+ var _a;
2418
+ if (response.addressesByLookupTableAddress) {
2419
+ return toAddressLookupTableAccounts(response.addressesByLookupTableAddress);
2420
+ }
2421
+ return fetchAddressLookupTableAccounts(
2422
+ connection,
2423
+ (_a = response.addressLookupTableAddresses) != null ? _a : []
2424
+ );
2425
+ });
2426
+ }
2427
+ function toTransactionInstruction(instruction) {
2428
+ return new import_web33.TransactionInstruction({
2429
+ keys: instruction.accounts.map((item) => ({
2430
+ pubkey: new import_web33.PublicKey(item.pubkey),
2431
+ isSigner: item.isSigner,
2432
+ isWritable: item.isWritable
2433
+ })),
2434
+ programId: new import_web33.PublicKey(instruction.programId),
2435
+ data: Buffer.from(instruction.data, "base64")
2436
+ });
2437
+ }
2438
+ function getJupiterCreateAtaInstructions(setupInstructions) {
2439
+ return setupInstructions.filter(isCreateAtaInstruction);
2440
+ }
2441
+ function buildJupiterSwapTransaction(connection, user, inputMint, outputMint, amount, slippageBps, jupiterQuoteResponse, config, jupiterQuoteOptions) {
2442
+ return __async(this, null, function* () {
2443
+ if (jupiterQuoteResponse && !isQuoteMatchesSwap(jupiterQuoteResponse, user, amount)) {
2444
+ throw new Error(
2445
+ `Jupiter quote does not match the swap of ${amount.toString()} by ${user.toBase58()} (quoted inAmount ${jupiterQuoteResponse.inAmount})`
2446
+ );
2447
+ }
1850
2448
  const quoteResponse = jupiterQuoteResponse != null ? jupiterQuoteResponse : yield getJupiterQuote(
1851
- inputMint,
1852
- outputMint,
1853
- amount,
1854
- maxAccounts,
1855
- slippageBps,
1856
- false,
1857
- true,
1858
- true,
1859
- true,
2449
+ {
2450
+ inputMint,
2451
+ outputMint,
2452
+ amount,
2453
+ user,
2454
+ queryParams: __spreadValues({ slippageBps }, jupiterQuoteOptions)
2455
+ },
1860
2456
  config
1861
2457
  );
1862
2458
  if (!quoteResponse) {
@@ -1869,47 +2465,51 @@ function buildJupiterSwapTransaction(_0, _1, _2, _3, _4, _5, _6) {
1869
2465
  quoteResponse,
1870
2466
  config
1871
2467
  );
1872
- const instruction = new import_web32.TransactionInstruction({
1873
- keys: swapInstructionResponse.swapInstruction.accounts.map((item) => {
1874
- return {
1875
- pubkey: new import_web32.PublicKey(item.pubkey),
1876
- isSigner: item.isSigner,
1877
- isWritable: item.isWritable
1878
- };
1879
- }),
1880
- programId: new import_web32.PublicKey(swapInstructionResponse.swapInstruction.programId),
1881
- data: Buffer.from(swapInstructionResponse.swapInstruction.data, "base64")
1882
- });
1883
- return { transaction: new import_web32.Transaction().add(instruction), quoteResponse };
2468
+ const instruction = toTransactionInstruction(
2469
+ swapInstructionResponse.swapInstruction
2470
+ );
2471
+ const setupInstructions = swapInstructionResponse.setupInstructions.map(
2472
+ toTransactionInstruction
2473
+ );
2474
+ const addressLookupTableAccounts = yield getJupiterAddressLookupTableAccounts(
2475
+ connection,
2476
+ swapInstructionResponse
2477
+ );
2478
+ return {
2479
+ instruction,
2480
+ setupInstructions,
2481
+ addressLookupTableAccounts,
2482
+ quoteResponse
2483
+ };
1884
2484
  });
1885
2485
  }
1886
2486
 
1887
2487
  // src/helpers/dammV2.ts
1888
- var import_spl_token = require("@solana/spl-token");
2488
+ var import_spl_token2 = require("@solana/spl-token");
1889
2489
  var import_cp_amm_sdk = require("@meteora-ag/cp-amm-sdk");
1890
2490
 
1891
2491
  // src/helpers/pda.ts
1892
- var import_web33 = require("@solana/web3.js");
2492
+ var import_web34 = require("@solana/web3.js");
1893
2493
  function deriveDammV2EventAuthority() {
1894
- return import_web33.PublicKey.findProgramAddressSync(
2494
+ return import_web34.PublicKey.findProgramAddressSync(
1895
2495
  [Buffer.from("__event_authority")],
1896
2496
  DAMM_V2_PROGRAM_ID
1897
2497
  )[0];
1898
2498
  }
1899
2499
  function deriveDammV2PoolAuthority() {
1900
- return import_web33.PublicKey.findProgramAddressSync(
2500
+ return import_web34.PublicKey.findProgramAddressSync(
1901
2501
  [Buffer.from("pool_authority")],
1902
2502
  DAMM_V2_PROGRAM_ID
1903
2503
  )[0];
1904
2504
  }
1905
2505
  function deriveLedgerAccount(owner) {
1906
- return import_web33.PublicKey.findProgramAddressSync(
2506
+ return import_web34.PublicKey.findProgramAddressSync(
1907
2507
  [Buffer.from("user_ledger"), owner.toBuffer()],
1908
2508
  ZAP_PROGRAM_ID
1909
2509
  )[0];
1910
2510
  }
1911
2511
  function deriveDlmmEventAuthority() {
1912
- return import_web33.PublicKey.findProgramAddressSync(
2512
+ return import_web34.PublicKey.findProgramAddressSync(
1913
2513
  [Buffer.from("__event_authority")],
1914
2514
  DLMM_PROGRAM_ID
1915
2515
  )[0];
@@ -1923,7 +2523,7 @@ function getDammV2Pool(connection, poolAddress) {
1923
2523
  });
1924
2524
  }
1925
2525
  function getDammV2RemainingAccounts(_0, _1, _2, _3) {
1926
- return __async(this, arguments, function* (poolAddress, user, userInputTokenAccount, userTokenOutAccount, tokenAProgram = import_spl_token.TOKEN_PROGRAM_ID, tokenBProgram = import_spl_token.TOKEN_PROGRAM_ID, poolState) {
2526
+ return __async(this, arguments, function* (poolAddress, user, userInputTokenAccount, userTokenOutAccount, tokenAProgram = import_spl_token2.TOKEN_PROGRAM_ID, tokenBProgram = import_spl_token2.TOKEN_PROGRAM_ID, poolState) {
1927
2527
  const remainingAccounts = [
1928
2528
  {
1929
2529
  isSigner: false,
@@ -2014,118 +2614,45 @@ function createDammV2SwapPayload(amountIn, minimumSwapAmountOut) {
2014
2614
  }
2015
2615
 
2016
2616
  // src/helpers/common.ts
2017
- var import_spl_token2 = require("@solana/spl-token");
2018
- var import_web34 = require("@solana/web3.js");
2019
- var import_decimal = __toESM(require("decimal.js"));
2020
- function getTokenProgramFromMint(connection, mint) {
2021
- return __async(this, null, function* () {
2022
- if (mint.equals(import_spl_token2.NATIVE_MINT)) {
2023
- return import_spl_token2.TOKEN_PROGRAM_ID;
2024
- }
2025
- try {
2026
- const mintInfo = yield connection.getAccountInfo(mint);
2027
- if (!mintInfo) {
2028
- throw new Error(`Mint account not found: ${mint.toString()}`);
2029
- }
2030
- if (mintInfo.owner.equals(
2031
- new import_web34.PublicKey("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb")
2032
- )) {
2033
- return new import_web34.PublicKey("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb");
2034
- } else {
2035
- return import_spl_token2.TOKEN_PROGRAM_ID;
2036
- }
2037
- } catch (error) {
2038
- console.warn(
2039
- `Failed to determine token program for ${mint.toString()}, defaulting to TOKEN_PROGRAM_ID:`,
2040
- error
2041
- );
2042
- return import_spl_token2.TOKEN_PROGRAM_ID;
2043
- }
2044
- });
2045
- }
2046
- function convertLamportsToUiAmount(amount, decimals) {
2047
- return amount.div(import_decimal.default.pow(10, decimals));
2048
- }
2049
- function convertUiAmountToLamports(amount, decimals) {
2050
- return amount.mul(import_decimal.default.pow(10, decimals));
2051
- }
2052
-
2053
- // src/helpers/token.ts
2054
2617
  var import_spl_token3 = require("@solana/spl-token");
2055
2618
  var import_web35 = require("@solana/web3.js");
2056
- var getOrCreateATAInstruction = (connection, tokenMint, owner, payer, allowOwnerOffCurve = true, tokenProgram) => __async(null, null, function* () {
2057
- const toAccount = (0, import_spl_token3.getAssociatedTokenAddressSync)(
2058
- tokenMint,
2059
- owner,
2060
- allowOwnerOffCurve,
2061
- tokenProgram
2062
- );
2063
- const ix = (0, import_spl_token3.createAssociatedTokenAccountIdempotentInstruction)(
2064
- payer,
2065
- toAccount,
2066
- owner,
2067
- tokenMint,
2068
- tokenProgram
2069
- );
2070
- return { ataPubkey: toAccount, ix };
2071
- });
2072
- function unwrapSOLInstruction(owner, receiver, allowOwnerOffCurve = true) {
2073
- const wSolATAAccount = (0, import_spl_token3.getAssociatedTokenAddressSync)(
2074
- import_spl_token3.NATIVE_MINT,
2075
- owner,
2076
- allowOwnerOffCurve
2077
- );
2078
- if (wSolATAAccount) {
2079
- const closedWrappedSolInstruction = (0, import_spl_token3.createCloseAccountInstruction)(
2080
- wSolATAAccount,
2081
- receiver,
2082
- owner,
2083
- [],
2084
- import_spl_token3.TOKEN_PROGRAM_ID
2085
- );
2086
- return closedWrappedSolInstruction;
2087
- }
2088
- return null;
2089
- }
2090
- function getTokenAccountBalance(connection, tokenAccount) {
2619
+ var import_decimal = __toESM(require("decimal.js"));
2620
+ function getTokenProgramFromMint(connection, mint) {
2091
2621
  return __async(this, null, function* () {
2092
- let balance;
2622
+ if (mint.equals(import_spl_token3.NATIVE_MINT)) {
2623
+ return import_spl_token3.TOKEN_PROGRAM_ID;
2624
+ }
2093
2625
  try {
2094
- balance = (yield connection.getTokenAccountBalance(tokenAccount)).value.amount;
2095
- } catch (e) {
2096
- balance = "0";
2626
+ const mintInfo = yield connection.getAccountInfo(mint);
2627
+ if (!mintInfo) {
2628
+ throw new Error(`Mint account not found: ${mint.toString()}`);
2629
+ }
2630
+ if (mintInfo.owner.equals(
2631
+ new import_web35.PublicKey("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb")
2632
+ )) {
2633
+ return new import_web35.PublicKey("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb");
2634
+ } else {
2635
+ return import_spl_token3.TOKEN_PROGRAM_ID;
2636
+ }
2637
+ } catch (error) {
2638
+ console.warn(
2639
+ `Failed to determine token program for ${mint.toString()}, defaulting to TOKEN_PROGRAM_ID:`,
2640
+ error
2641
+ );
2642
+ return import_spl_token3.TOKEN_PROGRAM_ID;
2097
2643
  }
2098
- return balance;
2099
2644
  });
2100
2645
  }
2101
- function wrapSOLInstruction(from, to, amount, tokenProgram = import_spl_token3.TOKEN_PROGRAM_ID) {
2102
- return [
2103
- import_web35.SystemProgram.transfer({
2104
- fromPubkey: from,
2105
- toPubkey: to,
2106
- lamports: amount
2107
- }),
2108
- new import_web35.TransactionInstruction({
2109
- keys: [
2110
- {
2111
- pubkey: to,
2112
- isSigner: false,
2113
- isWritable: true
2114
- }
2115
- ],
2116
- data: Buffer.from(new Uint8Array([17])),
2117
- programId: tokenProgram
2118
- })
2119
- ];
2646
+ function convertLamportsToUiAmount(amount, decimals) {
2647
+ return amount.div(import_decimal.default.pow(10, decimals));
2120
2648
  }
2121
- var CLOSE_ACCOUNT_DISCRIMINATOR = 9;
2122
- function filterOutCloseSplTokenAccountInstructions(instructions) {
2123
- return instructions.filter((ix) => {
2124
- if (ix.programId.equals(import_spl_token3.TOKEN_PROGRAM_ID)) {
2125
- return ix.data[0] !== CLOSE_ACCOUNT_DISCRIMINATOR;
2126
- }
2127
- return true;
2128
- });
2649
+ function convertUiAmountToLamports(amount, decimals) {
2650
+ return amount.mul(import_decimal.default.pow(10, decimals));
2651
+ }
2652
+ function filterOutComputeBudgetInstructions(instructions) {
2653
+ return instructions.filter(
2654
+ (ix) => !ix.programId.equals(import_web35.ComputeBudgetProgram.programId)
2655
+ );
2129
2656
  }
2130
2657
 
2131
2658
  // src/helpers/dlmm.ts
@@ -2489,8 +3016,8 @@ function estimateSwapOutput(inAmount, effectiveRate) {
2489
3016
  const estimatedOutput = inAmountDecimal.mul(effectiveRate);
2490
3017
  return new import_bn3.default(estimatedOutput.floor().toString());
2491
3018
  }
2492
- function getBestSwapQuoteJupiterDlmm(_0, _1, _2, _3, _4, _5, _6) {
2493
- return __async(this, arguments, function* (dlmm, inMint, outMint, inAmount, swapSlippageBps, swapForY, binArrays, config = {}) {
3019
+ function getBestSwapQuoteJupiterDlmm(dlmm, user, inMint, outMint, inAmount, swapSlippageBps, swapForY, binArrays, config, jupiterQuoteOptions) {
3020
+ return __async(this, null, function* () {
2494
3021
  let dlmmQuoteResult = null;
2495
3022
  try {
2496
3023
  dlmmQuoteResult = dlmm.swapQuote(
@@ -2503,15 +3030,15 @@ function getBestSwapQuoteJupiterDlmm(_0, _1, _2, _3, _4, _5, _6) {
2503
3030
  console.error("Error getting DLMM quote, using jupiter quote only:", error);
2504
3031
  }
2505
3032
  const jupiterQuoteResult = yield getJupiterQuote(
2506
- inMint,
2507
- outMint,
2508
- inAmount,
2509
- 50,
2510
- swapSlippageBps,
2511
- false,
2512
- true,
2513
- true,
2514
- true,
3033
+ {
3034
+ inputMint: inMint,
3035
+ outputMint: outMint,
3036
+ amount: inAmount,
3037
+ user,
3038
+ queryParams: __spreadValues({
3039
+ slippageBps: swapSlippageBps
3040
+ }, jupiterQuoteOptions)
3041
+ },
2515
3042
  config
2516
3043
  );
2517
3044
  const jupiterQuote = jupiterQuoteResult ? {
@@ -2627,6 +3154,7 @@ function binarySearchRefineDirectSwapAmount(dlmm, initialActiveBin, initialMinBi
2627
3154
  }
2628
3155
  function estimateDlmmIndirectSwap(_0) {
2629
3156
  return __async(this, arguments, function* ({
3157
+ user,
2630
3158
  amountIn,
2631
3159
  inputTokenMint,
2632
3160
  lbPair,
@@ -2636,7 +3164,8 @@ function estimateDlmmIndirectSwap(_0) {
2636
3164
  maxDeltaId,
2637
3165
  strategy,
2638
3166
  singleSided,
2639
- config = {}
3167
+ jupiterQuoteOptions,
3168
+ config
2640
3169
  }) {
2641
3170
  const dlmm = yield import_dlmm3.default.create(connection, lbPair);
2642
3171
  const activeBin = yield dlmm.getActiveBin();
@@ -2660,15 +3189,15 @@ function estimateDlmmIndirectSwap(_0) {
2660
3189
  const singleSidedX = singleSided === 0 /* X */;
2661
3190
  const outputTokenMint = singleSidedX ? tokenXMint : tokenYMint;
2662
3191
  const quote = yield getJupiterQuote(
2663
- inputTokenMint,
2664
- outputTokenMint,
2665
- amountIn,
2666
- 50,
2667
- swapSlippageBps,
2668
- false,
2669
- true,
2670
- true,
2671
- true,
3192
+ {
3193
+ inputMint: inputTokenMint,
3194
+ outputMint: outputTokenMint,
3195
+ amount: amountIn,
3196
+ user,
3197
+ queryParams: __spreadValues({
3198
+ slippageBps: swapSlippageBps
3199
+ }, jupiterQuoteOptions)
3200
+ },
2672
3201
  config
2673
3202
  );
2674
3203
  if (!quote) {
@@ -2691,27 +3220,27 @@ function estimateDlmmIndirectSwap(_0) {
2691
3220
  const halfAmount = amountIn.div(new import_bn3.default(2));
2692
3221
  const [quoteToXResult, quoteToYResult] = yield Promise.allSettled([
2693
3222
  getJupiterQuote(
2694
- inputTokenMint,
2695
- tokenXMint,
2696
- halfAmount,
2697
- 50,
2698
- swapSlippageBps,
2699
- false,
2700
- true,
2701
- true,
2702
- true,
3223
+ {
3224
+ inputMint: inputTokenMint,
3225
+ outputMint: tokenXMint,
3226
+ amount: halfAmount,
3227
+ user,
3228
+ queryParams: __spreadValues({
3229
+ slippageBps: swapSlippageBps
3230
+ }, jupiterQuoteOptions)
3231
+ },
2703
3232
  config
2704
3233
  ),
2705
3234
  getJupiterQuote(
2706
- inputTokenMint,
2707
- tokenYMint,
2708
- halfAmount,
2709
- 50,
2710
- swapSlippageBps,
2711
- false,
2712
- true,
2713
- true,
2714
- true,
3235
+ {
3236
+ inputMint: inputTokenMint,
3237
+ outputMint: tokenYMint,
3238
+ amount: halfAmount,
3239
+ user,
3240
+ queryParams: __spreadValues({
3241
+ slippageBps: swapSlippageBps
3242
+ }, jupiterQuoteOptions)
3243
+ },
2715
3244
  config
2716
3245
  )
2717
3246
  ]);
@@ -2807,27 +3336,27 @@ function estimateDlmmIndirectSwap(_0) {
2807
3336
  }
2808
3337
  const [finalQuoteToXResult, finalQuoteToYResult] = yield Promise.allSettled([
2809
3338
  getJupiterQuote(
2810
- inputTokenMint,
2811
- tokenXMint,
2812
- bestAmountToX,
2813
- 50,
2814
- swapSlippageBps,
2815
- false,
2816
- true,
2817
- true,
2818
- true,
3339
+ {
3340
+ inputMint: inputTokenMint,
3341
+ outputMint: tokenXMint,
3342
+ amount: bestAmountToX,
3343
+ user,
3344
+ queryParams: __spreadValues({
3345
+ slippageBps: swapSlippageBps
3346
+ }, jupiterQuoteOptions)
3347
+ },
2819
3348
  config
2820
3349
  ),
2821
3350
  getJupiterQuote(
2822
- inputTokenMint,
2823
- tokenYMint,
2824
- bestAmountToY,
2825
- 50,
2826
- swapSlippageBps,
2827
- false,
2828
- true,
2829
- true,
2830
- true,
3351
+ {
3352
+ inputMint: inputTokenMint,
3353
+ outputMint: tokenYMint,
3354
+ amount: bestAmountToY,
3355
+ user,
3356
+ queryParams: __spreadValues({
3357
+ slippageBps: swapSlippageBps
3358
+ }, jupiterQuoteOptions)
3359
+ },
2831
3360
  config
2832
3361
  )
2833
3362
  ]);
@@ -2863,6 +3392,7 @@ function estimateDlmmIndirectSwap(_0) {
2863
3392
  }
2864
3393
  function estimateDlmmDirectSwapCore(_0) {
2865
3394
  return __async(this, arguments, function* ({
3395
+ user,
2866
3396
  tokenXAmount,
2867
3397
  tokenYAmount,
2868
3398
  dlmm,
@@ -2871,7 +3401,8 @@ function estimateDlmmDirectSwapCore(_0) {
2871
3401
  maxDeltaId,
2872
3402
  strategy,
2873
3403
  singleSided,
2874
- config = {}
3404
+ jupiterQuoteOptions,
3405
+ config
2875
3406
  }) {
2876
3407
  if (singleSided !== void 0) {
2877
3408
  const singleSidedX = singleSided === 0 /* X */;
@@ -2883,13 +3414,15 @@ function estimateDlmmDirectSwapCore(_0) {
2883
3414
  );
2884
3415
  const quote = yield getBestSwapQuoteJupiterDlmm(
2885
3416
  dlmm,
3417
+ user,
2886
3418
  dlmm.lbPair.tokenYMint,
2887
3419
  dlmm.lbPair.tokenXMint,
2888
3420
  tokenYAmount,
2889
3421
  swapSlippageBps,
2890
3422
  swapForY2,
2891
3423
  binArrayForSwap2,
2892
- config
3424
+ config,
3425
+ jupiterQuoteOptions
2893
3426
  );
2894
3427
  if (!quote) {
2895
3428
  throw new Error(
@@ -2912,13 +3445,15 @@ function estimateDlmmDirectSwapCore(_0) {
2912
3445
  );
2913
3446
  const quote = yield getBestSwapQuoteJupiterDlmm(
2914
3447
  dlmm,
3448
+ user,
2915
3449
  dlmm.lbPair.tokenXMint,
2916
3450
  dlmm.lbPair.tokenYMint,
2917
3451
  tokenXAmount,
2918
3452
  swapSlippageBps,
2919
3453
  swapForY2,
2920
3454
  binArrayForSwap2,
2921
- config
3455
+ config,
3456
+ jupiterQuoteOptions
2922
3457
  );
2923
3458
  if (!quote) {
2924
3459
  throw new Error(
@@ -3007,13 +3542,15 @@ function estimateDlmmDirectSwapCore(_0) {
3007
3542
  );
3008
3543
  const initialQuote = yield getBestSwapQuoteJupiterDlmm(
3009
3544
  dlmm,
3545
+ user,
3010
3546
  inMint,
3011
3547
  outMint,
3012
3548
  initialSwapAmount,
3013
3549
  swapSlippageBps,
3014
3550
  swapForY,
3015
3551
  binArrayForSwap,
3016
- config
3552
+ config,
3553
+ jupiterQuoteOptions
3017
3554
  );
3018
3555
  if (!initialQuote) {
3019
3556
  throw new Error(
@@ -3065,13 +3602,15 @@ function estimateDlmmDirectSwapCore(_0) {
3065
3602
  );
3066
3603
  const finalQuote = yield getBestSwapQuoteJupiterDlmm(
3067
3604
  dlmm,
3605
+ user,
3068
3606
  inMint,
3069
3607
  outMint,
3070
3608
  refinedAmount,
3071
3609
  swapSlippageBps,
3072
3610
  swapForY,
3073
3611
  binArrayForSwap,
3074
- config
3612
+ config,
3613
+ jupiterQuoteOptions
3075
3614
  );
3076
3615
  if (!finalQuote) {
3077
3616
  return {
@@ -3097,6 +3636,7 @@ function estimateDlmmDirectSwapCore(_0) {
3097
3636
  }
3098
3637
  function estimateDlmmDirectSwap(_0) {
3099
3638
  return __async(this, arguments, function* ({
3639
+ user,
3100
3640
  amountIn,
3101
3641
  inputTokenMint,
3102
3642
  lbPair,
@@ -3106,7 +3646,8 @@ function estimateDlmmDirectSwap(_0) {
3106
3646
  maxDeltaId,
3107
3647
  strategy,
3108
3648
  singleSided,
3109
- config = {}
3649
+ jupiterQuoteOptions,
3650
+ config
3110
3651
  }) {
3111
3652
  const dlmm = yield import_dlmm3.default.create(connection, lbPair);
3112
3653
  (0, import_invariant.default)(
@@ -3117,6 +3658,7 @@ function estimateDlmmDirectSwap(_0) {
3117
3658
  const tokenXAmount = isInputTokenX ? amountIn : new import_bn3.default(0);
3118
3659
  const tokenYAmount = isInputTokenX ? new import_bn3.default(0) : amountIn;
3119
3660
  const result = yield estimateDlmmDirectSwapCore({
3661
+ user,
3120
3662
  tokenXAmount,
3121
3663
  tokenYAmount,
3122
3664
  dlmm,
@@ -3125,6 +3667,7 @@ function estimateDlmmDirectSwap(_0) {
3125
3667
  maxDeltaId,
3126
3668
  strategy,
3127
3669
  singleSided,
3670
+ jupiterQuoteOptions,
3128
3671
  config
3129
3672
  });
3130
3673
  return {
@@ -3144,6 +3687,7 @@ function estimateDlmmDirectSwap(_0) {
3144
3687
  }
3145
3688
  function estimateDlmmRebalanceSwap(_0) {
3146
3689
  return __async(this, arguments, function* ({
3690
+ user,
3147
3691
  lbPair,
3148
3692
  position,
3149
3693
  connection,
@@ -3151,11 +3695,13 @@ function estimateDlmmRebalanceSwap(_0) {
3151
3695
  minDeltaId,
3152
3696
  maxDeltaId,
3153
3697
  strategy,
3154
- config = {}
3698
+ jupiterQuoteOptions,
3699
+ config
3155
3700
  }) {
3156
3701
  const dlmm = yield import_dlmm3.default.create(connection, lbPair);
3157
3702
  const userPosition = yield dlmm.getPosition(position);
3158
3703
  const result = yield estimateDlmmDirectSwapCore({
3704
+ user,
3159
3705
  tokenXAmount: new import_bn3.default(userPosition.positionData.totalXAmount),
3160
3706
  tokenYAmount: new import_bn3.default(userPosition.positionData.totalYAmount),
3161
3707
  dlmm,
@@ -3165,6 +3711,7 @@ function estimateDlmmRebalanceSwap(_0) {
3165
3711
  strategy,
3166
3712
  singleSided: void 0,
3167
3713
  // rebalance does not use single-sided deposits
3714
+ jupiterQuoteOptions,
3168
3715
  config
3169
3716
  });
3170
3717
  return {
@@ -3232,13 +3779,15 @@ function getExtendMaxAmountTransfer(amount, percentage) {
3232
3779
  var Zap = class {
3233
3780
  /**
3234
3781
  * @param connection - The connection to the Solana cluster
3235
- * @param config - Optional configuration for Jupiter API URL (default: https://api.jup.ag) and API key (default: empty string)
3782
+ * @param config - Optional configuration for Jupiter API URL (default: https://api.jup.ag), API key (default: empty string) and API version (default: JupiterApiVersion.V2)
3236
3783
  */
3237
- constructor(connection, config = {}) {
3784
+ constructor(connection, config) {
3785
+ var _a;
3238
3786
  this.connection = connection;
3239
- this.zapProgram = new import_anchor.Program(idl_default, { connection });
3240
- this.jupiterApiUrl = config.jupiterApiUrl || DEFAULT_JUPITER_API_URL;
3241
- this.jupiterApiKey = config.jupiterApiKey || "";
3787
+ this.zapProgram = new import_core.Program(idl_default, { connection });
3788
+ this.jupiterApiUrl = (config == null ? void 0 : config.jupiterApiUrl) || DEFAULT_JUPITER_API_URL;
3789
+ this.jupiterApiKey = (config == null ? void 0 : config.jupiterApiKey) || "";
3790
+ this.jupiterApiVersion = (_a = config == null ? void 0 : config.jupiterApiVersion) != null ? _a : DEFAULT_JUPITER_API_VERSION;
3242
3791
  }
3243
3792
  /////// PRIVATE FUNDTIONS //////
3244
3793
  initializeLedgerAccount(owner, payer) {
@@ -3247,7 +3796,7 @@ var Zap = class {
3247
3796
  ledger: deriveLedgerAccount(owner),
3248
3797
  owner,
3249
3798
  payer
3250
- }).transaction();
3799
+ }).instruction();
3251
3800
  });
3252
3801
  }
3253
3802
  closeLedgerAccount(owner, rentReceiver) {
@@ -3256,7 +3805,7 @@ var Zap = class {
3256
3805
  ledger: deriveLedgerAccount(owner),
3257
3806
  owner,
3258
3807
  rentReceiver
3259
- }).transaction();
3808
+ }).instruction();
3260
3809
  });
3261
3810
  }
3262
3811
  setLedgerBalance(owner, amount, isTokenA) {
@@ -3264,7 +3813,7 @@ var Zap = class {
3264
3813
  return yield this.zapProgram.methods.setLedgerBalance(amount, isTokenA).accountsPartial({
3265
3814
  ledger: deriveLedgerAccount(owner),
3266
3815
  owner
3267
- }).transaction();
3816
+ }).instruction();
3268
3817
  });
3269
3818
  }
3270
3819
  updateLedgerBalanceAfterSwap(owner, tokenAccount, preSourceTokenAccount, maxTransferAmount, isTokenA) {
@@ -3277,7 +3826,7 @@ var Zap = class {
3277
3826
  ledger: deriveLedgerAccount(owner),
3278
3827
  tokenAccount,
3279
3828
  owner
3280
- }).transaction();
3829
+ }).instruction();
3281
3830
  });
3282
3831
  }
3283
3832
  zapInDammV2(params) {
@@ -3315,7 +3864,7 @@ var Zap = class {
3315
3864
  tokenBProgram,
3316
3865
  dammProgram: DAMM_V2_PROGRAM_ID,
3317
3866
  dammEventAuthority: deriveDammV2EventAuthority()
3318
- }).transaction();
3867
+ }).instruction();
3319
3868
  });
3320
3869
  }
3321
3870
  resetOrInitializeLedgerAccount(user) {
@@ -3325,9 +3874,9 @@ var Zap = class {
3325
3874
  if (accountInfo) {
3326
3875
  const closeLedger = yield this.closeLedgerAccount(user, user);
3327
3876
  const reInitializeLedger = yield this.initializeLedgerAccount(user, user);
3328
- return new import_web38.Transaction().add(closeLedger).add(reInitializeLedger);
3877
+ return [closeLedger, reInitializeLedger];
3329
3878
  }
3330
- return yield this.initializeLedgerAccount(user, user);
3879
+ return [yield this.initializeLedgerAccount(user, user)];
3331
3880
  });
3332
3881
  }
3333
3882
  zapInDlmmForInitializedPosition(params) {
@@ -3394,7 +3943,7 @@ var Zap = class {
3394
3943
  memoProgram: MEMO_PROGRAM_ID,
3395
3944
  dlmmEventAuthority: deriveDlmmEventAuthority(),
3396
3945
  systemProgram: import_web38.SystemProgram.programId
3397
- }).remainingAccounts(binArrays).transaction();
3946
+ }).remainingAccounts(binArrays).instruction();
3398
3947
  });
3399
3948
  }
3400
3949
  zapInDlmmForUninitializedPosition(params) {
@@ -3458,7 +4007,7 @@ var Zap = class {
3458
4007
  memoProgram: MEMO_PROGRAM_ID,
3459
4008
  dlmmEventAuthority: deriveDlmmEventAuthority(),
3460
4009
  systemProgram: import_web38.SystemProgram.programId
3461
- }).remainingAccounts(binArrays).transaction();
4010
+ }).remainingAccounts(binArrays).instruction();
3462
4011
  });
3463
4012
  }
3464
4013
  //#region Zap In
@@ -3472,12 +4021,12 @@ var Zap = class {
3472
4021
  * @param params.amountIn - The amount of input token
3473
4022
  * @param params.inputTokenMint - The input token mint
3474
4023
  * @param params.positionNftMint - The position NFT mint account's public key
3475
- * @param params.maxAccounts - The maximum number of accounts for the Jupiter swap query
3476
4024
  * @param params.maxSqrtPriceChangeBps - The maximum sqrt price change in basis points
3477
4025
  * @param params.slippageBps - The swap slippage tolerance in basis points
3478
4026
  * @param params.dammV2Quote - The DAMM V2 swap quote for exactly 1 input token in lamports (used for price calculation, not the actual amountIn)
3479
4027
  * @param params.jupiterQuote - The Jupiter swap quote for exactly 1 input token in lamports (used for price calculation, not the actual amountIn)
3480
4028
  * @param params.maxTransferAmountExtendPercentage - The percentage to extend the max transfer amount after the swap
4029
+ * @param params.jupiterQuoteOptions - Optional Jupiter query parameters, see `GetJupiterQuoteParams.queryParams`
3481
4030
  * @returns The zap-in transaction parameters for a DAMM V2 direct pool
3482
4031
  * @throws if input token mint matches either tokenA or tokenB in the pool
3483
4032
  * @throws if failed to get both Jupiter and DAMM v2 swap quotes
@@ -3491,12 +4040,12 @@ var Zap = class {
3491
4040
  inputTokenMint,
3492
4041
  amountIn,
3493
4042
  positionNftMint,
3494
- maxAccounts,
3495
4043
  maxSqrtPriceChangeBps,
3496
4044
  slippageBps,
3497
4045
  dammV2Quote,
3498
4046
  jupiterQuote,
3499
- maxTransferAmountExtendPercentage
4047
+ maxTransferAmountExtendPercentage,
4048
+ jupiterQuoteOptions
3500
4049
  } = params;
3501
4050
  const poolState = yield getDammV2Pool(this.connection, pool);
3502
4051
  const {
@@ -3516,7 +4065,7 @@ var Zap = class {
3516
4065
  const tokenAProgram = (0, import_cp_amm_sdk3.getTokenProgram)(tokenAFlag);
3517
4066
  const tokenBProgram = (0, import_cp_amm_sdk3.getTokenProgram)(tokenBFlag);
3518
4067
  const preInstructions = [];
3519
- if (tokenAMint.equals(import_spl_token6.NATIVE_MINT) || tokenBMint.equals(import_spl_token6.NATIVE_MINT)) {
4068
+ if (inputTokenMint.equals(import_spl_token6.NATIVE_MINT)) {
3520
4069
  const { ataPubkey: userWrapSolAcc, ix: initializeUserWrapSOLAta } = yield getOrCreateATAInstruction(
3521
4070
  this.connection,
3522
4071
  import_spl_token6.NATIVE_MINT,
@@ -3568,49 +4117,56 @@ var Zap = class {
3568
4117
  const singleSidedA = isSingleSidedA(poolState);
3569
4118
  const singleSidedB = isSingleSidedB(poolState);
3570
4119
  let amount;
3571
- let swapTransactions = [];
4120
+ let swapInstructions = [];
4121
+ let addressLookupTableAccounts = [];
3572
4122
  let maxTransferAmount;
3573
4123
  let swapInAmount;
3574
4124
  let swapRoute;
3575
4125
  if (singleSidedA && isInputTokenA || singleSidedB && !isInputTokenA) {
3576
4126
  amount = amountIn;
3577
- swapInAmount = new import_anchor.BN(0);
3578
- maxTransferAmount = new import_anchor.BN(0);
4127
+ swapInAmount = new import_core.BN(0);
4128
+ maxTransferAmount = new import_core.BN(0);
3579
4129
  swapRoute = "dammV2" /* DammV2 */;
3580
4130
  } else if (singleSidedA && !isInputTokenA || singleSidedB && isInputTokenA) {
3581
- if (jupiterQuote !== null && (dammV2Quote === null || new import_anchor.BN(jupiterQuote.outAmount).gte(dammV2Quote.swapOutAmount))) {
4131
+ if (jupiterQuote !== null && (dammV2Quote === null || new import_core.BN(jupiterQuote.outAmount).gte(dammV2Quote.swapOutAmount))) {
3582
4132
  swapInAmount = amountIn;
3583
- amount = new import_anchor.BN(0);
4133
+ amount = new import_core.BN(0);
3584
4134
  const result = yield buildJupiterSwapTransaction(
4135
+ this.connection,
3585
4136
  user,
3586
4137
  inputTokenMint,
3587
4138
  outputTokenMint,
3588
4139
  swapInAmount,
3589
- maxAccounts,
3590
4140
  slippageBps,
3591
4141
  void 0,
3592
4142
  {
3593
4143
  jupiterApiUrl: this.jupiterApiUrl,
3594
- jupiterApiKey: this.jupiterApiKey
3595
- }
4144
+ jupiterApiKey: this.jupiterApiKey,
4145
+ jupiterApiVersion: this.jupiterApiVersion
4146
+ },
4147
+ jupiterQuoteOptions
4148
+ );
4149
+ preInstructions.push(
4150
+ ...getJupiterCreateAtaInstructions(result.setupInstructions)
3596
4151
  );
3597
- swapTransactions = [result.transaction];
4152
+ swapInstructions = [result.instruction];
4153
+ addressLookupTableAccounts = result.addressLookupTableAccounts;
3598
4154
  maxTransferAmount = getExtendMaxAmountTransfer(
3599
4155
  result.quoteResponse.outAmount,
3600
4156
  maxTransferAmountExtendPercentage
3601
4157
  );
3602
4158
  swapRoute = "jupiter" /* Jupiter */;
3603
4159
  } else if (dammV2Quote !== null) {
3604
- swapInAmount = new import_anchor.BN(0);
4160
+ swapInAmount = new import_core.BN(0);
3605
4161
  amount = amountIn;
3606
- maxTransferAmount = new import_anchor.BN(0);
4162
+ maxTransferAmount = new import_core.BN(0);
3607
4163
  swapRoute = "dammV2" /* DammV2 */;
3608
4164
  } else {
3609
4165
  throw new Error(
3610
4166
  "No Jupiter or DAMM v2 quote found for single-sided swap, unable to proceed"
3611
4167
  );
3612
4168
  }
3613
- } else if (jupiterQuote !== null && (dammV2Quote === null || new import_anchor.BN(jupiterQuote.outAmount).gte(dammV2Quote.swapOutAmount))) {
4169
+ } else if (jupiterQuote !== null && (dammV2Quote === null || new import_core.BN(jupiterQuote.outAmount).gte(dammV2Quote.swapOutAmount))) {
3614
4170
  const price = convertLamportsToUiAmount(
3615
4171
  new import_decimal4.default(jupiterQuote.outAmount),
3616
4172
  isInputTokenA ? tokenBDecimal : tokenADecimal
@@ -3631,19 +4187,25 @@ var Zap = class {
3631
4187
  );
3632
4188
  amount = amountIn.sub(swapInAmount);
3633
4189
  const result = yield buildJupiterSwapTransaction(
4190
+ this.connection,
3634
4191
  user,
3635
4192
  inputTokenMint,
3636
4193
  outputTokenMint,
3637
4194
  swapInAmount,
3638
- maxAccounts,
3639
4195
  slippageBps,
3640
4196
  void 0,
3641
4197
  {
3642
4198
  jupiterApiUrl: this.jupiterApiUrl,
3643
- jupiterApiKey: this.jupiterApiKey
3644
- }
4199
+ jupiterApiKey: this.jupiterApiKey,
4200
+ jupiterApiVersion: this.jupiterApiVersion
4201
+ },
4202
+ jupiterQuoteOptions
4203
+ );
4204
+ preInstructions.push(
4205
+ ...getJupiterCreateAtaInstructions(result.setupInstructions)
3645
4206
  );
3646
- swapTransactions = [result.transaction];
4207
+ swapInstructions = [result.instruction];
4208
+ addressLookupTableAccounts = result.addressLookupTableAccounts;
3647
4209
  maxTransferAmount = getExtendMaxAmountTransfer(
3648
4210
  result.quoteResponse.outAmount,
3649
4211
  maxTransferAmountExtendPercentage
@@ -3701,7 +4263,8 @@ var Zap = class {
3701
4263
  preSqrtPrice: poolState.sqrtPrice,
3702
4264
  maxSqrtPriceChangeBps,
3703
4265
  preInstructions,
3704
- swapTransactions,
4266
+ swapInstructions,
4267
+ addressLookupTableAccounts,
3705
4268
  cleanUpInstructions,
3706
4269
  swapInEstimate: {
3707
4270
  inAmount: swapInAmount,
@@ -3720,12 +4283,12 @@ var Zap = class {
3720
4283
  * @param params.amountIn - The amount of input token
3721
4284
  * @param params.inputTokenMint - The input token mint
3722
4285
  * @param params.positionNftMint - The position NFT mint account's public key
3723
- * @param params.maxAccounts - The maximum number of accounts for the Jupiter swap query
3724
4286
  * @param params.maxSqrtPriceChangeBps - The maximum sqrt price change in basis points
3725
4287
  * @param params.slippageBps - The swap slippage tolerance in basis points
3726
4288
  * @param params.jupiterQuoteToA - The Jupiter quote for swapping to tokenA for exactly 1 input token in lamports (used for price calculation, not the actual amountIn)
3727
4289
  * @param params.jupiterQuoteToB - The Jupiter quote for swapping to tokenB for exactly 1 input token in lamports (used for price calculation, not the actual amountIn)
3728
4290
  * @param params.maxTransferAmountExtendPercentage - The percentage to extend the max transfer amount after the swap
4291
+ * @param params.jupiterQuoteOptions - Optional Jupiter query parameters, see `GetJupiterQuoteParams.queryParams`
3729
4292
  * @returns The zap-in transaction parameters for a DAMM V2 indirect pool
3730
4293
  * @throws if input token mint matches either tokenA or tokenB in the pool
3731
4294
  * @throws if no Jupiter quote provided for both tokens
@@ -3739,12 +4302,12 @@ var Zap = class {
3739
4302
  pool,
3740
4303
  positionNftMint,
3741
4304
  amountIn,
3742
- maxAccounts,
3743
4305
  maxSqrtPriceChangeBps,
3744
4306
  maxTransferAmountExtendPercentage,
3745
4307
  slippageBps,
3746
4308
  jupiterQuoteToA,
3747
- jupiterQuoteToB
4309
+ jupiterQuoteToB,
4310
+ jupiterQuoteOptions
3748
4311
  } = params;
3749
4312
  const poolState = yield getDammV2Pool(this.connection, pool);
3750
4313
  const {
@@ -3829,17 +4392,22 @@ var Zap = class {
3829
4392
  );
3830
4393
  }
3831
4394
  const result = yield buildJupiterSwapTransaction(
4395
+ this.connection,
3832
4396
  user,
3833
4397
  inputTokenMint,
3834
4398
  tokenAMint,
3835
4399
  amountIn,
3836
- maxAccounts,
3837
4400
  slippageBps,
3838
4401
  void 0,
3839
4402
  {
3840
4403
  jupiterApiUrl: this.jupiterApiUrl,
3841
- jupiterApiKey: this.jupiterApiKey
3842
- }
4404
+ jupiterApiKey: this.jupiterApiKey,
4405
+ jupiterApiVersion: this.jupiterApiVersion
4406
+ },
4407
+ jupiterQuoteOptions
4408
+ );
4409
+ preInstructions.push(
4410
+ ...getJupiterCreateAtaInstructions(result.setupInstructions)
3843
4411
  );
3844
4412
  return {
3845
4413
  user,
@@ -3847,7 +4415,7 @@ var Zap = class {
3847
4415
  position,
3848
4416
  positionNftAccount,
3849
4417
  maxSqrtPriceChangeBps,
3850
- amount: new import_anchor.BN(0),
4418
+ amount: new import_core.BN(0),
3851
4419
  isDirectPool: false,
3852
4420
  tokenAMint,
3853
4421
  tokenBMint,
@@ -3860,14 +4428,15 @@ var Zap = class {
3860
4428
  maxTransferAmountExtendPercentage
3861
4429
  ),
3862
4430
  swapType: 0 /* swapToA */,
3863
- maxTransferAmountB: new import_anchor.BN(0),
4431
+ maxTransferAmountB: new import_core.BN(0),
3864
4432
  preSqrtPrice: poolState.sqrtPrice,
3865
4433
  preInstructions,
3866
- swapTransactions: [result.transaction],
4434
+ swapInstructions: [result.instruction],
4435
+ addressLookupTableAccounts: result.addressLookupTableAccounts,
3867
4436
  cleanUpInstructions,
3868
4437
  swapInEstimate: {
3869
4438
  inAmountA: amountIn,
3870
- inAmountB: new import_anchor.BN(0),
4439
+ inAmountB: new import_core.BN(0),
3871
4440
  routeA: "jupiter" /* Jupiter */,
3872
4441
  routeB: "dammV2" /* DammV2 */
3873
4442
  }
@@ -3880,17 +4449,22 @@ var Zap = class {
3880
4449
  );
3881
4450
  }
3882
4451
  const result = yield buildJupiterSwapTransaction(
4452
+ this.connection,
3883
4453
  user,
3884
4454
  inputTokenMint,
3885
4455
  tokenBMint,
3886
4456
  amountIn,
3887
- maxAccounts,
3888
4457
  slippageBps,
3889
4458
  void 0,
3890
4459
  {
3891
4460
  jupiterApiUrl: this.jupiterApiUrl,
3892
- jupiterApiKey: this.jupiterApiKey
3893
- }
4461
+ jupiterApiKey: this.jupiterApiKey,
4462
+ jupiterApiVersion: this.jupiterApiVersion
4463
+ },
4464
+ jupiterQuoteOptions
4465
+ );
4466
+ preInstructions.push(
4467
+ ...getJupiterCreateAtaInstructions(result.setupInstructions)
3894
4468
  );
3895
4469
  return {
3896
4470
  user,
@@ -3898,7 +4472,7 @@ var Zap = class {
3898
4472
  position,
3899
4473
  positionNftAccount,
3900
4474
  maxSqrtPriceChangeBps,
3901
- amount: new import_anchor.BN(0),
4475
+ amount: new import_core.BN(0),
3902
4476
  isDirectPool: false,
3903
4477
  tokenAMint,
3904
4478
  tokenBMint,
@@ -3906,7 +4480,7 @@ var Zap = class {
3906
4480
  tokenBVault,
3907
4481
  tokenAProgram,
3908
4482
  tokenBProgram,
3909
- maxTransferAmountA: new import_anchor.BN(0),
4483
+ maxTransferAmountA: new import_core.BN(0),
3910
4484
  maxTransferAmountB: getExtendMaxAmountTransfer(
3911
4485
  result.quoteResponse.outAmount,
3912
4486
  maxTransferAmountExtendPercentage
@@ -3914,10 +4488,11 @@ var Zap = class {
3914
4488
  swapType: 1 /* swapToB */,
3915
4489
  preSqrtPrice: poolState.sqrtPrice,
3916
4490
  preInstructions,
3917
- swapTransactions: [result.transaction],
4491
+ swapInstructions: [result.instruction],
4492
+ addressLookupTableAccounts: result.addressLookupTableAccounts,
3918
4493
  cleanUpInstructions,
3919
4494
  swapInEstimate: {
3920
- inAmountA: new import_anchor.BN(0),
4495
+ inAmountA: new import_core.BN(0),
3921
4496
  inAmountB: amountIn,
3922
4497
  routeA: "dammV2" /* DammV2 */,
3923
4498
  routeB: "jupiter" /* Jupiter */
@@ -3926,17 +4501,22 @@ var Zap = class {
3926
4501
  }
3927
4502
  if (jupiterQuoteToA && jupiterQuoteToB === null) {
3928
4503
  const result = yield buildJupiterSwapTransaction(
4504
+ this.connection,
3929
4505
  user,
3930
4506
  inputTokenMint,
3931
4507
  tokenAMint,
3932
4508
  amountIn,
3933
- maxAccounts,
3934
4509
  slippageBps,
3935
4510
  void 0,
3936
4511
  {
3937
4512
  jupiterApiUrl: this.jupiterApiUrl,
3938
- jupiterApiKey: this.jupiterApiKey
3939
- }
4513
+ jupiterApiKey: this.jupiterApiKey,
4514
+ jupiterApiVersion: this.jupiterApiVersion
4515
+ },
4516
+ jupiterQuoteOptions
4517
+ );
4518
+ preInstructions.push(
4519
+ ...getJupiterCreateAtaInstructions(result.setupInstructions)
3940
4520
  );
3941
4521
  return {
3942
4522
  user,
@@ -3944,7 +4524,7 @@ var Zap = class {
3944
4524
  position,
3945
4525
  positionNftAccount,
3946
4526
  maxSqrtPriceChangeBps,
3947
- amount: new import_anchor.BN(0),
4527
+ amount: new import_core.BN(0),
3948
4528
  isDirectPool: false,
3949
4529
  tokenAMint,
3950
4530
  tokenBMint,
@@ -3957,14 +4537,15 @@ var Zap = class {
3957
4537
  maxTransferAmountExtendPercentage
3958
4538
  ),
3959
4539
  swapType: 0 /* swapToA */,
3960
- maxTransferAmountB: new import_anchor.BN(0),
4540
+ maxTransferAmountB: new import_core.BN(0),
3961
4541
  preSqrtPrice: poolState.sqrtPrice,
3962
4542
  preInstructions,
3963
- swapTransactions: [result.transaction],
4543
+ swapInstructions: [result.instruction],
4544
+ addressLookupTableAccounts: result.addressLookupTableAccounts,
3964
4545
  cleanUpInstructions,
3965
4546
  swapInEstimate: {
3966
4547
  inAmountA: amountIn,
3967
- inAmountB: new import_anchor.BN(0),
4548
+ inAmountB: new import_core.BN(0),
3968
4549
  routeA: "jupiter" /* Jupiter */,
3969
4550
  routeB: "dammV2" /* DammV2 */
3970
4551
  }
@@ -3972,17 +4553,22 @@ var Zap = class {
3972
4553
  }
3973
4554
  if (jupiterQuoteToB && jupiterQuoteToA === null) {
3974
4555
  const result = yield buildJupiterSwapTransaction(
4556
+ this.connection,
3975
4557
  user,
3976
4558
  inputTokenMint,
3977
4559
  tokenBMint,
3978
4560
  amountIn,
3979
- maxAccounts,
3980
4561
  slippageBps,
3981
4562
  void 0,
3982
4563
  {
3983
4564
  jupiterApiUrl: this.jupiterApiUrl,
3984
- jupiterApiKey: this.jupiterApiKey
3985
- }
4565
+ jupiterApiKey: this.jupiterApiKey,
4566
+ jupiterApiVersion: this.jupiterApiVersion
4567
+ },
4568
+ jupiterQuoteOptions
4569
+ );
4570
+ preInstructions.push(
4571
+ ...getJupiterCreateAtaInstructions(result.setupInstructions)
3986
4572
  );
3987
4573
  return {
3988
4574
  user,
@@ -3990,7 +4576,7 @@ var Zap = class {
3990
4576
  position,
3991
4577
  positionNftAccount,
3992
4578
  maxSqrtPriceChangeBps,
3993
- amount: new import_anchor.BN(0),
4579
+ amount: new import_core.BN(0),
3994
4580
  isDirectPool: false,
3995
4581
  tokenAMint,
3996
4582
  tokenBMint,
@@ -3998,7 +4584,7 @@ var Zap = class {
3998
4584
  tokenBVault,
3999
4585
  tokenAProgram,
4000
4586
  tokenBProgram,
4001
- maxTransferAmountA: new import_anchor.BN(0),
4587
+ maxTransferAmountA: new import_core.BN(0),
4002
4588
  maxTransferAmountB: getExtendMaxAmountTransfer(
4003
4589
  result.quoteResponse.outAmount,
4004
4590
  maxTransferAmountExtendPercentage
@@ -4006,10 +4592,11 @@ var Zap = class {
4006
4592
  swapType: 1 /* swapToB */,
4007
4593
  preSqrtPrice: poolState.sqrtPrice,
4008
4594
  preInstructions,
4009
- swapTransactions: [result.transaction],
4595
+ swapInstructions: [result.instruction],
4596
+ addressLookupTableAccounts: result.addressLookupTableAccounts,
4010
4597
  cleanUpInstructions,
4011
4598
  swapInEstimate: {
4012
- inAmountA: new import_anchor.BN(0),
4599
+ inAmountA: new import_core.BN(0),
4013
4600
  inAmountB: amountIn,
4014
4601
  routeA: "dammV2" /* DammV2 */,
4015
4602
  routeB: "jupiter" /* Jupiter */
@@ -4046,7 +4633,7 @@ var Zap = class {
4046
4633
  position,
4047
4634
  positionNftAccount,
4048
4635
  maxSqrtPriceChangeBps,
4049
- amount: new import_anchor.BN(0),
4636
+ amount: new import_core.BN(0),
4050
4637
  isDirectPool: false,
4051
4638
  tokenAMint,
4052
4639
  tokenBMint,
@@ -4058,43 +4645,57 @@ var Zap = class {
4058
4645
  preSqrtPrice: poolState.sqrtPrice,
4059
4646
  cleanUpInstructions
4060
4647
  };
4061
- const { transaction: swapToATransaction, quoteResponse: swapToAQuote } = yield buildJupiterSwapTransaction(
4648
+ const swapToA = yield buildJupiterSwapTransaction(
4649
+ this.connection,
4062
4650
  user,
4063
4651
  inputTokenMint,
4064
4652
  tokenAMint,
4065
4653
  swapAmountToA,
4066
- maxAccounts,
4067
4654
  slippageBps,
4068
4655
  void 0,
4069
4656
  {
4070
4657
  jupiterApiUrl: this.jupiterApiUrl,
4071
- jupiterApiKey: this.jupiterApiKey
4072
- }
4658
+ jupiterApiKey: this.jupiterApiKey,
4659
+ jupiterApiVersion: this.jupiterApiVersion
4660
+ },
4661
+ jupiterQuoteOptions
4662
+ );
4663
+ preInstructions.push(
4664
+ ...getJupiterCreateAtaInstructions(swapToA.setupInstructions)
4073
4665
  );
4074
- const { transaction: swapToBTransaction, quoteResponse: swapToBQuote } = yield buildJupiterSwapTransaction(
4666
+ const swapToB = yield buildJupiterSwapTransaction(
4667
+ this.connection,
4075
4668
  user,
4076
4669
  inputTokenMint,
4077
4670
  tokenBMint,
4078
4671
  swapAmountToB,
4079
- maxAccounts,
4080
4672
  slippageBps,
4081
4673
  void 0,
4082
4674
  {
4083
4675
  jupiterApiUrl: this.jupiterApiUrl,
4084
- jupiterApiKey: this.jupiterApiKey
4085
- }
4676
+ jupiterApiKey: this.jupiterApiKey,
4677
+ jupiterApiVersion: this.jupiterApiVersion
4678
+ },
4679
+ jupiterQuoteOptions
4680
+ );
4681
+ preInstructions.push(
4682
+ ...getJupiterCreateAtaInstructions(swapToB.setupInstructions)
4086
4683
  );
4087
4684
  return __spreadProps(__spreadValues({}, baseParams), {
4088
4685
  maxTransferAmountA: getExtendMaxAmountTransfer(
4089
- swapToAQuote.outAmount,
4686
+ swapToA.quoteResponse.outAmount,
4090
4687
  maxTransferAmountExtendPercentage
4091
4688
  ),
4092
4689
  maxTransferAmountB: getExtendMaxAmountTransfer(
4093
- swapToBQuote.outAmount,
4690
+ swapToB.quoteResponse.outAmount,
4094
4691
  maxTransferAmountExtendPercentage
4095
4692
  ),
4096
4693
  swapType: 2 /* swapToBoth */,
4097
- swapTransactions: [swapToATransaction, swapToBTransaction],
4694
+ swapInstructions: [swapToA.instruction, swapToB.instruction],
4695
+ addressLookupTableAccounts: mergeAddressLookupTableAccounts(
4696
+ swapToA.addressLookupTableAccounts,
4697
+ swapToB.addressLookupTableAccounts
4698
+ ),
4098
4699
  swapInEstimate: {
4099
4700
  inAmountA: swapAmountToA,
4100
4701
  inAmountB: swapAmountToB,
@@ -4109,7 +4710,12 @@ var Zap = class {
4109
4710
  });
4110
4711
  }
4111
4712
  /**
4112
- * Build DAMM v2 zap-in transaction
4713
+ * Build the DAMM v2 zap-in instructions
4714
+ *
4715
+ * Instructions are ordered: setup (token accounts, wrap SOL), swaps, ledger, zap in, clean up.
4716
+ * They must run in that order but can be split across several transactions, because state
4717
+ * between steps lives in accounts. Splitting loses atomicity, so send split transactions as a bundle.
4718
+ * No compute budget instructions are included; the client sets the compute budget for each transaction.
4113
4719
  *
4114
4720
  * @param params.user - The user's public key
4115
4721
  * @param params.pool - The pool's public key
@@ -4125,10 +4731,11 @@ var Zap = class {
4125
4731
  * @param params.amount - The amount to deposit
4126
4732
  * @param params.preSqrtPrice - The sqrt price before the operation
4127
4733
  * @param params.maxSqrtPriceChangeBps - The maximum sqrt price change in basis points
4128
- * @param params.preInstructions - Instructions to run before the zap in
4129
- * @param params.swapTransactions - Swap transactions to execute
4734
+ * @param params.preInstructions - Instructions to run before the zap in, including the token accounts the Jupiter swaps need. Token account creation repeated here is removed
4735
+ * @param params.swapInstructions - Swap instructions to execute
4736
+ * @param params.addressLookupTableAccounts - Lookup tables used by the swap instructions
4130
4737
  * @param params.cleanUpInstructions - Instructions to run after the zap in
4131
- * @returns Response containing transaction components
4738
+ * @returns The ordered instructions and the lookup tables they use
4132
4739
  */
4133
4740
  buildZapInDammV2Transaction(params) {
4134
4741
  return __async(this, null, function* () {
@@ -4148,61 +4755,41 @@ var Zap = class {
4148
4755
  preSqrtPrice,
4149
4756
  maxSqrtPriceChangeBps,
4150
4757
  preInstructions,
4151
- swapTransactions,
4758
+ swapInstructions,
4759
+ addressLookupTableAccounts,
4152
4760
  cleanUpInstructions
4153
4761
  } = params;
4154
- const [
4155
- { ataPubkey: tokenAAccount, ix: initializeTokenAIx },
4156
- { ataPubkey: tokenBAccount, ix: initializeTokenBIx }
4157
- ] = yield Promise.all([
4158
- getOrCreateATAInstruction(
4159
- this.connection,
4160
- tokenAMint,
4161
- user,
4162
- user,
4163
- false,
4164
- tokenAProgram
4165
- ),
4166
- getOrCreateATAInstruction(
4167
- this.connection,
4168
- tokenBMint,
4169
- user,
4170
- user,
4171
- false,
4172
- tokenBProgram
4173
- )
4762
+ const {
4763
+ tokenAccounts: [tokenAAccount, tokenBAccount],
4764
+ instructions: setupInstructions
4765
+ } = getCreateAtaInstructions(user, [
4766
+ { mint: tokenAMint, tokenProgram: tokenAProgram },
4767
+ { mint: tokenBMint, tokenProgram: tokenBProgram }
4174
4768
  ]);
4175
- const setupTransaction = new import_web38.Transaction();
4176
- initializeTokenAIx && setupTransaction.add(initializeTokenAIx);
4177
- initializeTokenBIx && setupTransaction.add(initializeTokenBIx);
4178
- if (preInstructions.length > 0) {
4179
- setupTransaction.add(...preInstructions);
4180
- }
4181
- const ledgerTransaction = new import_web38.Transaction();
4182
- const resetOrInitializeLedgerTx = yield this.resetOrInitializeLedgerAccount(user);
4183
- ledgerTransaction.add(resetOrInitializeLedgerTx);
4769
+ setupInstructions.push(...preInstructions);
4770
+ const ledgerInstructions = yield this.resetOrInitializeLedgerAccount(user);
4184
4771
  if (isDirectPool) {
4185
4772
  const isTokenA = params.isTokenA;
4186
- const setLedgerBalanceTx = yield this.setLedgerBalance(
4773
+ const setLedgerBalanceIx = yield this.setLedgerBalance(
4187
4774
  user,
4188
4775
  amount,
4189
4776
  isTokenA
4190
4777
  );
4191
- ledgerTransaction.add(setLedgerBalanceTx);
4192
- if (swapTransactions.length > 0) {
4778
+ ledgerInstructions.push(setLedgerBalanceIx);
4779
+ if (swapInstructions.length > 0) {
4193
4780
  const tokenAccount = isTokenA ? tokenBAccount : tokenAAccount;
4194
4781
  const preTokenBalance = yield getTokenAccountBalance(
4195
4782
  this.connection,
4196
4783
  tokenAccount
4197
4784
  );
4198
- const updateLedgerBalanceAfterSwapTx = yield this.updateLedgerBalanceAfterSwap(
4785
+ const updateLedgerBalanceAfterSwapIx = yield this.updateLedgerBalanceAfterSwap(
4199
4786
  user,
4200
4787
  tokenAccount,
4201
- new import_anchor.BN(preTokenBalance),
4788
+ new import_core.BN(preTokenBalance),
4202
4789
  params.maxTransferAmount,
4203
4790
  !isTokenA
4204
4791
  );
4205
- ledgerTransaction.add(updateLedgerBalanceAfterSwapTx);
4792
+ ledgerInstructions.push(updateLedgerBalanceAfterSwapIx);
4206
4793
  }
4207
4794
  } else {
4208
4795
  const preTokenABalance = yield getTokenAccountBalance(
@@ -4213,32 +4800,35 @@ var Zap = class {
4213
4800
  this.connection,
4214
4801
  tokenBAccount
4215
4802
  );
4216
- const updateLedgerBalanceTokenAAfterSwapTx = yield this.updateLedgerBalanceAfterSwap(
4803
+ const updateLedgerBalanceTokenAAfterSwapIx = yield this.updateLedgerBalanceAfterSwap(
4217
4804
  user,
4218
4805
  tokenAAccount,
4219
- new import_anchor.BN(preTokenABalance),
4806
+ new import_core.BN(preTokenABalance),
4220
4807
  params.maxTransferAmountA,
4221
4808
  true
4222
4809
  // is token A
4223
4810
  );
4224
- const updateLedgerBalanceTokenBAfterSwapTx = yield this.updateLedgerBalanceAfterSwap(
4811
+ const updateLedgerBalanceTokenBAfterSwapIx = yield this.updateLedgerBalanceAfterSwap(
4225
4812
  user,
4226
4813
  tokenBAccount,
4227
- new import_anchor.BN(preTokenBBalance),
4814
+ new import_core.BN(preTokenBBalance),
4228
4815
  params.maxTransferAmountB,
4229
4816
  false
4230
4817
  // isn't token A
4231
4818
  );
4232
4819
  const swapType = params.swapType;
4233
4820
  if (swapType == 0 /* swapToA */) {
4234
- ledgerTransaction.add(updateLedgerBalanceTokenAAfterSwapTx);
4821
+ ledgerInstructions.push(updateLedgerBalanceTokenAAfterSwapIx);
4235
4822
  } else if (swapType == 1 /* swapToB */) {
4236
- ledgerTransaction.add(updateLedgerBalanceTokenBAfterSwapTx);
4823
+ ledgerInstructions.push(updateLedgerBalanceTokenBAfterSwapIx);
4237
4824
  } else {
4238
- ledgerTransaction.add(updateLedgerBalanceTokenAAfterSwapTx).add(updateLedgerBalanceTokenBAfterSwapTx);
4825
+ ledgerInstructions.push(
4826
+ updateLedgerBalanceTokenAAfterSwapIx,
4827
+ updateLedgerBalanceTokenBAfterSwapIx
4828
+ );
4239
4829
  }
4240
4830
  }
4241
- const zapInTransaction = yield this.zapInDammV2({
4831
+ const zapInInstruction = yield this.zapInDammV2({
4242
4832
  user,
4243
4833
  pool,
4244
4834
  position,
@@ -4254,18 +4844,17 @@ var Zap = class {
4254
4844
  tokenAAccount,
4255
4845
  tokenBAccount
4256
4846
  });
4257
- const cleanUpTransaction = new import_web38.Transaction();
4258
- const closeLedgerTransaction = yield this.closeLedgerAccount(user, user);
4259
- cleanUpTransaction.add(closeLedgerTransaction);
4260
- if (cleanUpInstructions.length > 0) {
4261
- cleanUpTransaction.add(...cleanUpInstructions);
4262
- }
4847
+ const closeLedgerInstruction = yield this.closeLedgerAccount(user, user);
4263
4848
  return {
4264
- setupTransaction: setupTransaction.instructions.length > 0 ? setupTransaction : void 0,
4265
- swapTransactions,
4266
- ledgerTransaction,
4267
- zapInTransaction,
4268
- cleanUpTransaction
4849
+ instructions: [
4850
+ ...dedupeCreateAtaInstructions(setupInstructions),
4851
+ ...swapInstructions,
4852
+ ...ledgerInstructions,
4853
+ zapInInstruction,
4854
+ closeLedgerInstruction,
4855
+ ...cleanUpInstructions
4856
+ ],
4857
+ addressLookupTableAccounts
4269
4858
  };
4270
4859
  });
4271
4860
  }
@@ -4282,8 +4871,7 @@ var Zap = class {
4282
4871
  * @param params.maxDeltaId - The bin delta relative to the active bin for the upper bin position
4283
4872
  * @param params.strategy - The liquidity distribution strategy
4284
4873
  * @param params.favorXInActiveId - Whether to favor token X in the active bin
4285
- * @param params.maxAccounts - The maximum number of accounts for the Jupiter swap query
4286
- * @param params.swapSlippageBps - The swap slippage tolerance in basis points
4874
+ * @param params.swapSlippageBps - Swap slippage vs the **quoted route**, not vs a fair input/output price. Validate `directSwapEstimate` / `outAmount` before send.
4287
4875
  * @param params.maxTransferAmountExtendPercentage - The percentage to extend the max transfer amount after the swap
4288
4876
  * @param params.maxActiveBinSlippage - The maximum active bin slippage
4289
4877
  * @param params.directSwapEstimate - The result from the direct swap estimate
@@ -4303,7 +4891,6 @@ var Zap = class {
4303
4891
  maxDeltaId,
4304
4892
  strategy,
4305
4893
  favorXInActiveId,
4306
- maxAccounts,
4307
4894
  swapSlippageBps,
4308
4895
  maxTransferAmountExtendPercentage,
4309
4896
  maxActiveBinSlippage,
@@ -4336,25 +4923,31 @@ var Zap = class {
4336
4923
  initializeWrapSolIx && preInstructions.push(initializeWrapSolIx);
4337
4924
  preInstructions.push(...wrapSOL);
4338
4925
  }
4339
- const swapTransactions = [];
4926
+ const swapInstructions = [];
4927
+ let addressLookupTableAccounts = [];
4340
4928
  let maxTransferAmount;
4341
4929
  if (directSwapEstimate.swapType !== 2 /* NoSwap */ && directSwapEstimate.quote) {
4342
4930
  const swapQuote = directSwapEstimate.quote;
4343
4931
  if (swapQuote.route === 0 /* Jupiter */) {
4344
- const { transaction: swapTx } = yield buildJupiterSwapTransaction(
4932
+ const swap = yield buildJupiterSwapTransaction(
4933
+ this.connection,
4345
4934
  user,
4346
4935
  directSwapEstimate.swapType === 0 /* XToY */ ? tokenXMint : tokenYMint,
4347
4936
  directSwapEstimate.swapType === 0 /* XToY */ ? tokenYMint : tokenXMint,
4348
4937
  directSwapEstimate.swapAmount,
4349
- maxAccounts,
4350
4938
  swapSlippageBps,
4351
4939
  swapQuote.originalQuote,
4352
4940
  {
4353
4941
  jupiterApiUrl: this.jupiterApiUrl,
4354
- jupiterApiKey: this.jupiterApiKey
4942
+ jupiterApiKey: this.jupiterApiKey,
4943
+ jupiterApiVersion: this.jupiterApiVersion
4355
4944
  }
4356
4945
  );
4357
- swapTransactions.push(swapTx);
4946
+ preInstructions.push(
4947
+ ...getJupiterCreateAtaInstructions(swap.setupInstructions)
4948
+ );
4949
+ swapInstructions.push(swap.instruction);
4950
+ addressLookupTableAccounts = swap.addressLookupTableAccounts;
4358
4951
  } else {
4359
4952
  const swapForY = directSwapEstimate.swapType === 0 /* XToY */;
4360
4953
  const binArrays2 = yield dlmm.getBinArrayForSwap(swapForY);
@@ -4367,6 +4960,9 @@ var Zap = class {
4367
4960
  user,
4368
4961
  binArraysPubkey: binArrays2.map((item) => item.publicKey)
4369
4962
  });
4963
+ swapTx.instructions = filterOutComputeBudgetInstructions(
4964
+ swapTx.instructions
4965
+ );
4370
4966
  if (tokenXMint.equals(import_spl_token6.NATIVE_MINT) || tokenYMint.equals(import_spl_token6.NATIVE_MINT)) {
4371
4967
  swapTx.instructions = swapTx.instructions.filter((ix) => {
4372
4968
  if (ix.programId.equals(import_spl_token6.TOKEN_PROGRAM_ID)) {
@@ -4375,14 +4971,14 @@ var Zap = class {
4375
4971
  return true;
4376
4972
  });
4377
4973
  }
4378
- swapTransactions.push(swapTx);
4974
+ swapInstructions.push(...swapTx.instructions);
4379
4975
  }
4380
4976
  maxTransferAmount = getExtendMaxAmountTransfer(
4381
4977
  directSwapEstimate.expectedOutput.toString(),
4382
4978
  maxTransferAmountExtendPercentage
4383
4979
  );
4384
4980
  } else {
4385
- maxTransferAmount = new import_anchor.BN(0);
4981
+ maxTransferAmount = new import_core.BN(0);
4386
4982
  }
4387
4983
  const amount = isTokenX ? directSwapEstimate.postSwapX : directSwapEstimate.postSwapY;
4388
4984
  const cleanUpInstructions = [];
@@ -4392,8 +4988,8 @@ var Zap = class {
4392
4988
  }
4393
4989
  const binArrays = (0, import_dlmm4.getBinArraysRequiredByPositionRange)(
4394
4990
  lbPair,
4395
- new import_anchor.BN(activeId + minDeltaId),
4396
- new import_anchor.BN(activeId + maxDeltaId),
4991
+ new import_core.BN(activeId + minDeltaId),
4992
+ new import_core.BN(activeId + maxDeltaId),
4397
4993
  DLMM_PROGRAM_ID
4398
4994
  ).map((item) => ({
4399
4995
  pubkey: item.key,
@@ -4425,7 +5021,8 @@ var Zap = class {
4425
5021
  amount,
4426
5022
  maxTransferAmount,
4427
5023
  preInstructions,
4428
- swapTransactions,
5024
+ swapInstructions,
5025
+ addressLookupTableAccounts,
4429
5026
  cleanUpInstructions,
4430
5027
  binArrays,
4431
5028
  binArrayBitmapExtension: binArrayBitmapExtensionData ? binArrayBitmapExtension : null,
@@ -4447,7 +5044,6 @@ var Zap = class {
4447
5044
  * @param params.strategy - The liquidity distribution strategy
4448
5045
  * @param params.favorXInActiveId - Whether to favor token X in the active bin
4449
5046
  * @param params.indirectSwapEstimate - The result from the indirect swap estimate
4450
- * @param params.maxAccounts - The maximum number of accounts for the Jupiter swap query
4451
5047
  * @param params.swapSlippageBps - The swap slippage tolerance in basis points
4452
5048
  * @param params.maxTransferAmountExtendPercentage - The percentage to extend the max transfer amount after the swap
4453
5049
  * @param params.maxActiveBinSlippage - The maximum active bin slippage
@@ -4468,7 +5064,6 @@ var Zap = class {
4468
5064
  strategy,
4469
5065
  favorXInActiveId,
4470
5066
  indirectSwapEstimate,
4471
- maxAccounts,
4472
5067
  swapSlippageBps,
4473
5068
  maxTransferAmountExtendPercentage,
4474
5069
  maxActiveBinSlippage,
@@ -4506,41 +5101,55 @@ var Zap = class {
4506
5101
  }
4507
5102
  let maxTransferAmountX;
4508
5103
  let maxTransferAmountY;
4509
- const swapTransactions = [];
5104
+ const swapInstructions = [];
5105
+ const swapLookupTables = [];
4510
5106
  const swapAmountToXInLamports = indirectSwapEstimate.swapAmountToX;
4511
5107
  const swapAmountToYInLamports = indirectSwapEstimate.swapAmountToY;
4512
5108
  if (!indirectSwapEstimate.swapAmountToX.isZero() && indirectSwapEstimate.swapToX !== null) {
4513
- const { transaction: swapToXTransaction } = yield buildJupiterSwapTransaction(
5109
+ const swapToX = yield buildJupiterSwapTransaction(
5110
+ this.connection,
4514
5111
  user,
4515
5112
  inputTokenMint,
4516
5113
  tokenXMint,
4517
5114
  swapAmountToXInLamports,
4518
- maxAccounts,
4519
5115
  swapSlippageBps,
4520
5116
  indirectSwapEstimate.swapToX,
4521
5117
  {
4522
5118
  jupiterApiUrl: this.jupiterApiUrl,
4523
- jupiterApiKey: this.jupiterApiKey
5119
+ jupiterApiKey: this.jupiterApiKey,
5120
+ jupiterApiVersion: this.jupiterApiVersion
4524
5121
  }
4525
5122
  );
4526
- swapTransactions.push(swapToXTransaction);
5123
+ preInstructions.push(
5124
+ ...getJupiterCreateAtaInstructions(swapToX.setupInstructions)
5125
+ );
5126
+ swapInstructions.push(swapToX.instruction);
5127
+ swapLookupTables.push(swapToX.addressLookupTableAccounts);
4527
5128
  }
4528
5129
  if (!indirectSwapEstimate.swapAmountToY.isZero() && indirectSwapEstimate.swapToY !== null) {
4529
- const { transaction: swapToYTransaction } = yield buildJupiterSwapTransaction(
5130
+ const swapToY = yield buildJupiterSwapTransaction(
5131
+ this.connection,
4530
5132
  user,
4531
5133
  inputTokenMint,
4532
5134
  tokenYMint,
4533
5135
  swapAmountToYInLamports,
4534
- maxAccounts,
4535
5136
  swapSlippageBps,
4536
5137
  indirectSwapEstimate.swapToY,
4537
5138
  {
4538
5139
  jupiterApiUrl: this.jupiterApiUrl,
4539
- jupiterApiKey: this.jupiterApiKey
5140
+ jupiterApiKey: this.jupiterApiKey,
5141
+ jupiterApiVersion: this.jupiterApiVersion
4540
5142
  }
4541
5143
  );
4542
- swapTransactions.push(swapToYTransaction);
5144
+ preInstructions.push(
5145
+ ...getJupiterCreateAtaInstructions(swapToY.setupInstructions)
5146
+ );
5147
+ swapInstructions.push(swapToY.instruction);
5148
+ swapLookupTables.push(swapToY.addressLookupTableAccounts);
4543
5149
  }
5150
+ const addressLookupTableAccounts = mergeAddressLookupTableAccounts(
5151
+ ...swapLookupTables
5152
+ );
4544
5153
  maxTransferAmountX = getExtendMaxAmountTransfer(
4545
5154
  indirectSwapEstimate.postSwapX.toString(),
4546
5155
  maxTransferAmountExtendPercentage
@@ -4551,8 +5160,8 @@ var Zap = class {
4551
5160
  );
4552
5161
  const binArrays = (0, import_dlmm4.getBinArraysRequiredByPositionRange)(
4553
5162
  lbPair,
4554
- new import_anchor.BN(activeId + minDeltaId),
4555
- new import_anchor.BN(activeId + maxDeltaId),
5163
+ new import_core.BN(activeId + minDeltaId),
5164
+ new import_core.BN(activeId + maxDeltaId),
4556
5165
  DLMM_PROGRAM_ID
4557
5166
  ).map((item) => ({
4558
5167
  pubkey: item.key,
@@ -4582,7 +5191,8 @@ var Zap = class {
4582
5191
  maxTransferAmountX,
4583
5192
  maxTransferAmountY,
4584
5193
  preInstructions,
4585
- swapTransactions,
5194
+ swapInstructions,
5195
+ addressLookupTableAccounts,
4586
5196
  cleanUpInstructions,
4587
5197
  binArrays,
4588
5198
  binArrayBitmapExtension: binArrayBitmapExtensionData ? binArrayBitmapExtension : null,
@@ -4592,7 +5202,12 @@ var Zap = class {
4592
5202
  });
4593
5203
  }
4594
5204
  /**
4595
- * Builds a DLMM zap-in transaction
5205
+ * Build the DLMM zap-in instructions
5206
+ *
5207
+ * Instructions are ordered: setup (token accounts, wrap SOL), swaps, ledger, zap in, clean up.
5208
+ * They must run in that order but can be split across several transactions, because state
5209
+ * between steps lives in accounts. Splitting loses atomicity, so send split transactions as a bundle.
5210
+ * No compute budget instructions are included; the client sets the compute budget for each transaction.
4596
5211
  *
4597
5212
  * @param params.user - The user's public key
4598
5213
  * @param params.lbPair - The DLMM pool's public key
@@ -4607,14 +5222,15 @@ var Zap = class {
4607
5222
  * @param params.maxActiveBinSlippage - The maximum active bin slippage
4608
5223
  * @param params.favorXInActiveId - Whether to favor token X in the active bin
4609
5224
  * @param params.strategy - The liquidity distribution strategy
4610
- * @param params.preInstructions - Instructions to run before the zap in
4611
- * @param params.swapTransactions - Swap transactions to execute
5225
+ * @param params.preInstructions - Instructions to run before the zap in, including the token accounts the Jupiter swaps need. Token account creation repeated here is removed
5226
+ * @param params.swapInstructions - Swap instructions to execute
5227
+ * @param params.addressLookupTableAccounts - Lookup tables used by the swap instructions
4612
5228
  * @param params.cleanUpInstructions - Instructions to run after the zap in
4613
5229
  * @param params.binArrays - The bin arrays required for the position
4614
5230
  * @param params.binArrayBitmapExtension - The bin array bitmap extension account if it exists
4615
5231
  * @param params.isDirectRoute - Whether this is a direct route
4616
5232
  * @param params.singleSided - Optional single-sided deposit mode (X or Y only) - default is non-single-sided
4617
- * @returns Response containing transaction components
5233
+ * @returns The ordered instructions and the lookup tables they use
4618
5234
  */
4619
5235
  buildZapInDlmmTransaction(params) {
4620
5236
  return __async(this, null, function* () {
@@ -4633,90 +5249,70 @@ var Zap = class {
4633
5249
  favorXInActiveId,
4634
5250
  strategy,
4635
5251
  preInstructions,
4636
- swapTransactions,
5252
+ swapInstructions,
5253
+ addressLookupTableAccounts,
4637
5254
  cleanUpInstructions,
4638
5255
  binArrays,
4639
5256
  binArrayBitmapExtension,
4640
5257
  isDirectRoute,
4641
5258
  singleSided
4642
5259
  } = params;
4643
- const [
4644
- { ataPubkey: tokenXAccount, ix: initializeTokenXIx },
4645
- { ataPubkey: tokenYAccount, ix: initializeTokenYIx }
4646
- ] = yield Promise.all([
4647
- getOrCreateATAInstruction(
4648
- this.connection,
4649
- tokenXMint,
4650
- user,
4651
- user,
4652
- false,
4653
- tokenXProgram
4654
- ),
4655
- getOrCreateATAInstruction(
4656
- this.connection,
4657
- tokenYMint,
4658
- user,
4659
- user,
4660
- false,
4661
- tokenYProgram
4662
- )
5260
+ const {
5261
+ tokenAccounts: [tokenXAccount, tokenYAccount],
5262
+ instructions: setupInstructions
5263
+ } = getCreateAtaInstructions(user, [
5264
+ { mint: tokenXMint, tokenProgram: tokenXProgram },
5265
+ { mint: tokenYMint, tokenProgram: tokenYProgram }
4663
5266
  ]);
4664
- const setupTransaction = new import_web38.Transaction();
4665
- initializeTokenXIx && setupTransaction.add(initializeTokenXIx);
4666
- initializeTokenYIx && setupTransaction.add(initializeTokenYIx);
4667
- if (preInstructions.length > 0) {
4668
- setupTransaction.add(...preInstructions);
4669
- }
4670
- const ledgerTransaction = new import_web38.Transaction();
4671
- const resetOrInitializeLedgerTx = yield this.resetOrInitializeLedgerAccount(user);
4672
- ledgerTransaction.add(resetOrInitializeLedgerTx);
5267
+ setupInstructions.push(...preInstructions);
5268
+ const ledgerInstructions = yield this.resetOrInitializeLedgerAccount(user);
4673
5269
  if (isDirectRoute) {
4674
5270
  const { isTokenX, amount, maxTransferAmount } = params;
4675
5271
  if (singleSided !== void 0) {
4676
5272
  const singleSidedX = singleSided === 0 /* X */;
4677
- if (swapTransactions.length > 0) {
5273
+ if (swapInstructions.length > 0) {
4678
5274
  const swappedTokenAccount = singleSidedX ? tokenXAccount : tokenYAccount;
4679
5275
  const preSwappedTokenBalance = yield getTokenAccountBalance(
4680
5276
  this.connection,
4681
5277
  swappedTokenAccount
4682
5278
  );
4683
- const updateLedgerBalanceAfterSwapTx = yield this.updateLedgerBalanceAfterSwap(
5279
+ const updateLedgerBalanceAfterSwapIx = yield this.updateLedgerBalanceAfterSwap(
4684
5280
  user,
4685
5281
  swappedTokenAccount,
4686
- new import_anchor.BN(preSwappedTokenBalance),
5282
+ new import_core.BN(preSwappedTokenBalance),
4687
5283
  maxTransferAmount,
4688
5284
  singleSidedX
4689
5285
  );
4690
- ledgerTransaction.add(updateLedgerBalanceAfterSwapTx);
5286
+ ledgerInstructions.push(updateLedgerBalanceAfterSwapIx);
4691
5287
  } else {
4692
- const setLedgerBalanceTx = yield this.setLedgerBalance(
5288
+ const setLedgerBalanceIx = yield this.setLedgerBalance(
4693
5289
  user,
4694
5290
  amount,
4695
5291
  singleSidedX
4696
5292
  );
4697
- ledgerTransaction.add(setLedgerBalanceTx);
5293
+ ledgerInstructions.push(setLedgerBalanceIx);
4698
5294
  }
4699
5295
  } else {
4700
- const setLedgerBalanceTx = yield this.setLedgerBalance(
5296
+ const setLedgerBalanceIx = yield this.setLedgerBalance(
4701
5297
  user,
4702
5298
  amount,
4703
5299
  isTokenX
4704
5300
  );
4705
- ledgerTransaction.add(setLedgerBalanceTx);
4706
- if (swapTransactions.length > 0) {
5301
+ ledgerInstructions.push(setLedgerBalanceIx);
5302
+ if (swapInstructions.length > 0) {
4707
5303
  const swappedTokenAccount = isTokenX ? tokenYAccount : tokenXAccount;
4708
5304
  const preSwappedTokenBalance = yield getTokenAccountBalance(
4709
5305
  this.connection,
4710
5306
  swappedTokenAccount
4711
5307
  );
4712
- const updateLedgerBalanceAfterSwapTx = yield this.updateLedgerBalanceAfterSwap(
5308
+ const updateLedgerBalanceAfterSwapIx = yield this.updateLedgerBalanceAfterSwap(
4713
5309
  user,
4714
5310
  swappedTokenAccount,
4715
- new import_anchor.BN(preSwappedTokenBalance),
5311
+ new import_core.BN(preSwappedTokenBalance),
4716
5312
  maxTransferAmount,
4717
5313
  !isTokenX
4718
5314
  );
4719
- ledgerTransaction.add(updateLedgerBalanceAfterSwapTx);
5315
+ ledgerInstructions.push(updateLedgerBalanceAfterSwapIx);
4720
5316
  }
4721
5317
  }
4722
5318
  } else {
@@ -4728,14 +5324,14 @@ var Zap = class {
4728
5324
  this.connection,
4729
5325
  swappedTokenAccount
4730
5326
  );
4731
- const updateLedgerBalanceAfterSwapTx = yield this.updateLedgerBalanceAfterSwap(
5327
+ const updateLedgerBalanceAfterSwapIx = yield this.updateLedgerBalanceAfterSwap(
4732
5328
  user,
4733
5329
  swappedTokenAccount,
4734
- new import_anchor.BN(preSwappedTokenBalance),
5330
+ new import_core.BN(preSwappedTokenBalance),
4735
5331
  singleSidedX ? maxTransferAmountX : maxTransferAmountY,
4736
5332
  singleSidedX
4737
5333
  );
4738
- ledgerTransaction.add(updateLedgerBalanceAfterSwapTx);
5334
+ ledgerInstructions.push(updateLedgerBalanceAfterSwapIx);
4739
5335
  } else {
4740
5336
  const preTokenXBalance = yield getTokenAccountBalance(
4741
5337
  this.connection,
@@ -4745,22 +5341,22 @@ var Zap = class {
4745
5341
  this.connection,
4746
5342
  tokenYAccount
4747
5343
  );
4748
- const updateLedgerBalanceTokenXAfterSwapTx = yield this.updateLedgerBalanceAfterSwap(
5344
+ const updateLedgerBalanceTokenXAfterSwapIx = yield this.updateLedgerBalanceAfterSwap(
4749
5345
  user,
4750
5346
  tokenXAccount,
4751
- new import_anchor.BN(preTokenXBalance),
5347
+ new import_core.BN(preTokenXBalance),
4752
5348
  maxTransferAmountX,
4753
5349
  true
4754
5350
  );
4755
- const updateLedgerBalanceTokenYAfterSwapTx = yield this.updateLedgerBalanceAfterSwap(
5351
+ const updateLedgerBalanceTokenYAfterSwapIx = yield this.updateLedgerBalanceAfterSwap(
4756
5352
  user,
4757
5353
  tokenYAccount,
4758
- new import_anchor.BN(preTokenYBalance),
5354
+ new import_core.BN(preTokenYBalance),
4759
5355
  maxTransferAmountY,
4760
5356
  false
4761
5357
  );
4762
- ledgerTransaction.add(updateLedgerBalanceTokenXAfterSwapTx);
4763
- ledgerTransaction.add(updateLedgerBalanceTokenYAfterSwapTx);
5358
+ ledgerInstructions.push(updateLedgerBalanceTokenXAfterSwapIx);
5359
+ ledgerInstructions.push(updateLedgerBalanceTokenYAfterSwapIx);
4764
5360
  }
4765
5361
  }
4766
5362
  const dlmm = yield import_dlmm4.default.create(this.connection, lbPair);
@@ -4774,7 +5370,7 @@ var Zap = class {
4774
5370
  tokenYProgram,
4775
5371
  dlmm.lbPair
4776
5372
  );
4777
- const zapInTransaction = yield this.zapInDlmmForUninitializedPosition({
5373
+ const zapInInstruction = yield this.zapInDlmmForUninitializedPosition({
4778
5374
  user,
4779
5375
  lbPair,
4780
5376
  position,
@@ -4788,18 +5384,17 @@ var Zap = class {
4788
5384
  strategy,
4789
5385
  remainingAccountInfo
4790
5386
  });
4791
- const cleanUpTransaction = new import_web38.Transaction();
4792
- const closeLedgerTransaction = yield this.closeLedgerAccount(user, user);
4793
- cleanUpTransaction.add(closeLedgerTransaction);
4794
- if (cleanUpInstructions.length > 0) {
4795
- cleanUpTransaction.add(...cleanUpInstructions);
4796
- }
5387
+ const closeLedgerInstruction = yield this.closeLedgerAccount(user, user);
4797
5388
  return {
4798
- setupTransaction: setupTransaction.instructions.length > 0 ? setupTransaction : void 0,
4799
- swapTransactions,
4800
- ledgerTransaction,
4801
- zapInTransaction,
4802
- cleanUpTransaction
5389
+ instructions: [
5390
+ ...dedupeCreateAtaInstructions(setupInstructions),
5391
+ ...swapInstructions,
5392
+ ...ledgerInstructions,
5393
+ zapInInstruction,
5394
+ closeLedgerInstruction,
5395
+ ...cleanUpInstructions
5396
+ ],
5397
+ addressLookupTableAccounts
4803
5398
  };
4804
5399
  });
4805
5400
  }
@@ -4807,6 +5402,13 @@ var Zap = class {
4807
5402
  * High level method for DLMM position rebalancing
4808
5403
  * Consist of remove liquidity, zap out through Jupiter or DLMM, and zap back in to rebalance the position
4809
5404
  *
5405
+ * Instructions are ordered: setup (token accounts), init bin arrays, rebalance position, swap, ledger,
5406
+ * zap in, clean up. They must run in that order but can be split across several transactions.
5407
+ * Splitting loses atomicity, so send split transactions as a bundle.
5408
+ * No compute budget instructions are included. The zap-in instruction was measured at about 462k
5409
+ * compute units. Without a compute unit limit, a transaction gets 200k units per instruction (up to
5410
+ * 1.4M), which may not cover it, so set a compute unit limit on the transaction that holds it.
5411
+ *
4810
5412
  * @param params - Rebalancing DLMM position parameters
4811
5413
  * @param params.lbPairAddress - The DLMM pool address
4812
5414
  * @param params.positionAddress - The position address
@@ -4818,8 +5420,7 @@ var Zap = class {
4818
5420
  * @param params.strategy - The strategy to use for the rebalance
4819
5421
  * @param params.favorXInActiveId - Whether to favor token X in the active bin
4820
5422
  * @param params.directSwapEstimate - The estimate of the direct swap operation
4821
- * @param params.maxAccounts - The maximum number of accounts to use for the swap operation
4822
- * @returns Response containing transactions and estimation details
5423
+ * @returns The ordered instructions, the lookup tables they use, and the estimated balances
4823
5424
  * @throws if fail to get jupiter quote or jupiter swap instruction
4824
5425
  */
4825
5426
  rebalanceDlmmPosition(params) {
@@ -4834,45 +5435,27 @@ var Zap = class {
4834
5435
  swapSlippageBps,
4835
5436
  strategy,
4836
5437
  favorXInActiveId,
4837
- directSwapEstimate,
4838
- maxAccounts = 50
5438
+ directSwapEstimate
4839
5439
  } = params;
4840
5440
  const dlmm = yield import_dlmm4.default.create(this.connection, lbPair);
4841
5441
  const userPosition = yield dlmm.getPosition(position);
4842
5442
  const { tokenXProgram, tokenYProgram } = (0, import_dlmm4.getTokenProgramId)(dlmm.lbPair);
4843
- const [
4844
- { ataPubkey: userTokenX, ix: userTokenXIx },
4845
- { ataPubkey: userTokenY, ix: userTokenYIx }
4846
- ] = yield Promise.all([
4847
- getOrCreateATAInstruction(
4848
- this.connection,
4849
- dlmm.lbPair.tokenXMint,
4850
- user,
4851
- user,
4852
- true,
4853
- tokenXProgram
4854
- ),
4855
- getOrCreateATAInstruction(
4856
- this.connection,
4857
- dlmm.lbPair.tokenYMint,
4858
- user,
4859
- user,
4860
- true,
4861
- tokenYProgram
4862
- )
5443
+ const {
5444
+ tokenAccounts: [userTokenX, userTokenY],
5445
+ instructions: setupInstructions
5446
+ } = getCreateAtaInstructions(user, [
5447
+ { mint: dlmm.lbPair.tokenXMint, tokenProgram: tokenXProgram },
5448
+ { mint: dlmm.lbPair.tokenYMint, tokenProgram: tokenYProgram }
4863
5449
  ]);
4864
- const setupTransaction = new import_web38.Transaction();
4865
- userTokenXIx && setupTransaction.add(userTokenXIx);
4866
- userTokenYIx && setupTransaction.add(userTokenYIx);
4867
5450
  const strategyParamBuilder = (0, import_dlmm4.getLiquidityStrategyParameterBuilder)(strategy);
4868
5451
  const { x0, y0, deltaX, deltaY } = (0, import_dlmm4.buildLiquidityStrategyParameters)(
4869
- new import_anchor.BN(0),
4870
- new import_anchor.BN(0),
4871
- new import_anchor.BN(minDeltaId),
4872
- new import_anchor.BN(maxDeltaId),
4873
- new import_anchor.BN(dlmm.lbPair.binStep),
5452
+ new import_core.BN(0),
5453
+ new import_core.BN(0),
5454
+ new import_core.BN(minDeltaId),
5455
+ new import_core.BN(maxDeltaId),
5456
+ new import_core.BN(dlmm.lbPair.binStep),
4874
5457
  favorXInActiveId,
4875
- new import_anchor.BN(dlmm.lbPair.activeId),
5458
+ new import_core.BN(dlmm.lbPair.activeId),
4876
5459
  strategyParamBuilder
4877
5460
  );
4878
5461
  const { rebalancePosition, simulationResult } = yield dlmm.simulateRebalancePosition(
@@ -4888,16 +5471,16 @@ var Zap = class {
4888
5471
  y0,
4889
5472
  deltaX,
4890
5473
  deltaY,
4891
- minDeltaId: new import_anchor.BN(minDeltaId),
4892
- maxDeltaId: new import_anchor.BN(maxDeltaId),
5474
+ minDeltaId: new import_core.BN(minDeltaId),
5475
+ maxDeltaId: new import_core.BN(maxDeltaId),
4893
5476
  favorXInActiveBin: favorXInActiveId
4894
5477
  }
4895
5478
  ],
4896
5479
  [
4897
5480
  {
4898
- minBinId: new import_anchor.BN(userPosition.positionData.lowerBinId),
4899
- maxBinId: new import_anchor.BN(userPosition.positionData.upperBinId),
4900
- bps: new import_anchor.BN(import_dlmm4.BASIS_POINT_MAX)
5481
+ minBinId: new import_core.BN(userPosition.positionData.lowerBinId),
5482
+ maxBinId: new import_core.BN(userPosition.positionData.upperBinId),
5483
+ bps: new import_core.BN(import_dlmm4.BASIS_POINT_MAX)
4901
5484
  // remove all liquidity
4902
5485
  }
4903
5486
  ]
@@ -4912,34 +5495,42 @@ var Zap = class {
4912
5495
  rebalancePositionInstruction: _rebalancePositionInstruction
4913
5496
  } = yield dlmm.rebalancePosition(
4914
5497
  { simulationResult, rebalancePosition },
4915
- new import_anchor.BN(maxActiveBinSlippage)
5498
+ new import_core.BN(maxActiveBinSlippage)
5499
+ );
5500
+ let rebalancePositionInstruction = filterOutComputeBudgetInstructions(
5501
+ _rebalancePositionInstruction
4916
5502
  );
4917
- let rebalancePositionInstruction = _rebalancePositionInstruction;
4918
5503
  if (dlmm.lbPair.tokenXMint.equals(import_spl_token6.NATIVE_MINT) || dlmm.lbPair.tokenYMint.equals(import_spl_token6.NATIVE_MINT)) {
4919
5504
  rebalancePositionInstruction = filterOutCloseSplTokenAccountInstructions(
4920
- _rebalancePositionInstruction
5505
+ rebalancePositionInstruction
4921
5506
  );
4922
5507
  }
4923
- const tokenXAmount = new import_anchor.BN(userPosition.positionData.totalXAmount);
4924
- const tokenYAmount = new import_anchor.BN(userPosition.positionData.totalYAmount);
4925
- let swapTransaction;
5508
+ const tokenXAmount = new import_core.BN(userPosition.positionData.totalXAmount);
5509
+ const tokenYAmount = new import_core.BN(userPosition.positionData.totalYAmount);
5510
+ let swapInstructions = [];
5511
+ let addressLookupTableAccounts = [];
4926
5512
  if (directSwapEstimate.swapType !== 2 /* NoSwap */ && directSwapEstimate.quote) {
4927
5513
  const swapQuote = directSwapEstimate.quote;
4928
5514
  if (swapQuote.route === 0 /* Jupiter */) {
4929
- const { transaction: swapTx } = yield buildJupiterSwapTransaction(
5515
+ const swap = yield buildJupiterSwapTransaction(
5516
+ this.connection,
4930
5517
  user,
4931
5518
  directSwapEstimate.swapType === 0 /* XToY */ ? dlmm.lbPair.tokenXMint : dlmm.lbPair.tokenYMint,
4932
5519
  directSwapEstimate.swapType === 0 /* XToY */ ? dlmm.lbPair.tokenYMint : dlmm.lbPair.tokenXMint,
4933
5520
  directSwapEstimate.swapAmount,
4934
- maxAccounts,
4935
5521
  swapSlippageBps,
4936
5522
  swapQuote.originalQuote,
4937
5523
  {
4938
5524
  jupiterApiUrl: this.jupiterApiUrl,
4939
- jupiterApiKey: this.jupiterApiKey
5525
+ jupiterApiKey: this.jupiterApiKey,
5526
+ jupiterApiVersion: this.jupiterApiVersion
4940
5527
  }
4941
5528
  );
4942
- swapTransaction = swapTx;
5529
+ setupInstructions.push(
5530
+ ...getJupiterCreateAtaInstructions(swap.setupInstructions)
5531
+ );
5532
+ swapInstructions = [swap.instruction];
5533
+ addressLookupTableAccounts = swap.addressLookupTableAccounts;
4943
5534
  } else {
4944
5535
  const swapForY = directSwapEstimate.swapType === 0 /* XToY */;
4945
5536
  const binArrays2 = yield dlmm.getBinArrayForSwap(swapForY);
@@ -4952,12 +5543,15 @@ var Zap = class {
4952
5543
  user,
4953
5544
  binArraysPubkey: binArrays2.map((item) => item.publicKey)
4954
5545
  });
5546
+ swapTx.instructions = filterOutComputeBudgetInstructions(
5547
+ swapTx.instructions
5548
+ );
4955
5549
  if (dlmm.lbPair.tokenXMint.equals(import_spl_token6.NATIVE_MINT) || dlmm.lbPair.tokenYMint.equals(import_spl_token6.NATIVE_MINT)) {
4956
5550
  swapTx.instructions = filterOutCloseSplTokenAccountInstructions(
4957
5551
  swapTx.instructions
4958
5552
  );
4959
5553
  }
4960
- swapTransaction = swapTx;
5554
+ swapInstructions = swapTx.instructions;
4961
5555
  }
4962
5556
  }
4963
5557
  const preTokenXBalance = yield getTokenAccountBalance(
@@ -4972,31 +5566,31 @@ var Zap = class {
4972
5566
  const tokenYAmountAfterSwap = directSwapEstimate.swapType === 0 /* XToY */ ? tokenYAmount.add(directSwapEstimate.expectedOutput) : directSwapEstimate.swapType === 1 /* YToX */ ? tokenYAmount.sub(directSwapEstimate.swapAmount) : tokenYAmount;
4973
5567
  const ledgerAddress = deriveLedgerAccount(user);
4974
5568
  const ledgerAccountInfo = yield this.connection.getAccountInfo(ledgerAddress);
4975
- const ledgerTransaction = new import_web38.Transaction();
5569
+ const ledgerInstructions = [];
4976
5570
  if (!ledgerAccountInfo) {
4977
- const initLedgerTx = yield this.initializeLedgerAccount(user, user);
4978
- ledgerTransaction.add(...initLedgerTx.instructions);
5571
+ const initLedgerIx = yield this.initializeLedgerAccount(user, user);
5572
+ ledgerInstructions.push(initLedgerIx);
4979
5573
  }
4980
- const updateLedgerXTx = yield this.updateLedgerBalanceAfterSwap(
5574
+ const updateLedgerXIx = yield this.updateLedgerBalanceAfterSwap(
4981
5575
  user,
4982
5576
  userTokenX,
4983
- new import_anchor.BN(preTokenXBalance),
5577
+ new import_core.BN(preTokenXBalance),
4984
5578
  tokenXAmountAfterSwap,
4985
5579
  true
4986
5580
  );
4987
- ledgerTransaction.add(...updateLedgerXTx.instructions);
4988
- const updateLedgerYTx = yield this.updateLedgerBalanceAfterSwap(
5581
+ ledgerInstructions.push(updateLedgerXIx);
5582
+ const updateLedgerYIx = yield this.updateLedgerBalanceAfterSwap(
4989
5583
  user,
4990
5584
  userTokenY,
4991
- new import_anchor.BN(preTokenYBalance),
5585
+ new import_core.BN(preTokenYBalance),
4992
5586
  tokenYAmountAfterSwap,
4993
5587
  false
4994
5588
  );
4995
- ledgerTransaction.add(...updateLedgerYTx.instructions);
5589
+ ledgerInstructions.push(updateLedgerYIx);
4996
5590
  const binArrays = (0, import_dlmm4.getBinArraysRequiredByPositionRange)(
4997
5591
  lbPair,
4998
- new import_anchor.BN(dlmm.lbPair.activeId + minDeltaId),
4999
- new import_anchor.BN(dlmm.lbPair.activeId + maxDeltaId),
5592
+ new import_core.BN(dlmm.lbPair.activeId + minDeltaId),
5593
+ new import_core.BN(dlmm.lbPair.activeId + maxDeltaId),
5000
5594
  DLMM_PROGRAM_ID
5001
5595
  ).map((item) => ({
5002
5596
  pubkey: item.key,
@@ -5013,7 +5607,7 @@ var Zap = class {
5013
5607
  tokenYProgram,
5014
5608
  dlmm.lbPair
5015
5609
  );
5016
- const zapInTransaction = yield this.zapInDlmmForInitializedPosition({
5610
+ const zapInInstruction = yield this.zapInDlmmForInitializedPosition({
5017
5611
  user,
5018
5612
  lbPair,
5019
5613
  position,
@@ -5026,28 +5620,26 @@ var Zap = class {
5026
5620
  strategy,
5027
5621
  remainingAccountInfo
5028
5622
  });
5029
- zapInTransaction.instructions.unshift(
5030
- // based on 1 tx that consumed 462_610. Add 20% for safety and round up to nearest 100,000
5031
- // 462_610 * 1.2 = 555_132 => 600_000
5032
- import_web38.ComputeBudgetProgram.setComputeUnitLimit({ units: 6e5 })
5033
- );
5034
- const cleanUpTransaction = new import_web38.Transaction();
5035
- const closeLedgerTx = yield this.closeLedgerAccount(user, user);
5036
- cleanUpTransaction.add(closeLedgerTx);
5623
+ const cleanUpInstructions = [
5624
+ yield this.closeLedgerAccount(user, user)
5625
+ ];
5037
5626
  if (dlmm.lbPair.tokenXMint.equals(import_spl_token6.NATIVE_MINT) || dlmm.lbPair.tokenYMint.equals(import_spl_token6.NATIVE_MINT)) {
5038
5627
  const unwrapIx = unwrapSOLInstruction(user, user);
5039
5628
  if (unwrapIx) {
5040
- cleanUpTransaction.add(unwrapIx);
5629
+ cleanUpInstructions.push(unwrapIx);
5041
5630
  }
5042
5631
  }
5043
5632
  return {
5044
- setupTransaction,
5045
- initBinArrayTransaction: initBinArrayInstructions.length > 0 ? new import_web38.Transaction().add(...initBinArrayInstructions) : void 0,
5046
- rebalancePositionTransaction: rebalancePositionInstruction.length > 0 ? new import_web38.Transaction().add(...rebalancePositionInstruction) : void 0,
5047
- swapTransaction,
5048
- ledgerTransaction,
5049
- zapInTransaction,
5050
- cleanUpTransaction,
5633
+ instructions: [
5634
+ ...dedupeCreateAtaInstructions(setupInstructions),
5635
+ ...filterOutComputeBudgetInstructions(initBinArrayInstructions),
5636
+ ...rebalancePositionInstruction,
5637
+ ...swapInstructions,
5638
+ ...ledgerInstructions,
5639
+ zapInInstruction,
5640
+ ...cleanUpInstructions
5641
+ ],
5642
+ addressLookupTableAccounts,
5051
5643
  estimation: {
5052
5644
  currentBalances: {
5053
5645
  tokenX: tokenXAmount,
@@ -5073,7 +5665,7 @@ var Zap = class {
5073
5665
  * @param params.ammProgram - AMM program ID to interact with
5074
5666
  * @param params.preInstructions - Instructions to run before the zap out
5075
5667
  * @param params.postInstructions - Instructions to run after the zap out
5076
- * @returns builder transaction
5668
+ * @returns The pre-instructions, the zap out instruction and the post-instructions in order, with no lookup tables
5077
5669
  */
5078
5670
  zapOut(params) {
5079
5671
  return __async(this, null, function* () {
@@ -5085,10 +5677,18 @@ var Zap = class {
5085
5677
  preInstructions,
5086
5678
  postInstructions
5087
5679
  } = params;
5088
- return this.zapProgram.methods.zapOut(zapOutParams).accountsPartial({
5680
+ const zapOutInstruction = yield this.zapProgram.methods.zapOut(zapOutParams).accountsPartial({
5089
5681
  userTokenInAccount,
5090
5682
  ammProgram
5091
- }).remainingAccounts(remainingAccounts).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
5683
+ }).remainingAccounts(remainingAccounts).instruction();
5684
+ return {
5685
+ instructions: [
5686
+ ...preInstructions,
5687
+ zapOutInstruction,
5688
+ ...postInstructions
5689
+ ],
5690
+ addressLookupTableAccounts: []
5691
+ };
5092
5692
  });
5093
5693
  }
5094
5694
  /**
@@ -5099,15 +5699,16 @@ var Zap = class {
5099
5699
  * @param params.user - Public key of the user performing the swap
5100
5700
  * @param params.inputMint - Token mint being swapped from
5101
5701
  * @param params.outputMint - Token mint being swapped to
5102
- * @param params.jupiterSwapResponse - Jupiter swap instruction response
5702
+ * @param params.jupiterSwapResponse - Jupiter swap instruction response. Its lookup table fields, when present, are returned as `addressLookupTableAccounts` (v1 tables are fetched with one RPC call)
5103
5703
  * @param params.inputTokenProgram - Token program for the input token (defaults to SPL Token)
5104
5704
  * @param params.outputTokenProgram - Token program for the output token (defaults to SPL Token)
5105
5705
  * @param params.maxSwapAmount - Maximum amount of input token to swap
5106
5706
  * @param params.percentageToZapOut - Percentage of input token to zap out
5107
- * @returns built transaction
5707
+ * @returns The ordered instructions and the lookup tables of the Jupiter swap
5108
5708
  */
5109
5709
  zapOutThroughJupiter(params) {
5110
5710
  return __async(this, null, function* () {
5711
+ var _a;
5111
5712
  const {
5112
5713
  user,
5113
5714
  inputMint,
@@ -5120,29 +5721,21 @@ var Zap = class {
5120
5721
  } = params;
5121
5722
  const preInstructions = [];
5122
5723
  const postInstructions = [];
5123
- const [
5124
- { ataPubkey: inputTokenAccount, ix: inputTokenAccountIx },
5125
- { ataPubkey: outputTokenAccount, ix: outputTokenAccountIx }
5126
- ] = yield Promise.all([
5127
- getOrCreateATAInstruction(
5128
- this.connection,
5129
- inputMint,
5130
- user,
5131
- user,
5132
- true,
5133
- inputTokenProgram
5134
- ),
5135
- getOrCreateATAInstruction(
5136
- this.connection,
5137
- outputMint,
5138
- user,
5139
- user,
5140
- true,
5141
- outputTokenProgram
5142
- )
5724
+ const {
5725
+ tokenAccounts: [inputTokenAccount],
5726
+ instructions: createAtaInstructions
5727
+ } = getCreateAtaInstructions(user, [
5728
+ { mint: inputMint, tokenProgram: inputTokenProgram },
5729
+ { mint: outputMint, tokenProgram: outputTokenProgram }
5143
5730
  ]);
5144
- inputTokenAccountIx && preInstructions.push(inputTokenAccountIx);
5145
- outputTokenAccountIx && preInstructions.push(outputTokenAccountIx);
5731
+ preInstructions.push(...createAtaInstructions);
5732
+ preInstructions.push(
5733
+ ...getJupiterCreateAtaInstructions(
5734
+ ((_a = jupiterSwapResponse.setupInstructions) != null ? _a : []).map(
5735
+ toTransactionInstruction
5736
+ )
5737
+ )
5738
+ );
5146
5739
  let preUserTokenBalance;
5147
5740
  try {
5148
5741
  preUserTokenBalance = yield getTokenAccountBalance(
@@ -5166,27 +5759,36 @@ var Zap = class {
5166
5759
  jupiterSwapResponse.swapInstruction.data,
5167
5760
  "base64"
5168
5761
  );
5169
- const offsetAmountIn = payloadData.length - AMOUNT_IN_JUP_V6_REVERSE_OFFSET;
5762
+ const offsetAmountIn = getJupiterInstructionLayout(
5763
+ payloadData
5764
+ ).amountInOffset(payloadData.length);
5170
5765
  if (inputMint.equals(import_spl_token6.NATIVE_MINT) || outputMint.equals(import_spl_token6.NATIVE_MINT)) {
5171
5766
  const unwrapInstructions = unwrapSOLInstruction(user, user);
5172
5767
  if (unwrapInstructions) {
5173
5768
  postInstructions.push(unwrapInstructions);
5174
5769
  }
5175
5770
  }
5176
- return yield this.zapOut({
5177
- userTokenInAccount: inputTokenAccount,
5178
- zapOutParams: {
5179
- percentage: percentageToZapOut,
5180
- offsetAmountIn,
5181
- preUserTokenBalance: new import_anchor.BN(preUserTokenBalance),
5182
- maxSwapAmount,
5183
- payloadData
5184
- },
5185
- remainingAccounts,
5186
- ammProgram: JUP_V6_PROGRAM_ID,
5187
- preInstructions,
5188
- postInstructions
5189
- });
5771
+ const [{ instructions }, addressLookupTableAccounts] = yield Promise.all([
5772
+ this.zapOut({
5773
+ userTokenInAccount: inputTokenAccount,
5774
+ zapOutParams: {
5775
+ percentage: percentageToZapOut,
5776
+ offsetAmountIn,
5777
+ preUserTokenBalance: new import_core.BN(preUserTokenBalance),
5778
+ maxSwapAmount,
5779
+ payloadData
5780
+ },
5781
+ remainingAccounts,
5782
+ ammProgram: JUP_V6_PROGRAM_ID,
5783
+ preInstructions: dedupeCreateAtaInstructions(preInstructions),
5784
+ postInstructions
5785
+ }),
5786
+ getJupiterAddressLookupTableAccounts(
5787
+ this.connection,
5788
+ jupiterSwapResponse
5789
+ )
5790
+ ]);
5791
+ return { instructions, addressLookupTableAccounts };
5190
5792
  });
5191
5793
  }
5192
5794
  /**
@@ -5202,6 +5804,7 @@ var Zap = class {
5202
5804
  * @param params.minimumSwapAmountOut - Minimum amount of output token to receive
5203
5805
  * @param params.maxSwapAmount - Maximum amount of input token to swap
5204
5806
  * @param params.percentageToZapOut - Percentage of input token to zap out
5807
+ * @returns The ordered instructions, with no lookup tables
5205
5808
  */
5206
5809
  zapOutThroughDammV2(params) {
5207
5810
  return __async(this, null, function* () {
@@ -5220,29 +5823,14 @@ var Zap = class {
5220
5823
  const poolState = yield getDammV2Pool(this.connection, poolAddress);
5221
5824
  const preInstructions = [];
5222
5825
  const postInstructions = [];
5223
- const [
5224
- { ataPubkey: inputTokenAccount, ix: inputTokenAccountIx },
5225
- { ataPubkey: outputTokenAccount, ix: outputTokenAccountIx }
5226
- ] = yield Promise.all([
5227
- getOrCreateATAInstruction(
5228
- this.connection,
5229
- inputMint,
5230
- user,
5231
- user,
5232
- true,
5233
- inputTokenProgram
5234
- ),
5235
- getOrCreateATAInstruction(
5236
- this.connection,
5237
- outputMint,
5238
- user,
5239
- user,
5240
- true,
5241
- outputTokenProgram
5242
- )
5826
+ const {
5827
+ tokenAccounts: [inputTokenAccount, outputTokenAccount],
5828
+ instructions: createAtaInstructions
5829
+ } = getCreateAtaInstructions(user, [
5830
+ { mint: inputMint, tokenProgram: inputTokenProgram },
5831
+ { mint: outputMint, tokenProgram: outputTokenProgram }
5243
5832
  ]);
5244
- inputTokenAccountIx && preInstructions.push(inputTokenAccountIx);
5245
- outputTokenAccountIx && preInstructions.push(outputTokenAccountIx);
5833
+ preInstructions.push(...createAtaInstructions);
5246
5834
  let preUserTokenBalance;
5247
5835
  try {
5248
5836
  preUserTokenBalance = yield getTokenAccountBalance(
@@ -5274,7 +5862,7 @@ var Zap = class {
5274
5862
  zapOutParams: {
5275
5863
  percentage: percentageToZapOut,
5276
5864
  offsetAmountIn,
5277
- preUserTokenBalance: new import_anchor.BN(preUserTokenBalance),
5865
+ preUserTokenBalance: new import_core.BN(preUserTokenBalance),
5278
5866
  maxSwapAmount,
5279
5867
  payloadData
5280
5868
  },
@@ -5298,6 +5886,7 @@ var Zap = class {
5298
5886
  * @param params.minimumSwapAmountOut - Minimum amount of output token to receive
5299
5887
  * @param params.maxSwapAmount - Maximum amount of input token to swap
5300
5888
  * @param params.percentageToZapOut - Percentage of input token to zap out
5889
+ * @returns The ordered instructions, with no lookup tables
5301
5890
  */
5302
5891
  zapOutThroughDlmm(params) {
5303
5892
  return __async(this, null, function* () {
@@ -5316,29 +5905,14 @@ var Zap = class {
5316
5905
  const lbPairState = yield getLbPairState(this.connection, lbPairAddress);
5317
5906
  const preInstructions = [];
5318
5907
  const postInstructions = [];
5319
- const [
5320
- { ataPubkey: inputTokenAccount, ix: inputTokenAccountIx },
5321
- { ataPubkey: outputTokenAccount, ix: outputTokenAccountIx }
5322
- ] = yield Promise.all([
5323
- getOrCreateATAInstruction(
5324
- this.connection,
5325
- inputMint,
5326
- user,
5327
- user,
5328
- true,
5329
- inputTokenProgram
5330
- ),
5331
- getOrCreateATAInstruction(
5332
- this.connection,
5333
- outputMint,
5334
- user,
5335
- user,
5336
- true,
5337
- outputTokenProgram
5338
- )
5908
+ const {
5909
+ tokenAccounts: [inputTokenAccount, outputTokenAccount],
5910
+ instructions: createAtaInstructions
5911
+ } = getCreateAtaInstructions(user, [
5912
+ { mint: inputMint, tokenProgram: inputTokenProgram },
5913
+ { mint: outputMint, tokenProgram: outputTokenProgram }
5339
5914
  ]);
5340
- inputTokenAccountIx && preInstructions.push(inputTokenAccountIx);
5341
- outputTokenAccountIx && preInstructions.push(outputTokenAccountIx);
5915
+ preInstructions.push(...createAtaInstructions);
5342
5916
  let preUserTokenBalance;
5343
5917
  try {
5344
5918
  preUserTokenBalance = yield getTokenAccountBalance(
@@ -5374,7 +5948,7 @@ var Zap = class {
5374
5948
  zapOutParams: {
5375
5949
  percentage: percentageToZapOut,
5376
5950
  offsetAmountIn: AMOUNT_IN_DLMM_OFFSET,
5377
- preUserTokenBalance: new import_anchor.BN(preUserTokenBalance),
5951
+ preUserTokenBalance: new import_core.BN(preUserTokenBalance),
5378
5952
  maxSwapAmount,
5379
5953
  payloadData
5380
5954
  },
@@ -5396,12 +5970,14 @@ var Zap = class {
5396
5970
  DAMM_V2_PROGRAM_ID,
5397
5971
  DAMM_V2_SWAP_DISCRIMINATOR,
5398
5972
  DEFAULT_JUPITER_API_URL,
5973
+ DEFAULT_JUPITER_API_VERSION,
5399
5974
  DLMM_PROGRAM_ID,
5400
5975
  DLMM_SWAP_DISCRIMINATOR,
5401
5976
  DlmmDirectSwapQuoteRoute,
5402
5977
  DlmmSingleSided,
5403
5978
  DlmmSwapType,
5404
5979
  JUP_V6_PROGRAM_ID,
5980
+ JupiterApiVersion,
5405
5981
  MEMO_PROGRAM_ID,
5406
5982
  SwapExternalType,
5407
5983
  ZAP_PROGRAM_ID,