@1inch/swap-vm-sdk 0.1.2-rc.5 → 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.
- package/dist/index.js +258 -118
- package/dist/index.mjs +259 -119
- package/dist/swap-vm/instructions/balances/balances-args.d.mts +2 -2
- package/dist/swap-vm/instructions/balances/balances-args.d.ts +2 -2
- package/dist/swap-vm/instructions/base-fee-adjuster/base-fee-adjuster-args.d.mts +2 -2
- package/dist/swap-vm/instructions/base-fee-adjuster/base-fee-adjuster-args.d.ts +2 -2
- package/dist/swap-vm/instructions/base-fee-adjuster/opcodes.d.mts +3 -1
- package/dist/swap-vm/instructions/base-fee-adjuster/opcodes.d.ts +3 -1
- package/dist/swap-vm/instructions/concentrate/concentrate-grow-liquidity-2d-args.d.mts +2 -2
- package/dist/swap-vm/instructions/concentrate/concentrate-grow-liquidity-2d-args.d.ts +2 -2
- 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 +7 -2
- package/dist/swap-vm/instructions/concentrate/index.d.ts +7 -2
- package/dist/swap-vm/instructions/concentrate/opcodes.d.mts +3 -1
- package/dist/swap-vm/instructions/concentrate/opcodes.d.ts +3 -1
- 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/truncate-human-decimal-string.d.mts +9 -0
- package/dist/swap-vm/instructions/concentrate/price/truncate-human-decimal-string.d.ts +9 -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/controls/deadline-args.d.mts +2 -2
- package/dist/swap-vm/instructions/controls/deadline-args.d.ts +2 -2
- package/dist/swap-vm/instructions/controls/jump-args.d.mts +2 -2
- package/dist/swap-vm/instructions/controls/jump-args.d.ts +2 -2
- package/dist/swap-vm/instructions/controls/jump-if-token-args.d.mts +2 -2
- package/dist/swap-vm/instructions/controls/jump-if-token-args.d.ts +2 -2
- package/dist/swap-vm/instructions/controls/only-taker-token-balance-gte-args.d.mts +2 -2
- package/dist/swap-vm/instructions/controls/only-taker-token-balance-gte-args.d.ts +2 -2
- package/dist/swap-vm/instructions/controls/only-taker-token-balance-non-zero-args.d.mts +2 -2
- package/dist/swap-vm/instructions/controls/only-taker-token-balance-non-zero-args.d.ts +2 -2
- package/dist/swap-vm/instructions/controls/salt-args.d.mts +2 -2
- package/dist/swap-vm/instructions/controls/salt-args.d.ts +2 -2
- package/dist/swap-vm/instructions/debug/debug-empty-args.d.mts +1 -6
- package/dist/swap-vm/instructions/debug/debug-empty-args.d.ts +1 -6
- package/dist/swap-vm/instructions/debug/opcodes.d.mts +8 -7
- package/dist/swap-vm/instructions/debug/opcodes.d.ts +8 -7
- package/dist/swap-vm/instructions/debug/print-amount-for-swap.d.mts +1 -6
- package/dist/swap-vm/instructions/debug/print-amount-for-swap.d.ts +1 -6
- package/dist/swap-vm/instructions/debug/print-context.d.mts +1 -6
- package/dist/swap-vm/instructions/debug/print-context.d.ts +1 -6
- package/dist/swap-vm/instructions/debug/print-free-memory-pointer.d.mts +1 -6
- package/dist/swap-vm/instructions/debug/print-free-memory-pointer.d.ts +1 -6
- package/dist/swap-vm/instructions/debug/print-gas-left.d.mts +1 -6
- package/dist/swap-vm/instructions/debug/print-gas-left.d.ts +1 -6
- package/dist/swap-vm/instructions/debug/print-swap-query.d.mts +1 -6
- package/dist/swap-vm/instructions/debug/print-swap-query.d.ts +1 -6
- package/dist/swap-vm/instructions/debug/print-swap-registers.d.mts +1 -6
- package/dist/swap-vm/instructions/debug/print-swap-registers.d.ts +1 -6
- package/dist/swap-vm/instructions/decay/decay-xd-args.d.mts +2 -2
- package/dist/swap-vm/instructions/decay/decay-xd-args.d.ts +2 -2
- package/dist/swap-vm/instructions/decay/opcodes.d.mts +3 -1
- package/dist/swap-vm/instructions/decay/opcodes.d.ts +3 -1
- package/dist/swap-vm/instructions/dutch-auction/dutch-auction-args.d.mts +2 -2
- package/dist/swap-vm/instructions/dutch-auction/dutch-auction-args.d.ts +2 -2
- package/dist/swap-vm/instructions/dutch-auction/opcodes.d.mts +4 -2
- package/dist/swap-vm/instructions/dutch-auction/opcodes.d.ts +4 -2
- package/dist/swap-vm/instructions/extruction/extruction-args.d.mts +2 -2
- package/dist/swap-vm/instructions/extruction/extruction-args.d.ts +2 -2
- package/dist/swap-vm/instructions/extruction/opcodes.d.mts +3 -1
- package/dist/swap-vm/instructions/extruction/opcodes.d.ts +3 -1
- package/dist/swap-vm/instructions/fee/dynamic-protocol-fee/dynamic-protocol-fee-args.d.mts +2 -2
- package/dist/swap-vm/instructions/fee/dynamic-protocol-fee/dynamic-protocol-fee-args.d.ts +2 -2
- package/dist/swap-vm/instructions/fee/flat-fee/flat-fee-args.d.mts +2 -2
- package/dist/swap-vm/instructions/fee/flat-fee/flat-fee-args.d.ts +2 -2
- package/dist/swap-vm/instructions/fee/opcodes.d.mts +14 -10
- package/dist/swap-vm/instructions/fee/opcodes.d.ts +14 -10
- package/dist/swap-vm/instructions/fee/protocol-fee/protocol-fee-args.d.mts +2 -2
- package/dist/swap-vm/instructions/fee/protocol-fee/protocol-fee-args.d.ts +2 -2
- package/dist/swap-vm/instructions/invalidators/invalidate-bit-1d-args.d.mts +2 -2
- package/dist/swap-vm/instructions/invalidators/invalidate-bit-1d-args.d.ts +2 -2
- package/dist/swap-vm/instructions/invalidators/invalidate-token-in-1d-args.d.mts +2 -2
- package/dist/swap-vm/instructions/invalidators/invalidate-token-in-1d-args.d.ts +2 -2
- package/dist/swap-vm/instructions/invalidators/invalidate-token-out-1d-args.d.mts +2 -2
- package/dist/swap-vm/instructions/invalidators/invalidate-token-out-1d-args.d.ts +2 -2
- package/dist/swap-vm/instructions/invalidators/opcodes.d.mts +7 -3
- package/dist/swap-vm/instructions/invalidators/opcodes.d.ts +7 -3
- package/dist/swap-vm/instructions/limit-swap/limit-swap-direction-args.d.mts +2 -2
- package/dist/swap-vm/instructions/limit-swap/limit-swap-direction-args.d.ts +2 -2
- package/dist/swap-vm/instructions/limit-swap/opcodes.d.mts +4 -2
- package/dist/swap-vm/instructions/limit-swap/opcodes.d.ts +4 -2
- package/dist/swap-vm/instructions/min-rate/min-rate-args.d.mts +2 -2
- package/dist/swap-vm/instructions/min-rate/min-rate-args.d.ts +2 -2
- package/dist/swap-vm/instructions/min-rate/opcodes.d.mts +4 -2
- package/dist/swap-vm/instructions/min-rate/opcodes.d.ts +4 -2
- package/dist/swap-vm/instructions/oracle-price-adjuster/opcodes.d.mts +3 -1
- package/dist/swap-vm/instructions/oracle-price-adjuster/opcodes.d.ts +3 -1
- package/dist/swap-vm/instructions/oracle-price-adjuster/oracle-price-adjuster-args.d.mts +2 -2
- package/dist/swap-vm/instructions/oracle-price-adjuster/oracle-price-adjuster-args.d.ts +2 -2
- package/dist/swap-vm/instructions/pegged-swap/opcodes.d.mts +3 -1
- package/dist/swap-vm/instructions/pegged-swap/opcodes.d.ts +3 -1
- package/dist/swap-vm/instructions/pegged-swap/pegged-swap-args.d.mts +2 -2
- package/dist/swap-vm/instructions/pegged-swap/pegged-swap-args.d.ts +2 -2
- package/dist/swap-vm/instructions/twap-swap/opcodes.d.mts +3 -1
- package/dist/swap-vm/instructions/twap-swap/opcodes.d.ts +3 -1
- package/dist/swap-vm/instructions/twap-swap/twap-swap-args.d.mts +2 -2
- package/dist/swap-vm/instructions/twap-swap/twap-swap-args.d.ts +2 -2
- package/dist/swap-vm/instructions/xyc-swap/opcodes.d.mts +3 -1
- package/dist/swap-vm/instructions/xyc-swap/opcodes.d.ts +3 -1
- package/dist/swap-vm/instructions/xyc-swap/xyc-swap-xd-args.d.mts +2 -2
- package/dist/swap-vm/instructions/xyc-swap/xyc-swap-xd-args.d.ts +2 -2
- package/dist/swap-vm/maker-traits.d.mts +1 -1
- package/dist/swap-vm/maker-traits.d.ts +1 -1
- package/package.json +1 -1
- 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/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/
|
|
2028
|
+
//#region src/swap-vm/instructions/concentrate/price/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$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
|
-
*
|
|
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$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
|
-
*
|
|
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$1;
|
|
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$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
|
-
|
|
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
|
-
* 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
|
-
|
|
2144
|
-
|
|
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
|
-
|
|
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
|
-
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,
|