@crypticdot/defituna-client 3.1.19 → 3.2.0

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.mjs CHANGED
@@ -10418,273 +10418,6 @@ function calculateProtocolFee(collateralAmount, borrowAmount, protocolFeeRateOnC
10418
10418
  return collateralAmount * BigInt(protocolFeeRateOnCollateral) / BigInt(HUNDRED_PERCENT) + borrowAmount * BigInt(protocolFeeRate) / BigInt(HUNDRED_PERCENT);
10419
10419
  }
10420
10420
 
10421
- // src/utils/spotPositionMath.ts
10422
- import {
10423
- sqrtPriceToPrice,
10424
- swapQuoteByInputToken,
10425
- swapQuoteByOutputToken
10426
- } from "@crypticdot/fusionamm-core";
10427
- function getIncreaseSpotPositionQuote(args) {
10428
- const {
10429
- fusionPool,
10430
- tickArrays,
10431
- leverage,
10432
- protocolFeeRate,
10433
- protocolFeeRateOnCollateral,
10434
- increaseAmount,
10435
- collateralToken,
10436
- positionToken
10437
- } = args;
10438
- if (leverage < 1) {
10439
- throw new Error("leverage must be greater or equal than 1.0");
10440
- }
10441
- if (protocolFeeRate < 0 || protocolFeeRate >= HUNDRED_PERCENT) {
10442
- throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
10443
- }
10444
- if (protocolFeeRateOnCollateral < 0 || protocolFeeRateOnCollateral >= HUNDRED_PERCENT) {
10445
- throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
10446
- }
10447
- if (increaseAmount <= 0) {
10448
- throw new Error("increaseAmount must be greater than zero");
10449
- }
10450
- let borrow;
10451
- let collateral;
10452
- let estimatedAmount;
10453
- let swapInputAmount;
10454
- let nextSqrtPrice = fusionPool.sqrtPrice;
10455
- if (positionToken != collateralToken) {
10456
- const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
10457
- borrow = BigInt(Math.ceil(Number(increaseAmount) * (leverage - 1) / leverage));
10458
- collateral = increaseAmount - applySwapFee(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10459
- collateral = reverseApplySwapFee(collateral, fusionPool.feeRate, false);
10460
- collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
10461
- swapInputAmount = increaseAmount;
10462
- estimatedAmount = BigInt(
10463
- Math.round(collateralToken == 0 /* A */ ? Number(increaseAmount) * price : Number(increaseAmount) / price)
10464
- );
10465
- } else {
10466
- const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
10467
- const positionToBorrowedTokenPrice = collateralToken == 0 /* A */ ? price : 1 / price;
10468
- const borrowInPositionToken = Math.ceil(Number(increaseAmount) * (leverage - 1) / leverage);
10469
- borrow = BigInt(Math.ceil(borrowInPositionToken * positionToBorrowedTokenPrice));
10470
- const borrowInPositionTokenWithFeesApplied = applySwapFee(
10471
- applyTunaProtocolFee(BigInt(borrowInPositionToken), protocolFeeRate),
10472
- fusionPool.feeRate
10473
- );
10474
- collateral = increaseAmount - borrowInPositionTokenWithFeesApplied;
10475
- collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
10476
- swapInputAmount = applyTunaProtocolFee(borrow, protocolFeeRate);
10477
- estimatedAmount = increaseAmount;
10478
- }
10479
- if (swapInputAmount > 0) {
10480
- const is_token_a = positionToken == 1 /* B */;
10481
- nextSqrtPrice = swapQuoteByInputToken(swapInputAmount, is_token_a, 0, fusionPool, tickArrays).nextSqrtPrice;
10482
- }
10483
- const protocolFeeA = (collateralToken == 0 /* A */ ? collateral - applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral) : 0n) + (positionToken == 1 /* B */ ? borrow - applyTunaProtocolFee(borrow, protocolFeeRate) : 0n);
10484
- const protocolFeeB = (collateralToken == 1 /* B */ ? collateral - applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral) : 0n) + (positionToken == 0 /* A */ ? borrow - applyTunaProtocolFee(borrow, protocolFeeRate) : 0n);
10485
- const oldPrice = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
10486
- const newPrice = sqrtPriceToPrice(nextSqrtPrice, 1, 1);
10487
- const priceImpact = Math.abs(newPrice / oldPrice - 1) * 100;
10488
- return {
10489
- collateral,
10490
- borrow,
10491
- swapInputAmount,
10492
- estimatedAmount,
10493
- protocolFeeA,
10494
- protocolFeeB,
10495
- priceImpact
10496
- };
10497
- }
10498
- function getDecreaseSpotPositionQuote(args) {
10499
- const {
10500
- fusionPool,
10501
- tickArrays,
10502
- leverage,
10503
- protocolFeeRate,
10504
- protocolFeeRateOnCollateral,
10505
- decreaseAmount,
10506
- collateralToken,
10507
- positionToken,
10508
- positionAmount,
10509
- positionDebt,
10510
- reduceOnly
10511
- } = args;
10512
- if (leverage < 1) {
10513
- throw new Error("leverage must be greater or equal than 1.0");
10514
- }
10515
- if (protocolFeeRate < 0 || protocolFeeRate >= HUNDRED_PERCENT) {
10516
- throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
10517
- }
10518
- if (protocolFeeRateOnCollateral < 0 || protocolFeeRateOnCollateral >= HUNDRED_PERCENT) {
10519
- throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
10520
- }
10521
- if (decreaseAmount <= 0) {
10522
- throw new Error("decreaseAmount must be greater than zero");
10523
- }
10524
- let collateral = 0n;
10525
- let borrow = 0n;
10526
- let swapInputAmount = 0n;
10527
- let estimatedAmount = 0n;
10528
- let nextSqrtPrice = fusionPool.sqrtPrice;
10529
- let newPositionToken = positionToken;
10530
- let decreasePercent;
10531
- const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
10532
- const positionToOppositeTokenPrice = positionToken == 0 /* A */ ? price : 1 / price;
10533
- let decreaseAmountInPositionToken = collateralToken == positionToken ? decreaseAmount : BigInt(Math.round(Number(decreaseAmount) / positionToOppositeTokenPrice));
10534
- if (reduceOnly && decreaseAmountInPositionToken > positionAmount) {
10535
- decreaseAmountInPositionToken = positionAmount;
10536
- }
10537
- if (decreaseAmountInPositionToken <= positionAmount) {
10538
- decreasePercent = Math.min(
10539
- Math.floor(Number(decreaseAmountInPositionToken) * HUNDRED_PERCENT / Number(positionAmount)),
10540
- HUNDRED_PERCENT
10541
- );
10542
- estimatedAmount = positionAmount - decreaseAmountInPositionToken;
10543
- if (collateralToken == positionToken) {
10544
- if (positionDebt > 0) {
10545
- const swapOut = BigInt(Math.floor(Number(positionDebt) * decreasePercent / HUNDRED_PERCENT));
10546
- const swapQuote = swapQuoteByOutputToken(swapOut, positionToken == 1 /* B */, 0, fusionPool, tickArrays);
10547
- nextSqrtPrice = swapQuote.nextSqrtPrice;
10548
- swapInputAmount = swapQuote.tokenEstIn;
10549
- }
10550
- } else {
10551
- swapInputAmount = positionAmount - BigInt(Math.floor(Number(positionAmount) * (HUNDRED_PERCENT - decreasePercent) / HUNDRED_PERCENT));
10552
- const swapQuote = swapQuoteByInputToken(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10553
- nextSqrtPrice = swapQuote.nextSqrtPrice;
10554
- }
10555
- } else {
10556
- decreasePercent = HUNDRED_PERCENT;
10557
- newPositionToken = positionToken == 0 /* A */ ? 1 /* B */ : 0 /* A */;
10558
- const increaseAmount = decreaseAmountInPositionToken - positionAmount;
10559
- if (positionToken == collateralToken) {
10560
- estimatedAmount = BigInt(Math.round(Number(increaseAmount) * positionToOppositeTokenPrice));
10561
- borrow = BigInt(Math.round(Number(increaseAmount) * (leverage - 1) / leverage));
10562
- const borrowWithFeesApplied = applySwapFee(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10563
- collateral = increaseAmount - borrowWithFeesApplied;
10564
- if (positionDebt > 0) {
10565
- const swapQuote2 = swapQuoteByOutputToken(positionDebt, positionToken != 0 /* A */, 0, fusionPool, tickArrays);
10566
- swapInputAmount = swapQuote2.tokenEstIn;
10567
- }
10568
- swapInputAmount += collateral + applyTunaProtocolFee(borrow, protocolFeeRate);
10569
- const swapQuote = swapQuoteByInputToken(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10570
- nextSqrtPrice = swapQuote.nextSqrtPrice;
10571
- collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
10572
- } else {
10573
- estimatedAmount = BigInt(Math.round(Number(increaseAmount) * positionToOppositeTokenPrice));
10574
- borrow = BigInt(Math.round(Number(increaseAmount) * (leverage - 1) / leverage));
10575
- const borrowWithFeesApplied = applySwapFee(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
10576
- collateral = increaseAmount - borrowWithFeesApplied;
10577
- collateral = BigInt(Math.round(Number(collateral) * positionToOppositeTokenPrice));
10578
- collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
10579
- swapInputAmount = positionAmount + applyTunaProtocolFee(borrow, protocolFeeRate);
10580
- const swapQuote = swapQuoteByInputToken(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
10581
- nextSqrtPrice = swapQuote.nextSqrtPrice;
10582
- }
10583
- }
10584
- const protocolFeeA = (collateralToken == 0 /* A */ ? collateral - applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral) : 0n) + (positionToken == 1 /* B */ ? borrow - applyTunaProtocolFee(borrow, protocolFeeRate) : 0n);
10585
- const protocolFeeB = (collateralToken == 1 /* B */ ? collateral - applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral) : 0n) + (positionToken == 0 /* A */ ? borrow - applyTunaProtocolFee(borrow, protocolFeeRate) : 0n);
10586
- const newPrice = sqrtPriceToPrice(nextSqrtPrice, 1, 1);
10587
- const priceImpact = Math.abs(newPrice / price - 1) * 100;
10588
- return {
10589
- decreasePercent,
10590
- collateralToken,
10591
- positionToken: newPositionToken,
10592
- collateral,
10593
- borrow,
10594
- swapInputAmount,
10595
- estimatedAmount,
10596
- protocolFeeA,
10597
- protocolFeeB,
10598
- priceImpact
10599
- };
10600
- }
10601
- function getTradableAmount(args) {
10602
- const {
10603
- collateralToken,
10604
- newPositionToken,
10605
- positionToken,
10606
- positionAmount,
10607
- positionDebt,
10608
- reduceOnly,
10609
- fusionPool,
10610
- tickArrays,
10611
- leverage,
10612
- availableBalance,
10613
- protocolFeeRate,
10614
- protocolFeeRateOnCollateral
10615
- } = args;
10616
- if (leverage < 1) {
10617
- throw new Error("leverage must be greater or equal than 1.0");
10618
- }
10619
- if (protocolFeeRateOnCollateral < 0 || protocolFeeRateOnCollateral >= HUNDRED_PERCENT) {
10620
- throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
10621
- }
10622
- if (protocolFeeRate < 0 || protocolFeeRate >= HUNDRED_PERCENT) {
10623
- throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
10624
- }
10625
- if (positionAmount == 0n && newPositionToken != positionToken) {
10626
- throw new Error("positionToken must be set to newPositionToken if positionAmount is zero");
10627
- }
10628
- let availableToTrade = 0n;
10629
- const addLeverage = (collateral) => {
10630
- collateral = applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
10631
- if (collateralToken != newPositionToken) {
10632
- collateral = applySwapFee(collateral, fusionPool.feeRate);
10633
- }
10634
- const feeMultiplier = (1 - protocolFeeRate / HUNDRED_PERCENT) * (1 - fusionPool.feeRate / 1e6);
10635
- const total = Math.floor(Number(collateral) / (1 - feeMultiplier * (leverage - 1) / leverage));
10636
- return BigInt(total);
10637
- };
10638
- if (newPositionToken == positionToken) {
10639
- availableToTrade = addLeverage(availableBalance);
10640
- } else {
10641
- const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
10642
- const positionToOppositeTokenPrice = positionToken == 0 /* A */ ? price : 1 / price;
10643
- if (reduceOnly) {
10644
- if (collateralToken == positionToken) {
10645
- availableToTrade = positionAmount;
10646
- } else {
10647
- availableToTrade = BigInt(Math.round(Number(positionAmount) * positionToOppositeTokenPrice));
10648
- }
10649
- } else {
10650
- const positionAmountInCollateralToken = collateralToken == positionToken ? positionAmount : BigInt(Math.round(Number(positionAmount) * positionToOppositeTokenPrice));
10651
- let positionCollateral = collateralToken == positionToken ? positionAmount - (positionDebt > 0n ? swapQuoteByOutputToken(positionDebt, positionToken == 1 /* B */, 0, fusionPool, tickArrays).tokenEstIn : 0n) : positionAmount > 0n ? swapQuoteByInputToken(positionAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays).tokenEstOut - positionDebt : 0n;
10652
- if (positionCollateral < 0n) positionCollateral = 0n;
10653
- availableToTrade = positionAmountInCollateralToken + addLeverage(availableBalance + positionCollateral);
10654
- }
10655
- }
10656
- return availableToTrade;
10657
- }
10658
- function getLiquidationPrice(positionToken, amount, debt, liquidationThreshold) {
10659
- if (debt < 0) {
10660
- throw new Error("debt must be greater or equal than zero");
10661
- }
10662
- if (amount < 0) {
10663
- throw new Error("position amount must be greater or equal than zero");
10664
- }
10665
- if (liquidationThreshold <= 0 || liquidationThreshold >= 1) {
10666
- throw new Error("liquidationThreshold must be greater than zero and less than one");
10667
- }
10668
- if (debt == 0 || amount == 0) return 0;
10669
- if (positionToken == 0 /* A */) {
10670
- return debt / (amount * liquidationThreshold);
10671
- } else {
10672
- return amount * liquidationThreshold / debt;
10673
- }
10674
- }
10675
- function applyTunaProtocolFee(amount, protocolFeeRate, roundUp = false) {
10676
- return mulDiv(amount, BigInt(HUNDRED_PERCENT - protocolFeeRate), BigInt(HUNDRED_PERCENT), roundUp);
10677
- }
10678
- function reverseApplyTunaProtocolFee(amount, protocolFeeRate, roundUp = true) {
10679
- return mulDiv(amount, BigInt(HUNDRED_PERCENT), BigInt(HUNDRED_PERCENT - protocolFeeRate), roundUp);
10680
- }
10681
- function applySwapFee(amount, feeRate, roundUp = false) {
10682
- return mulDiv(amount, BigInt(1e6 - feeRate), 1000000n, roundUp);
10683
- }
10684
- function reverseApplySwapFee(amount, feeRate, roundUp = true) {
10685
- return mulDiv(amount, 1000000n, BigInt(1e6 - feeRate), roundUp);
10686
- }
10687
-
10688
10421
  // src/txbuilder/increaseTunaLpPositionOrca.ts
10689
10422
  import {
10690
10423
  fetchMaybeWhirlpool,
@@ -15836,6 +15569,24 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
15836
15569
  ix.accounts.push(...remainingAccounts);
15837
15570
  return ix;
15838
15571
  }
15572
+
15573
+ // src/casts.ts
15574
+ function toPoolTokenFacade(token) {
15575
+ switch (token) {
15576
+ case 0 /* A */:
15577
+ return "a";
15578
+ case 1 /* B */:
15579
+ return "b";
15580
+ }
15581
+ }
15582
+ function fromPoolTokenFacade(token) {
15583
+ switch (token) {
15584
+ case "a":
15585
+ return 0 /* A */;
15586
+ case "b":
15587
+ return 1 /* B */;
15588
+ }
15589
+ }
15839
15590
  export {
15840
15591
  AccountsType,
15841
15592
  CLOSE_ACTIVE_TUNA_SPOT_POSITION_FUSION_DISCRIMINATOR,
@@ -16063,6 +15814,7 @@ export {
16063
15814
  fetchTunaLpPosition,
16064
15815
  fetchTunaSpotPosition,
16065
15816
  fetchVault,
15817
+ fromPoolTokenFacade,
16066
15818
  getAccountsTypeCodec,
16067
15819
  getAccountsTypeDecoder,
16068
15820
  getAccountsTypeEncoder,
@@ -16133,7 +15885,6 @@ export {
16133
15885
  getCreateVaultInstructionDataCodec,
16134
15886
  getCreateVaultInstructionDataDecoder,
16135
15887
  getCreateVaultInstructionDataEncoder,
16136
- getDecreaseSpotPositionQuote,
16137
15888
  getDecreaseTunaLpPositionFusionDiscriminatorBytes,
16138
15889
  getDecreaseTunaLpPositionFusionInstruction,
16139
15890
  getDecreaseTunaLpPositionFusionInstructionDataCodec,
@@ -16160,7 +15911,6 @@ export {
16160
15911
  getDepositInstructionDataDecoder,
16161
15912
  getDepositInstructionDataEncoder,
16162
15913
  getIncreaseLpPositionQuote,
16163
- getIncreaseSpotPositionQuote,
16164
15914
  getIncreaseTunaLpPositionFusionDiscriminatorBytes,
16165
15915
  getIncreaseTunaLpPositionFusionInstruction,
16166
15916
  getIncreaseTunaLpPositionFusionInstructionDataCodec,
@@ -16208,7 +15958,6 @@ export {
16208
15958
  getLiquidateTunaSpotPositionOrcaInstructionDataCodec,
16209
15959
  getLiquidateTunaSpotPositionOrcaInstructionDataDecoder,
16210
15960
  getLiquidateTunaSpotPositionOrcaInstructionDataEncoder,
16211
- getLiquidationPrice,
16212
15961
  getMarketAddress,
16213
15962
  getMarketCodec,
16214
15963
  getMarketDecoder,
@@ -16358,7 +16107,6 @@ export {
16358
16107
  getSetTunaSpotPositionLimitOrdersInstructionDataCodec,
16359
16108
  getSetTunaSpotPositionLimitOrdersInstructionDataDecoder,
16360
16109
  getSetTunaSpotPositionLimitOrdersInstructionDataEncoder,
16361
- getTradableAmount,
16362
16110
  getTunaConfigAddress,
16363
16111
  getTunaConfigCodec,
16364
16112
  getTunaConfigDecoder,
@@ -16504,6 +16252,7 @@ export {
16504
16252
  resetTunaSpotPositionInstruction,
16505
16253
  setTunaLpPositionLimitOrdersInstruction,
16506
16254
  setTunaSpotPositionLimitOrdersInstruction,
16255
+ toPoolTokenFacade,
16507
16256
  tunaLpPositionAuthorityFilter,
16508
16257
  tunaLpPositionMarketMakerFilter,
16509
16258
  tunaLpPositionMintAFilter,
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.19",
4
+ "version": "3.2.0",
5
5
  "private": false,
6
6
  "license": "SEE LICENSE IN LICENSE",
7
7
  "main": "./dist/index.js",
@@ -29,7 +29,8 @@
29
29
  "@solana-program/token": "^0.5.1",
30
30
  "@solana-program/token-2022": "^0.4.2",
31
31
  "@solana-program/memo": "^0.7.0",
32
- "@solana-program/address-lookup-table": "^0.7.0"
32
+ "@solana-program/address-lookup-table": "^0.7.0",
33
+ "@crypticdot/defituna-core": "3.2.0"
33
34
  },
34
35
  "devDependencies": {
35
36
  "@crypticdot/typescript-config": "^1.0.0",