@crypticdot/defituna-client 3.1.17 → 3.1.19

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
@@ -614,6 +614,7 @@ __export(index_exports, {
614
614
  liquidateTunaSpotPositionFusionInstructions: () => liquidateTunaSpotPositionFusionInstructions,
615
615
  liquidateTunaSpotPositionOrcaInstruction: () => liquidateTunaSpotPositionOrcaInstruction,
616
616
  liquidateTunaSpotPositionOrcaInstructions: () => liquidateTunaSpotPositionOrcaInstructions,
617
+ mulDiv: () => mulDiv,
617
618
  openAndIncreaseTunaLpPositionFusionInstruction: () => openAndIncreaseTunaLpPositionFusionInstruction,
618
619
  openAndIncreaseTunaLpPositionFusionInstructions: () => openAndIncreaseTunaLpPositionFusionInstructions,
619
620
  openAndIncreaseTunaLpPositionOrcaInstruction: () => openAndIncreaseTunaLpPositionOrcaInstruction,
@@ -2251,7 +2252,7 @@ function getCloseActiveTunaSpotPositionFusionInstructionDataEncoder() {
2251
2252
  return (0, import_kit16.transformEncoder)(
2252
2253
  (0, import_kit16.getStructEncoder)([
2253
2254
  ["discriminator", (0, import_kit16.fixEncoderSize)((0, import_kit16.getBytesEncoder)(), 8)],
2254
- ["maxSwapSlippage", (0, import_kit16.getU32Encoder)()],
2255
+ ["maxSwapAmountIn", (0, import_kit16.getU64Encoder)()],
2255
2256
  ["remainingAccountsInfo", getRemainingAccountsInfoEncoder()]
2256
2257
  ]),
2257
2258
  (value) => ({
@@ -2263,7 +2264,7 @@ function getCloseActiveTunaSpotPositionFusionInstructionDataEncoder() {
2263
2264
  function getCloseActiveTunaSpotPositionFusionInstructionDataDecoder() {
2264
2265
  return (0, import_kit16.getStructDecoder)([
2265
2266
  ["discriminator", (0, import_kit16.fixDecoderSize)((0, import_kit16.getBytesDecoder)(), 8)],
2266
- ["maxSwapSlippage", (0, import_kit16.getU32Decoder)()],
2267
+ ["maxSwapAmountIn", (0, import_kit16.getU64Decoder)()],
2267
2268
  ["remainingAccountsInfo", getRemainingAccountsInfoDecoder()]
2268
2269
  ]);
2269
2270
  }
@@ -2410,7 +2411,7 @@ function getCloseActiveTunaSpotPositionOrcaInstructionDataEncoder() {
2410
2411
  return (0, import_kit17.transformEncoder)(
2411
2412
  (0, import_kit17.getStructEncoder)([
2412
2413
  ["discriminator", (0, import_kit17.fixEncoderSize)((0, import_kit17.getBytesEncoder)(), 8)],
2413
- ["maxSwapSlippage", (0, import_kit17.getU32Encoder)()],
2414
+ ["maxSwapAmountIn", (0, import_kit17.getU64Encoder)()],
2414
2415
  ["remainingAccountsInfo", getRemainingAccountsInfoEncoder()]
2415
2416
  ]),
2416
2417
  (value) => ({
@@ -2422,7 +2423,7 @@ function getCloseActiveTunaSpotPositionOrcaInstructionDataEncoder() {
2422
2423
  function getCloseActiveTunaSpotPositionOrcaInstructionDataDecoder() {
2423
2424
  return (0, import_kit17.getStructDecoder)([
2424
2425
  ["discriminator", (0, import_kit17.fixDecoderSize)((0, import_kit17.getBytesDecoder)(), 8)],
2425
- ["maxSwapSlippage", (0, import_kit17.getU32Decoder)()],
2426
+ ["maxSwapAmountIn", (0, import_kit17.getU64Decoder)()],
2426
2427
  ["remainingAccountsInfo", getRemainingAccountsInfoDecoder()]
2427
2428
  ]);
2428
2429
  }
@@ -4363,7 +4364,7 @@ function getDecreaseTunaSpotPositionFusionInstructionDataEncoder() {
4363
4364
  (0, import_kit31.getStructEncoder)([
4364
4365
  ["discriminator", (0, import_kit31.fixEncoderSize)((0, import_kit31.getBytesEncoder)(), 8)],
4365
4366
  ["withdrawPercent", (0, import_kit31.getU32Encoder)()],
4366
- ["maxSwapSlippage", (0, import_kit31.getU32Encoder)()],
4367
+ ["maxSwapAmountIn", (0, import_kit31.getU64Encoder)()],
4367
4368
  ["remainingAccountsInfo", getRemainingAccountsInfoEncoder()]
4368
4369
  ]),
4369
4370
  (value) => ({
@@ -4376,7 +4377,7 @@ function getDecreaseTunaSpotPositionFusionInstructionDataDecoder() {
4376
4377
  return (0, import_kit31.getStructDecoder)([
4377
4378
  ["discriminator", (0, import_kit31.fixDecoderSize)((0, import_kit31.getBytesDecoder)(), 8)],
4378
4379
  ["withdrawPercent", (0, import_kit31.getU32Decoder)()],
4379
- ["maxSwapSlippage", (0, import_kit31.getU32Decoder)()],
4380
+ ["maxSwapAmountIn", (0, import_kit31.getU64Decoder)()],
4380
4381
  ["remainingAccountsInfo", getRemainingAccountsInfoDecoder()]
4381
4382
  ]);
4382
4383
  }
@@ -4539,7 +4540,7 @@ function getDecreaseTunaSpotPositionOrcaInstructionDataEncoder() {
4539
4540
  (0, import_kit32.getStructEncoder)([
4540
4541
  ["discriminator", (0, import_kit32.fixEncoderSize)((0, import_kit32.getBytesEncoder)(), 8)],
4541
4542
  ["withdrawPercent", (0, import_kit32.getU32Encoder)()],
4542
- ["maxSwapSlippage", (0, import_kit32.getU32Encoder)()],
4543
+ ["maxSwapAmountIn", (0, import_kit32.getU64Encoder)()],
4543
4544
  ["remainingAccountsInfo", getRemainingAccountsInfoEncoder()]
4544
4545
  ]),
4545
4546
  (value) => ({
@@ -4552,7 +4553,7 @@ function getDecreaseTunaSpotPositionOrcaInstructionDataDecoder() {
4552
4553
  return (0, import_kit32.getStructDecoder)([
4553
4554
  ["discriminator", (0, import_kit32.fixDecoderSize)((0, import_kit32.getBytesDecoder)(), 8)],
4554
4555
  ["withdrawPercent", (0, import_kit32.getU32Decoder)()],
4555
- ["maxSwapSlippage", (0, import_kit32.getU32Decoder)()],
4556
+ ["maxSwapAmountIn", (0, import_kit32.getU64Decoder)()],
4556
4557
  ["remainingAccountsInfo", getRemainingAccountsInfoDecoder()]
4557
4558
  ]);
4558
4559
  }
@@ -5205,7 +5206,7 @@ function getIncreaseTunaSpotPositionFusionInstructionDataEncoder() {
5205
5206
  ["discriminator", (0, import_kit36.fixEncoderSize)((0, import_kit36.getBytesEncoder)(), 8)],
5206
5207
  ["collateralAmount", (0, import_kit36.getU64Encoder)()],
5207
5208
  ["borrowAmount", (0, import_kit36.getU64Encoder)()],
5208
- ["maxSwapSlippage", (0, import_kit36.getU32Encoder)()],
5209
+ ["minSwapAmountOut", (0, import_kit36.getU64Encoder)()],
5209
5210
  ["remainingAccountsInfo", getRemainingAccountsInfoEncoder()]
5210
5211
  ]),
5211
5212
  (value) => ({
@@ -5219,7 +5220,7 @@ function getIncreaseTunaSpotPositionFusionInstructionDataDecoder() {
5219
5220
  ["discriminator", (0, import_kit36.fixDecoderSize)((0, import_kit36.getBytesDecoder)(), 8)],
5220
5221
  ["collateralAmount", (0, import_kit36.getU64Decoder)()],
5221
5222
  ["borrowAmount", (0, import_kit36.getU64Decoder)()],
5222
- ["maxSwapSlippage", (0, import_kit36.getU32Decoder)()],
5223
+ ["minSwapAmountOut", (0, import_kit36.getU64Decoder)()],
5223
5224
  ["remainingAccountsInfo", getRemainingAccountsInfoDecoder()]
5224
5225
  ]);
5225
5226
  }
@@ -5395,7 +5396,7 @@ function getIncreaseTunaSpotPositionOrcaInstructionDataEncoder() {
5395
5396
  ["discriminator", (0, import_kit37.fixEncoderSize)((0, import_kit37.getBytesEncoder)(), 8)],
5396
5397
  ["collateralAmount", (0, import_kit37.getU64Encoder)()],
5397
5398
  ["borrowAmount", (0, import_kit37.getU64Encoder)()],
5398
- ["maxSwapSlippage", (0, import_kit37.getU32Encoder)()],
5399
+ ["minSwapAmountOut", (0, import_kit37.getU64Encoder)()],
5399
5400
  ["remainingAccountsInfo", getRemainingAccountsInfoEncoder()]
5400
5401
  ]),
5401
5402
  (value) => ({
@@ -5409,7 +5410,7 @@ function getIncreaseTunaSpotPositionOrcaInstructionDataDecoder() {
5409
5410
  ["discriminator", (0, import_kit37.fixDecoderSize)((0, import_kit37.getBytesDecoder)(), 8)],
5410
5411
  ["collateralAmount", (0, import_kit37.getU64Decoder)()],
5411
5412
  ["borrowAmount", (0, import_kit37.getU64Decoder)()],
5412
- ["maxSwapSlippage", (0, import_kit37.getU32Decoder)()],
5413
+ ["minSwapAmountOut", (0, import_kit37.getU64Decoder)()],
5413
5414
  ["remainingAccountsInfo", getRemainingAccountsInfoDecoder()]
5414
5415
  ]);
5415
5416
  }
@@ -6745,10 +6746,7 @@ function getOpenAndIncreaseTunaSpotPositionFusionInstructionDataEncoder() {
6745
6746
  ["collateralToken", getPoolTokenEncoder()],
6746
6747
  ["collateralAmount", (0, import_kit44.getU64Encoder)()],
6747
6748
  ["borrowAmount", (0, import_kit44.getU64Encoder)()],
6748
- ["lowerLimitOrderSqrtPrice", (0, import_kit44.getU128Encoder)()],
6749
- ["upperLimitOrderSqrtPrice", (0, import_kit44.getU128Encoder)()],
6750
- ["flags", (0, import_kit44.getU32Encoder)()],
6751
- ["maxSwapSlippage", (0, import_kit44.getU32Encoder)()],
6749
+ ["minSwapAmountOut", (0, import_kit44.getU64Encoder)()],
6752
6750
  ["remainingAccountsInfo", getRemainingAccountsInfoEncoder()]
6753
6751
  ]),
6754
6752
  (value) => ({
@@ -6764,10 +6762,7 @@ function getOpenAndIncreaseTunaSpotPositionFusionInstructionDataDecoder() {
6764
6762
  ["collateralToken", getPoolTokenDecoder()],
6765
6763
  ["collateralAmount", (0, import_kit44.getU64Decoder)()],
6766
6764
  ["borrowAmount", (0, import_kit44.getU64Decoder)()],
6767
- ["lowerLimitOrderSqrtPrice", (0, import_kit44.getU128Decoder)()],
6768
- ["upperLimitOrderSqrtPrice", (0, import_kit44.getU128Decoder)()],
6769
- ["flags", (0, import_kit44.getU32Decoder)()],
6770
- ["maxSwapSlippage", (0, import_kit44.getU32Decoder)()],
6765
+ ["minSwapAmountOut", (0, import_kit44.getU64Decoder)()],
6771
6766
  ["remainingAccountsInfo", getRemainingAccountsInfoDecoder()]
6772
6767
  ]);
6773
6768
  }
@@ -6942,10 +6937,7 @@ function getOpenAndIncreaseTunaSpotPositionOrcaInstructionDataEncoder() {
6942
6937
  ["collateralToken", getPoolTokenEncoder()],
6943
6938
  ["collateralAmount", (0, import_kit45.getU64Encoder)()],
6944
6939
  ["borrowAmount", (0, import_kit45.getU64Encoder)()],
6945
- ["lowerLimitOrderSqrtPrice", (0, import_kit45.getU128Encoder)()],
6946
- ["upperLimitOrderSqrtPrice", (0, import_kit45.getU128Encoder)()],
6947
- ["flags", (0, import_kit45.getU32Encoder)()],
6948
- ["maxSwapSlippage", (0, import_kit45.getU32Encoder)()],
6940
+ ["minSwapAmountOut", (0, import_kit45.getU64Encoder)()],
6949
6941
  ["remainingAccountsInfo", getRemainingAccountsInfoEncoder()]
6950
6942
  ]),
6951
6943
  (value) => ({
@@ -6961,10 +6953,7 @@ function getOpenAndIncreaseTunaSpotPositionOrcaInstructionDataDecoder() {
6961
6953
  ["collateralToken", getPoolTokenDecoder()],
6962
6954
  ["collateralAmount", (0, import_kit45.getU64Decoder)()],
6963
6955
  ["borrowAmount", (0, import_kit45.getU64Decoder)()],
6964
- ["lowerLimitOrderSqrtPrice", (0, import_kit45.getU128Decoder)()],
6965
- ["upperLimitOrderSqrtPrice", (0, import_kit45.getU128Decoder)()],
6966
- ["flags", (0, import_kit45.getU32Decoder)()],
6967
- ["maxSwapSlippage", (0, import_kit45.getU32Decoder)()],
6956
+ ["minSwapAmountOut", (0, import_kit45.getU64Decoder)()],
6968
6957
  ["remainingAccountsInfo", getRemainingAccountsInfoDecoder()]
6969
6958
  ]);
6970
6959
  }
@@ -7565,10 +7554,7 @@ function getOpenTunaSpotPositionFusionInstructionDataEncoder() {
7565
7554
  (0, import_kit49.getStructEncoder)([
7566
7555
  ["discriminator", (0, import_kit49.fixEncoderSize)((0, import_kit49.getBytesEncoder)(), 8)],
7567
7556
  ["positionToken", getPoolTokenEncoder()],
7568
- ["collateralToken", getPoolTokenEncoder()],
7569
- ["lowerLimitOrderSqrtPrice", (0, import_kit49.getU128Encoder)()],
7570
- ["upperLimitOrderSqrtPrice", (0, import_kit49.getU128Encoder)()],
7571
- ["flags", (0, import_kit49.getU32Encoder)()]
7557
+ ["collateralToken", getPoolTokenEncoder()]
7572
7558
  ]),
7573
7559
  (value) => ({
7574
7560
  ...value,
@@ -7580,10 +7566,7 @@ function getOpenTunaSpotPositionFusionInstructionDataDecoder() {
7580
7566
  return (0, import_kit49.getStructDecoder)([
7581
7567
  ["discriminator", (0, import_kit49.fixDecoderSize)((0, import_kit49.getBytesDecoder)(), 8)],
7582
7568
  ["positionToken", getPoolTokenDecoder()],
7583
- ["collateralToken", getPoolTokenDecoder()],
7584
- ["lowerLimitOrderSqrtPrice", (0, import_kit49.getU128Decoder)()],
7585
- ["upperLimitOrderSqrtPrice", (0, import_kit49.getU128Decoder)()],
7586
- ["flags", (0, import_kit49.getU32Decoder)()]
7569
+ ["collateralToken", getPoolTokenDecoder()]
7587
7570
  ]);
7588
7571
  }
7589
7572
  function getOpenTunaSpotPositionFusionInstructionDataCodec() {
@@ -7696,10 +7679,7 @@ function getOpenTunaSpotPositionOrcaInstructionDataEncoder() {
7696
7679
  (0, import_kit50.getStructEncoder)([
7697
7680
  ["discriminator", (0, import_kit50.fixEncoderSize)((0, import_kit50.getBytesEncoder)(), 8)],
7698
7681
  ["positionToken", getPoolTokenEncoder()],
7699
- ["collateralToken", getPoolTokenEncoder()],
7700
- ["lowerLimitOrderSqrtPrice", (0, import_kit50.getU128Encoder)()],
7701
- ["upperLimitOrderSqrtPrice", (0, import_kit50.getU128Encoder)()],
7702
- ["flags", (0, import_kit50.getU32Encoder)()]
7682
+ ["collateralToken", getPoolTokenEncoder()]
7703
7683
  ]),
7704
7684
  (value) => ({
7705
7685
  ...value,
@@ -7711,10 +7691,7 @@ function getOpenTunaSpotPositionOrcaInstructionDataDecoder() {
7711
7691
  return (0, import_kit50.getStructDecoder)([
7712
7692
  ["discriminator", (0, import_kit50.fixDecoderSize)((0, import_kit50.getBytesDecoder)(), 8)],
7713
7693
  ["positionToken", getPoolTokenDecoder()],
7714
- ["collateralToken", getPoolTokenDecoder()],
7715
- ["lowerLimitOrderSqrtPrice", (0, import_kit50.getU128Decoder)()],
7716
- ["upperLimitOrderSqrtPrice", (0, import_kit50.getU128Decoder)()],
7717
- ["flags", (0, import_kit50.getU32Decoder)()]
7694
+ ["collateralToken", getPoolTokenDecoder()]
7718
7695
  ]);
7719
7696
  }
7720
7697
  function getOpenTunaSpotPositionOrcaInstructionDataCodec() {
@@ -8399,9 +8376,7 @@ function getResetTunaSpotPositionInstructionDataEncoder() {
8399
8376
  (0, import_kit55.getStructEncoder)([
8400
8377
  ["discriminator", (0, import_kit55.fixEncoderSize)((0, import_kit55.getBytesEncoder)(), 8)],
8401
8378
  ["positionToken", getPoolTokenEncoder()],
8402
- ["collateralToken", getPoolTokenEncoder()],
8403
- ["lowerLimitOrderSqrtPrice", (0, import_kit55.getU128Encoder)()],
8404
- ["upperLimitOrderSqrtPrice", (0, import_kit55.getU128Encoder)()]
8379
+ ["collateralToken", getPoolTokenEncoder()]
8405
8380
  ]),
8406
8381
  (value) => ({
8407
8382
  ...value,
@@ -8413,9 +8388,7 @@ function getResetTunaSpotPositionInstructionDataDecoder() {
8413
8388
  return (0, import_kit55.getStructDecoder)([
8414
8389
  ["discriminator", (0, import_kit55.fixDecoderSize)((0, import_kit55.getBytesDecoder)(), 8)],
8415
8390
  ["positionToken", getPoolTokenDecoder()],
8416
- ["collateralToken", getPoolTokenDecoder()],
8417
- ["lowerLimitOrderSqrtPrice", (0, import_kit55.getU128Decoder)()],
8418
- ["upperLimitOrderSqrtPrice", (0, import_kit55.getU128Decoder)()]
8391
+ ["collateralToken", getPoolTokenDecoder()]
8419
8392
  ]);
8420
8393
  }
8421
8394
  function getResetTunaSpotPositionInstructionDataCodec() {
@@ -10037,6 +10010,15 @@ async function fetchAllMarketWithFilter(rpc, ...filters) {
10037
10010
  return fetchDecodedProgramAccounts(rpc, TUNA_PROGRAM_ADDRESS, [discriminatorFilter, ...filters], getMarketDecoder());
10038
10011
  }
10039
10012
 
10013
+ // src/utils/math.ts
10014
+ function mulDiv(x, y, d, roundUp = false) {
10015
+ if (!roundUp) {
10016
+ return x * y / d;
10017
+ } else {
10018
+ return (x * y + (d - 1n)) / d;
10019
+ }
10020
+ }
10021
+
10040
10022
  // src/utils/orca.ts
10041
10023
  var import_fusionamm_core = require("@crypticdot/fusionamm-core");
10042
10024
  var import_whirlpools_client = require("@orca-so/whirlpools-client");
@@ -10323,10 +10305,10 @@ function getIncreaseSpotPositionQuote(args) {
10323
10305
  let nextSqrtPrice = fusionPool.sqrtPrice;
10324
10306
  if (positionToken != collateralToken) {
10325
10307
  const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
10326
- borrow = BigInt(Math.round(Number(increaseAmount) * (leverage - 1) / leverage));
10327
- const borrowWithFeesApplied = (0, import_fusionamm_core4.tryApplySwapFee)(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10328
- collateral = reverseApplyTunaProtocolFee(increaseAmount - borrowWithFeesApplied, protocolFeeRateOnCollateral);
10329
- collateral = (0, import_fusionamm_core4.tryReverseApplySwapFee)(collateral, fusionPool.feeRate);
10308
+ borrow = BigInt(Math.ceil(Number(increaseAmount) * (leverage - 1) / leverage));
10309
+ collateral = increaseAmount - applySwapFee(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10310
+ collateral = reverseApplySwapFee(collateral, fusionPool.feeRate, false);
10311
+ collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
10330
10312
  swapInputAmount = increaseAmount;
10331
10313
  estimatedAmount = BigInt(
10332
10314
  Math.round(collateralToken == 0 /* A */ ? Number(increaseAmount) * price : Number(increaseAmount) / price)
@@ -10334,16 +10316,14 @@ function getIncreaseSpotPositionQuote(args) {
10334
10316
  } else {
10335
10317
  const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
10336
10318
  const positionToBorrowedTokenPrice = collateralToken == 0 /* A */ ? price : 1 / price;
10337
- const borrowInPositionToken = Number(increaseAmount) * (leverage - 1) / leverage;
10338
- borrow = BigInt(Math.round(borrowInPositionToken * positionToBorrowedTokenPrice));
10339
- const borrowInPositionTokenWithFeesApplied = (0, import_fusionamm_core4.tryApplySwapFee)(
10340
- applyTunaProtocolFee(BigInt(Math.floor(borrowInPositionToken)), protocolFeeRate),
10319
+ const borrowInPositionToken = Math.ceil(Number(increaseAmount) * (leverage - 1) / leverage);
10320
+ borrow = BigInt(Math.ceil(borrowInPositionToken * positionToBorrowedTokenPrice));
10321
+ const borrowInPositionTokenWithFeesApplied = applySwapFee(
10322
+ applyTunaProtocolFee(BigInt(borrowInPositionToken), protocolFeeRate),
10341
10323
  fusionPool.feeRate
10342
10324
  );
10343
- collateral = reverseApplyTunaProtocolFee(
10344
- increaseAmount - borrowInPositionTokenWithFeesApplied,
10345
- protocolFeeRateOnCollateral
10346
- );
10325
+ collateral = increaseAmount - borrowInPositionTokenWithFeesApplied;
10326
+ collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
10347
10327
  swapInputAmount = applyTunaProtocolFee(borrow, protocolFeeRate);
10348
10328
  estimatedAmount = increaseAmount;
10349
10329
  }
@@ -10430,7 +10410,7 @@ function getDecreaseSpotPositionQuote(args) {
10430
10410
  if (positionToken == collateralToken) {
10431
10411
  estimatedAmount = BigInt(Math.round(Number(increaseAmount) * positionToOppositeTokenPrice));
10432
10412
  borrow = BigInt(Math.round(Number(increaseAmount) * (leverage - 1) / leverage));
10433
- const borrowWithFeesApplied = (0, import_fusionamm_core4.tryApplySwapFee)(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10413
+ const borrowWithFeesApplied = applySwapFee(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10434
10414
  collateral = increaseAmount - borrowWithFeesApplied;
10435
10415
  if (positionDebt > 0) {
10436
10416
  const swapQuote2 = (0, import_fusionamm_core4.swapQuoteByOutputToken)(positionDebt, positionToken != 0 /* A */, 0, fusionPool, tickArrays);
@@ -10439,14 +10419,14 @@ function getDecreaseSpotPositionQuote(args) {
10439
10419
  swapInputAmount += collateral + applyTunaProtocolFee(borrow, protocolFeeRate);
10440
10420
  const swapQuote = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10441
10421
  nextSqrtPrice = swapQuote.nextSqrtPrice;
10442
- collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
10422
+ collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
10443
10423
  } else {
10444
10424
  estimatedAmount = BigInt(Math.round(Number(increaseAmount) * positionToOppositeTokenPrice));
10445
10425
  borrow = BigInt(Math.round(Number(increaseAmount) * (leverage - 1) / leverage));
10446
- const borrowWithFeesApplied = (0, import_fusionamm_core4.tryApplySwapFee)(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10426
+ const borrowWithFeesApplied = applySwapFee(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10447
10427
  collateral = increaseAmount - borrowWithFeesApplied;
10448
10428
  collateral = BigInt(Math.round(Number(collateral) * positionToOppositeTokenPrice));
10449
- collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
10429
+ collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
10450
10430
  swapInputAmount = positionAmount + applyTunaProtocolFee(borrow, protocolFeeRate);
10451
10431
  const swapQuote = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10452
10432
  nextSqrtPrice = swapQuote.nextSqrtPrice;
@@ -10499,16 +10479,12 @@ function getTradableAmount(args) {
10499
10479
  let availableToTrade = 0n;
10500
10480
  const addLeverage = (collateral) => {
10501
10481
  collateral = applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
10502
- const feeMultiplier = (1 - protocolFeeRate / HUNDRED_PERCENT) * (1 - fusionPool.feeRate / 1e6);
10503
- let borrow = BigInt(
10504
- Math.round(Number(collateral) * (leverage - 1) / (leverage + feeMultiplier * (1 - leverage)))
10505
- );
10506
- borrow = applyTunaProtocolFee(borrow, protocolFeeRate);
10507
- borrow = (0, import_fusionamm_core4.tryApplySwapFee)(borrow, fusionPool.feeRate);
10508
10482
  if (collateralToken != newPositionToken) {
10509
- collateral = (0, import_fusionamm_core4.tryApplySwapFee)(collateral, fusionPool.feeRate);
10483
+ collateral = applySwapFee(collateral, fusionPool.feeRate);
10510
10484
  }
10511
- return collateral + borrow;
10485
+ const feeMultiplier = (1 - protocolFeeRate / HUNDRED_PERCENT) * (1 - fusionPool.feeRate / 1e6);
10486
+ const total = Math.floor(Number(collateral) / (1 - feeMultiplier * (leverage - 1) / leverage));
10487
+ return BigInt(total);
10512
10488
  };
10513
10489
  if (newPositionToken == positionToken) {
10514
10490
  availableToTrade = addLeverage(availableBalance);
@@ -10547,11 +10523,17 @@ function getLiquidationPrice(positionToken, amount, debt, liquidationThreshold)
10547
10523
  return amount * liquidationThreshold / debt;
10548
10524
  }
10549
10525
  }
10550
- function applyTunaProtocolFee(amount, protocolFeeRate) {
10551
- return amount * BigInt(HUNDRED_PERCENT - protocolFeeRate) / BigInt(HUNDRED_PERCENT);
10526
+ function applyTunaProtocolFee(amount, protocolFeeRate, roundUp = false) {
10527
+ return mulDiv(amount, BigInt(HUNDRED_PERCENT - protocolFeeRate), BigInt(HUNDRED_PERCENT), roundUp);
10528
+ }
10529
+ function reverseApplyTunaProtocolFee(amount, protocolFeeRate, roundUp = true) {
10530
+ return mulDiv(amount, BigInt(HUNDRED_PERCENT), BigInt(HUNDRED_PERCENT - protocolFeeRate), roundUp);
10531
+ }
10532
+ function applySwapFee(amount, feeRate, roundUp = false) {
10533
+ return mulDiv(amount, BigInt(1e6 - feeRate), 1000000n, roundUp);
10552
10534
  }
10553
- function reverseApplyTunaProtocolFee(amount, protocolFeeRate) {
10554
- return amount * BigInt(HUNDRED_PERCENT) / BigInt(HUNDRED_PERCENT - protocolFeeRate);
10535
+ function reverseApplySwapFee(amount, feeRate, roundUp = true) {
10536
+ return mulDiv(amount, 1000000n, BigInt(1e6 - feeRate), roundUp);
10555
10537
  }
10556
10538
 
10557
10539
  // src/txbuilder/increaseTunaLpPositionOrca.ts
@@ -16064,6 +16046,7 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
16064
16046
  liquidateTunaSpotPositionFusionInstructions,
16065
16047
  liquidateTunaSpotPositionOrcaInstruction,
16066
16048
  liquidateTunaSpotPositionOrcaInstructions,
16049
+ mulDiv,
16067
16050
  openAndIncreaseTunaLpPositionFusionInstruction,
16068
16051
  openAndIncreaseTunaLpPositionFusionInstructions,
16069
16052
  openAndIncreaseTunaLpPositionOrcaInstruction,