@crypticdot/defituna-client 3.1.6 → 3.1.8

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,49 +10395,58 @@ 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;
10408
10402
  let decreasePercent;
10409
10403
  const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
10410
- if (decreaseAmount <= positionAmount || reduceOnly) {
10411
- let decreaseAmountInPositionToken;
10412
- if (collateralToken == positionToken) {
10413
- decreaseAmountInPositionToken = decreaseAmount;
10414
- } else {
10415
- decreaseAmountInPositionToken = BigInt(
10416
- Math.floor(collateralToken == 0 /* A */ ? Number(decreaseAmount) * price : Number(decreaseAmount) / price)
10417
- );
10418
- }
10404
+ let decreaseAmountInPositionToken = collateralToken == positionToken ? decreaseAmount : BigInt(
10405
+ Math.floor(collateralToken == 0 /* A */ ? Number(decreaseAmount) * price : Number(decreaseAmount) / price)
10406
+ );
10407
+ if (reduceOnly && decreaseAmountInPositionToken > positionAmount) {
10408
+ decreaseAmountInPositionToken = positionAmount;
10409
+ }
10410
+ if (decreaseAmountInPositionToken <= positionAmount) {
10419
10411
  decreasePercent = Math.min(
10420
10412
  Math.floor(Number(decreaseAmountInPositionToken) * HUNDRED_PERCENT / Number(positionAmount)),
10421
10413
  HUNDRED_PERCENT
10422
10414
  );
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;
10426
- estimatedAmount = decreaseAmountInPositionToken <= positionAmount ? positionAmount - decreaseAmountInPositionToken : 0n;
10415
+ estimatedAmount = positionAmount - decreaseAmountInPositionToken;
10416
+ if (collateralToken == positionToken) {
10417
+ if (positionDebt > 0) {
10418
+ const swapOut = BigInt(Math.floor(Number(positionDebt) * decreasePercent / HUNDRED_PERCENT));
10419
+ const swapQuote = (0, import_fusionamm_core4.swapQuoteByOutputToken)(swapOut, positionToken == 1 /* B */, 0, fusionPool, tickArrays);
10420
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10421
+ swapInputAmount = swapQuote.tokenEstIn;
10422
+ }
10423
+ } else {
10424
+ swapInputAmount = positionAmount - BigInt(Math.floor(Number(positionAmount) * (HUNDRED_PERCENT - decreasePercent) / HUNDRED_PERCENT));
10425
+ nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(
10426
+ swapInputAmount,
10427
+ positionToken == 0 /* A */,
10428
+ 0,
10429
+ fusionPool,
10430
+ tickArrays
10431
+ ).nextSqrtPrice;
10432
+ }
10427
10433
  } else {
10428
10434
  decreasePercent = HUNDRED_PERCENT;
10429
- const positionTokenIsA = positionToken == 0 /* A */;
10435
+ const posTokenIsA = positionToken == 0 /* A */;
10430
10436
  newPositionToken = positionToken == 0 /* A */ ? 1 /* B */ : 0 /* A */;
10431
- const increaseAmount = decreaseAmount - positionAmount;
10437
+ const increaseAmount = decreaseAmountInPositionToken - positionAmount;
10432
10438
  if (positionToken == collateralToken) {
10433
10439
  estimatedAmount = BigInt(
10434
- Math.floor(positionTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10440
+ Math.floor(posTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10435
10441
  );
10436
10442
  collateral = leverage > 1 ? BigInt(Math.floor(Number(increaseAmount) / leverage)) : estimatedAmount;
10437
10443
  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;
10444
+ if (positionDebt > 0) {
10445
+ const swapQuote = (0, import_fusionamm_core4.swapQuoteByOutputToken)(positionDebt, !posTokenIsA, 0, fusionPool, tickArrays);
10446
+ swapInputAmount = swapQuote.tokenEstIn;
10447
+ }
10448
+ swapInputAmount += increaseAmount;
10449
+ nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10448
10450
  } else {
10449
10451
  if (leverage > 1) {
10450
10452
  const collateralInPositionToken = BigInt(Math.floor(Number(increaseAmount) / leverage));
@@ -10458,13 +10460,8 @@ function getDecreaseSpotPositionQuote(args) {
10458
10460
  Math.floor(newPositionToken == 1 /* B */ ? Number(borrow) * price : Number(borrow) / price)
10459
10461
  );
10460
10462
  estimatedAmount = collateral + borrowInNewPositionToken;
10461
- nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(
10462
- positionAmount + borrow,
10463
- positionTokenIsA,
10464
- 0,
10465
- fusionPool,
10466
- tickArrays
10467
- ).nextSqrtPrice;
10463
+ swapInputAmount = positionAmount + borrow;
10464
+ nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10468
10465
  } else {
10469
10466
  collateral = increaseAmount;
10470
10467
  borrow = 0n;
@@ -10484,8 +10481,9 @@ function getDecreaseSpotPositionQuote(args) {
10484
10481
  collateralToken,
10485
10482
  positionToken: newPositionToken,
10486
10483
  collateral,
10487
- estimatedAmount,
10488
10484
  borrow,
10485
+ swapInputAmount,
10486
+ estimatedAmount,
10489
10487
  protocolFeeA,
10490
10488
  protocolFeeB,
10491
10489
  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,49 +10564,58 @@ 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;
10577
10571
  let decreasePercent;
10578
10572
  const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
10579
- if (decreaseAmount <= positionAmount || reduceOnly) {
10580
- let decreaseAmountInPositionToken;
10581
- if (collateralToken == positionToken) {
10582
- decreaseAmountInPositionToken = decreaseAmount;
10583
- } else {
10584
- decreaseAmountInPositionToken = BigInt(
10585
- Math.floor(collateralToken == 0 /* A */ ? Number(decreaseAmount) * price : Number(decreaseAmount) / price)
10586
- );
10587
- }
10573
+ let decreaseAmountInPositionToken = collateralToken == positionToken ? decreaseAmount : BigInt(
10574
+ Math.floor(collateralToken == 0 /* A */ ? Number(decreaseAmount) * price : Number(decreaseAmount) / price)
10575
+ );
10576
+ if (reduceOnly && decreaseAmountInPositionToken > positionAmount) {
10577
+ decreaseAmountInPositionToken = positionAmount;
10578
+ }
10579
+ if (decreaseAmountInPositionToken <= positionAmount) {
10588
10580
  decreasePercent = Math.min(
10589
10581
  Math.floor(Number(decreaseAmountInPositionToken) * HUNDRED_PERCENT / Number(positionAmount)),
10590
10582
  HUNDRED_PERCENT
10591
10583
  );
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;
10595
- estimatedAmount = decreaseAmountInPositionToken <= positionAmount ? positionAmount - decreaseAmountInPositionToken : 0n;
10584
+ estimatedAmount = positionAmount - decreaseAmountInPositionToken;
10585
+ if (collateralToken == positionToken) {
10586
+ if (positionDebt > 0) {
10587
+ const swapOut = BigInt(Math.floor(Number(positionDebt) * decreasePercent / HUNDRED_PERCENT));
10588
+ const swapQuote = swapQuoteByOutputToken(swapOut, positionToken == 1 /* B */, 0, fusionPool, tickArrays);
10589
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10590
+ swapInputAmount = swapQuote.tokenEstIn;
10591
+ }
10592
+ } else {
10593
+ 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;
10601
+ }
10596
10602
  } else {
10597
10603
  decreasePercent = HUNDRED_PERCENT;
10598
- const positionTokenIsA = positionToken == 0 /* A */;
10604
+ const posTokenIsA = positionToken == 0 /* A */;
10599
10605
  newPositionToken = positionToken == 0 /* A */ ? 1 /* B */ : 0 /* A */;
10600
- const increaseAmount = decreaseAmount - positionAmount;
10606
+ const increaseAmount = decreaseAmountInPositionToken - positionAmount;
10601
10607
  if (positionToken == collateralToken) {
10602
10608
  estimatedAmount = BigInt(
10603
- Math.floor(positionTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10609
+ Math.floor(posTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10604
10610
  );
10605
10611
  collateral = leverage > 1 ? BigInt(Math.floor(Number(increaseAmount) / leverage)) : estimatedAmount;
10606
10612
  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;
10613
+ if (positionDebt > 0) {
10614
+ const swapQuote = swapQuoteByOutputToken(positionDebt, !posTokenIsA, 0, fusionPool, tickArrays);
10615
+ swapInputAmount = swapQuote.tokenEstIn;
10616
+ }
10617
+ swapInputAmount += increaseAmount;
10618
+ nextSqrtPrice = swapQuoteByInputToken(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10617
10619
  } else {
10618
10620
  if (leverage > 1) {
10619
10621
  const collateralInPositionToken = BigInt(Math.floor(Number(increaseAmount) / leverage));
@@ -10627,13 +10629,8 @@ function getDecreaseSpotPositionQuote(args) {
10627
10629
  Math.floor(newPositionToken == 1 /* B */ ? Number(borrow) * price : Number(borrow) / price)
10628
10630
  );
10629
10631
  estimatedAmount = collateral + borrowInNewPositionToken;
10630
- nextSqrtPrice = swapQuoteByInputToken(
10631
- positionAmount + borrow,
10632
- positionTokenIsA,
10633
- 0,
10634
- fusionPool,
10635
- tickArrays
10636
- ).nextSqrtPrice;
10632
+ swapInputAmount = positionAmount + borrow;
10633
+ nextSqrtPrice = swapQuoteByInputToken(swapInputAmount, posTokenIsA, 0, fusionPool, tickArrays).nextSqrtPrice;
10637
10634
  } else {
10638
10635
  collateral = increaseAmount;
10639
10636
  borrow = 0n;
@@ -10653,8 +10650,9 @@ function getDecreaseSpotPositionQuote(args) {
10653
10650
  collateralToken,
10654
10651
  positionToken: newPositionToken,
10655
10652
  collateral,
10656
- estimatedAmount,
10657
10653
  borrow,
10654
+ swapInputAmount,
10655
+ estimatedAmount,
10658
10656
  protocolFeeA,
10659
10657
  protocolFeeB,
10660
10658
  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.8",
5
5
  "private": false,
6
6
  "license": "SEE LICENSE IN LICENSE",
7
7
  "main": "./dist/index.js",