@sodax/sdk 1.5.5-beta → 1.5.6-beta

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.cjs CHANGED
@@ -35085,7 +35085,7 @@ var AssetService = class {
35085
35085
  });
35086
35086
  }
35087
35087
  };
35088
- var ClService = class {
35088
+ var ClService = class _ClService {
35089
35089
  config;
35090
35090
  relayerApiEndpoint;
35091
35091
  hubProvider;
@@ -36103,6 +36103,68 @@ var ClService = class {
36103
36103
  );
36104
36104
  return amount0;
36105
36105
  }
36106
+ /**
36107
+ * Calculate amount0Max and amount1Max for a given liquidity and slippage tolerance.
36108
+ * For concentrated liquidity, a price drop increases the amount0 needed and a price rise increases the amount1 needed.
36109
+ * This calculates the worst-case amounts for each token independently.
36110
+ */
36111
+ static calculateMaxAmountsForSlippage(liquidity, tickLower, tickUpper, currentTick, sqrtPriceX96, slippagePercent) {
36112
+ const amount0AtCurrent = v3Sdk.PositionMath.getToken0Amount(
36113
+ Number(currentTick),
36114
+ Number(tickLower),
36115
+ Number(tickUpper),
36116
+ sqrtPriceX96,
36117
+ liquidity
36118
+ );
36119
+ const amount1AtCurrent = v3Sdk.PositionMath.getToken1Amount(
36120
+ Number(currentTick),
36121
+ Number(tickLower),
36122
+ Number(tickUpper),
36123
+ sqrtPriceX96,
36124
+ liquidity
36125
+ );
36126
+ const SLIPPAGE_SCALE = 1000000000n;
36127
+ const slippageScaled = BigInt(Math.round(slippagePercent * Number(SLIPPAGE_SCALE) / 100));
36128
+ const sqrtPriceX96Squared = sqrtPriceX96 * sqrtPriceX96;
36129
+ const sqrtPriceX96Down = _ClService.sqrtBigInt(
36130
+ sqrtPriceX96Squared * (SLIPPAGE_SCALE - slippageScaled) / SLIPPAGE_SCALE
36131
+ );
36132
+ const sqrtPriceX96Up = _ClService.sqrtBigInt(
36133
+ sqrtPriceX96Squared * (SLIPPAGE_SCALE + slippageScaled) / SLIPPAGE_SCALE
36134
+ );
36135
+ const tickDown = v3Sdk.TickMath.getTickAtSqrtRatio(sqrtPriceX96Down);
36136
+ const tickUp = v3Sdk.TickMath.getTickAtSqrtRatio(sqrtPriceX96Up);
36137
+ const amount0AtPriceDrop = v3Sdk.PositionMath.getToken0Amount(
36138
+ tickDown,
36139
+ Number(tickLower),
36140
+ Number(tickUpper),
36141
+ sqrtPriceX96Down,
36142
+ liquidity
36143
+ );
36144
+ const amount1AtPriceRise = v3Sdk.PositionMath.getToken1Amount(
36145
+ tickUp,
36146
+ Number(tickLower),
36147
+ Number(tickUpper),
36148
+ sqrtPriceX96Up,
36149
+ liquidity
36150
+ );
36151
+ const amount0Max = amount0AtPriceDrop > amount0AtCurrent ? amount0AtPriceDrop : amount0AtCurrent;
36152
+ const amount1Max = amount1AtPriceRise > amount1AtCurrent ? amount1AtPriceRise : amount1AtCurrent;
36153
+ return { amount0Max, amount1Max };
36154
+ }
36155
+ /**
36156
+ * Integer square root via Newton's method. Returns floor(sqrt(n)).
36157
+ */
36158
+ static sqrtBigInt(n) {
36159
+ if (n < 0n) throw new Error("sqrtBigInt: negative input");
36160
+ if (n < 2n) return n;
36161
+ let x = 1n << (BigInt(n.toString(2).length) + 1n) / 2n;
36162
+ while (true) {
36163
+ const next = (x + n / x) / 2n;
36164
+ if (next >= x) return x;
36165
+ x = next;
36166
+ }
36167
+ }
36106
36168
  /**
36107
36169
  * Helper: Convert price to nearest valid tick
36108
36170
  * @param price - The price as a number