@crypticdot/defituna-client 3.1.12 → 3.1.13

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
@@ -10400,9 +10400,8 @@ function getDecreaseSpotPositionQuote(args) {
10400
10400
  let newPositionToken = positionToken;
10401
10401
  let decreasePercent;
10402
10402
  const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
10403
- let decreaseAmountInPositionToken = collateralToken == positionToken ? decreaseAmount : BigInt(
10404
- Math.floor(collateralToken == 0 /* A */ ? Number(decreaseAmount) * price : Number(decreaseAmount) / price)
10405
- );
10403
+ const positionToOppositeTokenPrice = positionToken == 0 /* A */ ? price : 1 / price;
10404
+ let decreaseAmountInPositionToken = collateralToken == positionToken ? decreaseAmount : BigInt(Math.floor(Number(decreaseAmount) / positionToOppositeTokenPrice));
10406
10405
  if (reduceOnly && decreaseAmountInPositionToken > positionAmount) {
10407
10406
  decreaseAmountInPositionToken = positionAmount;
10408
10407
  }
@@ -10421,49 +10420,36 @@ function getDecreaseSpotPositionQuote(args) {
10421
10420
  }
10422
10421
  } else {
10423
10422
  swapInputAmount = positionAmount - BigInt(Math.floor(Number(positionAmount) * (HUNDRED_PERCENT - decreasePercent) / HUNDRED_PERCENT));
10424
- nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(
10425
- swapInputAmount,
10426
- positionToken == 0 /* A */,
10427
- 0,
10428
- fusionPool,
10429
- tickArrays
10430
- ).nextSqrtPrice;
10423
+ const swapQuote = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10424
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10431
10425
  }
10432
10426
  } else {
10433
10427
  decreasePercent = HUNDRED_PERCENT;
10434
- const posTokenIsA = positionToken == 0 /* A */;
10435
10428
  newPositionToken = positionToken == 0 /* A */ ? 1 /* B */ : 0 /* A */;
10436
10429
  const increaseAmount = decreaseAmountInPositionToken - positionAmount;
10437
10430
  if (positionToken == collateralToken) {
10438
- estimatedAmount = BigInt(
10439
- Math.floor(posTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10440
- );
10441
- collateral = BigInt(Math.floor(Number(increaseAmount) / leverage));
10442
- borrow = increaseAmount - collateral;
10431
+ estimatedAmount = BigInt(Math.floor(Number(increaseAmount) * positionToOppositeTokenPrice));
10432
+ borrow = BigInt(Math.floor(Number(increaseAmount) * (leverage - 1)) / leverage);
10433
+ const borrowWithFeesApplied = (0, import_fusionamm_core4.tryApplySwapFee)(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10434
+ collateral = increaseAmount - borrowWithFeesApplied;
10443
10435
  if (positionDebt > 0) {
10444
- const swapQuote = (0, import_fusionamm_core4.swapQuoteByOutputToken)(positionDebt, !posTokenIsA, 0, fusionPool, tickArrays);
10445
- swapInputAmount = swapQuote.tokenEstIn;
10436
+ const swapQuote2 = (0, import_fusionamm_core4.swapQuoteByOutputToken)(positionDebt, positionToken != 0 /* A */, 0, fusionPool, tickArrays);
10437
+ swapInputAmount = swapQuote2.tokenEstIn;
10446
10438
  }
10447
- swapInputAmount += increaseAmount;
10448
- nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10439
+ swapInputAmount += collateral + applyTunaProtocolFee(borrow, protocolFeeRate);
10440
+ const swapQuote = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10441
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10449
10442
  collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
10450
- borrow = reverseApplyTunaProtocolFee(borrow, protocolFeeRate);
10451
10443
  } else {
10452
- const collateralInPositionToken = BigInt(Math.floor(Number(increaseAmount) / leverage));
10453
- borrow = increaseAmount - collateralInPositionToken;
10454
- collateral = BigInt(
10455
- Math.floor(
10456
- positionToken == 0 /* A */ ? Number(collateralInPositionToken) * price : Number(collateralInPositionToken) / price
10457
- )
10458
- );
10459
- const borrowInNewPositionToken = BigInt(
10460
- Math.floor(newPositionToken == 1 /* B */ ? Number(borrow) * price : Number(borrow) / price)
10461
- );
10462
- estimatedAmount = collateral + borrowInNewPositionToken;
10463
- swapInputAmount = positionAmount + borrow;
10464
- nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10444
+ estimatedAmount = BigInt(Math.floor(Number(increaseAmount) * positionToOppositeTokenPrice));
10445
+ borrow = BigInt(Math.floor(Number(increaseAmount) * (leverage - 1) / leverage));
10446
+ const borrowWithFeesApplied = (0, import_fusionamm_core4.tryApplySwapFee)(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10447
+ collateral = increaseAmount - borrowWithFeesApplied;
10448
+ collateral = BigInt(Math.floor(Number(collateral) * positionToOppositeTokenPrice));
10465
10449
  collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
10466
- borrow = reverseApplyTunaProtocolFee(borrow, protocolFeeRate);
10450
+ swapInputAmount = positionAmount + applyTunaProtocolFee(borrow, protocolFeeRate);
10451
+ const swapQuote = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10452
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10467
10453
  }
10468
10454
  }
10469
10455
  const protocolFeeA = (collateralToken == 0 /* A */ ? collateral - applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral) : 0n) + (positionToken == 1 /* B */ ? borrow - applyTunaProtocolFee(borrow, protocolFeeRate) : 0n);
package/dist/index.mjs CHANGED
@@ -10570,9 +10570,8 @@ function getDecreaseSpotPositionQuote(args) {
10570
10570
  let newPositionToken = positionToken;
10571
10571
  let decreasePercent;
10572
10572
  const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
10573
- let decreaseAmountInPositionToken = collateralToken == positionToken ? decreaseAmount : BigInt(
10574
- Math.floor(collateralToken == 0 /* A */ ? Number(decreaseAmount) * price : Number(decreaseAmount) / price)
10575
- );
10573
+ const positionToOppositeTokenPrice = positionToken == 0 /* A */ ? price : 1 / price;
10574
+ let decreaseAmountInPositionToken = collateralToken == positionToken ? decreaseAmount : BigInt(Math.floor(Number(decreaseAmount) / positionToOppositeTokenPrice));
10576
10575
  if (reduceOnly && decreaseAmountInPositionToken > positionAmount) {
10577
10576
  decreaseAmountInPositionToken = positionAmount;
10578
10577
  }
@@ -10591,49 +10590,36 @@ function getDecreaseSpotPositionQuote(args) {
10591
10590
  }
10592
10591
  } else {
10593
10592
  swapInputAmount = positionAmount - BigInt(Math.floor(Number(positionAmount) * (HUNDRED_PERCENT - decreasePercent) / HUNDRED_PERCENT));
10594
- nextSqrtPrice = swapQuoteByInputToken(
10595
- swapInputAmount,
10596
- positionToken == 0 /* A */,
10597
- 0,
10598
- fusionPool,
10599
- tickArrays
10600
- ).nextSqrtPrice;
10593
+ const swapQuote = swapQuoteByInputToken(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10594
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10601
10595
  }
10602
10596
  } else {
10603
10597
  decreasePercent = HUNDRED_PERCENT;
10604
- const posTokenIsA = positionToken == 0 /* A */;
10605
10598
  newPositionToken = positionToken == 0 /* A */ ? 1 /* B */ : 0 /* A */;
10606
10599
  const increaseAmount = decreaseAmountInPositionToken - positionAmount;
10607
10600
  if (positionToken == collateralToken) {
10608
- estimatedAmount = BigInt(
10609
- Math.floor(posTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10610
- );
10611
- collateral = BigInt(Math.floor(Number(increaseAmount) / leverage));
10612
- borrow = increaseAmount - collateral;
10601
+ estimatedAmount = BigInt(Math.floor(Number(increaseAmount) * positionToOppositeTokenPrice));
10602
+ borrow = BigInt(Math.floor(Number(increaseAmount) * (leverage - 1)) / leverage);
10603
+ const borrowWithFeesApplied = tryApplySwapFee2(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10604
+ collateral = increaseAmount - borrowWithFeesApplied;
10613
10605
  if (positionDebt > 0) {
10614
- const swapQuote = swapQuoteByOutputToken(positionDebt, !posTokenIsA, 0, fusionPool, tickArrays);
10615
- swapInputAmount = swapQuote.tokenEstIn;
10606
+ const swapQuote2 = swapQuoteByOutputToken(positionDebt, positionToken != 0 /* A */, 0, fusionPool, tickArrays);
10607
+ swapInputAmount = swapQuote2.tokenEstIn;
10616
10608
  }
10617
- swapInputAmount += increaseAmount;
10618
- nextSqrtPrice = swapQuoteByInputToken(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10609
+ swapInputAmount += collateral + applyTunaProtocolFee(borrow, protocolFeeRate);
10610
+ const swapQuote = swapQuoteByInputToken(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10611
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10619
10612
  collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
10620
- borrow = reverseApplyTunaProtocolFee(borrow, protocolFeeRate);
10621
10613
  } else {
10622
- const collateralInPositionToken = BigInt(Math.floor(Number(increaseAmount) / leverage));
10623
- borrow = increaseAmount - collateralInPositionToken;
10624
- collateral = BigInt(
10625
- Math.floor(
10626
- positionToken == 0 /* A */ ? Number(collateralInPositionToken) * price : Number(collateralInPositionToken) / price
10627
- )
10628
- );
10629
- const borrowInNewPositionToken = BigInt(
10630
- Math.floor(newPositionToken == 1 /* B */ ? Number(borrow) * price : Number(borrow) / price)
10631
- );
10632
- estimatedAmount = collateral + borrowInNewPositionToken;
10633
- swapInputAmount = positionAmount + borrow;
10634
- nextSqrtPrice = swapQuoteByInputToken(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10614
+ estimatedAmount = BigInt(Math.floor(Number(increaseAmount) * positionToOppositeTokenPrice));
10615
+ borrow = BigInt(Math.floor(Number(increaseAmount) * (leverage - 1) / leverage));
10616
+ const borrowWithFeesApplied = tryApplySwapFee2(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10617
+ collateral = increaseAmount - borrowWithFeesApplied;
10618
+ collateral = BigInt(Math.floor(Number(collateral) * positionToOppositeTokenPrice));
10635
10619
  collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
10636
- borrow = reverseApplyTunaProtocolFee(borrow, protocolFeeRate);
10620
+ swapInputAmount = positionAmount + applyTunaProtocolFee(borrow, protocolFeeRate);
10621
+ const swapQuote = swapQuoteByInputToken(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10622
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10637
10623
  }
10638
10624
  }
10639
10625
  const protocolFeeA = (collateralToken == 0 /* A */ ? collateral - applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral) : 0n) + (positionToken == 1 /* B */ ? borrow - applyTunaProtocolFee(borrow, protocolFeeRate) : 0n);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@crypticdot/defituna-client",
3
3
  "description": "Typescript client to interact with DefiTuna's on-chain program.",
4
- "version": "3.1.12",
4
+ "version": "3.1.13",
5
5
  "private": false,
6
6
  "license": "SEE LICENSE IN LICENSE",
7
7
  "main": "./dist/index.js",