@pancakeswap/v3-sdk 3.8.8 → 3.8.10
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/functions/getCurrencyPrice.d.ts +11 -0
- package/dist/functions/getCurrencyPrice.d.ts.map +1 -0
- package/dist/functions/getPool.d.ts +23 -0
- package/dist/functions/getPool.d.ts.map +1 -0
- package/dist/functions/getPriceOfCurrency.d.ts +6 -0
- package/dist/functions/getPriceOfCurrency.d.ts.map +1 -0
- package/dist/functions/hasInvolvedCurrency.d.ts +4 -0
- package/dist/functions/hasInvolvedCurrency.d.ts.map +1 -0
- package/dist/functions/index.d.ts +6 -0
- package/dist/functions/index.d.ts.map +1 -0
- package/dist/functions/swap.d.ts +31 -0
- package/dist/functions/swap.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +199 -12
- package/dist/index.mjs +191 -14
- package/dist/utils/maxLiquidityForAmounts.d.ts.map +1 -1
- package/dist/utils/sqrtPriceMath.d.ts.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BigintIsh, Price } from '@pancakeswap/sdk';
|
|
2
|
+
import { Currency } from '@pancakeswap/swap-sdk-core';
|
|
3
|
+
/**
|
|
4
|
+
* Returns the current price of the given tokens
|
|
5
|
+
*/
|
|
6
|
+
export declare const getCurrency0Price: ([currency0, currency1]: [Currency, Currency], currentSqrtRatioX96: BigintIsh) => Price<Currency, Currency>;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the current price of the given tokens
|
|
9
|
+
*/
|
|
10
|
+
export declare const getCurrency1Price: ([currency0, currency1]: [Currency, Currency], currentSqrtRatioX96: BigintIsh) => Price<Currency, Currency>;
|
|
11
|
+
//# sourceMappingURL=getCurrencyPrice.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCurrencyPrice.d.ts","sourceRoot":"","sources":["../../src/functions/getCurrencyPrice.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAA;AAGrD;;GAEG;AACH,eAAO,MAAM,iBAAiB,2BACJ,CAAC,QAAQ,EAAE,QAAQ,CAAC,uBACvB,SAAS,KAC7B,MAAM,QAAQ,EAAE,QAAQ,CAI1B,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,iBAAiB,2BACJ,CAAC,QAAQ,EAAE,QAAQ,CAAC,uBACvB,SAAS,KAC7B,MAAM,QAAQ,EAAE,QAAQ,CAI1B,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { BigintIsh, Currency } from '@pancakeswap/swap-sdk-core';
|
|
2
|
+
import { Tick, TickConstructorArgs, TickDataProvider } from '../entities';
|
|
3
|
+
export type PoolState = {
|
|
4
|
+
currency0: Currency;
|
|
5
|
+
currency1: Currency;
|
|
6
|
+
fee: number;
|
|
7
|
+
tickCurrent: number;
|
|
8
|
+
tickSpacing: number;
|
|
9
|
+
sqrtRatioX96: bigint;
|
|
10
|
+
liquidity: bigint;
|
|
11
|
+
tickDataProvider: TickDataProvider;
|
|
12
|
+
};
|
|
13
|
+
export declare const getPool: ({ currencyA, currencyB, fee, sqrtRatioX96, liquidity, tickCurrent, tickSpacing, ticks, }: {
|
|
14
|
+
currencyA: Currency;
|
|
15
|
+
currencyB: Currency;
|
|
16
|
+
fee: number;
|
|
17
|
+
sqrtRatioX96: BigintIsh;
|
|
18
|
+
liquidity: BigintIsh;
|
|
19
|
+
tickCurrent: number;
|
|
20
|
+
tickSpacing: number;
|
|
21
|
+
ticks?: TickDataProvider | (TickConstructorArgs | Tick)[] | undefined;
|
|
22
|
+
}) => PoolState;
|
|
23
|
+
//# sourceMappingURL=getPool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPool.d.ts","sourceRoot":"","sources":["../../src/functions/getPool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAkB,MAAM,4BAA4B,CAAA;AAEhF,OAAO,EAAsB,IAAI,EAAE,mBAAmB,EAAE,gBAAgB,EAAwB,MAAM,aAAa,CAAA;AAEnH,MAAM,MAAM,SAAS,GAAG;IACtB,SAAS,EAAE,QAAQ,CAAA;IACnB,SAAS,EAAE,QAAQ,CAAA;IACnB,GAAG,EAAE,MAAM,CAAA;IACX,WAAW,EAAE,MAAM,CAAA;IACnB,WAAW,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE,MAAM,CAAA;IACpB,SAAS,EAAE,MAAM,CAAA;IACjB,gBAAgB,EAAE,gBAAgB,CAAA;CACnC,CAAA;AAQD,eAAO,MAAM,OAAO;eAUP,QAAQ;eACR,QAAQ;SACd,MAAM;kBACG,SAAS;eACZ,SAAS;iBACP,MAAM;iBACN,MAAM;;MAEjB,SAeH,CAAA"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Currency } from '@pancakeswap/swap-sdk-core';
|
|
2
|
+
import { PoolState } from './getPool';
|
|
3
|
+
export declare const getPriceOfCurrency: (pool: Pick<PoolState, 'currency0' | 'currency1' | 'sqrtRatioX96'>, currency: Currency) => import("@pancakeswap/swap-sdk-core").Price<Currency, Currency>;
|
|
4
|
+
export declare const getPriceOfCurrency0: (pool: Pick<PoolState, 'currency0' | 'currency1' | 'sqrtRatioX96'>) => import("@pancakeswap/swap-sdk-core").Price<Currency, Currency>;
|
|
5
|
+
export declare const getPriceOfCurrency1: (pool: Pick<PoolState, 'currency0' | 'currency1' | 'sqrtRatioX96'>) => import("@pancakeswap/swap-sdk-core").Price<Currency, Currency>;
|
|
6
|
+
//# sourceMappingURL=getPriceOfCurrency.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPriceOfCurrency.d.ts","sourceRoot":"","sources":["../../src/functions/getPriceOfCurrency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAA;AAGrD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErC,eAAO,MAAM,kBAAkB,SACvB,KAAK,SAAS,EAAE,WAAW,GAAG,WAAW,GAAG,cAAc,CAAC,YACvD,QAAQ,mEAWnB,CAAA;AAED,eAAO,MAAM,mBAAmB,SAAU,KAAK,SAAS,EAAE,WAAW,GAAG,WAAW,GAAG,cAAc,CAAC,mEAC7B,CAAA;AAExE,eAAO,MAAM,mBAAmB,SAAU,KAAK,SAAS,EAAE,WAAW,GAAG,WAAW,GAAG,cAAc,CAAC,mEAC7B,CAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Currency } from '@pancakeswap/swap-sdk-core';
|
|
2
|
+
import { PoolState } from './getPool';
|
|
3
|
+
export declare const hasInvolvedCurrency: (pool: Pick<PoolState, 'currency0' | 'currency1'>, currency: Currency) => boolean;
|
|
4
|
+
//# sourceMappingURL=hasInvolvedCurrency.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hasInvolvedCurrency.d.ts","sourceRoot":"","sources":["../../src/functions/hasInvolvedCurrency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAA;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErC,eAAO,MAAM,mBAAmB,SAAU,KAAK,SAAS,EAAE,WAAW,GAAG,WAAW,CAAC,YAAY,QAAQ,YAGvG,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,WAAW,CAAA;AACzB,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,QAAQ,CAAA"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Currency, CurrencyAmount } from '@pancakeswap/sdk';
|
|
2
|
+
import { PoolState } from './getPool';
|
|
3
|
+
export declare const swap: ({ pool, zeroForOne, amountSpecified, sqrtPriceLimitX96, }: {
|
|
4
|
+
pool: PoolState;
|
|
5
|
+
zeroForOne: boolean;
|
|
6
|
+
amountSpecified: bigint;
|
|
7
|
+
sqrtPriceLimitX96?: bigint | undefined;
|
|
8
|
+
}) => Promise<{
|
|
9
|
+
amountCalculated: bigint;
|
|
10
|
+
sqrtRatioX96: bigint;
|
|
11
|
+
liquidity: bigint;
|
|
12
|
+
tickCurrent: number;
|
|
13
|
+
amountSpecifiedRemaining: bigint;
|
|
14
|
+
}>;
|
|
15
|
+
/**
|
|
16
|
+
* Given a desired input amount of a token, return the computed output
|
|
17
|
+
* amount and a pool with state updated after the trade
|
|
18
|
+
*/
|
|
19
|
+
export declare const getOutputAmount: <TCurrencyOut extends Currency = Currency, TCurrencyIn extends Currency = Currency>(pool: PoolState, inputAmount: CurrencyAmount<TCurrencyIn>, options?: {
|
|
20
|
+
sqrtPriceLimitX96?: bigint;
|
|
21
|
+
exact?: boolean;
|
|
22
|
+
}) => Promise<[CurrencyAmount<TCurrencyOut>, PoolState]>;
|
|
23
|
+
/**
|
|
24
|
+
* Given a desired output amount of a token, return the computed input
|
|
25
|
+
* amount and a pool with state updated after the trade
|
|
26
|
+
*/
|
|
27
|
+
export declare const getInputAmount: <TCurrencyIn extends Currency = Currency, TCurrencyOut extends Currency = Currency>(pool: PoolState, outputAmount: CurrencyAmount<TCurrencyOut>, options?: {
|
|
28
|
+
sqrtPriceLimitX96?: bigint;
|
|
29
|
+
exact?: boolean;
|
|
30
|
+
}) => Promise<[CurrencyAmount<TCurrencyIn>, PoolState]>;
|
|
31
|
+
//# sourceMappingURL=swap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"swap.d.ts","sourceRoot":"","sources":["../../src/functions/swap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AAI3D,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAarC,eAAO,MAAM,IAAI;UAMT,SAAS;gBACH,OAAO;qBACF,MAAM;;MAErB,QAAQ;IACV,gBAAgB,EAAE,MAAM,CAAA;IACxB,YAAY,EAAE,MAAM,CAAA;IACpB,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,wBAAwB,EAAE,MAAM,CAAA;CACjC,CA0FA,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,eAAe,4FAIpB,SAAS,sDAEL;IACR,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAE1B,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,uDAkCF,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,4FACnB,SAAS,wDAEL;IACR,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAE1B,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,sDAmCF,CAAA"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AAEvB,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,8BAA8B,CAAA;AAC5C,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAC5B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAG5B,cAAc,sBAAsB,CAAA;AACpC,cAAc,oBAAoB,CAAA;AAClC,cAAc,kCAAkC,CAAA;AAChD,cAAc,gCAAgC,CAAA;AAC9C,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AAEvB,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,8BAA8B,CAAA;AAC5C,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAC5B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAG5B,cAAc,sBAAsB,CAAA;AACpC,cAAc,oBAAoB,CAAA;AAClC,cAAc,kCAAkC,CAAA;AAChD,cAAc,gCAAgC,CAAA;AAC9C,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -212,8 +212,7 @@ var SqrtPriceMath = class {
|
|
|
212
212
|
}
|
|
213
213
|
static getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
214
214
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
215
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
216
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
215
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
217
216
|
}
|
|
218
217
|
const numerator1 = liquidity << 96n;
|
|
219
218
|
const numerator2 = sqrtRatioBX96 - sqrtRatioAX96;
|
|
@@ -221,8 +220,7 @@ var SqrtPriceMath = class {
|
|
|
221
220
|
}
|
|
222
221
|
static getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
223
222
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
224
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
225
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
223
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
226
224
|
}
|
|
227
225
|
return roundUp ? FullMath.mulDivRoundingUp(liquidity, sqrtRatioBX96 - sqrtRatioAX96, Q96) : liquidity * (sqrtRatioBX96 - sqrtRatioAX96) / Q96;
|
|
228
226
|
}
|
|
@@ -811,16 +809,14 @@ var Pool = class {
|
|
|
811
809
|
// src/utils/maxLiquidityForAmounts.ts
|
|
812
810
|
function maxLiquidityForAmount0Imprecise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
813
811
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
814
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
815
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
812
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
816
813
|
}
|
|
817
814
|
const intermediate = sqrtRatioAX96 * sqrtRatioBX96 / Q96;
|
|
818
815
|
return BigInt(amount0) * intermediate / (sqrtRatioBX96 - sqrtRatioAX96);
|
|
819
816
|
}
|
|
820
817
|
function maxLiquidityForAmount0Precise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
821
818
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
822
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
823
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
819
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
824
820
|
}
|
|
825
821
|
const numerator = BigInt(amount0) * sqrtRatioAX96 * sqrtRatioBX96;
|
|
826
822
|
const denominator = Q96 * (sqrtRatioBX96 - sqrtRatioAX96);
|
|
@@ -828,15 +824,13 @@ function maxLiquidityForAmount0Precise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
|
828
824
|
}
|
|
829
825
|
function maxLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1) {
|
|
830
826
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
831
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
832
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
827
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
833
828
|
}
|
|
834
829
|
return BigInt(amount1) * Q96 / (sqrtRatioBX96 - sqrtRatioAX96);
|
|
835
830
|
}
|
|
836
831
|
function maxLiquidityForAmounts(sqrtRatioCurrentX96, sqrtRatioAX96, sqrtRatioBX96, amount0, amount1, useFullPrecision) {
|
|
837
832
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
838
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
839
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
833
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
840
834
|
}
|
|
841
835
|
const maxLiquidityForAmount0 = useFullPrecision ? maxLiquidityForAmount0Precise : maxLiquidityForAmount0Imprecise;
|
|
842
836
|
if (sqrtRatioCurrentX96 <= sqrtRatioAX96) {
|
|
@@ -1693,6 +1687,56 @@ var Trade = class {
|
|
|
1693
1687
|
return bestTrades;
|
|
1694
1688
|
}
|
|
1695
1689
|
};
|
|
1690
|
+
var getCurrency0Price = ([currency0, currency1], currentSqrtRatioX96) => {
|
|
1691
|
+
const ratio192 = BigInt(currentSqrtRatioX96) * BigInt(currentSqrtRatioX96);
|
|
1692
|
+
return new sdk.Price(currency0, currency1, Q192, ratio192);
|
|
1693
|
+
};
|
|
1694
|
+
var getCurrency1Price = ([currency0, currency1], currentSqrtRatioX96) => {
|
|
1695
|
+
const ratio192 = BigInt(currentSqrtRatioX96) * BigInt(currentSqrtRatioX96);
|
|
1696
|
+
return new sdk.Price(currency1, currency0, Q192, ratio192);
|
|
1697
|
+
};
|
|
1698
|
+
var NO_TICK_DATA_PROVIDER_DEFAULT2 = new NoTickDataProvider();
|
|
1699
|
+
var MAX_FEE_AMOUNT = 1000000n;
|
|
1700
|
+
var getPool = ({
|
|
1701
|
+
currencyA,
|
|
1702
|
+
currencyB,
|
|
1703
|
+
fee,
|
|
1704
|
+
sqrtRatioX96,
|
|
1705
|
+
liquidity,
|
|
1706
|
+
tickCurrent,
|
|
1707
|
+
tickSpacing,
|
|
1708
|
+
ticks = NO_TICK_DATA_PROVIDER_DEFAULT2
|
|
1709
|
+
}) => {
|
|
1710
|
+
invariant9__default.default(Number.isInteger(fee) && BigInt(fee) < MAX_FEE_AMOUNT, "FEE");
|
|
1711
|
+
const [currency0, currency1] = swapSdkCore.sortCurrencies([currencyA, currencyB]);
|
|
1712
|
+
return {
|
|
1713
|
+
currency0,
|
|
1714
|
+
currency1,
|
|
1715
|
+
fee,
|
|
1716
|
+
tickCurrent,
|
|
1717
|
+
tickSpacing,
|
|
1718
|
+
sqrtRatioX96: BigInt(sqrtRatioX96),
|
|
1719
|
+
liquidity: BigInt(liquidity),
|
|
1720
|
+
tickDataProvider: Array.isArray(ticks) ? new TickListDataProvider(ticks) : ticks
|
|
1721
|
+
};
|
|
1722
|
+
};
|
|
1723
|
+
var getPriceOfCurrency = (pool, currency) => {
|
|
1724
|
+
if (currency.isNative) {
|
|
1725
|
+
invariant9__default.default(pool.currency0.isNative, "CURRENCY");
|
|
1726
|
+
return getCurrency0Price([pool.currency0, pool.currency1], pool.sqrtRatioX96);
|
|
1727
|
+
}
|
|
1728
|
+
invariant9__default.default(pool.currency0.equals(currency) || pool.currency1.equals(currency), "CURRENCY");
|
|
1729
|
+
return pool.currency0.equals(currency) ? getCurrency0Price([pool.currency0, pool.currency1], pool.sqrtRatioX96) : getCurrency1Price([pool.currency0, pool.currency1], pool.sqrtRatioX96);
|
|
1730
|
+
};
|
|
1731
|
+
var getPriceOfCurrency0 = (pool) => getCurrency0Price([pool.currency0, pool.currency1], pool.sqrtRatioX96);
|
|
1732
|
+
var getPriceOfCurrency1 = (pool) => getCurrency1Price([pool.currency0, pool.currency1], pool.sqrtRatioX96);
|
|
1733
|
+
|
|
1734
|
+
// src/functions/hasInvolvedCurrency.ts
|
|
1735
|
+
var hasInvolvedCurrency = (pool, currency) => {
|
|
1736
|
+
if (currency.isNative)
|
|
1737
|
+
return pool.currency0.isNative && pool.currency1.chainId === currency.chainId;
|
|
1738
|
+
return pool.currency0.equals(currency) || pool.currency1.equals(currency);
|
|
1739
|
+
};
|
|
1696
1740
|
|
|
1697
1741
|
// src/utils/calldata.ts
|
|
1698
1742
|
function toHex(bigintIsh) {
|
|
@@ -2006,6 +2050,139 @@ var isPoolTickInRange = (pool, tickLower, tickUpper) => {
|
|
|
2006
2050
|
const above = typeof tickUpper === "number" ? pool.tickCurrent >= tickUpper : void 0;
|
|
2007
2051
|
return typeof below === "boolean" && typeof above === "boolean" ? !below && !above : false;
|
|
2008
2052
|
};
|
|
2053
|
+
|
|
2054
|
+
// src/functions/swap.ts
|
|
2055
|
+
var swap = async ({
|
|
2056
|
+
pool,
|
|
2057
|
+
zeroForOne,
|
|
2058
|
+
amountSpecified,
|
|
2059
|
+
sqrtPriceLimitX96
|
|
2060
|
+
}) => {
|
|
2061
|
+
if (!sqrtPriceLimitX96)
|
|
2062
|
+
sqrtPriceLimitX96 = zeroForOne ? TickMath.MIN_SQRT_RATIO + ONE : TickMath.MAX_SQRT_RATIO - ONE;
|
|
2063
|
+
if (zeroForOne) {
|
|
2064
|
+
invariant9__default.default(sqrtPriceLimitX96 > TickMath.MIN_SQRT_RATIO, "RATIO_MIN");
|
|
2065
|
+
invariant9__default.default(sqrtPriceLimitX96 < pool.sqrtRatioX96, "RATIO_CURRENT");
|
|
2066
|
+
} else {
|
|
2067
|
+
invariant9__default.default(sqrtPriceLimitX96 < TickMath.MAX_SQRT_RATIO, "RATIO_MAX");
|
|
2068
|
+
invariant9__default.default(sqrtPriceLimitX96 > pool.sqrtRatioX96, "RATIO_CURRENT");
|
|
2069
|
+
}
|
|
2070
|
+
const exactInput = amountSpecified >= ZERO;
|
|
2071
|
+
const state = {
|
|
2072
|
+
amountSpecifiedRemaining: amountSpecified,
|
|
2073
|
+
amountCalculated: ZERO,
|
|
2074
|
+
sqrtPriceX96: pool.sqrtRatioX96,
|
|
2075
|
+
tick: pool.tickCurrent,
|
|
2076
|
+
liquidity: pool.liquidity
|
|
2077
|
+
};
|
|
2078
|
+
while (state.amountSpecifiedRemaining !== ZERO && state.sqrtPriceX96 != sqrtPriceLimitX96) {
|
|
2079
|
+
const step = {};
|
|
2080
|
+
step.sqrtPriceStartX96 = state.sqrtPriceX96;
|
|
2081
|
+
[step.tickNext, step.initialized] = await pool.tickDataProvider.nextInitializedTickWithinOneWord(
|
|
2082
|
+
state.tick,
|
|
2083
|
+
zeroForOne,
|
|
2084
|
+
pool.tickSpacing
|
|
2085
|
+
);
|
|
2086
|
+
if (step.tickNext < TickMath.MIN_TICK) {
|
|
2087
|
+
step.tickNext = TickMath.MIN_TICK;
|
|
2088
|
+
} else if (step.tickNext > TickMath.MAX_TICK) {
|
|
2089
|
+
step.tickNext = TickMath.MAX_TICK;
|
|
2090
|
+
}
|
|
2091
|
+
step.sqrtPriceNextX96 = TickMath.getSqrtRatioAtTick(step.tickNext);
|
|
2092
|
+
[state.sqrtPriceX96, step.amountIn, step.amountOut, step.feeAmount] = SwapMath.computeSwapStep(
|
|
2093
|
+
state.sqrtPriceX96,
|
|
2094
|
+
(zeroForOne ? step.sqrtPriceNextX96 < sqrtPriceLimitX96 : step.sqrtPriceNextX96 > sqrtPriceLimitX96) ? sqrtPriceLimitX96 : step.sqrtPriceNextX96,
|
|
2095
|
+
state.liquidity,
|
|
2096
|
+
state.amountSpecifiedRemaining,
|
|
2097
|
+
pool.fee
|
|
2098
|
+
);
|
|
2099
|
+
if (exactInput) {
|
|
2100
|
+
state.amountSpecifiedRemaining -= step.amountIn + step.feeAmount;
|
|
2101
|
+
state.amountCalculated = state.amountCalculated - step.amountOut;
|
|
2102
|
+
} else {
|
|
2103
|
+
state.amountSpecifiedRemaining = state.amountSpecifiedRemaining + step.amountOut;
|
|
2104
|
+
state.amountCalculated = state.amountCalculated + (step.amountIn + step.feeAmount);
|
|
2105
|
+
}
|
|
2106
|
+
if (state.sqrtPriceX96 === step.sqrtPriceNextX96) {
|
|
2107
|
+
if (step.initialized) {
|
|
2108
|
+
let liquidityNet = BigInt((await pool.tickDataProvider.getTick(step.tickNext)).liquidityNet);
|
|
2109
|
+
if (zeroForOne)
|
|
2110
|
+
liquidityNet *= NEGATIVE_ONE;
|
|
2111
|
+
state.liquidity = LiquidityMath.addDelta(state.liquidity, liquidityNet);
|
|
2112
|
+
}
|
|
2113
|
+
state.tick = zeroForOne ? step.tickNext - 1 : step.tickNext;
|
|
2114
|
+
} else if (state.sqrtPriceX96 !== step.sqrtPriceStartX96) {
|
|
2115
|
+
state.tick = TickMath.getTickAtSqrtRatio(state.sqrtPriceX96);
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
return {
|
|
2119
|
+
amountSpecifiedRemaining: state.amountSpecifiedRemaining,
|
|
2120
|
+
amountCalculated: state.amountCalculated,
|
|
2121
|
+
sqrtRatioX96: state.sqrtPriceX96,
|
|
2122
|
+
liquidity: state.liquidity,
|
|
2123
|
+
tickCurrent: state.tick
|
|
2124
|
+
};
|
|
2125
|
+
};
|
|
2126
|
+
var getOutputAmount = async (pool, inputAmount, options) => {
|
|
2127
|
+
invariant9__default.default(hasInvolvedCurrency(pool, inputAmount.currency), "CURRENCY");
|
|
2128
|
+
const zeroForOne = inputAmount.currency.equals(pool.currency0);
|
|
2129
|
+
const { sqrtPriceLimitX96, exact } = options || {};
|
|
2130
|
+
const {
|
|
2131
|
+
amountCalculated: outputAmount,
|
|
2132
|
+
sqrtRatioX96,
|
|
2133
|
+
liquidity,
|
|
2134
|
+
tickCurrent,
|
|
2135
|
+
amountSpecifiedRemaining
|
|
2136
|
+
} = await swap({
|
|
2137
|
+
pool,
|
|
2138
|
+
zeroForOne,
|
|
2139
|
+
amountSpecified: inputAmount.quotient,
|
|
2140
|
+
sqrtPriceLimitX96
|
|
2141
|
+
});
|
|
2142
|
+
const outputToken = zeroForOne ? pool.currency1 : pool.currency0;
|
|
2143
|
+
if (exact) {
|
|
2144
|
+
invariant9__default.default(amountSpecifiedRemaining === ZERO, "INSUFFICIENT_LIQUIDITY");
|
|
2145
|
+
}
|
|
2146
|
+
return [
|
|
2147
|
+
sdk.CurrencyAmount.fromRawAmount(outputToken, outputAmount * NEGATIVE_ONE),
|
|
2148
|
+
{
|
|
2149
|
+
...pool,
|
|
2150
|
+
sqrtRatioX96,
|
|
2151
|
+
liquidity,
|
|
2152
|
+
tickCurrent
|
|
2153
|
+
}
|
|
2154
|
+
];
|
|
2155
|
+
};
|
|
2156
|
+
var getInputAmount = async (pool, outputAmount, options) => {
|
|
2157
|
+
invariant9__default.default(hasInvolvedCurrency(pool, outputAmount.currency), "CURRENCY");
|
|
2158
|
+
const zeroForOne = outputAmount.currency.equals(pool.currency1);
|
|
2159
|
+
const { sqrtPriceLimitX96, exact } = options || {};
|
|
2160
|
+
const {
|
|
2161
|
+
amountCalculated: inputAmount,
|
|
2162
|
+
sqrtRatioX96,
|
|
2163
|
+
liquidity,
|
|
2164
|
+
tickCurrent,
|
|
2165
|
+
amountSpecifiedRemaining
|
|
2166
|
+
} = await swap({
|
|
2167
|
+
pool,
|
|
2168
|
+
zeroForOne,
|
|
2169
|
+
amountSpecified: outputAmount.quotient * NEGATIVE_ONE,
|
|
2170
|
+
sqrtPriceLimitX96
|
|
2171
|
+
});
|
|
2172
|
+
if (exact) {
|
|
2173
|
+
invariant9__default.default(amountSpecifiedRemaining === ZERO, "INSUFFICIENT_LIQUIDITY");
|
|
2174
|
+
}
|
|
2175
|
+
const inputCurrency = zeroForOne ? pool.currency0 : pool.currency1;
|
|
2176
|
+
return [
|
|
2177
|
+
sdk.CurrencyAmount.fromRawAmount(inputCurrency, inputAmount),
|
|
2178
|
+
{
|
|
2179
|
+
...pool,
|
|
2180
|
+
sqrtRatioX96,
|
|
2181
|
+
liquidity,
|
|
2182
|
+
tickCurrent
|
|
2183
|
+
}
|
|
2184
|
+
];
|
|
2185
|
+
};
|
|
2009
2186
|
var IMulticall = [
|
|
2010
2187
|
{
|
|
2011
2188
|
inputs: [
|
|
@@ -6651,15 +6828,24 @@ exports.encodeSqrtRatioX96 = encodeSqrtRatioX96;
|
|
|
6651
6828
|
exports.getAmountsAtNewPrice = getAmountsAtNewPrice;
|
|
6652
6829
|
exports.getAmountsByLiquidityAndPrice = getAmountsByLiquidityAndPrice;
|
|
6653
6830
|
exports.getAverageLiquidity = getAverageLiquidity;
|
|
6831
|
+
exports.getCurrency0Price = getCurrency0Price;
|
|
6832
|
+
exports.getCurrency1Price = getCurrency1Price;
|
|
6654
6833
|
exports.getDependentAmount = getDependentAmount;
|
|
6655
6834
|
exports.getEstimatedLPFee = getEstimatedLPFee;
|
|
6656
6835
|
exports.getEstimatedLPFeeByAmounts = getEstimatedLPFeeByAmounts;
|
|
6657
6836
|
exports.getEstimatedLPFeeByAmountsWithProtocolFee = getEstimatedLPFeeByAmountsWithProtocolFee;
|
|
6658
6837
|
exports.getEstimatedLPFeeWithProtocolFee = getEstimatedLPFeeWithProtocolFee;
|
|
6838
|
+
exports.getInputAmount = getInputAmount;
|
|
6659
6839
|
exports.getLiquidityByAmountsAndPrice = getLiquidityByAmountsAndPrice;
|
|
6660
6840
|
exports.getLiquidityBySingleAmount = getLiquidityBySingleAmount;
|
|
6661
6841
|
exports.getLiquidityFromSqrtRatioX96 = getLiquidityFromSqrtRatioX96;
|
|
6662
6842
|
exports.getLiquidityFromTick = getLiquidityFromTick;
|
|
6843
|
+
exports.getOutputAmount = getOutputAmount;
|
|
6844
|
+
exports.getPool = getPool;
|
|
6845
|
+
exports.getPriceOfCurrency = getPriceOfCurrency;
|
|
6846
|
+
exports.getPriceOfCurrency0 = getPriceOfCurrency0;
|
|
6847
|
+
exports.getPriceOfCurrency1 = getPriceOfCurrency1;
|
|
6848
|
+
exports.hasInvolvedCurrency = hasInvolvedCurrency;
|
|
6663
6849
|
exports.isMint = isMint;
|
|
6664
6850
|
exports.isPoolTickInRange = isPoolTickInRange;
|
|
6665
6851
|
exports.isSorted = isSorted;
|
|
@@ -6677,6 +6863,7 @@ exports.quoterV2ABI = quoterV2ABI;
|
|
|
6677
6863
|
exports.selfPermitABI = selfPermitABI;
|
|
6678
6864
|
exports.sqrtRatioX96ToPrice = sqrtRatioX96ToPrice;
|
|
6679
6865
|
exports.subIn256 = subIn256;
|
|
6866
|
+
exports.swap = swap;
|
|
6680
6867
|
exports.swapRouterABI = swapRouterABI;
|
|
6681
6868
|
exports.tickToPrice = tickToPrice;
|
|
6682
6869
|
exports.toHex = toHex;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Price, CurrencyAmount, Percent as Percent$1, MaxUint256 as MaxUint256$1, TradeType, Fraction, sortedInsert, validateAndParseAddress, ZERO as ZERO$2, ONE as ONE$1 } from '@pancakeswap/sdk';
|
|
2
2
|
import invariant9 from 'tiny-invariant';
|
|
3
3
|
import { ChainId } from '@pancakeswap/chains';
|
|
4
|
-
import { Percent, MaxUint256, sqrt, Price as Price$1, CurrencyAmount as CurrencyAmount$1, Fraction as Fraction$1, ZERO as ZERO$1, TradeType as TradeType$1 } from '@pancakeswap/swap-sdk-core';
|
|
4
|
+
import { Percent, MaxUint256, sqrt, Price as Price$1, sortCurrencies, CurrencyAmount as CurrencyAmount$1, Fraction as Fraction$1, ZERO as ZERO$1, TradeType as TradeType$1 } from '@pancakeswap/swap-sdk-core';
|
|
5
5
|
import { keccak256, encodeAbiParameters, parseAbiParameters, encodePacked, encodeFunctionData, toBytes, getAddress, pad, isBytes, slice, concat } from 'viem';
|
|
6
6
|
|
|
7
7
|
// src/entities/pool.ts
|
|
@@ -206,8 +206,7 @@ var SqrtPriceMath = class {
|
|
|
206
206
|
}
|
|
207
207
|
static getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
208
208
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
209
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
210
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
209
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
211
210
|
}
|
|
212
211
|
const numerator1 = liquidity << 96n;
|
|
213
212
|
const numerator2 = sqrtRatioBX96 - sqrtRatioAX96;
|
|
@@ -215,8 +214,7 @@ var SqrtPriceMath = class {
|
|
|
215
214
|
}
|
|
216
215
|
static getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
217
216
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
218
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
219
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
217
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
220
218
|
}
|
|
221
219
|
return roundUp ? FullMath.mulDivRoundingUp(liquidity, sqrtRatioBX96 - sqrtRatioAX96, Q96) : liquidity * (sqrtRatioBX96 - sqrtRatioAX96) / Q96;
|
|
222
220
|
}
|
|
@@ -805,16 +803,14 @@ var Pool = class {
|
|
|
805
803
|
// src/utils/maxLiquidityForAmounts.ts
|
|
806
804
|
function maxLiquidityForAmount0Imprecise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
807
805
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
808
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
809
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
806
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
810
807
|
}
|
|
811
808
|
const intermediate = sqrtRatioAX96 * sqrtRatioBX96 / Q96;
|
|
812
809
|
return BigInt(amount0) * intermediate / (sqrtRatioBX96 - sqrtRatioAX96);
|
|
813
810
|
}
|
|
814
811
|
function maxLiquidityForAmount0Precise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
815
812
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
816
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
817
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
813
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
818
814
|
}
|
|
819
815
|
const numerator = BigInt(amount0) * sqrtRatioAX96 * sqrtRatioBX96;
|
|
820
816
|
const denominator = Q96 * (sqrtRatioBX96 - sqrtRatioAX96);
|
|
@@ -822,15 +818,13 @@ function maxLiquidityForAmount0Precise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
|
822
818
|
}
|
|
823
819
|
function maxLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1) {
|
|
824
820
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
825
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
826
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
821
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
827
822
|
}
|
|
828
823
|
return BigInt(amount1) * Q96 / (sqrtRatioBX96 - sqrtRatioAX96);
|
|
829
824
|
}
|
|
830
825
|
function maxLiquidityForAmounts(sqrtRatioCurrentX96, sqrtRatioAX96, sqrtRatioBX96, amount0, amount1, useFullPrecision) {
|
|
831
826
|
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
832
|
-
sqrtRatioAX96 = sqrtRatioBX96;
|
|
833
|
-
sqrtRatioBX96 = sqrtRatioAX96;
|
|
827
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
834
828
|
}
|
|
835
829
|
const maxLiquidityForAmount0 = useFullPrecision ? maxLiquidityForAmount0Precise : maxLiquidityForAmount0Imprecise;
|
|
836
830
|
if (sqrtRatioCurrentX96 <= sqrtRatioAX96) {
|
|
@@ -1687,6 +1681,56 @@ var Trade = class {
|
|
|
1687
1681
|
return bestTrades;
|
|
1688
1682
|
}
|
|
1689
1683
|
};
|
|
1684
|
+
var getCurrency0Price = ([currency0, currency1], currentSqrtRatioX96) => {
|
|
1685
|
+
const ratio192 = BigInt(currentSqrtRatioX96) * BigInt(currentSqrtRatioX96);
|
|
1686
|
+
return new Price(currency0, currency1, Q192, ratio192);
|
|
1687
|
+
};
|
|
1688
|
+
var getCurrency1Price = ([currency0, currency1], currentSqrtRatioX96) => {
|
|
1689
|
+
const ratio192 = BigInt(currentSqrtRatioX96) * BigInt(currentSqrtRatioX96);
|
|
1690
|
+
return new Price(currency1, currency0, Q192, ratio192);
|
|
1691
|
+
};
|
|
1692
|
+
var NO_TICK_DATA_PROVIDER_DEFAULT2 = new NoTickDataProvider();
|
|
1693
|
+
var MAX_FEE_AMOUNT = 1000000n;
|
|
1694
|
+
var getPool = ({
|
|
1695
|
+
currencyA,
|
|
1696
|
+
currencyB,
|
|
1697
|
+
fee,
|
|
1698
|
+
sqrtRatioX96,
|
|
1699
|
+
liquidity,
|
|
1700
|
+
tickCurrent,
|
|
1701
|
+
tickSpacing,
|
|
1702
|
+
ticks = NO_TICK_DATA_PROVIDER_DEFAULT2
|
|
1703
|
+
}) => {
|
|
1704
|
+
invariant9(Number.isInteger(fee) && BigInt(fee) < MAX_FEE_AMOUNT, "FEE");
|
|
1705
|
+
const [currency0, currency1] = sortCurrencies([currencyA, currencyB]);
|
|
1706
|
+
return {
|
|
1707
|
+
currency0,
|
|
1708
|
+
currency1,
|
|
1709
|
+
fee,
|
|
1710
|
+
tickCurrent,
|
|
1711
|
+
tickSpacing,
|
|
1712
|
+
sqrtRatioX96: BigInt(sqrtRatioX96),
|
|
1713
|
+
liquidity: BigInt(liquidity),
|
|
1714
|
+
tickDataProvider: Array.isArray(ticks) ? new TickListDataProvider(ticks) : ticks
|
|
1715
|
+
};
|
|
1716
|
+
};
|
|
1717
|
+
var getPriceOfCurrency = (pool, currency) => {
|
|
1718
|
+
if (currency.isNative) {
|
|
1719
|
+
invariant9(pool.currency0.isNative, "CURRENCY");
|
|
1720
|
+
return getCurrency0Price([pool.currency0, pool.currency1], pool.sqrtRatioX96);
|
|
1721
|
+
}
|
|
1722
|
+
invariant9(pool.currency0.equals(currency) || pool.currency1.equals(currency), "CURRENCY");
|
|
1723
|
+
return pool.currency0.equals(currency) ? getCurrency0Price([pool.currency0, pool.currency1], pool.sqrtRatioX96) : getCurrency1Price([pool.currency0, pool.currency1], pool.sqrtRatioX96);
|
|
1724
|
+
};
|
|
1725
|
+
var getPriceOfCurrency0 = (pool) => getCurrency0Price([pool.currency0, pool.currency1], pool.sqrtRatioX96);
|
|
1726
|
+
var getPriceOfCurrency1 = (pool) => getCurrency1Price([pool.currency0, pool.currency1], pool.sqrtRatioX96);
|
|
1727
|
+
|
|
1728
|
+
// src/functions/hasInvolvedCurrency.ts
|
|
1729
|
+
var hasInvolvedCurrency = (pool, currency) => {
|
|
1730
|
+
if (currency.isNative)
|
|
1731
|
+
return pool.currency0.isNative && pool.currency1.chainId === currency.chainId;
|
|
1732
|
+
return pool.currency0.equals(currency) || pool.currency1.equals(currency);
|
|
1733
|
+
};
|
|
1690
1734
|
|
|
1691
1735
|
// src/utils/calldata.ts
|
|
1692
1736
|
function toHex(bigintIsh) {
|
|
@@ -2000,6 +2044,139 @@ var isPoolTickInRange = (pool, tickLower, tickUpper) => {
|
|
|
2000
2044
|
const above = typeof tickUpper === "number" ? pool.tickCurrent >= tickUpper : void 0;
|
|
2001
2045
|
return typeof below === "boolean" && typeof above === "boolean" ? !below && !above : false;
|
|
2002
2046
|
};
|
|
2047
|
+
|
|
2048
|
+
// src/functions/swap.ts
|
|
2049
|
+
var swap = async ({
|
|
2050
|
+
pool,
|
|
2051
|
+
zeroForOne,
|
|
2052
|
+
amountSpecified,
|
|
2053
|
+
sqrtPriceLimitX96
|
|
2054
|
+
}) => {
|
|
2055
|
+
if (!sqrtPriceLimitX96)
|
|
2056
|
+
sqrtPriceLimitX96 = zeroForOne ? TickMath.MIN_SQRT_RATIO + ONE : TickMath.MAX_SQRT_RATIO - ONE;
|
|
2057
|
+
if (zeroForOne) {
|
|
2058
|
+
invariant9(sqrtPriceLimitX96 > TickMath.MIN_SQRT_RATIO, "RATIO_MIN");
|
|
2059
|
+
invariant9(sqrtPriceLimitX96 < pool.sqrtRatioX96, "RATIO_CURRENT");
|
|
2060
|
+
} else {
|
|
2061
|
+
invariant9(sqrtPriceLimitX96 < TickMath.MAX_SQRT_RATIO, "RATIO_MAX");
|
|
2062
|
+
invariant9(sqrtPriceLimitX96 > pool.sqrtRatioX96, "RATIO_CURRENT");
|
|
2063
|
+
}
|
|
2064
|
+
const exactInput = amountSpecified >= ZERO;
|
|
2065
|
+
const state = {
|
|
2066
|
+
amountSpecifiedRemaining: amountSpecified,
|
|
2067
|
+
amountCalculated: ZERO,
|
|
2068
|
+
sqrtPriceX96: pool.sqrtRatioX96,
|
|
2069
|
+
tick: pool.tickCurrent,
|
|
2070
|
+
liquidity: pool.liquidity
|
|
2071
|
+
};
|
|
2072
|
+
while (state.amountSpecifiedRemaining !== ZERO && state.sqrtPriceX96 != sqrtPriceLimitX96) {
|
|
2073
|
+
const step = {};
|
|
2074
|
+
step.sqrtPriceStartX96 = state.sqrtPriceX96;
|
|
2075
|
+
[step.tickNext, step.initialized] = await pool.tickDataProvider.nextInitializedTickWithinOneWord(
|
|
2076
|
+
state.tick,
|
|
2077
|
+
zeroForOne,
|
|
2078
|
+
pool.tickSpacing
|
|
2079
|
+
);
|
|
2080
|
+
if (step.tickNext < TickMath.MIN_TICK) {
|
|
2081
|
+
step.tickNext = TickMath.MIN_TICK;
|
|
2082
|
+
} else if (step.tickNext > TickMath.MAX_TICK) {
|
|
2083
|
+
step.tickNext = TickMath.MAX_TICK;
|
|
2084
|
+
}
|
|
2085
|
+
step.sqrtPriceNextX96 = TickMath.getSqrtRatioAtTick(step.tickNext);
|
|
2086
|
+
[state.sqrtPriceX96, step.amountIn, step.amountOut, step.feeAmount] = SwapMath.computeSwapStep(
|
|
2087
|
+
state.sqrtPriceX96,
|
|
2088
|
+
(zeroForOne ? step.sqrtPriceNextX96 < sqrtPriceLimitX96 : step.sqrtPriceNextX96 > sqrtPriceLimitX96) ? sqrtPriceLimitX96 : step.sqrtPriceNextX96,
|
|
2089
|
+
state.liquidity,
|
|
2090
|
+
state.amountSpecifiedRemaining,
|
|
2091
|
+
pool.fee
|
|
2092
|
+
);
|
|
2093
|
+
if (exactInput) {
|
|
2094
|
+
state.amountSpecifiedRemaining -= step.amountIn + step.feeAmount;
|
|
2095
|
+
state.amountCalculated = state.amountCalculated - step.amountOut;
|
|
2096
|
+
} else {
|
|
2097
|
+
state.amountSpecifiedRemaining = state.amountSpecifiedRemaining + step.amountOut;
|
|
2098
|
+
state.amountCalculated = state.amountCalculated + (step.amountIn + step.feeAmount);
|
|
2099
|
+
}
|
|
2100
|
+
if (state.sqrtPriceX96 === step.sqrtPriceNextX96) {
|
|
2101
|
+
if (step.initialized) {
|
|
2102
|
+
let liquidityNet = BigInt((await pool.tickDataProvider.getTick(step.tickNext)).liquidityNet);
|
|
2103
|
+
if (zeroForOne)
|
|
2104
|
+
liquidityNet *= NEGATIVE_ONE;
|
|
2105
|
+
state.liquidity = LiquidityMath.addDelta(state.liquidity, liquidityNet);
|
|
2106
|
+
}
|
|
2107
|
+
state.tick = zeroForOne ? step.tickNext - 1 : step.tickNext;
|
|
2108
|
+
} else if (state.sqrtPriceX96 !== step.sqrtPriceStartX96) {
|
|
2109
|
+
state.tick = TickMath.getTickAtSqrtRatio(state.sqrtPriceX96);
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
return {
|
|
2113
|
+
amountSpecifiedRemaining: state.amountSpecifiedRemaining,
|
|
2114
|
+
amountCalculated: state.amountCalculated,
|
|
2115
|
+
sqrtRatioX96: state.sqrtPriceX96,
|
|
2116
|
+
liquidity: state.liquidity,
|
|
2117
|
+
tickCurrent: state.tick
|
|
2118
|
+
};
|
|
2119
|
+
};
|
|
2120
|
+
var getOutputAmount = async (pool, inputAmount, options) => {
|
|
2121
|
+
invariant9(hasInvolvedCurrency(pool, inputAmount.currency), "CURRENCY");
|
|
2122
|
+
const zeroForOne = inputAmount.currency.equals(pool.currency0);
|
|
2123
|
+
const { sqrtPriceLimitX96, exact } = options || {};
|
|
2124
|
+
const {
|
|
2125
|
+
amountCalculated: outputAmount,
|
|
2126
|
+
sqrtRatioX96,
|
|
2127
|
+
liquidity,
|
|
2128
|
+
tickCurrent,
|
|
2129
|
+
amountSpecifiedRemaining
|
|
2130
|
+
} = await swap({
|
|
2131
|
+
pool,
|
|
2132
|
+
zeroForOne,
|
|
2133
|
+
amountSpecified: inputAmount.quotient,
|
|
2134
|
+
sqrtPriceLimitX96
|
|
2135
|
+
});
|
|
2136
|
+
const outputToken = zeroForOne ? pool.currency1 : pool.currency0;
|
|
2137
|
+
if (exact) {
|
|
2138
|
+
invariant9(amountSpecifiedRemaining === ZERO, "INSUFFICIENT_LIQUIDITY");
|
|
2139
|
+
}
|
|
2140
|
+
return [
|
|
2141
|
+
CurrencyAmount.fromRawAmount(outputToken, outputAmount * NEGATIVE_ONE),
|
|
2142
|
+
{
|
|
2143
|
+
...pool,
|
|
2144
|
+
sqrtRatioX96,
|
|
2145
|
+
liquidity,
|
|
2146
|
+
tickCurrent
|
|
2147
|
+
}
|
|
2148
|
+
];
|
|
2149
|
+
};
|
|
2150
|
+
var getInputAmount = async (pool, outputAmount, options) => {
|
|
2151
|
+
invariant9(hasInvolvedCurrency(pool, outputAmount.currency), "CURRENCY");
|
|
2152
|
+
const zeroForOne = outputAmount.currency.equals(pool.currency1);
|
|
2153
|
+
const { sqrtPriceLimitX96, exact } = options || {};
|
|
2154
|
+
const {
|
|
2155
|
+
amountCalculated: inputAmount,
|
|
2156
|
+
sqrtRatioX96,
|
|
2157
|
+
liquidity,
|
|
2158
|
+
tickCurrent,
|
|
2159
|
+
amountSpecifiedRemaining
|
|
2160
|
+
} = await swap({
|
|
2161
|
+
pool,
|
|
2162
|
+
zeroForOne,
|
|
2163
|
+
amountSpecified: outputAmount.quotient * NEGATIVE_ONE,
|
|
2164
|
+
sqrtPriceLimitX96
|
|
2165
|
+
});
|
|
2166
|
+
if (exact) {
|
|
2167
|
+
invariant9(amountSpecifiedRemaining === ZERO, "INSUFFICIENT_LIQUIDITY");
|
|
2168
|
+
}
|
|
2169
|
+
const inputCurrency = zeroForOne ? pool.currency0 : pool.currency1;
|
|
2170
|
+
return [
|
|
2171
|
+
CurrencyAmount.fromRawAmount(inputCurrency, inputAmount),
|
|
2172
|
+
{
|
|
2173
|
+
...pool,
|
|
2174
|
+
sqrtRatioX96,
|
|
2175
|
+
liquidity,
|
|
2176
|
+
tickCurrent
|
|
2177
|
+
}
|
|
2178
|
+
];
|
|
2179
|
+
};
|
|
2003
2180
|
var IMulticall = [
|
|
2004
2181
|
{
|
|
2005
2182
|
inputs: [
|
|
@@ -6606,4 +6783,4 @@ var v3PoolAbi = [
|
|
|
6606
6783
|
}
|
|
6607
6784
|
];
|
|
6608
6785
|
|
|
6609
|
-
export { ADDRESS_ZERO, DEPLOYER_ADDRESSES, FACTORY_ADDRESSES, FeeAmount, FeeCalculator, FullMath, LiquidityMath, MasterChefV3, MaxUint128, Multicall, NFT_POSITION_MANAGER_ADDRESSES, NoTickDataProvider, NonfungiblePositionManager, POOL_INIT_CODE_HASHES, Payments, Pool, Position, PositionLibrary, PositionMath, Route, SelfPermit, SqrtPriceMath, Staker, SwapMath, SwapQuoter, SwapRouter, TICK_SPACINGS, Tick, TickLibrary, TickList, TickListDataProvider, TickMath, Trade, computePoolAddress, encodeRouteToPath, encodeSqrtRatioX96, getAmountsAtNewPrice, getAmountsByLiquidityAndPrice, getAverageLiquidity, getDependentAmount, getEstimatedLPFee, getEstimatedLPFeeByAmounts, getEstimatedLPFeeByAmountsWithProtocolFee, getEstimatedLPFeeWithProtocolFee, getLiquidityByAmountsAndPrice, getLiquidityBySingleAmount, getLiquidityFromSqrtRatioX96, getLiquidityFromTick, isMint, isPoolTickInRange, isSorted, masterChefV3ABI, maxLiquidityForAmounts, mostSignificantBit, nearestUsableTick, nonfungiblePositionManagerABI, pancakeV3PoolABI, parseProtocolFees, peripheryPaymentsWithFeeABI, priceToClosestTick, quoterABI, quoterV2ABI, selfPermitABI, sqrtRatioX96ToPrice, subIn256, swapRouterABI, tickToPrice, toHex, tradeComparator, v3PoolAbi, v3StakerABI };
|
|
6786
|
+
export { ADDRESS_ZERO, DEPLOYER_ADDRESSES, FACTORY_ADDRESSES, FeeAmount, FeeCalculator, FullMath, LiquidityMath, MasterChefV3, MaxUint128, Multicall, NFT_POSITION_MANAGER_ADDRESSES, NoTickDataProvider, NonfungiblePositionManager, POOL_INIT_CODE_HASHES, Payments, Pool, Position, PositionLibrary, PositionMath, Route, SelfPermit, SqrtPriceMath, Staker, SwapMath, SwapQuoter, SwapRouter, TICK_SPACINGS, Tick, TickLibrary, TickList, TickListDataProvider, TickMath, Trade, computePoolAddress, encodeRouteToPath, encodeSqrtRatioX96, getAmountsAtNewPrice, getAmountsByLiquidityAndPrice, getAverageLiquidity, getCurrency0Price, getCurrency1Price, getDependentAmount, getEstimatedLPFee, getEstimatedLPFeeByAmounts, getEstimatedLPFeeByAmountsWithProtocolFee, getEstimatedLPFeeWithProtocolFee, getInputAmount, getLiquidityByAmountsAndPrice, getLiquidityBySingleAmount, getLiquidityFromSqrtRatioX96, getLiquidityFromTick, getOutputAmount, getPool, getPriceOfCurrency, getPriceOfCurrency0, getPriceOfCurrency1, hasInvolvedCurrency, isMint, isPoolTickInRange, isSorted, masterChefV3ABI, maxLiquidityForAmounts, mostSignificantBit, nearestUsableTick, nonfungiblePositionManagerABI, pancakeV3PoolABI, parseProtocolFees, peripheryPaymentsWithFeeABI, priceToClosestTick, quoterABI, quoterV2ABI, selfPermitABI, sqrtRatioX96ToPrice, subIn256, swap, swapRouterABI, tickToPrice, toHex, tradeComparator, v3PoolAbi, v3StakerABI };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"maxLiquidityForAmounts.d.ts","sourceRoot":"","sources":["../../src/utils/maxLiquidityForAmounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"maxLiquidityForAmounts.d.ts","sourceRoot":"","sources":["../../src/utils/maxLiquidityForAmounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAuD5C;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CACpC,mBAAmB,EAAE,MAAM,EAC3B,aAAa,EAAE,MAAM,EACrB,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,SAAS,EAClB,OAAO,EAAE,SAAS,EAClB,gBAAgB,EAAE,OAAO,GACxB,MAAM,CAgBR"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqrtPriceMath.d.ts","sourceRoot":"","sources":["../../src/utils/sqrtPriceMath.ts"],"names":[],"mappings":"AAiBA,8BAAsB,aAAa;IACjC;;OAEG;IACH,OAAO;WAEO,eAAe,CAC3B,aAAa,EAAE,MAAM,EACrB,aAAa,EAAE,MAAM,EACrB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,OAAO,GACf,MAAM;
|
|
1
|
+
{"version":3,"file":"sqrtPriceMath.d.ts","sourceRoot":"","sources":["../../src/utils/sqrtPriceMath.ts"],"names":[],"mappings":"AAiBA,8BAAsB,aAAa;IACjC;;OAEG;IACH,OAAO;WAEO,eAAe,CAC3B,aAAa,EAAE,MAAM,EACrB,aAAa,EAAE,MAAM,EACrB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,OAAO,GACf,MAAM;WAaK,eAAe,CAC3B,aAAa,EAAE,MAAM,EACrB,aAAa,EAAE,MAAM,EACrB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,OAAO,GACf,MAAM;WAUK,yBAAyB,CACrC,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,OAAO,GAClB,MAAM;WASK,0BAA0B,CACtC,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,OAAO,GAClB,MAAM;IAST,OAAO,CAAC,MAAM,CAAC,qCAAqC;IA4BpD,OAAO,CAAC,MAAM,CAAC,uCAAuC;CAgBvD"}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "3.8.
|
|
7
|
+
"version": "3.8.10",
|
|
8
8
|
"description": "⚒️ An SDK for building applications on top of Pancakeswap V3",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist"
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"tiny-invariant": "^1.3.0",
|
|
24
24
|
"tiny-warning": "^1.0.3",
|
|
25
25
|
"toformat": "^2.0.0",
|
|
26
|
-
"viem": "2.
|
|
26
|
+
"viem": "^2.17.3",
|
|
27
27
|
"@pancakeswap/chains": "0.4.6",
|
|
28
28
|
"@pancakeswap/sdk": "5.8.7",
|
|
29
29
|
"@pancakeswap/swap-sdk-core": "1.1.0"
|