@1inch/swap-vm-sdk 0.1.2-rc.6 → 0.1.2
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.js +454 -135
- package/dist/index.mjs +455 -136
- package/dist/swap-vm/instructions/concentrate/concentrate-liquidity-calculator/types.d.mts +9 -21
- package/dist/swap-vm/instructions/concentrate/concentrate-liquidity-calculator/types.d.ts +9 -21
- package/dist/swap-vm/instructions/concentrate/index.d.mts +8 -3
- package/dist/swap-vm/instructions/concentrate/index.d.ts +8 -3
- package/dist/swap-vm/instructions/concentrate/price/index.d.mts +3 -0
- package/dist/swap-vm/instructions/concentrate/price/index.d.ts +3 -0
- package/dist/swap-vm/instructions/concentrate/price/price.d.mts +45 -0
- package/dist/swap-vm/instructions/concentrate/price/price.d.ts +45 -0
- package/dist/swap-vm/instructions/concentrate/price/types.d.mts +21 -0
- package/dist/swap-vm/instructions/concentrate/price/types.d.ts +21 -0
- package/dist/swap-vm/instructions/concentrate/price-range/index.d.mts +2 -0
- package/dist/swap-vm/instructions/concentrate/price-range/index.d.ts +2 -0
- package/dist/swap-vm/instructions/concentrate/price-range/price-range.d.mts +18 -0
- package/dist/swap-vm/instructions/concentrate/price-range/price-range.d.ts +18 -0
- package/dist/swap-vm/instructions/concentrate/price-range/types.d.mts +26 -0
- package/dist/swap-vm/instructions/concentrate/price-range/types.d.ts +26 -0
- package/dist/swap-vm/instructions/concentrate/token-reserve/index.d.mts +2 -0
- package/dist/swap-vm/instructions/concentrate/token-reserve/index.d.ts +2 -0
- package/dist/swap-vm/instructions/concentrate/token-reserve/token-reserve.d.mts +10 -0
- package/dist/swap-vm/instructions/concentrate/token-reserve/token-reserve.d.ts +10 -0
- package/dist/swap-vm/instructions/concentrate/token-reserve/types.d.mts +10 -0
- package/dist/swap-vm/instructions/concentrate/token-reserve/types.d.ts +10 -0
- package/dist/swap-vm/instructions/index.d.mts +1 -0
- package/dist/swap-vm/instructions/index.d.ts +1 -0
- package/dist/swap-vm/instructions/pegged-swap/index.d.mts +4 -0
- package/dist/swap-vm/instructions/pegged-swap/index.d.ts +4 -0
- package/dist/swap-vm/instructions/pegged-swap/pegged-swap-calculator/index.d.mts +2 -0
- package/dist/swap-vm/instructions/pegged-swap/pegged-swap-calculator/index.d.ts +2 -0
- package/dist/swap-vm/instructions/pegged-swap/pegged-swap-calculator/pegged-swap-calculator.d.mts +16 -0
- package/dist/swap-vm/instructions/pegged-swap/pegged-swap-calculator/pegged-swap-calculator.d.ts +16 -0
- package/dist/swap-vm/instructions/pegged-swap/pegged-swap-calculator/types.d.mts +9 -0
- package/dist/swap-vm/instructions/pegged-swap/pegged-swap-calculator/types.d.ts +9 -0
- package/dist/swap-vm/instructions/pegged-swap/pegged-swap-math/pegged-swap-math.d.mts +18 -0
- package/dist/swap-vm/instructions/pegged-swap/pegged-swap-math/pegged-swap-math.d.ts +18 -0
- package/dist/swap-vm/instructions/pegged-swap/price/index.d.mts +2 -0
- package/dist/swap-vm/instructions/pegged-swap/price/index.d.ts +2 -0
- package/dist/swap-vm/instructions/pegged-swap/price/pegged-price.d.mts +28 -0
- package/dist/swap-vm/instructions/pegged-swap/price/pegged-price.d.ts +28 -0
- package/dist/swap-vm/instructions/pegged-swap/price/types.d.mts +30 -0
- package/dist/swap-vm/instructions/pegged-swap/price/types.d.ts +30 -0
- package/dist/swap-vm/instructions/utils/index.d.mts +2 -0
- package/dist/swap-vm/instructions/utils/index.d.ts +2 -0
- package/dist/swap-vm/instructions/utils/truncate-human-decimal-string.d.mts +9 -0
- package/dist/swap-vm/instructions/utils/truncate-human-decimal-string.d.ts +9 -0
- package/package.json +4 -4
- package/dist/swap-vm/instructions/concentrate/concentrate-liquidity-calculator/concentrate-liquidity-calculator.d.mts +0 -61
- package/dist/swap-vm/instructions/concentrate/concentrate-liquidity-calculator/concentrate-liquidity-calculator.d.ts +0 -61
- /package/dist/swap-vm/instructions/{concentrate → utils}/bigint-sqrt.d.mts +0 -0
- /package/dist/swap-vm/instructions/{concentrate → utils}/bigint-sqrt.d.ts +0 -0
package/dist/index.js
CHANGED
|
@@ -1866,7 +1866,7 @@ var ConcentrateGrowLiquidity2DArgsCoder = class {
|
|
|
1866
1866
|
};
|
|
1867
1867
|
|
|
1868
1868
|
//#endregion
|
|
1869
|
-
//#region src/swap-vm/instructions/
|
|
1869
|
+
//#region src/swap-vm/instructions/utils/bigint-sqrt.ts
|
|
1870
1870
|
function bigintSqrt(value) {
|
|
1871
1871
|
if (value < 0n) throw new Error("square root of negative numbers is not supported");
|
|
1872
1872
|
if (value < 2n) return value;
|
|
@@ -1882,7 +1882,7 @@ function bigintSqrt(value) {
|
|
|
1882
1882
|
|
|
1883
1883
|
//#endregion
|
|
1884
1884
|
//#region src/swap-vm/instructions/concentrate/concentrate-grow-liquidity-2d-args.ts
|
|
1885
|
-
const ONE_E18 = 10n ** 18n;
|
|
1885
|
+
const ONE_E18$1 = 10n ** 18n;
|
|
1886
1886
|
/**
|
|
1887
1887
|
* Arguments for concentrateGrowLiquidity2D instruction
|
|
1888
1888
|
* Contract encodes sqrtPriceMin and sqrtPriceMax (2 × uint256, 64 bytes)
|
|
@@ -1910,8 +1910,8 @@ var ConcentrateGrowLiquidity2DArgs = class ConcentrateGrowLiquidity2DArgs {
|
|
|
1910
1910
|
* Computes sqrtPrice = sqrt(P * 1e18) so that (sqrtPrice/1e18)^2 = P/1e18.
|
|
1911
1911
|
**/
|
|
1912
1912
|
static fromRawPrices(rawPriceMin, rawPriceMax) {
|
|
1913
|
-
const sqrtPriceMin = bigintSqrt(rawPriceMin * ONE_E18);
|
|
1914
|
-
const sqrtPriceMax = bigintSqrt(rawPriceMax * ONE_E18);
|
|
1913
|
+
const sqrtPriceMin = bigintSqrt(rawPriceMin * ONE_E18$1);
|
|
1914
|
+
const sqrtPriceMax = bigintSqrt(rawPriceMax * ONE_E18$1);
|
|
1915
1915
|
return new ConcentrateGrowLiquidity2DArgs(sqrtPriceMin, sqrtPriceMax);
|
|
1916
1916
|
}
|
|
1917
1917
|
/**
|
|
@@ -1955,8 +1955,8 @@ const ONE = 10n ** 18n;
|
|
|
1955
1955
|
*/
|
|
1956
1956
|
function computeLiquidityFromAmounts(availableLt, availableGt, sqrtPspot, sqrtPmin, sqrtPmax) {
|
|
1957
1957
|
if (sqrtPmin >= sqrtPmax) throw new Error("sqrtPmax should be greater than sqrtPmin");
|
|
1958
|
-
const lFromLt = sqrtPmax > sqrtPspot ? mulDiv(availableLt, mulDiv(sqrtPmax, sqrtPspot, ONE), sqrtPmax - sqrtPspot) : __1inch_byte_utils.UINT_256_MAX;
|
|
1959
|
-
const lFromGt = sqrtPspot > sqrtPmin ? mulDiv(availableGt, ONE, sqrtPspot - sqrtPmin) : __1inch_byte_utils.UINT_256_MAX;
|
|
1958
|
+
const lFromLt = sqrtPmax > sqrtPspot ? mulDiv$1(availableLt, mulDiv$1(sqrtPmax, sqrtPspot, ONE), sqrtPmax - sqrtPspot) : __1inch_byte_utils.UINT_256_MAX;
|
|
1959
|
+
const lFromGt = sqrtPspot > sqrtPmin ? mulDiv$1(availableGt, ONE, sqrtPspot - sqrtPmin) : __1inch_byte_utils.UINT_256_MAX;
|
|
1960
1960
|
const targetL = lFromLt < lFromGt ? lFromLt : lFromGt;
|
|
1961
1961
|
const { bLt: actualLt, bGt: actualGt } = computeBalances(targetL, sqrtPspot, sqrtPmin, sqrtPmax);
|
|
1962
1962
|
return {
|
|
@@ -1980,8 +1980,8 @@ function computeLiquidityFromAmounts(availableLt, availableGt, sqrtPspot, sqrtPm
|
|
|
1980
1980
|
*/
|
|
1981
1981
|
function computeBalances(targetL, sqrtPspot, sqrtPmin, sqrtPmax) {
|
|
1982
1982
|
if (sqrtPmin >= sqrtPmax) throw new Error("sqrtPmax should be greater than sqrtPmin");
|
|
1983
|
-
const bLt = sqrtPmax > sqrtPspot ? mulDiv(targetL, sqrtPmax - sqrtPspot, mulDiv(sqrtPmax, sqrtPspot, ONE)) : 0n;
|
|
1984
|
-
const bGt = sqrtPspot > sqrtPmin ? mulDiv(targetL, sqrtPspot - sqrtPmin, ONE) : 0n;
|
|
1983
|
+
const bLt = sqrtPmax > sqrtPspot ? mulDiv$1(targetL, sqrtPmax - sqrtPspot, mulDiv$1(sqrtPmax, sqrtPspot, ONE)) : 0n;
|
|
1984
|
+
const bGt = sqrtPspot > sqrtPmin ? mulDiv$1(targetL, sqrtPspot - sqrtPmin, ONE) : 0n;
|
|
1985
1985
|
return {
|
|
1986
1986
|
bLt,
|
|
1987
1987
|
bGt
|
|
@@ -2000,9 +2000,9 @@ function computeBalances(targetL, sqrtPspot, sqrtPmin, sqrtPmax) {
|
|
|
2000
2000
|
*/
|
|
2001
2001
|
function computeLiquidityAndPrice(balanceLt, balanceGt, sqrtPriceMin, sqrtPriceMax) {
|
|
2002
2002
|
const liquidity = computeL(balanceLt, balanceGt, sqrtPriceMin, sqrtPriceMax);
|
|
2003
|
-
const virtualLt = balanceLt + mulDiv(liquidity, ONE, sqrtPriceMax);
|
|
2004
|
-
const virtualGt = balanceGt + mulDiv(liquidity, sqrtPriceMin, ONE);
|
|
2005
|
-
const sqrtPriceSpot = bigintSqrt(mulDiv(virtualGt, ONE * ONE, virtualLt));
|
|
2003
|
+
const virtualLt = balanceLt + mulDiv$1(liquidity, ONE, sqrtPriceMax);
|
|
2004
|
+
const virtualGt = balanceGt + mulDiv$1(liquidity, sqrtPriceMin, ONE);
|
|
2005
|
+
const sqrtPriceSpot = bigintSqrt(mulDiv$1(virtualGt, ONE * ONE, virtualLt));
|
|
2006
2006
|
return {
|
|
2007
2007
|
liquidity,
|
|
2008
2008
|
sqrtPriceSpot
|
|
@@ -2013,161 +2013,299 @@ function computeLiquidityAndPrice(balanceLt, balanceGt, sqrtPriceMin, sqrtPriceM
|
|
|
2013
2013
|
* Mirrors XYCConcentrateArgsBuilder._computeL in XYCConcentrate.sol.
|
|
2014
2014
|
*/
|
|
2015
2015
|
function computeL(bLt, bGt, sqrtPriceMin, sqrtPriceMax) {
|
|
2016
|
-
const alpha = ONE - mulDiv(sqrtPriceMin, ONE, sqrtPriceMax);
|
|
2017
|
-
const beta = mulDiv(bLt, sqrtPriceMin, ONE) + mulDiv(bGt, ONE, sqrtPriceMax);
|
|
2018
|
-
const fourAC = mulDiv(4n * alpha, bLt, ONE) * bGt;
|
|
2016
|
+
const alpha = ONE - mulDiv$1(sqrtPriceMin, ONE, sqrtPriceMax);
|
|
2017
|
+
const beta = mulDiv$1(bLt, sqrtPriceMin, ONE) + mulDiv$1(bGt, ONE, sqrtPriceMax);
|
|
2018
|
+
const fourAC = mulDiv$1(4n * alpha, bLt, ONE) * bGt;
|
|
2019
2019
|
const disc = beta * beta + fourAC;
|
|
2020
|
-
return mulDiv(beta + bigintSqrt(disc), ONE, 2n * alpha);
|
|
2020
|
+
return mulDiv$1(beta + bigintSqrt(disc), ONE, 2n * alpha);
|
|
2021
2021
|
}
|
|
2022
|
-
function mulDiv(a, b, c) {
|
|
2022
|
+
function mulDiv$1(a, b, c) {
|
|
2023
2023
|
if (c === 0n) throw new Error("mulDiv: division by zero");
|
|
2024
2024
|
return a * b / c;
|
|
2025
2025
|
}
|
|
2026
2026
|
|
|
2027
2027
|
//#endregion
|
|
2028
|
-
//#region src/swap-vm/instructions/
|
|
2028
|
+
//#region src/swap-vm/instructions/utils/truncate-human-decimal-string.ts
|
|
2029
2029
|
/**
|
|
2030
|
-
*
|
|
2031
|
-
*
|
|
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
|
-
*
|
|
2035
|
-
*
|
|
2036
|
-
*
|
|
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
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
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$2 = 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
|
-
*
|
|
2082
|
+
* Human decimal string for **quote per 1 base**`.
|
|
2046
2083
|
*/
|
|
2047
|
-
|
|
2048
|
-
|
|
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$2;
|
|
2096
|
+
return new Price(bigintSqrt(numerator * ONE_E18$2 / 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$2 * ONE_E18$2 / 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
|
-
*
|
|
2139
|
+
* Raw price `P` with 1e18 fixed-point (`(sqrtP^2) / 1e18`), matching typical on-chain use.
|
|
2052
2140
|
*/
|
|
2053
|
-
|
|
2054
|
-
return this.
|
|
2141
|
+
toRaw() {
|
|
2142
|
+
return this.sqrtP * this.sqrtP / ONE_E18$2;
|
|
2055
2143
|
}
|
|
2056
|
-
|
|
2057
|
-
return
|
|
2144
|
+
toSqrt() {
|
|
2145
|
+
return this.sqrtP;
|
|
2058
2146
|
}
|
|
2059
2147
|
/**
|
|
2060
|
-
*
|
|
2061
|
-
*
|
|
2062
|
-
*
|
|
2063
|
-
*
|
|
2064
|
-
*
|
|
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
|
-
|
|
2068
|
-
|
|
2069
|
-
const
|
|
2070
|
-
const
|
|
2071
|
-
const
|
|
2072
|
-
const
|
|
2073
|
-
const
|
|
2074
|
-
const
|
|
2075
|
-
|
|
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
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
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
|
-
*
|
|
2086
|
-
*
|
|
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
|
-
|
|
2091
|
-
const
|
|
2092
|
-
const
|
|
2093
|
-
const
|
|
2094
|
-
|
|
2095
|
-
|
|
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$2;
|
|
2187
|
+
return numerator * ONE_E18$2 / sqrtP2;
|
|
2188
|
+
}
|
|
2189
|
+
if (quoteToken.equal(this.token1.address)) {
|
|
2190
|
+
const numerator = 10n ** (d0 + d0);
|
|
2191
|
+
return sqrtP2 * numerator / (ONE_E18$2 * ONE_E18$2);
|
|
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
|
-
|
|
2098
|
-
|
|
2099
|
-
sqrtPriceMax,
|
|
2100
|
-
token0Reserve: actualLt,
|
|
2101
|
-
token1Reserve: actualGt
|
|
2212
|
+
token: this.token.toString(),
|
|
2213
|
+
reserve: this.reserve.toString()
|
|
2102
2214
|
};
|
|
2103
2215
|
}
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
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
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
const
|
|
2124
|
-
const
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
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;
|
|
2233
|
+
}
|
|
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
|
+
});
|
|
2142
2248
|
}
|
|
2143
|
-
|
|
2144
|
-
const
|
|
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
|
-
|
|
2147
|
-
|
|
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
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
}
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
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
|
-
|
|
2180
|
-
|
|
2317
|
+
ONE_E18: () => ONE_E18$1,
|
|
2318
|
+
Price: () => Price,
|
|
2319
|
+
PriceRange: () => PriceRange,
|
|
2320
|
+
TokenReserve: () => TokenReserve,
|
|
2181
2321
|
bigintSqrt: () => bigintSqrt,
|
|
2182
2322
|
computeBalances: () => computeBalances,
|
|
2183
2323
|
computeLiquidityAndPrice: () => computeLiquidityAndPrice,
|
|
@@ -3083,11 +3223,188 @@ var PeggedSwapArgs = class PeggedSwapArgs {
|
|
|
3083
3223
|
**/
|
|
3084
3224
|
const peggedSwapGrowPriceRange2D = new Opcode(Symbol("PeggedSwap.peggedSwapGrowPriceRange2D"), PeggedSwapArgs.CODER);
|
|
3085
3225
|
|
|
3226
|
+
//#endregion
|
|
3227
|
+
//#region src/swap-vm/instructions/pegged-swap/pegged-swap-calculator/pegged-swap-calculator.ts
|
|
3228
|
+
const MARGINAL_PRICE_ONE$1 = 10n ** 18n;
|
|
3229
|
+
var PeggedSwapCalculator = class PeggedSwapCalculator {
|
|
3230
|
+
constructor(tokenA, tokenB) {
|
|
3231
|
+
this.tokenA = tokenA;
|
|
3232
|
+
this.tokenB = tokenB;
|
|
3233
|
+
(0, assert.default)(!tokenA.address.equal(tokenB.address), "tokens must be different");
|
|
3234
|
+
}
|
|
3235
|
+
get tokenLt() {
|
|
3236
|
+
return this.tokenA.address.lt(this.tokenB.address) ? this.tokenA : this.tokenB;
|
|
3237
|
+
}
|
|
3238
|
+
get tokenGt() {
|
|
3239
|
+
return this.tokenA.address.lt(this.tokenB.address) ? this.tokenB : this.tokenA;
|
|
3240
|
+
}
|
|
3241
|
+
static new(args) {
|
|
3242
|
+
return new PeggedSwapCalculator(args.tokenA, args.tokenB);
|
|
3243
|
+
}
|
|
3244
|
+
/**
|
|
3245
|
+
* Initial balances before deployment (`currentReserve = initialReserve`, u = v = 1).
|
|
3246
|
+
* Given spot price and one raw initial reserve, returns the other.
|
|
3247
|
+
*/
|
|
3248
|
+
computeFixedAllocation(spotPrice, fixedReserveForToken, fixedReserve) {
|
|
3249
|
+
(0, assert.default)(fixedReserve > 0n, "fixed reserve must be positive");
|
|
3250
|
+
(0, assert.default)(spotPrice.matchesTokens(this.tokenLt.address, this.tokenGt.address), "spot price must match calculator token pair");
|
|
3251
|
+
const marginalE18 = spotPrice.toGtPerLtE18();
|
|
3252
|
+
if (fixedReserveForToken.equal(this.tokenLt.address)) return {
|
|
3253
|
+
reserveLt: fixedReserve,
|
|
3254
|
+
reserveGt: fixedReserve * marginalE18 / MARGINAL_PRICE_ONE$1
|
|
3255
|
+
};
|
|
3256
|
+
if (fixedReserveForToken.equal(this.tokenGt.address)) return {
|
|
3257
|
+
reserveLt: fixedReserve * MARGINAL_PRICE_ONE$1 / marginalE18,
|
|
3258
|
+
reserveGt: fixedReserve
|
|
3259
|
+
};
|
|
3260
|
+
throw new Error("fixedReserveForToken token must be one of the two pair tokens");
|
|
3261
|
+
}
|
|
3262
|
+
};
|
|
3263
|
+
|
|
3264
|
+
//#endregion
|
|
3265
|
+
//#region src/swap-vm/instructions/pegged-swap/pegged-swap-math/pegged-swap-math.ts
|
|
3266
|
+
/** Matches `PeggedSwapMath.ONE` in swap-vm. */
|
|
3267
|
+
const PEGGED_SWAP_ONE = 10n ** 27n;
|
|
3268
|
+
const MARGINAL_PRICE_ONE = 10n ** 18n;
|
|
3269
|
+
/**
|
|
3270
|
+
* Spot price tokenGt per tokenLt (raw) in 1e18 fixed-point.
|
|
3271
|
+
*
|
|
3272
|
+
* P = (Y₀/X₀) · (1/(2√u) + A) / (1/(2√v) + A) · (rateLt/rateGt)
|
|
3273
|
+
*
|
|
3274
|
+
* where u = x·ONE/X₀, v = y·ONE/Y₀, x/y are rate-adjusted Lt/Gt balances, A = `linearWidth`.
|
|
3275
|
+
*/
|
|
3276
|
+
function peggedSwapMarginalGtPerLtE18(balanceLtNorm, balanceGtNorm, x0, y0, linearWidth, rateLt, rateGt) {
|
|
3277
|
+
const u = normalizeReserve(balanceLtNorm, x0);
|
|
3278
|
+
const v = normalizeReserve(balanceGtNorm, y0);
|
|
3279
|
+
(0, assert.default)(u !== 0n && v !== 0n, "PeggedSwapMath: reserves cannot be zero");
|
|
3280
|
+
const slopeLt = peggedSwapMarginalWeight(bigintSqrt(u * PEGGED_SWAP_ONE), linearWidth);
|
|
3281
|
+
const slopeGt = peggedSwapMarginalWeight(bigintSqrt(v * PEGGED_SWAP_ONE), linearWidth);
|
|
3282
|
+
return y0 * slopeLt * rateLt * MARGINAL_PRICE_ONE / (x0 * slopeGt * rateGt);
|
|
3283
|
+
}
|
|
3284
|
+
/**
|
|
3285
|
+
* u = x·ONE/X₀, v = y·ONE/Y₀ (x, y are rate-adjusted reserves).
|
|
3286
|
+
*/
|
|
3287
|
+
function normalizeReserve(currentReserve, initialReserve) {
|
|
3288
|
+
return mulDiv(currentReserve, PEGGED_SWAP_ONE, initialReserve);
|
|
3289
|
+
}
|
|
3290
|
+
/**
|
|
3291
|
+
* Marginal weight `1/(2√u) + A` (Lt side) or `1/(2√v) + A` (Gt side), A = `linearWidth`.
|
|
3292
|
+
*/
|
|
3293
|
+
function peggedSwapMarginalWeight(sqrtCoord, linearWidth) {
|
|
3294
|
+
return mulDiv(PEGGED_SWAP_ONE, PEGGED_SWAP_ONE, 2n * sqrtCoord) + linearWidth;
|
|
3295
|
+
}
|
|
3296
|
+
function mulDiv(a, b, c) {
|
|
3297
|
+
if (c === 0n) throw new Error("mulDiv: division by zero");
|
|
3298
|
+
return a * b / c;
|
|
3299
|
+
}
|
|
3300
|
+
|
|
3301
|
+
//#endregion
|
|
3302
|
+
//#region src/swap-vm/instructions/pegged-swap/price/pegged-price.ts
|
|
3303
|
+
const ONE_E18 = 10n ** 18n;
|
|
3304
|
+
var PeggedPrice = class PeggedPrice {
|
|
3305
|
+
constructor(gtPerLtRaw, tokenLt, tokenGt) {
|
|
3306
|
+
this.gtPerLtRaw = gtPerLtRaw;
|
|
3307
|
+
this.tokenLt = tokenLt;
|
|
3308
|
+
this.tokenGt = tokenGt;
|
|
3309
|
+
(0, assert.default)(gtPerLtRaw > 0n, "price must be positive");
|
|
3310
|
+
(0, assert.default)(tokenLt.address.lt(tokenGt.address), "internal pair order violated");
|
|
3311
|
+
}
|
|
3312
|
+
/**
|
|
3313
|
+
* Spot price from per-token `initialReserve` / `currentReserve` (raw, not rate-scaled) and `linearWidth`.
|
|
3314
|
+
* Use currentReserve = initialReserve to calculate the spot price before the strategy was deployed
|
|
3315
|
+
*/
|
|
3316
|
+
static fromReserves(input) {
|
|
3317
|
+
(0, assert.default)(input.reserveA.currentReserve > 0n && input.reserveB.currentReserve > 0n, "current reserves should be positive");
|
|
3318
|
+
(0, assert.default)(input.reserveA.initialReserve > 0n && input.reserveB.initialReserve > 0n, "initial reserves should be positive");
|
|
3319
|
+
const zeroForOne = input.reserveA.address.lt(input.reserveB.address);
|
|
3320
|
+
const reserveLt = zeroForOne ? input.reserveA : input.reserveB;
|
|
3321
|
+
const reserveGt = zeroForOne ? input.reserveB : input.reserveA;
|
|
3322
|
+
const rateLt = resolveRate(reserveLt.decimals, reserveGt.decimals);
|
|
3323
|
+
const rateGt = resolveRate(reserveGt.decimals, reserveLt.decimals);
|
|
3324
|
+
const initialLtNorm = reserveLt.initialReserve * rateLt;
|
|
3325
|
+
const initialGtNorm = reserveGt.initialReserve * rateGt;
|
|
3326
|
+
const marginalE18 = peggedSwapMarginalGtPerLtE18(reserveLt.currentReserve * rateLt, reserveGt.currentReserve * rateGt, initialLtNorm, initialGtNorm, input.linearWidth, rateLt, rateGt);
|
|
3327
|
+
return PeggedPrice.fromGtPerLtE18(marginalE18, reserveLt, reserveGt);
|
|
3328
|
+
}
|
|
3329
|
+
/**
|
|
3330
|
+
* Human decimal string for **quote per 1 base**.
|
|
3331
|
+
*/
|
|
3332
|
+
static fromHuman(price, pair) {
|
|
3333
|
+
(0, assert.default)(!pair.quoteToken.address.equal(pair.baseToken.address), "quote and base must be different tokens");
|
|
3334
|
+
const quoteToBase = pair.quoteToken.address.lt(pair.baseToken.address);
|
|
3335
|
+
const tokenLt = quoteToBase ? pair.quoteToken : pair.baseToken;
|
|
3336
|
+
const tokenGt = quoteToBase ? pair.baseToken : pair.quoteToken;
|
|
3337
|
+
const parsed = (0, viem.parseUnits)(price.trim(), Number(pair.quoteToken.decimals));
|
|
3338
|
+
const ltDecimals = BigInt(tokenLt.decimals);
|
|
3339
|
+
const gtDecimals = BigInt(tokenGt.decimals);
|
|
3340
|
+
const marginalE18 = quoteToBase ? 10n ** (gtDecimals + 18n + ltDecimals) / (parsed * 10n ** gtDecimals) : parsed * ONE_E18 / 10n ** ltDecimals;
|
|
3341
|
+
return PeggedPrice.fromGtPerLtE18(marginalE18, tokenLt, tokenGt);
|
|
3342
|
+
}
|
|
3343
|
+
static fromJSON(input) {
|
|
3344
|
+
const tokenLt = {
|
|
3345
|
+
address: new __1inch_sdk_core.Address(input.tokenLt.address),
|
|
3346
|
+
decimals: Number(input.tokenLt.decimals)
|
|
3347
|
+
};
|
|
3348
|
+
const tokenGt = {
|
|
3349
|
+
address: new __1inch_sdk_core.Address(input.tokenGt.address),
|
|
3350
|
+
decimals: Number(input.tokenGt.decimals)
|
|
3351
|
+
};
|
|
3352
|
+
(0, assert.default)(tokenLt.address.lt(tokenGt.address), "tokenLt address must be less than tokenGt (canonical order)");
|
|
3353
|
+
return new PeggedPrice(BigInt(input.gtPerLtRaw), tokenLt, tokenGt);
|
|
3354
|
+
}
|
|
3355
|
+
static fromGtPerLtE18(marginalGtPerLtE18, tokenLt, tokenGt) {
|
|
3356
|
+
(0, assert.default)(marginalGtPerLtE18 > 0n, "marginal rate must be positive");
|
|
3357
|
+
const scale = BigInt(tokenLt.decimals + tokenGt.decimals);
|
|
3358
|
+
const gtPerLtRaw = marginalGtPerLtE18 * 10n ** scale / ONE_E18;
|
|
3359
|
+
return new PeggedPrice(gtPerLtRaw, tokenLt, tokenGt);
|
|
3360
|
+
}
|
|
3361
|
+
matchesTokens(tokenA, tokenB) {
|
|
3362
|
+
return tokenA.equal(this.tokenLt.address) && tokenB.equal(this.tokenGt.address) || tokenA.equal(this.tokenGt.address) && tokenB.equal(this.tokenLt.address);
|
|
3363
|
+
}
|
|
3364
|
+
equals(other) {
|
|
3365
|
+
return this.gtPerLtRaw === other.gtPerLtRaw && this.tokenLt.address.equal(other.tokenLt.address) && this.tokenGt.address.equal(other.tokenGt.address) && BigInt(this.tokenLt.decimals) === BigInt(other.tokenLt.decimals) && BigInt(this.tokenGt.decimals) === BigInt(other.tokenGt.decimals);
|
|
3366
|
+
}
|
|
3367
|
+
/**
|
|
3368
|
+
* Decimal string for **quote per 1 base**; rounded half-up to quote token decimals.
|
|
3369
|
+
*/
|
|
3370
|
+
toHuman(quoteToken) {
|
|
3371
|
+
(0, assert.default)(quoteToken.equal(this.tokenLt.address) || quoteToken.equal(this.tokenGt.address), "quote token must be one of the pair tokens");
|
|
3372
|
+
const isQuoteLt = quoteToken.equal(this.tokenLt.address);
|
|
3373
|
+
const quoteDecimals = isQuoteLt ? this.tokenLt.decimals : this.tokenGt.decimals;
|
|
3374
|
+
const ltDecimals = BigInt(this.tokenLt.decimals);
|
|
3375
|
+
const gtDecimals = BigInt(this.tokenGt.decimals);
|
|
3376
|
+
const marginalE18 = this.toGtPerLtE18();
|
|
3377
|
+
const scaled = quoteToken.equal(this.tokenGt.address) ? marginalE18 * 10n ** ltDecimals / ONE_E18 : 10n ** (gtDecimals + 18n + ltDecimals) / (marginalE18 * 10n ** gtDecimals);
|
|
3378
|
+
const full = (0, viem.formatUnits)(scaled, Number(quoteDecimals));
|
|
3379
|
+
return truncateHumanDecimalString(full, Number(quoteDecimals));
|
|
3380
|
+
}
|
|
3381
|
+
/** Marginal gt-per-lt rate in 1e18 fixed-point. */
|
|
3382
|
+
toGtPerLtE18() {
|
|
3383
|
+
const scale = BigInt(this.tokenLt.decimals + this.tokenGt.decimals);
|
|
3384
|
+
return this.gtPerLtRaw * ONE_E18 / 10n ** scale;
|
|
3385
|
+
}
|
|
3386
|
+
toJSON() {
|
|
3387
|
+
return {
|
|
3388
|
+
gtPerLtRaw: this.gtPerLtRaw.toString(),
|
|
3389
|
+
tokenLt: {
|
|
3390
|
+
address: this.tokenLt.address.toString(),
|
|
3391
|
+
decimals: String(this.tokenLt.decimals)
|
|
3392
|
+
},
|
|
3393
|
+
tokenGt: {
|
|
3394
|
+
address: this.tokenGt.address.toString(),
|
|
3395
|
+
decimals: String(this.tokenGt.decimals)
|
|
3396
|
+
}
|
|
3397
|
+
};
|
|
3398
|
+
}
|
|
3399
|
+
};
|
|
3400
|
+
|
|
3086
3401
|
//#endregion
|
|
3087
3402
|
//#region src/swap-vm/instructions/pegged-swap/index.ts
|
|
3088
3403
|
var pegged_swap_exports = {};
|
|
3089
3404
|
__export(pegged_swap_exports, {
|
|
3405
|
+
PeggedPrice: () => PeggedPrice,
|
|
3090
3406
|
PeggedSwapArgs: () => PeggedSwapArgs,
|
|
3407
|
+
PeggedSwapCalculator: () => PeggedSwapCalculator,
|
|
3091
3408
|
peggedSwapGrowPriceRange2D: () => peggedSwapGrowPriceRange2D
|
|
3092
3409
|
});
|
|
3093
3410
|
|
|
@@ -3193,6 +3510,7 @@ __export(instructions_exports, {
|
|
|
3193
3510
|
aquaInstructions: () => aquaInstructions,
|
|
3194
3511
|
balances: () => balances_exports,
|
|
3195
3512
|
baseFeeAdjuster: () => base_fee_adjuster_exports,
|
|
3513
|
+
bigintSqrt: () => bigintSqrt,
|
|
3196
3514
|
concentrate: () => concentrate_exports,
|
|
3197
3515
|
controls: () => controls_exports,
|
|
3198
3516
|
decay: () => decay_exports,
|
|
@@ -3205,6 +3523,7 @@ __export(instructions_exports, {
|
|
|
3205
3523
|
oraclePriceAdjuster: () => oracle_price_adjuster_exports,
|
|
3206
3524
|
peggedSwap: () => pegged_swap_exports,
|
|
3207
3525
|
stableSwap: () => pegged_swap_exports,
|
|
3526
|
+
truncateHumanDecimalString: () => truncateHumanDecimalString,
|
|
3208
3527
|
twapSwap: () => twap_swap_exports,
|
|
3209
3528
|
xycSwap: () => xyc_swap_exports
|
|
3210
3529
|
});
|