@1inch/swap-vm-sdk 0.1.2-rc.6 → 0.1.2-rc.7

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 (29) hide show
  1. package/dist/index.js +258 -118
  2. package/dist/index.mjs +259 -119
  3. package/dist/swap-vm/instructions/concentrate/concentrate-liquidity-calculator/types.d.mts +9 -21
  4. package/dist/swap-vm/instructions/concentrate/concentrate-liquidity-calculator/types.d.ts +9 -21
  5. package/dist/swap-vm/instructions/concentrate/index.d.mts +7 -2
  6. package/dist/swap-vm/instructions/concentrate/index.d.ts +7 -2
  7. package/dist/swap-vm/instructions/concentrate/price/index.d.mts +3 -0
  8. package/dist/swap-vm/instructions/concentrate/price/index.d.ts +3 -0
  9. package/dist/swap-vm/instructions/concentrate/price/price.d.mts +45 -0
  10. package/dist/swap-vm/instructions/concentrate/price/price.d.ts +45 -0
  11. package/dist/swap-vm/instructions/concentrate/price/truncate-human-decimal-string.d.mts +9 -0
  12. package/dist/swap-vm/instructions/concentrate/price/truncate-human-decimal-string.d.ts +9 -0
  13. package/dist/swap-vm/instructions/concentrate/price/types.d.mts +21 -0
  14. package/dist/swap-vm/instructions/concentrate/price/types.d.ts +21 -0
  15. package/dist/swap-vm/instructions/concentrate/price-range/index.d.mts +2 -0
  16. package/dist/swap-vm/instructions/concentrate/price-range/index.d.ts +2 -0
  17. package/dist/swap-vm/instructions/concentrate/price-range/price-range.d.mts +18 -0
  18. package/dist/swap-vm/instructions/concentrate/price-range/price-range.d.ts +18 -0
  19. package/dist/swap-vm/instructions/concentrate/price-range/types.d.mts +26 -0
  20. package/dist/swap-vm/instructions/concentrate/price-range/types.d.ts +26 -0
  21. package/dist/swap-vm/instructions/concentrate/token-reserve/index.d.mts +2 -0
  22. package/dist/swap-vm/instructions/concentrate/token-reserve/index.d.ts +2 -0
  23. package/dist/swap-vm/instructions/concentrate/token-reserve/token-reserve.d.mts +10 -0
  24. package/dist/swap-vm/instructions/concentrate/token-reserve/token-reserve.d.ts +10 -0
  25. package/dist/swap-vm/instructions/concentrate/token-reserve/types.d.mts +10 -0
  26. package/dist/swap-vm/instructions/concentrate/token-reserve/types.d.ts +10 -0
  27. package/package.json +1 -1
  28. package/dist/swap-vm/instructions/concentrate/concentrate-liquidity-calculator/concentrate-liquidity-calculator.d.mts +0 -61
  29. package/dist/swap-vm/instructions/concentrate/concentrate-liquidity-calculator/concentrate-liquidity-calculator.d.ts +0 -61
package/dist/index.js CHANGED
@@ -2025,149 +2025,287 @@ function mulDiv(a, b, c) {
2025
2025
  }
2026
2026
 
2027
2027
  //#endregion
2028
- //#region src/swap-vm/instructions/concentrate/concentrate-liquidity-calculator/concentrate-liquidity-calculator.ts
2028
+ //#region src/swap-vm/instructions/concentrate/price/truncate-human-decimal-string.ts
2029
2029
  /**
2030
- * Calculator for concentrated liquidity: given two tokens and a price range (min, spot, max),
2031
- * computes sqrt prices and token reserves for "max allocation" (use all available balances)
2032
- * or "fixed allocation" (fix one token amount and solve for the other).
2030
+ * Round a decimal string to `maxFrac` fractional digits (half-up: if the first dropped digit
2031
+ * is `5`–`9`, round the last kept digit up), then strip trailing zeros after the dot.
2033
2032
  *
2034
- * Token ordering follows the pool convention: token0 = lower address, token1 = higher address.
2035
- * Prices are supplied as ScaledPrices with scale 10^(token0Decimals + token1Decimals); they are
2036
- * converted internally to P = token1/token0 in 1e18 and then to sqrt(P * 1e18) for the math.
2033
+ * @param s Decimal string as produced by e.g. `formatUnits` (no scientific notation).
2034
+ * @param maxFrac Maximum number of digits after `.`; `0` means integer only (round using the
2035
+ * first fractional digit).
2037
2036
  */
2038
- var ConcentrateLiquidityCalculator = class ConcentrateLiquidityCalculator {
2039
- static ONE_E18 = 10n ** 18n;
2040
- constructor(tokenA, tokenB) {
2041
- this.tokenA = tokenA;
2042
- this.tokenB = tokenB;
2037
+ function truncateHumanDecimalString(s, maxFrac) {
2038
+ if (maxFrac < 0) throw new Error("maxFrac must be non-negative");
2039
+ const dot = s.indexOf(".");
2040
+ if (dot === -1) return s;
2041
+ const intPartStr = s.slice(0, dot) || "0";
2042
+ const fracFull = s.slice(dot + 1);
2043
+ if (maxFrac === 0) {
2044
+ let intPart = BigInt(intPartStr);
2045
+ const first = fracFull[0];
2046
+ if (first !== void 0 && first >= "5" && first <= "9") intPart += 1n;
2047
+ return intPart.toString();
2048
+ }
2049
+ const fracPadded = (fracFull + "0".repeat(maxFrac)).slice(0, maxFrac);
2050
+ const nextDigit = fracFull.length > maxFrac ? fracFull[maxFrac] : void 0;
2051
+ const roundUp = nextDigit !== void 0 && nextDigit >= "5" && nextDigit <= "9";
2052
+ const scale = 10n ** BigInt(maxFrac);
2053
+ let scaled = BigInt(intPartStr) * scale + BigInt(fracPadded || "0");
2054
+ if (roundUp) scaled += 1n;
2055
+ const intOut = scaled / scale;
2056
+ let fracOut = (scaled % scale).toString().padStart(maxFrac, "0");
2057
+ fracOut = fracOut.replace(/0+$/, "");
2058
+ return fracOut.length > 0 ? `${intOut}.${fracOut}` : intOut.toString();
2059
+ }
2060
+
2061
+ //#endregion
2062
+ //#region src/swap-vm/instructions/concentrate/price/price.ts
2063
+ const ONE_E18$1 = 10n ** 18n;
2064
+ var Price = class Price {
2065
+ constructor(sqrtP, token0, token1) {
2066
+ this.sqrtP = sqrtP;
2067
+ this.token0 = token0;
2068
+ this.token1 = token1;
2069
+ (0, assert.default)(sqrtP > 0n, "sqrt price must be positive");
2070
+ (0, assert.default)(!token0.address.equal(token1.address), "price tokens should be different");
2071
+ }
2072
+ /**
2073
+ * Fixed-point sqrt price as used on-chain (`sqrt(P * 1e18)`).
2074
+ */
2075
+ static fromSqrt(price, pair) {
2076
+ const zeroForOne = pair.tokenA.address.lt(pair.tokenB.address);
2077
+ const token0 = zeroForOne ? pair.tokenA : pair.tokenB;
2078
+ const token1 = zeroForOne ? pair.tokenB : pair.tokenA;
2079
+ return new Price(price, token0, token1);
2043
2080
  }
2044
2081
  /**
2045
- * Token with the smaller address (token0 in pool convention; "Lt" in the math).
2082
+ * Human decimal string for **quote per 1 base**`.
2046
2083
  */
2047
- get token0() {
2048
- return this.tokenA.address.lt(this.tokenB.address) ? this.tokenA : this.tokenB;
2084
+ static fromHuman(price, pair) {
2085
+ (0, assert.default)(!pair.quoteToken.address.equal(pair.baseToken.address), "quote and base must be different tokens");
2086
+ const zeroForOne = pair.quoteToken.address.lt(pair.baseToken.address);
2087
+ const t0 = zeroForOne ? pair.quoteToken : pair.baseToken;
2088
+ const t1 = zeroForOne ? pair.baseToken : pair.quoteToken;
2089
+ const d0 = t0.decimals;
2090
+ const d1 = t1.decimals;
2091
+ const scale = d0 + d1;
2092
+ if (scale > BigInt(Number.MAX_SAFE_INTEGER)) throw new Error("decimals sum too large for parseUnits");
2093
+ const scaledRaw = (0, viem.parseUnits)(price.trim(), Number(scale));
2094
+ if (pair.quoteToken.address.equal(t0.address)) {
2095
+ const numerator = 10n ** (d1 + d1) * ONE_E18$1;
2096
+ return new Price(bigintSqrt(numerator * ONE_E18$1 / scaledRaw), t0, t1);
2097
+ }
2098
+ if (pair.quoteToken.address.equal(t1.address)) {
2099
+ const denominator = 10n ** (d0 + d0);
2100
+ return new Price(bigintSqrt(scaledRaw * ONE_E18$1 * ONE_E18$1 / denominator), t0, t1);
2101
+ }
2102
+ throw new Error("quote token must be one of the two pair tokens");
2103
+ }
2104
+ static fromJSON(input) {
2105
+ const token0 = {
2106
+ address: new __1inch_sdk_core.Address(input.token0.address),
2107
+ decimals: BigInt(input.token0.decimals)
2108
+ };
2109
+ const token1 = {
2110
+ address: new __1inch_sdk_core.Address(input.token1.address),
2111
+ decimals: BigInt(input.token1.decimals)
2112
+ };
2113
+ (0, assert.default)(token0.address.lt(token1.address), "token0 address must be less than token1 (canonical order)");
2114
+ return new Price(BigInt(input.sqrtP), token0, token1);
2115
+ }
2116
+ equals(other) {
2117
+ return this.sqrtP === other.sqrtP && this.token0.address.equal(other.token0.address) && this.token1.address.equal(other.token1.address) && this.token0.decimals === other.token0.decimals && this.token1.decimals === other.token1.decimals;
2118
+ }
2119
+ lt(other) {
2120
+ (0, assert.default)(this.isSamePair(other), "cannot compare prices for different pairs");
2121
+ return this.sqrtP < other.sqrtP;
2122
+ }
2123
+ lte(other) {
2124
+ (0, assert.default)(this.isSamePair(other), "cannot compare prices for different pairs");
2125
+ return this.sqrtP <= other.sqrtP;
2126
+ }
2127
+ gt(other) {
2128
+ (0, assert.default)(this.isSamePair(other), "cannot compare prices for different pairs");
2129
+ return this.sqrtP > other.sqrtP;
2130
+ }
2131
+ gte(other) {
2132
+ (0, assert.default)(this.isSamePair(other), "cannot compare prices for different pairs");
2133
+ return this.sqrtP >= other.sqrtP;
2134
+ }
2135
+ isSamePair(other) {
2136
+ return this.token0.address.equal(other.token0.address) && this.token1.address.equal(other.token1.address) && this.token0.decimals === other.token0.decimals && this.token1.decimals === other.token1.decimals;
2049
2137
  }
2050
2138
  /**
2051
- * Token with the larger address (token1 in pool convention; "Gt" in the math).
2139
+ * Raw price `P` with 1e18 fixed-point (`(sqrtP^2) / 1e18`), matching typical on-chain use.
2052
2140
  */
2053
- get token1() {
2054
- return this.tokenA.address.lt(this.tokenB.address) ? this.tokenB : this.tokenA;
2141
+ toRaw() {
2142
+ return this.sqrtP * this.sqrtP / ONE_E18$1;
2055
2143
  }
2056
- static new(data) {
2057
- return new ConcentrateLiquidityCalculator(data.tokenA, data.tokenB);
2144
+ toSqrt() {
2145
+ return this.sqrtP;
2058
2146
  }
2059
2147
  /**
2060
- * Compute reserves when one token amount is fixed: the position uses exactly
2061
- * `fixedReserve` of `fixedReserveForToken` and the other token amount is derived
2062
- * to keep the same liquidity L. Returns sqrt prices and token0/token1
2063
- * reserves (raw amounts).
2064
- * Due to integer math (floor division, sqrt), the fixed asset amount in the result
2065
- * may be less than requested by a few wei.
2148
+ * Decimal string for **quote per 1 base** at scale `10^(token0Decimals + token1Decimals)`.
2149
+ * Fractional digits after the dot are **rounded half-up** to {@link PriceToken.decimals} of
2150
+ * the quote token (then trailing zeros are removed).
2151
+ *
2152
+ * @param quoteToken Which token is the quote currency; base is the other token.
2066
2153
  */
2067
- computeFixedAllocation(scaledPrices, fixedReserveForToken, fixedReserve) {
2068
- const { minPriceRaw, spotPriceRaw, maxPriceRaw } = this.computeRawPrices(scaledPrices);
2069
- const sqrtPriceMin = bigintSqrt(minPriceRaw * ConcentrateLiquidityCalculator.ONE_E18);
2070
- const sqrtPriceSpot = bigintSqrt(spotPriceRaw * ConcentrateLiquidityCalculator.ONE_E18);
2071
- const sqrtPriceMax = bigintSqrt(maxPriceRaw * ConcentrateLiquidityCalculator.ONE_E18);
2072
- const isFixedLt = fixedReserveForToken.equal(this.token0.address);
2073
- const availableLt = isFixedLt ? fixedReserve : __1inch_byte_utils.UINT_256_MAX;
2074
- const availableGt = isFixedLt ? __1inch_byte_utils.UINT_256_MAX : fixedReserve;
2075
- const { actualLt, actualGt } = computeLiquidityFromAmounts(availableLt, availableGt, sqrtPriceSpot, sqrtPriceMin, sqrtPriceMax);
2154
+ toHuman(quoteToken) {
2155
+ (0, assert.default)(quoteToken.equal(this.token0.address) || quoteToken.equal(this.token1.address), "quote token should be one of pair price tokens");
2156
+ const d0 = this.token0.decimals;
2157
+ const d1 = this.token1.decimals;
2158
+ const scale = d0 + d1;
2159
+ const scaled = this.scaledRawAmountForQuote(quoteToken);
2160
+ const quoteDecimals = quoteToken.equal(this.token0.address) ? d0 : d1;
2161
+ const full = (0, viem.formatUnits)(scaled, Number(scale));
2162
+ return truncateHumanDecimalString(full, Number(quoteDecimals));
2163
+ }
2164
+ toJSON() {
2076
2165
  return {
2077
- sqrtPriceMin,
2078
- sqrtPriceSpot,
2079
- sqrtPriceMax,
2080
- token0Reserve: actualLt,
2081
- token1Reserve: actualGt
2166
+ sqrtP: this.sqrtP.toString(),
2167
+ token0: {
2168
+ address: this.token0.address.toString(),
2169
+ decimals: this.token0.decimals.toString()
2170
+ },
2171
+ token1: {
2172
+ address: this.token1.address.toString(),
2173
+ decimals: this.token1.decimals.toString()
2174
+ }
2082
2175
  };
2083
2176
  }
2084
2177
  /**
2085
- * Compute reserves when both token amounts are taken from token info: uses
2086
- * token0.maxAvailableLiquidity and token1.maxAvailableLiquidity to maximize
2087
- * L. Returns sqrt prices and the token0/token1 reserves that achieve
2088
- * that maximum.
2178
+ * Quote per 1 base scaled by `10^(token0Decimals + token1Decimals)`.
2179
+ * Uses `sqrtP^2` in one step so we do not compound truncation from {@link toRaw}.
2089
2180
  */
2090
- computeMaxAllocation(scaledPrices) {
2091
- const { minPriceRaw, spotPriceRaw, maxPriceRaw } = this.computeRawPrices(scaledPrices);
2092
- const sqrtPriceMin = bigintSqrt(minPriceRaw * ConcentrateLiquidityCalculator.ONE_E18);
2093
- const sqrtPriceSpot = bigintSqrt(spotPriceRaw * ConcentrateLiquidityCalculator.ONE_E18);
2094
- const sqrtPriceMax = bigintSqrt(maxPriceRaw * ConcentrateLiquidityCalculator.ONE_E18);
2095
- const { actualLt, actualGt } = computeLiquidityFromAmounts(this.token0.maxAvailableLiquidity, this.token1.maxAvailableLiquidity, sqrtPriceSpot, sqrtPriceMin, sqrtPriceMax);
2181
+ scaledRawAmountForQuote(quoteToken) {
2182
+ const d0 = this.token0.decimals;
2183
+ const d1 = this.token1.decimals;
2184
+ const sqrtP2 = this.sqrtP * this.sqrtP;
2185
+ if (quoteToken.equal(this.token0.address)) {
2186
+ const numerator = 10n ** (d1 + d1) * ONE_E18$1;
2187
+ return numerator * ONE_E18$1 / sqrtP2;
2188
+ }
2189
+ if (quoteToken.equal(this.token1.address)) {
2190
+ const numerator = 10n ** (d0 + d0);
2191
+ return sqrtP2 * numerator / (ONE_E18$1 * ONE_E18$1);
2192
+ }
2193
+ throw new Error("quote token must be one of the pair tokens");
2194
+ }
2195
+ };
2196
+
2197
+ //#endregion
2198
+ //#region src/swap-vm/instructions/concentrate/token-reserve/token-reserve.ts
2199
+ var TokenReserve = class TokenReserve {
2200
+ constructor(token, reserve) {
2201
+ this.token = token;
2202
+ this.reserve = reserve;
2203
+ }
2204
+ static new(args) {
2205
+ return new TokenReserve(args.token, args.reserve);
2206
+ }
2207
+ static fromJSON(input) {
2208
+ return new TokenReserve(new __1inch_sdk_core.Address(input.token), BigInt(input.reserve));
2209
+ }
2210
+ toJSON() {
2096
2211
  return {
2097
- sqrtPriceMin,
2098
- sqrtPriceSpot,
2099
- sqrtPriceMax,
2100
- token0Reserve: actualLt,
2101
- token1Reserve: actualGt
2212
+ token: this.token.toString(),
2213
+ reserve: this.reserve.toString()
2102
2214
  };
2103
2215
  }
2104
- /**
2105
- * Implied spot sqrt price from token0/token1 balances and a scaled price range
2106
- * (same convention as {@link computeMaxAllocation}). Inverse of the allocation math; wraps
2107
- * {@link computeLiquidityAndPrice} from concentrate-liquidity-math.
2108
- *
2109
- * @returns sqrt(P_spot) in 1e18 fixed-point (same as {@link ConcentratedLiquidityInfo.sqrtPriceSpot}).
2110
- */
2111
- computeSpotPrice(token0Balance, token1Balance, scaledPriceBounds) {
2112
- const { sqrtPriceMin, sqrtPriceMax } = this.computeSqrtPriceBounds(scaledPriceBounds);
2113
- const { sqrtPriceSpot } = computeLiquidityAndPrice(token0Balance, token1Balance, sqrtPriceMin, sqrtPriceMax);
2114
- return sqrtPriceSpot;
2216
+ };
2217
+
2218
+ //#endregion
2219
+ //#region src/swap-vm/instructions/concentrate/price-range/price-range.ts
2220
+ var PriceRange = class PriceRange {
2221
+ constructor(minPrice, spotPrice, maxPrice) {
2222
+ this.minPrice = minPrice;
2223
+ this.spotPrice = spotPrice;
2224
+ this.maxPrice = maxPrice;
2225
+ (0, assert.default)(maxPrice.gte(spotPrice), "maxPrice should be >= spotPrice");
2226
+ (0, assert.default)(spotPrice.gte(minPrice), "spotPrice should be >= minPrice");
2227
+ (0, assert.default)(minPrice.lt(maxPrice), "minPrice should be < maxPrice");
2228
+ (0, assert.default)(minPrice.isSamePair(spotPrice), "cannot create price range for different pairs");
2229
+ (0, assert.default)(maxPrice.isSamePair(spotPrice), "cannot create price range for different pairs");
2115
2230
  }
2116
- /**
2117
- * Convert user-facing ScaledPrices (scale 10^(token0Decimals+token1Decimals)) into internal
2118
- * raw prices P = token1/token0 (before sqrt), so that sqrt(P * 1e18) can be
2119
- * passed to the liquidity math. Handles both quote = token0 and quote = token1.
2120
- */
2121
- computeRawPrices(scaledPrices) {
2122
- const { minPriceRaw, maxPriceRaw } = this.computeRawPriceBounds(scaledPrices);
2123
- const token0 = this.token0;
2124
- const token1 = this.token1;
2125
- if (scaledPrices.quoteToken.equal(token0.address)) {
2126
- const numerator = 10n ** (token1.decimals + token1.decimals) * ConcentrateLiquidityCalculator.ONE_E18;
2127
- return {
2128
- minPriceRaw,
2129
- spotPriceRaw: numerator / scaledPrices.spotPriceRaw,
2130
- maxPriceRaw
2131
- };
2132
- }
2133
- if (scaledPrices.quoteToken.equal(token1.address)) {
2134
- const denominator = 10n ** (token0.decimals + token0.decimals);
2135
- return {
2136
- minPriceRaw,
2137
- spotPriceRaw: scaledPrices.spotPriceRaw * ConcentrateLiquidityCalculator.ONE_E18 / denominator,
2138
- maxPriceRaw
2139
- };
2140
- }
2141
- throw new Error("unknown quote token");
2231
+ get token0() {
2232
+ return this.minPrice.token0;
2142
2233
  }
2143
- computeSqrtPriceBounds(scaledPriceBounds) {
2144
- const { minPriceRaw, maxPriceRaw } = this.computeRawPriceBounds(scaledPriceBounds);
2234
+ get token1() {
2235
+ return this.minPrice.token1;
2236
+ }
2237
+ static new(range) {
2238
+ const minPrice = range.minPrice.lte(range.spotPrice) ? range.minPrice : range.maxPrice;
2239
+ const maxPrice = range.maxPrice.gte(range.spotPrice) ? range.maxPrice : range.minPrice;
2240
+ return new PriceRange(minPrice, range.spotPrice, maxPrice);
2241
+ }
2242
+ static fromJSON(input) {
2243
+ return PriceRange.new({
2244
+ minPrice: Price.fromJSON(input.minPrice),
2245
+ spotPrice: Price.fromJSON(input.spotPrice),
2246
+ maxPrice: Price.fromJSON(input.maxPrice)
2247
+ });
2248
+ }
2249
+ static fromPriceBounds(bounds, reserves) {
2250
+ const minPrice = bounds.minPrice.lt(bounds.maxPrice) ? bounds.minPrice : bounds.maxPrice;
2251
+ const maxPrice = bounds.minPrice.lt(bounds.maxPrice) ? bounds.maxPrice : bounds.minPrice;
2252
+ const zeroForOne = reserves.reserveA.token.equal(bounds.minPrice.token0.address);
2253
+ const reserve0 = zeroForOne ? reserves.reserveA : reserves.reserveB;
2254
+ const reserve1 = zeroForOne ? reserves.reserveB : reserves.reserveA;
2255
+ (0, assert.default)(reserve0.token.equal(bounds.minPrice.token0.address), "provided reserve for unknown token");
2256
+ (0, assert.default)(reserve1.token.equal(bounds.minPrice.token1.address), "provided reserve for unknown token");
2257
+ const { sqrtPriceSpot } = computeLiquidityAndPrice(reserve0.reserve, reserve1.reserve, minPrice.toSqrt(), maxPrice.toSqrt());
2258
+ const spotPrice = Price.fromSqrt(sqrtPriceSpot, {
2259
+ tokenA: bounds.minPrice.token0,
2260
+ tokenB: bounds.minPrice.token1
2261
+ });
2262
+ return PriceRange.new({
2263
+ minPrice,
2264
+ spotPrice,
2265
+ maxPrice
2266
+ });
2267
+ }
2268
+ computeFixedAllocation(fixedReserve) {
2269
+ (0, assert.default)(fixedReserve.token.equal(this.token0.address) || fixedReserve.token.equal(this.token1.address), "fixed reserve should be in some pair token");
2270
+ const isFixedLt = fixedReserve.token.equal(this.token0.address);
2271
+ const availableLt = isFixedLt ? fixedReserve.reserve : __1inch_byte_utils.UINT_256_MAX;
2272
+ const availableGt = isFixedLt ? __1inch_byte_utils.UINT_256_MAX : fixedReserve.reserve;
2273
+ const { actualLt, actualGt } = computeLiquidityFromAmounts(availableLt, availableGt, this.spotPrice.toSqrt(), this.minPrice.toSqrt(), this.maxPrice.toSqrt());
2145
2274
  return {
2146
- sqrtPriceMin: bigintSqrt(minPriceRaw * ConcentrateLiquidityCalculator.ONE_E18),
2147
- sqrtPriceMax: bigintSqrt(maxPriceRaw * ConcentrateLiquidityCalculator.ONE_E18)
2275
+ reserve0: TokenReserve.new({
2276
+ token: this.token0.address,
2277
+ reserve: actualLt
2278
+ }),
2279
+ reserve1: TokenReserve.new({
2280
+ token: this.token1.address,
2281
+ reserve: actualGt
2282
+ })
2148
2283
  };
2149
2284
  }
2150
- /**
2151
- * Internal P = token1/token0 (1e18) from scaled min/max only (same mapping as {@link computeRawPrices}).
2152
- */
2153
- computeRawPriceBounds(scaledPriceBounds) {
2154
- const token0 = this.token0;
2155
- const token1 = this.token1;
2156
- if (scaledPriceBounds.quoteToken.equal(token0.address)) {
2157
- const numerator = 10n ** (token1.decimals + token1.decimals) * ConcentrateLiquidityCalculator.ONE_E18;
2158
- return {
2159
- minPriceRaw: numerator / scaledPriceBounds.maxPriceRaw,
2160
- maxPriceRaw: numerator / scaledPriceBounds.minPriceRaw
2161
- };
2162
- }
2163
- if (scaledPriceBounds.quoteToken.equal(token1.address)) {
2164
- const denominator = 10n ** (token0.decimals + token0.decimals);
2165
- return {
2166
- minPriceRaw: scaledPriceBounds.minPriceRaw * ConcentrateLiquidityCalculator.ONE_E18 / denominator,
2167
- maxPriceRaw: scaledPriceBounds.maxPriceRaw * ConcentrateLiquidityCalculator.ONE_E18 / denominator
2168
- };
2169
- }
2170
- throw new Error("unknown quote token");
2285
+ computeMaxAllocation(maxAvailableLiquidity) {
2286
+ const zeroForOne = maxAvailableLiquidity.reserveA.token.equal(this.token0.address);
2287
+ const reserve0 = zeroForOne ? maxAvailableLiquidity.reserveA : maxAvailableLiquidity.reserveB;
2288
+ const reserve1 = zeroForOne ? maxAvailableLiquidity.reserveB : maxAvailableLiquidity.reserveA;
2289
+ (0, assert.default)(reserve0.token.equal(this.token0.address), "provided reserve for unknown token");
2290
+ (0, assert.default)(reserve1.token.equal(this.token1.address), "provided reserve for unknown token");
2291
+ const { actualLt, actualGt } = computeLiquidityFromAmounts(reserve0.reserve, reserve1.reserve, this.spotPrice.toSqrt(), this.minPrice.toSqrt(), this.maxPrice.toSqrt());
2292
+ return {
2293
+ reserve0: TokenReserve.new({
2294
+ token: this.token0.address,
2295
+ reserve: actualLt
2296
+ }),
2297
+ reserve1: TokenReserve.new({
2298
+ token: this.token1.address,
2299
+ reserve: actualGt
2300
+ })
2301
+ };
2302
+ }
2303
+ toJSON() {
2304
+ return {
2305
+ minPrice: this.minPrice.toJSON(),
2306
+ spotPrice: this.spotPrice.toJSON(),
2307
+ maxPrice: this.maxPrice.toJSON()
2308
+ };
2171
2309
  }
2172
2310
  };
2173
2311
 
@@ -2176,8 +2314,10 @@ var ConcentrateLiquidityCalculator = class ConcentrateLiquidityCalculator {
2176
2314
  var concentrate_exports = {};
2177
2315
  __export(concentrate_exports, {
2178
2316
  ConcentrateGrowLiquidity2DArgs: () => ConcentrateGrowLiquidity2DArgs,
2179
- ConcentrateLiquidityCalculator: () => ConcentrateLiquidityCalculator,
2180
2317
  ONE_E18: () => ONE_E18,
2318
+ Price: () => Price,
2319
+ PriceRange: () => PriceRange,
2320
+ TokenReserve: () => TokenReserve,
2181
2321
  bigintSqrt: () => bigintSqrt,
2182
2322
  computeBalances: () => computeBalances,
2183
2323
  computeLiquidityAndPrice: () => computeLiquidityAndPrice,