@exponent-labs/market-three-math 0.1.8 → 0.9.1

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.
Files changed (55) hide show
  1. package/build/addLiquidity.d.ts +65 -4
  2. package/build/addLiquidity.js +762 -36
  3. package/build/addLiquidity.js.map +1 -1
  4. package/build/bisect.d.ts +11 -0
  5. package/build/bisect.js +22 -12
  6. package/build/bisect.js.map +1 -1
  7. package/build/index.d.ts +5 -4
  8. package/build/index.js +14 -7
  9. package/build/index.js.map +1 -1
  10. package/build/liquidityHistogram.d.ts +6 -1
  11. package/build/liquidityHistogram.js +57 -12
  12. package/build/liquidityHistogram.js.map +1 -1
  13. package/build/quote.d.ts +1 -1
  14. package/build/quote.js +70 -84
  15. package/build/quote.js.map +1 -1
  16. package/build/swap.js +36 -18
  17. package/build/swap.js.map +1 -1
  18. package/build/swapV2.d.ts +6 -1
  19. package/build/swapV2.js +394 -52
  20. package/build/swapV2.js.map +1 -1
  21. package/build/types.d.ts +51 -0
  22. package/build/utils.d.ts +8 -2
  23. package/build/utils.js +37 -19
  24. package/build/utils.js.map +1 -1
  25. package/build/utilsV2.d.ts +9 -0
  26. package/build/utilsV2.js +131 -9
  27. package/build/utilsV2.js.map +1 -1
  28. package/build/withdrawLiquidity.js +12 -7
  29. package/build/withdrawLiquidity.js.map +1 -1
  30. package/build/ytTrades.d.ts +7 -0
  31. package/build/ytTrades.js +166 -146
  32. package/build/ytTrades.js.map +1 -1
  33. package/build/ytTradesLegacy.js +3 -4
  34. package/build/ytTradesLegacy.js.map +1 -1
  35. package/package.json +2 -2
  36. package/src/addLiquidity.ts +1012 -38
  37. package/src/bisect.ts +22 -11
  38. package/src/index.ts +11 -5
  39. package/src/liquidityHistogram.ts +54 -9
  40. package/src/quote.ts +73 -95
  41. package/src/swap.ts +35 -19
  42. package/src/swapV2.ts +999 -0
  43. package/src/types.ts +55 -0
  44. package/src/utils.ts +24 -3
  45. package/src/utilsV2.ts +337 -0
  46. package/src/withdrawLiquidity.ts +12 -6
  47. package/src/ytTrades.ts +191 -172
  48. package/src/ytTradesLegacy.ts +419 -0
  49. package/build/swap-v2.d.ts +0 -20
  50. package/build/swap-v2.js +0 -261
  51. package/build/swap-v2.js.map +0 -1
  52. package/build/swapLegacy.d.ts +0 -16
  53. package/build/swapLegacy.js +0 -229
  54. package/build/swapLegacy.js.map +0 -1
  55. package/src/swapLegacy.ts +0 -272
@@ -1,14 +1,43 @@
1
+ import { AddLiquidityArgs, AddLiquidityOutcome, CrossingScaleParams, CrossingTickState, LiquidityNeeds, MarketThreeState } from "./types";
2
+ import { EffSnap } from "./utilsV2";
3
+ import { Ticks } from "@exponent-labs/exponent-fetcher";
1
4
  /**
2
- * CLMM Add Liquidity simulation
3
- * Ported from exponent_clmm/src/state/market_three/helpers/add_liquidity.rs
5
+ * Get crossing tick state and price boundaries from Ticks.
6
+ * Matches wrapper_provide_liquidity.rs: current node's spot_price, principal_pt/sy/supply, successor's spot_price.
4
7
  */
5
- import { AddLiquidityArgs, AddLiquidityOutcome, LiquidityNeeds, MarketThreeState } from "./types";
6
- import { EffSnap } from "./utils";
8
+ export declare function getCrossingTickStateFromTicks(ticks: Ticks): {
9
+ crossingTickState: CrossingTickState;
10
+ crossingTickPriceLeft: number;
11
+ crossingTickPriceRight: number;
12
+ };
7
13
  /**
8
14
  * Compute liquidity target and token needs based on position range and budgets
9
15
  * Ported from compute_liquidity_target_and_token_needs in math.rs
10
16
  */
11
17
  export declare function computeLiquidityTargetAndTokenNeeds(snap: EffSnap, spotPriceCurrent: number, priceEffLower: number, priceEffUpper: number, lowerPrice: number, upperPrice: number, lowerTickIdx: number, upperTickIdx: number, currentIndex: number, maxSy: number, maxPt: number, epsilonClamp: number): LiquidityNeeds;
18
+ /**
19
+ * Scale crossing tick token inputs from original max values to prevent double trimming.
20
+ *
21
+ * When adding liquidity that spans the current price (crossing tick), tokens get trimmed twice:
22
+ * 1. First by CLMM formula in computeLiquidityTargetAndTokenNeeds
23
+ * 2. Then by AMM formula in simulateAddLiquidityProportional
24
+ *
25
+ * This function calculates the segment's proportion of the total distribution and scales
26
+ * the original max values accordingly, ensuring maximum token utilization.
27
+ *
28
+ * @param scaleParams - Original max values and CLMM distribution extents
29
+ * @param duPtPart - PT segment length (spot price delta for this segment)
30
+ * @param syPerL1 - SY per unit liquidity for this segment
31
+ * @param clmmPtDelta - PT amount calculated from CLMM formula
32
+ * @param clmmSyDelta - SY amount calculated from CLMM formula
33
+ * @returns Tuple of [scaledPtIn, scaledSyIn] - maximum of CLMM-calculated and scaled original values
34
+ */
35
+ export declare function scaleCrossingTickInputs(scaleParams: CrossingScaleParams, duPtPart: number, syPerL1: number, clmmPtDelta: number, clmmSyDelta: number): [number, number];
36
+ /**
37
+ * Compute token needs with crossing tick adjustment
38
+ * This matches the Rust compute_token_needs_with_crossing function
39
+ */
40
+ export declare function computeTokenNeedsWithCrossing(snap: EffSnap, spotPriceCurrent: number, priceEffLower: number, priceEffUpper: number, lowerPrice: number, upperPrice: number, maxSy: number, maxPt: number, epsilonClamp: number, crossingTickState: CrossingTickState, crossingTickPriceLeft: number, crossingTickPriceRight: number): [number, number];
12
41
  /**
13
42
  * Simulate adding liquidity to the CLMM market
14
43
  * This is a pure function that does not mutate the market state
@@ -36,6 +65,13 @@ export declare function calcDepositSyAndPtFromBaseAmount(params: {
36
65
  expirationTs: number;
37
66
  currentSpotPrice: number;
38
67
  syExchangeRate: number;
68
+ /** Optional crossing tick state for accurate ratio prediction.
69
+ * When provided, the prediction accounts for the existing PT/SY proportions
70
+ * in the active tick, matching the on-chain behaviour more closely. */
71
+ crossingTickState?: CrossingTickState;
72
+ crossingTickPriceLeft?: number;
73
+ crossingTickPriceRight?: number;
74
+ epsilonClamp?: number;
39
75
  }): {
40
76
  syNeeded: number;
41
77
  ptNeeded: number;
@@ -65,3 +101,28 @@ export declare function simulateWrapperProvideLiquidity(marketState: MarketThree
65
101
  ptDeposited: number;
66
102
  syDeposited: number;
67
103
  } | null;
104
+ /**
105
+ * Simulate swap & supply operation (ixProvideLiquidityBase)
106
+ * 1. Mints SY from base asset
107
+ * 2. Swaps some SY for PT on the market (instead of stripping)
108
+ * 3. Deposits remaining SY + bought PT into liquidity position
109
+ *
110
+ * This approach has lower slippage than minting because it uses market liquidity.
111
+ *
112
+ * @param marketState - Current market state
113
+ * @param amountBase - Amount of base tokens (in lamports)
114
+ * @param lowerTick - Lower tick key (APY in basis points)
115
+ * @param upperTick - Upper tick key (APY in basis points)
116
+ * @param syExchangeRate - SY exchange rate
117
+ * @returns Simulation result with LP out, PT to buy, SY constraint, etc.
118
+ */
119
+ export declare function simulateSwapAndSupply(marketState: MarketThreeState, amountBase: number, lowerTick: number, upperTick: number, syExchangeRate: number): {
120
+ lpOut: number;
121
+ ptToBuy: number;
122
+ syConstraint: number;
123
+ syForSwap: number;
124
+ syRemainder: number;
125
+ ptFromSwap: number;
126
+ syDeposited: number;
127
+ ptDeposited: number;
128
+ } | null;