@crypticdot/defituna-client 3.1.6 → 3.1.7

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.d.mts CHANGED
@@ -6762,6 +6762,8 @@ type IncreaseSpotPositionQuoteResult = {
6762
6762
  borrow: bigint;
6763
6763
  /** Estimated position size in the position token. */
6764
6764
  estimatedAmount: bigint;
6765
+ /** Swap input amount. */
6766
+ swapInputAmount: bigint;
6765
6767
  /** Protocol fee in token A */
6766
6768
  protocolFeeA: bigint;
6767
6769
  /** Protocol fee in token B */
@@ -6805,6 +6807,8 @@ type DecreaseSpotPositionQuoteResult = {
6805
6807
  collateral: bigint;
6806
6808
  /** Required amount to borrow */
6807
6809
  borrow: bigint;
6810
+ /** Swap input amount. */
6811
+ swapInputAmount: bigint;
6808
6812
  /** Estimated total amount of the new position */
6809
6813
  estimatedAmount: bigint;
6810
6814
  /** Protocol fee in token A */
package/dist/index.d.ts CHANGED
@@ -6762,6 +6762,8 @@ type IncreaseSpotPositionQuoteResult = {
6762
6762
  borrow: bigint;
6763
6763
  /** Estimated position size in the position token. */
6764
6764
  estimatedAmount: bigint;
6765
+ /** Swap input amount. */
6766
+ swapInputAmount: bigint;
6765
6767
  /** Protocol fee in token A */
6766
6768
  protocolFeeA: bigint;
6767
6769
  /** Protocol fee in token B */
@@ -6805,6 +6807,8 @@ type DecreaseSpotPositionQuoteResult = {
6805
6807
  collateral: bigint;
6806
6808
  /** Required amount to borrow */
6807
6809
  borrow: bigint;
6810
+ /** Swap input amount. */
6811
+ swapInputAmount: bigint;
6808
6812
  /** Estimated total amount of the new position */
6809
6813
  estimatedAmount: bigint;
6810
6814
  /** Protocol fee in token A */
package/dist/index.js CHANGED
@@ -10315,24 +10315,19 @@ function getIncreaseSpotPositionQuote(args) {
10315
10315
  if (increaseAmount <= 0) {
10316
10316
  throw new Error("increaseAmount must be greater than zero");
10317
10317
  }
10318
- let borrow;
10318
+ let borrow = 0n;
10319
10319
  let collateral;
10320
10320
  let estimatedAmount;
10321
+ let swapInputAmount = 0n;
10321
10322
  let nextSqrtPrice = fusionPool.sqrtPrice;
10322
10323
  if (positionToken != collateralToken) {
10323
10324
  const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
10324
10325
  collateral = leverage > 1 ? BigInt(Math.floor(Number(increaseAmount) / leverage)) : increaseAmount;
10325
10326
  borrow = increaseAmount - collateral;
10327
+ swapInputAmount = increaseAmount;
10326
10328
  estimatedAmount = BigInt(
10327
10329
  Math.floor(collateralToken == 0 /* A */ ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10328
10330
  );
10329
- nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(
10330
- increaseAmount,
10331
- collateralToken == 0 /* A */,
10332
- 0,
10333
- fusionPool,
10334
- tickArrays
10335
- ).nextSqrtPrice;
10336
10331
  } else {
10337
10332
  if (leverage > 1) {
10338
10333
  const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
@@ -10342,20 +10337,17 @@ function getIncreaseSpotPositionQuote(args) {
10342
10337
  collateralToken == 0 /* A */ ? Number(increaseAmount - collateral) * price : Number(increaseAmount - collateral) / price
10343
10338
  )
10344
10339
  );
10340
+ swapInputAmount = borrow;
10345
10341
  estimatedAmount = increaseAmount;
10346
- nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByOutputToken)(
10347
- increaseAmount - collateral,
10348
- positionToken == 0 /* A */,
10349
- 0,
10350
- fusionPool,
10351
- tickArrays
10352
- ).nextSqrtPrice;
10353
10342
  } else {
10354
10343
  collateral = increaseAmount;
10355
- borrow = 0n;
10356
10344
  estimatedAmount = increaseAmount;
10357
10345
  }
10358
10346
  }
10347
+ if (swapInputAmount > 0) {
10348
+ const is_token_a = positionToken == 1 /* B */;
10349
+ nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, is_token_a, 0, fusionPool, tickArrays).nextSqrtPrice;
10350
+ }
10359
10351
  const collateralExcludingFee = collateral;
10360
10352
  const borrowExcludingFee = borrow;
10361
10353
  collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
@@ -10368,6 +10360,7 @@ function getIncreaseSpotPositionQuote(args) {
10368
10360
  return {
10369
10361
  collateral,
10370
10362
  borrow,
10363
+ swapInputAmount,
10371
10364
  estimatedAmount,
10372
10365
  protocolFeeA,
10373
10366
  protocolFeeB,
@@ -10402,6 +10395,7 @@ function getDecreaseSpotPositionQuote(args) {
10402
10395
  }
10403
10396
  let collateral = 0n;
10404
10397
  let borrow = 0n;
10398
+ let swapInputAmount = 0n;
10405
10399
  let estimatedAmount = 0n;
10406
10400
  let nextSqrtPrice = fusionPool.sqrtPrice;
10407
10401
  let newPositionToken = positionToken;
@@ -10420,31 +10414,41 @@ function getDecreaseSpotPositionQuote(args) {
10420
10414
  Math.floor(Number(decreaseAmountInPositionToken) * HUNDRED_PERCENT / Number(positionAmount)),
10421
10415
  HUNDRED_PERCENT
10422
10416
  );
10423
- const swapOut = BigInt(Math.floor(Number(positionDebt) * decreasePercent / HUNDRED_PERCENT));
10424
- const swapQuote = (0, import_fusionamm_core4.swapQuoteByOutputToken)(swapOut, positionToken == 1 /* B */, 0, fusionPool, tickArrays);
10425
- nextSqrtPrice = swapQuote.nextSqrtPrice;
10417
+ if (collateralToken == positionToken) {
10418
+ if (positionDebt > 0) {
10419
+ const swapOut = BigInt(Math.floor(Number(positionDebt) * decreasePercent / HUNDRED_PERCENT));
10420
+ const swapQuote = (0, import_fusionamm_core4.swapQuoteByOutputToken)(swapOut, positionToken == 1 /* B */, 0, fusionPool, tickArrays);
10421
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10422
+ swapInputAmount = swapQuote.tokenEstIn;
10423
+ }
10424
+ } else {
10425
+ swapInputAmount = positionAmount - BigInt(Math.floor(Number(positionAmount) * (HUNDRED_PERCENT - decreasePercent) / HUNDRED_PERCENT));
10426
+ nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(
10427
+ swapInputAmount,
10428
+ positionToken == 0 /* A */,
10429
+ 0,
10430
+ fusionPool,
10431
+ tickArrays
10432
+ ).nextSqrtPrice;
10433
+ }
10426
10434
  estimatedAmount = decreaseAmountInPositionToken <= positionAmount ? positionAmount - decreaseAmountInPositionToken : 0n;
10427
10435
  } else {
10428
10436
  decreasePercent = HUNDRED_PERCENT;
10429
- const positionTokenIsA = positionToken == 0 /* A */;
10437
+ const posTokenIsA = positionToken == 0 /* A */;
10430
10438
  newPositionToken = positionToken == 0 /* A */ ? 1 /* B */ : 0 /* A */;
10431
10439
  const increaseAmount = decreaseAmount - positionAmount;
10432
10440
  if (positionToken == collateralToken) {
10433
10441
  estimatedAmount = BigInt(
10434
- Math.floor(positionTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10442
+ Math.floor(posTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10435
10443
  );
10436
10444
  collateral = leverage > 1 ? BigInt(Math.floor(Number(increaseAmount) / leverage)) : estimatedAmount;
10437
10445
  borrow = increaseAmount - collateral;
10438
- const borrowInNewPositionToken = BigInt(
10439
- Math.floor(positionTokenIsA ? Number(borrow) * price : Number(borrow) / price)
10440
- );
10441
- nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(
10442
- positionDebt + borrowInNewPositionToken,
10443
- !positionTokenIsA,
10444
- 0,
10445
- fusionPool,
10446
- tickArrays
10447
- ).nextSqrtPrice;
10446
+ if (positionDebt > 0) {
10447
+ const swapQuote = (0, import_fusionamm_core4.swapQuoteByOutputToken)(positionDebt, !posTokenIsA, 0, fusionPool, tickArrays);
10448
+ swapInputAmount = swapQuote.tokenEstIn;
10449
+ }
10450
+ swapInputAmount += increaseAmount;
10451
+ nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10448
10452
  } else {
10449
10453
  if (leverage > 1) {
10450
10454
  const collateralInPositionToken = BigInt(Math.floor(Number(increaseAmount) / leverage));
@@ -10458,13 +10462,8 @@ function getDecreaseSpotPositionQuote(args) {
10458
10462
  Math.floor(newPositionToken == 1 /* B */ ? Number(borrow) * price : Number(borrow) / price)
10459
10463
  );
10460
10464
  estimatedAmount = collateral + borrowInNewPositionToken;
10461
- nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(
10462
- positionAmount + borrow,
10463
- positionTokenIsA,
10464
- 0,
10465
- fusionPool,
10466
- tickArrays
10467
- ).nextSqrtPrice;
10465
+ swapInputAmount = positionAmount + borrow;
10466
+ nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10468
10467
  } else {
10469
10468
  collateral = increaseAmount;
10470
10469
  borrow = 0n;
@@ -10484,8 +10483,9 @@ function getDecreaseSpotPositionQuote(args) {
10484
10483
  collateralToken,
10485
10484
  positionToken: newPositionToken,
10486
10485
  collateral,
10487
- estimatedAmount,
10488
10486
  borrow,
10487
+ swapInputAmount,
10488
+ estimatedAmount,
10489
10489
  protocolFeeA,
10490
10490
  protocolFeeB,
10491
10491
  priceImpact
package/dist/index.mjs CHANGED
@@ -10484,24 +10484,19 @@ function getIncreaseSpotPositionQuote(args) {
10484
10484
  if (increaseAmount <= 0) {
10485
10485
  throw new Error("increaseAmount must be greater than zero");
10486
10486
  }
10487
- let borrow;
10487
+ let borrow = 0n;
10488
10488
  let collateral;
10489
10489
  let estimatedAmount;
10490
+ let swapInputAmount = 0n;
10490
10491
  let nextSqrtPrice = fusionPool.sqrtPrice;
10491
10492
  if (positionToken != collateralToken) {
10492
10493
  const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
10493
10494
  collateral = leverage > 1 ? BigInt(Math.floor(Number(increaseAmount) / leverage)) : increaseAmount;
10494
10495
  borrow = increaseAmount - collateral;
10496
+ swapInputAmount = increaseAmount;
10495
10497
  estimatedAmount = BigInt(
10496
10498
  Math.floor(collateralToken == 0 /* A */ ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10497
10499
  );
10498
- nextSqrtPrice = swapQuoteByInputToken(
10499
- increaseAmount,
10500
- collateralToken == 0 /* A */,
10501
- 0,
10502
- fusionPool,
10503
- tickArrays
10504
- ).nextSqrtPrice;
10505
10500
  } else {
10506
10501
  if (leverage > 1) {
10507
10502
  const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
@@ -10511,20 +10506,17 @@ function getIncreaseSpotPositionQuote(args) {
10511
10506
  collateralToken == 0 /* A */ ? Number(increaseAmount - collateral) * price : Number(increaseAmount - collateral) / price
10512
10507
  )
10513
10508
  );
10509
+ swapInputAmount = borrow;
10514
10510
  estimatedAmount = increaseAmount;
10515
- nextSqrtPrice = swapQuoteByOutputToken(
10516
- increaseAmount - collateral,
10517
- positionToken == 0 /* A */,
10518
- 0,
10519
- fusionPool,
10520
- tickArrays
10521
- ).nextSqrtPrice;
10522
10511
  } else {
10523
10512
  collateral = increaseAmount;
10524
- borrow = 0n;
10525
10513
  estimatedAmount = increaseAmount;
10526
10514
  }
10527
10515
  }
10516
+ if (swapInputAmount > 0) {
10517
+ const is_token_a = positionToken == 1 /* B */;
10518
+ nextSqrtPrice = swapQuoteByInputToken(swapInputAmount, is_token_a, 0, fusionPool, tickArrays).nextSqrtPrice;
10519
+ }
10528
10520
  const collateralExcludingFee = collateral;
10529
10521
  const borrowExcludingFee = borrow;
10530
10522
  collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
@@ -10537,6 +10529,7 @@ function getIncreaseSpotPositionQuote(args) {
10537
10529
  return {
10538
10530
  collateral,
10539
10531
  borrow,
10532
+ swapInputAmount,
10540
10533
  estimatedAmount,
10541
10534
  protocolFeeA,
10542
10535
  protocolFeeB,
@@ -10571,6 +10564,7 @@ function getDecreaseSpotPositionQuote(args) {
10571
10564
  }
10572
10565
  let collateral = 0n;
10573
10566
  let borrow = 0n;
10567
+ let swapInputAmount = 0n;
10574
10568
  let estimatedAmount = 0n;
10575
10569
  let nextSqrtPrice = fusionPool.sqrtPrice;
10576
10570
  let newPositionToken = positionToken;
@@ -10589,31 +10583,41 @@ function getDecreaseSpotPositionQuote(args) {
10589
10583
  Math.floor(Number(decreaseAmountInPositionToken) * HUNDRED_PERCENT / Number(positionAmount)),
10590
10584
  HUNDRED_PERCENT
10591
10585
  );
10592
- const swapOut = BigInt(Math.floor(Number(positionDebt) * decreasePercent / HUNDRED_PERCENT));
10593
- const swapQuote = swapQuoteByOutputToken(swapOut, positionToken == 1 /* B */, 0, fusionPool, tickArrays);
10594
- nextSqrtPrice = swapQuote.nextSqrtPrice;
10586
+ if (collateralToken == positionToken) {
10587
+ if (positionDebt > 0) {
10588
+ const swapOut = BigInt(Math.floor(Number(positionDebt) * decreasePercent / HUNDRED_PERCENT));
10589
+ const swapQuote = swapQuoteByOutputToken(swapOut, positionToken == 1 /* B */, 0, fusionPool, tickArrays);
10590
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10591
+ swapInputAmount = swapQuote.tokenEstIn;
10592
+ }
10593
+ } else {
10594
+ swapInputAmount = positionAmount - BigInt(Math.floor(Number(positionAmount) * (HUNDRED_PERCENT - decreasePercent) / HUNDRED_PERCENT));
10595
+ nextSqrtPrice = swapQuoteByInputToken(
10596
+ swapInputAmount,
10597
+ positionToken == 0 /* A */,
10598
+ 0,
10599
+ fusionPool,
10600
+ tickArrays
10601
+ ).nextSqrtPrice;
10602
+ }
10595
10603
  estimatedAmount = decreaseAmountInPositionToken <= positionAmount ? positionAmount - decreaseAmountInPositionToken : 0n;
10596
10604
  } else {
10597
10605
  decreasePercent = HUNDRED_PERCENT;
10598
- const positionTokenIsA = positionToken == 0 /* A */;
10606
+ const posTokenIsA = positionToken == 0 /* A */;
10599
10607
  newPositionToken = positionToken == 0 /* A */ ? 1 /* B */ : 0 /* A */;
10600
10608
  const increaseAmount = decreaseAmount - positionAmount;
10601
10609
  if (positionToken == collateralToken) {
10602
10610
  estimatedAmount = BigInt(
10603
- Math.floor(positionTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10611
+ Math.floor(posTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10604
10612
  );
10605
10613
  collateral = leverage > 1 ? BigInt(Math.floor(Number(increaseAmount) / leverage)) : estimatedAmount;
10606
10614
  borrow = increaseAmount - collateral;
10607
- const borrowInNewPositionToken = BigInt(
10608
- Math.floor(positionTokenIsA ? Number(borrow) * price : Number(borrow) / price)
10609
- );
10610
- nextSqrtPrice = swapQuoteByInputToken(
10611
- positionDebt + borrowInNewPositionToken,
10612
- !positionTokenIsA,
10613
- 0,
10614
- fusionPool,
10615
- tickArrays
10616
- ).nextSqrtPrice;
10615
+ if (positionDebt > 0) {
10616
+ const swapQuote = swapQuoteByOutputToken(positionDebt, !posTokenIsA, 0, fusionPool, tickArrays);
10617
+ swapInputAmount = swapQuote.tokenEstIn;
10618
+ }
10619
+ swapInputAmount += increaseAmount;
10620
+ nextSqrtPrice = swapQuoteByInputToken(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10617
10621
  } else {
10618
10622
  if (leverage > 1) {
10619
10623
  const collateralInPositionToken = BigInt(Math.floor(Number(increaseAmount) / leverage));
@@ -10627,13 +10631,8 @@ function getDecreaseSpotPositionQuote(args) {
10627
10631
  Math.floor(newPositionToken == 1 /* B */ ? Number(borrow) * price : Number(borrow) / price)
10628
10632
  );
10629
10633
  estimatedAmount = collateral + borrowInNewPositionToken;
10630
- nextSqrtPrice = swapQuoteByInputToken(
10631
- positionAmount + borrow,
10632
- positionTokenIsA,
10633
- 0,
10634
- fusionPool,
10635
- tickArrays
10636
- ).nextSqrtPrice;
10634
+ swapInputAmount = positionAmount + borrow;
10635
+ nextSqrtPrice = swapQuoteByInputToken(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10637
10636
  } else {
10638
10637
  collateral = increaseAmount;
10639
10638
  borrow = 0n;
@@ -10653,8 +10652,9 @@ function getDecreaseSpotPositionQuote(args) {
10653
10652
  collateralToken,
10654
10653
  positionToken: newPositionToken,
10655
10654
  collateral,
10656
- estimatedAmount,
10657
10655
  borrow,
10656
+ swapInputAmount,
10657
+ estimatedAmount,
10658
10658
  protocolFeeA,
10659
10659
  protocolFeeB,
10660
10660
  priceImpact
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.6",
4
+ "version": "3.1.7",
5
5
  "private": false,
6
6
  "license": "SEE LICENSE IN LICENSE",
7
7
  "main": "./dist/index.js",