@exponent-labs/market-three-math 0.9.15 → 0.9.17

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 (42) hide show
  1. package/build/addLiquidity.d.ts +11 -41
  2. package/build/addLiquidity.js +169 -201
  3. package/build/addLiquidity.js.map +1 -1
  4. package/build/bisect.js +1 -2
  5. package/build/bisect.js.map +1 -1
  6. package/build/existingPositionEqualization.d.ts +3 -3
  7. package/build/existingPositionEqualization.js +45 -66
  8. package/build/existingPositionEqualization.js.map +1 -1
  9. package/build/index.d.ts +2 -2
  10. package/build/index.js +1 -3
  11. package/build/index.js.map +1 -1
  12. package/build/liquidityHistogram.js +2 -3
  13. package/build/liquidityHistogram.js.map +1 -1
  14. package/build/quote.js +2 -2
  15. package/build/quote.js.map +1 -1
  16. package/build/swap.js +2 -2
  17. package/build/swap.js.map +1 -1
  18. package/build/swapV2.js +56 -20
  19. package/build/swapV2.js.map +1 -1
  20. package/build/types.d.ts +4 -21
  21. package/build/utils.js +17 -17
  22. package/build/utils.js.map +1 -1
  23. package/build/utilsV2.js +10 -7
  24. package/build/utilsV2.js.map +1 -1
  25. package/build/withdrawLiquidity.d.ts +1 -1
  26. package/build/withdrawLiquidity.js +120 -72
  27. package/build/withdrawLiquidity.js.map +1 -1
  28. package/build/ytTrades.js +3 -4
  29. package/build/ytTrades.js.map +1 -1
  30. package/build/ytTradesLegacy.js +3 -4
  31. package/build/ytTradesLegacy.js.map +1 -1
  32. package/package.json +2 -2
  33. package/src/addLiquidity.ts +203 -246
  34. package/src/existingPositionEqualization.test.ts +33 -0
  35. package/src/existingPositionEqualization.ts +52 -83
  36. package/src/index.ts +0 -4
  37. package/src/swap.ts +1 -0
  38. package/src/swapV2.ts +96 -18
  39. package/src/types.ts +4 -23
  40. package/src/utilsV2.ts +9 -4
  41. package/src/withdrawLiquidity.test.ts +189 -0
  42. package/src/withdrawLiquidity.ts +148 -89
@@ -2,46 +2,19 @@
2
2
  * CLMM Add Liquidity simulation
3
3
  * Ported from exponent_clmm/src/state/market_three/helpers/add_liquidity.rs
4
4
  */
5
- import { Ticks } from "@exponent-labs/exponent-fetcher";
6
- import { AddLiquidityArgs, AddLiquidityOutcome, CrossingScaleParams, CrossingTickState, LiquidityNeeds, MarketThreeState } from "./types";
5
+ import type { LpPositionCLMM } from "@exponent-labs/exponent-fetcher";
6
+ import { AddLiquidityArgs, AddLiquidityOutcome, LiquidityNeeds, MarketThreeState } from "./types";
7
7
  import { EffSnap } from "./utilsV2";
8
- /**
9
- * Get crossing tick state and price boundaries from Ticks.
10
- * Matches wrapper_provide_liquidity.rs: current node's spot_price, principal_pt/sy/supply, successor's spot_price.
11
- */
12
- export declare function getCrossingTickStateFromTicks(ticks: Ticks): {
13
- crossingTickState: CrossingTickState;
14
- crossingTickPriceLeft: number;
15
- crossingTickPriceRight: number;
8
+ type AddLiquiditySimulationOptions = {
9
+ existingPosition?: LpPositionCLMM;
16
10
  };
17
11
  /**
18
12
  * Compute liquidity target and token needs based on position range and budgets
19
13
  * Ported from compute_liquidity_target_and_token_needs in math.rs
20
14
  */
21
15
  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;
22
- /**
23
- * Scale crossing tick token inputs from original max values to prevent double trimming.
24
- *
25
- * When adding liquidity that spans the current price (crossing tick), tokens get trimmed twice:
26
- * 1. First by CLMM formula in computeLiquidityTargetAndTokenNeeds
27
- * 2. Then by AMM formula in simulateAddLiquidityProportional
28
- *
29
- * This function calculates the segment's proportion of the total distribution and scales
30
- * the original max values accordingly, ensuring maximum token utilization.
31
- *
32
- * @param scaleParams - Original max values and CLMM distribution extents
33
- * @param duPtPart - PT segment length (spot price delta for this segment)
34
- * @param syPerL1 - SY per unit liquidity for this segment
35
- * @param clmmPtDelta - PT amount calculated from CLMM formula
36
- * @param clmmSyDelta - SY amount calculated from CLMM formula
37
- * @returns Tuple of [scaledPtIn, scaledSyIn] - maximum of CLMM-calculated and scaled original values
38
- */
39
- export declare function scaleCrossingTickInputs(scaleParams: CrossingScaleParams, duPtPart: number, syPerL1: number, clmmPtDelta: number, clmmSyDelta: number): [number, number];
40
- /**
41
- * Compute token needs with crossing tick adjustment
42
- * This matches the Rust compute_token_needs_with_crossing function
43
- */
44
- 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];
16
+ /** Compute curve-level token needs without raw crossing-tick cap scaling. */
17
+ export declare function computeTokenNeedsWithCrossing(snap: EffSnap, spotPriceCurrent: number, priceEffLower: number, priceEffUpper: number, lowerPrice: number, upperPrice: number, maxSy: number, maxPt: number, epsilonClamp: number): [number, number];
45
18
  /**
46
19
  * Simulate adding liquidity to the CLMM market
47
20
  * This is a pure function that does not mutate the market state
@@ -69,12 +42,6 @@ export declare function calcDepositSyAndPtFromBaseAmount(params: {
69
42
  expirationTs: number;
70
43
  currentSpotPrice: number;
71
44
  syExchangeRate: number;
72
- /** Optional crossing tick state for accurate ratio prediction.
73
- * When provided, the prediction accounts for the existing PT/SY proportions
74
- * in the active tick, matching the on-chain behaviour more closely. */
75
- crossingTickState?: CrossingTickState;
76
- crossingTickPriceLeft?: number;
77
- crossingTickPriceRight?: number;
78
45
  epsilonClamp?: number;
79
46
  }): {
80
47
  syNeeded: number;
@@ -94,9 +61,10 @@ export declare function calcDepositSyAndPtFromBaseAmount(params: {
94
61
  * @param lowerTick - Lower tick (APY in basis points)
95
62
  * @param upperTick - Upper tick (APY in basis points)
96
63
  * @param syExchangeRate - SY exchange rate
64
+ * @param options - Optional existing LP position to include fixed crossing-split equalization spend
97
65
  * @returns Simulation result with LP out, YT out, amounts spent, etc.
98
66
  */
99
- export declare function simulateWrapperProvideLiquidity(marketState: MarketThreeState, amountBase: number, lowerTick: number, upperTick: number, syExchangeRate: number): {
67
+ export declare function simulateWrapperProvideLiquidity(marketState: MarketThreeState, amountBase: number, lowerTick: number, upperTick: number, syExchangeRate: number, options?: AddLiquiditySimulationOptions): {
100
68
  lpOut: number;
101
69
  ytOut: number;
102
70
  syToStrip: number;
@@ -118,9 +86,10 @@ export declare function simulateWrapperProvideLiquidity(marketState: MarketThree
118
86
  * @param lowerTick - Lower tick key (APY in basis points)
119
87
  * @param upperTick - Upper tick key (APY in basis points)
120
88
  * @param syExchangeRate - SY exchange rate
89
+ * @param options - Optional existing LP position to include fixed crossing-split equalization spend
121
90
  * @returns Simulation result with LP out, PT to buy, SY constraint, etc.
122
91
  */
123
- export declare function simulateSwapAndSupply(marketState: MarketThreeState, amountBase: number, lowerTick: number, upperTick: number, syExchangeRate: number): {
92
+ export declare function simulateSwapAndSupply(marketState: MarketThreeState, amountBase: number, lowerTick: number, upperTick: number, syExchangeRate: number, options?: AddLiquiditySimulationOptions): {
124
93
  lpOut: number;
125
94
  ptToBuy: number;
126
95
  syConstraint: number;
@@ -130,3 +99,4 @@ export declare function simulateSwapAndSupply(marketState: MarketThreeState, amo
130
99
  syDeposited: number;
131
100
  ptDeposited: number;
132
101
  } | null;
102
+ export {};