@centurion_project/v3-sdk 3.30.0-ctn.0
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/LICENSE +21 -0
- package/README.md +13 -0
- package/dist/cjs/src/constants.d.ts +23 -0
- package/dist/cjs/src/constants.js +44 -0
- package/dist/cjs/src/constants.js.map +1 -0
- package/dist/cjs/src/entities/index.d.ts +7 -0
- package/dist/cjs/src/entities/index.js +11 -0
- package/dist/cjs/src/entities/index.js.map +1 -0
- package/dist/cjs/src/entities/pool.d.ts +81 -0
- package/dist/cjs/src/entities/pool.js +142 -0
- package/dist/cjs/src/entities/pool.js.map +1 -0
- package/dist/cjs/src/entities/position.d.ts +131 -0
- package/dist/cjs/src/entities/position.js +248 -0
- package/dist/cjs/src/entities/position.js.map +1 -0
- package/dist/cjs/src/entities/route.d.ts +26 -0
- package/dist/cjs/src/entities/route.js +75 -0
- package/dist/cjs/src/entities/route.js.map +1 -0
- package/dist/cjs/src/entities/tick.d.ts +13 -0
- package/dist/cjs/src/entities/tick.js +17 -0
- package/dist/cjs/src/entities/tick.js.map +1 -0
- package/dist/cjs/src/entities/tickDataProvider.d.ts +31 -0
- package/dist/cjs/src/entities/tickDataProvider.js +18 -0
- package/dist/cjs/src/entities/tickDataProvider.js.map +1 -0
- package/dist/cjs/src/entities/tickListDataProvider.d.ts +15 -0
- package/dist/cjs/src/entities/tickListDataProvider.js +23 -0
- package/dist/cjs/src/entities/tickListDataProvider.js.map +1 -0
- package/dist/cjs/src/entities/trade.d.ts +220 -0
- package/dist/cjs/src/entities/trade.js +440 -0
- package/dist/cjs/src/entities/trade.js.map +1 -0
- package/dist/cjs/src/index.d.ts +10 -0
- package/dist/cjs/src/index.js +14 -0
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/cjs/src/internalConstants.d.ts +6 -0
- package/dist/cjs/src/internalConstants.js +13 -0
- package/dist/cjs/src/internalConstants.js.map +1 -0
- package/dist/cjs/src/multicall.d.ts +10 -0
- package/dist/cjs/src/multicall.js +24 -0
- package/dist/cjs/src/multicall.js.map +1 -0
- package/dist/cjs/src/nonfungiblePositionManager.d.ts +159 -0
- package/dist/cjs/src/nonfungiblePositionManager.js +250 -0
- package/dist/cjs/src/nonfungiblePositionManager.js.map +1 -0
- package/dist/cjs/src/payments.d.ts +24 -0
- package/dist/cjs/src/payments.js +56 -0
- package/dist/cjs/src/payments.js.map +1 -0
- package/dist/cjs/src/quoter.d.ts +37 -0
- package/dist/cjs/src/quoter.js +69 -0
- package/dist/cjs/src/quoter.js.map +1 -0
- package/dist/cjs/src/selfPermit.d.ts +25 -0
- package/dist/cjs/src/selfPermit.js +38 -0
- package/dist/cjs/src/selfPermit.js.map +1 -0
- package/dist/cjs/src/staker.d.ts +101 -0
- package/dist/cjs/src/staker.js +129 -0
- package/dist/cjs/src/staker.js.map +1 -0
- package/dist/cjs/src/swapRouter.d.ts +51 -0
- package/dist/cjs/src/swapRouter.js +145 -0
- package/dist/cjs/src/swapRouter.js.map +1 -0
- package/dist/cjs/src/utils/calldata.d.ts +20 -0
- package/dist/cjs/src/utils/calldata.js +20 -0
- package/dist/cjs/src/utils/calldata.js.map +1 -0
- package/dist/cjs/src/utils/computePoolAddress.d.ts +20 -0
- package/dist/cjs/src/utils/computePoolAddress.js +33 -0
- package/dist/cjs/src/utils/computePoolAddress.js.map +1 -0
- package/dist/cjs/src/utils/encodeRouteToPath.d.ts +8 -0
- package/dist/cjs/src/utils/encodeRouteToPath.js +32 -0
- package/dist/cjs/src/utils/encodeRouteToPath.js.map +1 -0
- package/dist/cjs/src/utils/encodeSqrtRatioX96.d.ts +9 -0
- package/dist/cjs/src/utils/encodeSqrtRatioX96.js +20 -0
- package/dist/cjs/src/utils/encodeSqrtRatioX96.js.map +1 -0
- package/dist/cjs/src/utils/fullMath.d.ts +8 -0
- package/dist/cjs/src/utils/fullMath.js +21 -0
- package/dist/cjs/src/utils/fullMath.js.map +1 -0
- package/dist/cjs/src/utils/index.d.ts +18 -0
- package/dist/cjs/src/utils/index.js +22 -0
- package/dist/cjs/src/utils/index.js.map +1 -0
- package/dist/cjs/src/utils/isSorted.d.ts +7 -0
- package/dist/cjs/src/utils/isSorted.js +19 -0
- package/dist/cjs/src/utils/isSorted.js.map +1 -0
- package/dist/cjs/src/utils/liquidityMath.d.ts +8 -0
- package/dist/cjs/src/utils/liquidityMath.js +22 -0
- package/dist/cjs/src/utils/liquidityMath.js.map +1 -0
- package/dist/cjs/src/utils/maxLiquidityForAmounts.d.ts +14 -0
- package/dist/cjs/src/utils/maxLiquidityForAmounts.js +87 -0
- package/dist/cjs/src/utils/maxLiquidityForAmounts.js.map +1 -0
- package/dist/cjs/src/utils/mostSignificantBit.d.ts +2 -0
- package/dist/cjs/src/utils/mostSignificantBit.js +27 -0
- package/dist/cjs/src/utils/mostSignificantBit.js.map +1 -0
- package/dist/cjs/src/utils/nearestUsableTick.d.ts +6 -0
- package/dist/cjs/src/utils/nearestUsableTick.js +25 -0
- package/dist/cjs/src/utils/nearestUsableTick.js.map +1 -0
- package/dist/cjs/src/utils/position.d.ts +8 -0
- package/dist/cjs/src/utils/position.js +21 -0
- package/dist/cjs/src/utils/position.js.map +1 -0
- package/dist/cjs/src/utils/priceTickConversions.d.ts +15 -0
- package/dist/cjs/src/utils/priceTickConversions.js +50 -0
- package/dist/cjs/src/utils/priceTickConversions.js.map +1 -0
- package/dist/cjs/src/utils/sqrtPriceMath.d.ts +13 -0
- package/dist/cjs/src/utils/sqrtPriceMath.js +95 -0
- package/dist/cjs/src/utils/sqrtPriceMath.js.map +1 -0
- package/dist/cjs/src/utils/swapMath.d.ts +9 -0
- package/dist/cjs/src/utils/swapMath.js +78 -0
- package/dist/cjs/src/utils/swapMath.js.map +1 -0
- package/dist/cjs/src/utils/tickLibrary.d.ts +14 -0
- package/dist/cjs/src/utils/tickLibrary.js +51 -0
- package/dist/cjs/src/utils/tickLibrary.js.map +1 -0
- package/dist/cjs/src/utils/tickList.d.ts +23 -0
- package/dist/cjs/src/utils/tickList.js +108 -0
- package/dist/cjs/src/utils/tickList.js.map +1 -0
- package/dist/cjs/src/utils/tickMath.d.ts +34 -0
- package/dist/cjs/src/utils/tickMath.js +125 -0
- package/dist/cjs/src/utils/tickMath.js.map +1 -0
- package/dist/cjs/src/utils/v3swap.d.ts +8 -0
- package/dist/cjs/src/utils/v3swap.js +85 -0
- package/dist/cjs/src/utils/v3swap.js.map +1 -0
- package/dist/esm/src/constants.d.ts +23 -0
- package/dist/esm/src/constants.js +40 -0
- package/dist/esm/src/constants.js.map +1 -0
- package/dist/esm/src/entities/index.d.ts +7 -0
- package/dist/esm/src/entities/index.js +8 -0
- package/dist/esm/src/entities/index.js.map +1 -0
- package/dist/esm/src/entities/pool.d.ts +81 -0
- package/dist/esm/src/entities/pool.js +137 -0
- package/dist/esm/src/entities/pool.js.map +1 -0
- package/dist/esm/src/entities/position.d.ts +131 -0
- package/dist/esm/src/entities/position.js +243 -0
- package/dist/esm/src/entities/position.js.map +1 -0
- package/dist/esm/src/entities/route.d.ts +26 -0
- package/dist/esm/src/entities/route.js +70 -0
- package/dist/esm/src/entities/route.js.map +1 -0
- package/dist/esm/src/entities/tick.d.ts +13 -0
- package/dist/esm/src/entities/tick.js +12 -0
- package/dist/esm/src/entities/tick.js.map +1 -0
- package/dist/esm/src/entities/tickDataProvider.d.ts +31 -0
- package/dist/esm/src/entities/tickDataProvider.js +14 -0
- package/dist/esm/src/entities/tickDataProvider.js.map +1 -0
- package/dist/esm/src/entities/tickListDataProvider.d.ts +15 -0
- package/dist/esm/src/entities/tickListDataProvider.js +19 -0
- package/dist/esm/src/entities/tickListDataProvider.js.map +1 -0
- package/dist/esm/src/entities/trade.d.ts +220 -0
- package/dist/esm/src/entities/trade.js +434 -0
- package/dist/esm/src/entities/trade.js.map +1 -0
- package/dist/esm/src/index.d.ts +10 -0
- package/dist/esm/src/index.js +11 -0
- package/dist/esm/src/index.js.map +1 -0
- package/dist/esm/src/internalConstants.d.ts +6 -0
- package/dist/esm/src/internalConstants.js +9 -0
- package/dist/esm/src/internalConstants.js.map +1 -0
- package/dist/esm/src/multicall.d.ts +10 -0
- package/dist/esm/src/multicall.js +19 -0
- package/dist/esm/src/multicall.js.map +1 -0
- package/dist/esm/src/nonfungiblePositionManager.d.ts +159 -0
- package/dist/esm/src/nonfungiblePositionManager.js +245 -0
- package/dist/esm/src/nonfungiblePositionManager.js.map +1 -0
- package/dist/esm/src/payments.d.ts +24 -0
- package/dist/esm/src/payments.js +51 -0
- package/dist/esm/src/payments.js.map +1 -0
- package/dist/esm/src/quoter.d.ts +37 -0
- package/dist/esm/src/quoter.js +64 -0
- package/dist/esm/src/quoter.js.map +1 -0
- package/dist/esm/src/selfPermit.d.ts +25 -0
- package/dist/esm/src/selfPermit.js +33 -0
- package/dist/esm/src/selfPermit.js.map +1 -0
- package/dist/esm/src/staker.d.ts +101 -0
- package/dist/esm/src/staker.js +124 -0
- package/dist/esm/src/staker.js.map +1 -0
- package/dist/esm/src/swapRouter.d.ts +51 -0
- package/dist/esm/src/swapRouter.js +140 -0
- package/dist/esm/src/swapRouter.js.map +1 -0
- package/dist/esm/src/utils/calldata.d.ts +20 -0
- package/dist/esm/src/utils/calldata.js +15 -0
- package/dist/esm/src/utils/calldata.js.map +1 -0
- package/dist/esm/src/utils/computePoolAddress.d.ts +20 -0
- package/dist/esm/src/utils/computePoolAddress.js +29 -0
- package/dist/esm/src/utils/computePoolAddress.js.map +1 -0
- package/dist/esm/src/utils/encodeRouteToPath.d.ts +8 -0
- package/dist/esm/src/utils/encodeRouteToPath.js +28 -0
- package/dist/esm/src/utils/encodeRouteToPath.js.map +1 -0
- package/dist/esm/src/utils/encodeSqrtRatioX96.d.ts +9 -0
- package/dist/esm/src/utils/encodeSqrtRatioX96.js +15 -0
- package/dist/esm/src/utils/encodeSqrtRatioX96.js.map +1 -0
- package/dist/esm/src/utils/fullMath.d.ts +8 -0
- package/dist/esm/src/utils/fullMath.js +16 -0
- package/dist/esm/src/utils/fullMath.js.map +1 -0
- package/dist/esm/src/utils/index.d.ts +18 -0
- package/dist/esm/src/utils/index.js +19 -0
- package/dist/esm/src/utils/index.js.map +1 -0
- package/dist/esm/src/utils/isSorted.d.ts +7 -0
- package/dist/esm/src/utils/isSorted.js +15 -0
- package/dist/esm/src/utils/isSorted.js.map +1 -0
- package/dist/esm/src/utils/liquidityMath.d.ts +8 -0
- package/dist/esm/src/utils/liquidityMath.js +17 -0
- package/dist/esm/src/utils/liquidityMath.js.map +1 -0
- package/dist/esm/src/utils/maxLiquidityForAmounts.d.ts +14 -0
- package/dist/esm/src/utils/maxLiquidityForAmounts.js +82 -0
- package/dist/esm/src/utils/maxLiquidityForAmounts.js.map +1 -0
- package/dist/esm/src/utils/mostSignificantBit.d.ts +2 -0
- package/dist/esm/src/utils/mostSignificantBit.js +22 -0
- package/dist/esm/src/utils/mostSignificantBit.js.map +1 -0
- package/dist/esm/src/utils/nearestUsableTick.d.ts +6 -0
- package/dist/esm/src/utils/nearestUsableTick.js +20 -0
- package/dist/esm/src/utils/nearestUsableTick.js.map +1 -0
- package/dist/esm/src/utils/position.d.ts +8 -0
- package/dist/esm/src/utils/position.js +16 -0
- package/dist/esm/src/utils/position.js.map +1 -0
- package/dist/esm/src/utils/priceTickConversions.d.ts +15 -0
- package/dist/esm/src/utils/priceTickConversions.js +44 -0
- package/dist/esm/src/utils/priceTickConversions.js.map +1 -0
- package/dist/esm/src/utils/sqrtPriceMath.d.ts +13 -0
- package/dist/esm/src/utils/sqrtPriceMath.js +90 -0
- package/dist/esm/src/utils/sqrtPriceMath.js.map +1 -0
- package/dist/esm/src/utils/swapMath.d.ts +9 -0
- package/dist/esm/src/utils/swapMath.js +73 -0
- package/dist/esm/src/utils/swapMath.js.map +1 -0
- package/dist/esm/src/utils/tickLibrary.d.ts +14 -0
- package/dist/esm/src/utils/tickLibrary.js +45 -0
- package/dist/esm/src/utils/tickLibrary.js.map +1 -0
- package/dist/esm/src/utils/tickList.d.ts +23 -0
- package/dist/esm/src/utils/tickList.js +103 -0
- package/dist/esm/src/utils/tickList.js.map +1 -0
- package/dist/esm/src/utils/tickMath.d.ts +34 -0
- package/dist/esm/src/utils/tickMath.js +120 -0
- package/dist/esm/src/utils/tickMath.js.map +1 -0
- package/dist/esm/src/utils/v3swap.d.ts +8 -0
- package/dist/esm/src/utils/v3swap.js +80 -0
- package/dist/esm/src/utils/v3swap.js.map +1 -0
- package/dist/types/src/constants.d.ts +23 -0
- package/dist/types/src/entities/index.d.ts +7 -0
- package/dist/types/src/entities/pool.d.ts +81 -0
- package/dist/types/src/entities/position.d.ts +131 -0
- package/dist/types/src/entities/route.d.ts +26 -0
- package/dist/types/src/entities/tick.d.ts +13 -0
- package/dist/types/src/entities/tickDataProvider.d.ts +31 -0
- package/dist/types/src/entities/tickListDataProvider.d.ts +15 -0
- package/dist/types/src/entities/trade.d.ts +220 -0
- package/dist/types/src/index.d.ts +10 -0
- package/dist/types/src/internalConstants.d.ts +6 -0
- package/dist/types/src/multicall.d.ts +10 -0
- package/dist/types/src/nonfungiblePositionManager.d.ts +159 -0
- package/dist/types/src/payments.d.ts +24 -0
- package/dist/types/src/quoter.d.ts +37 -0
- package/dist/types/src/selfPermit.d.ts +25 -0
- package/dist/types/src/staker.d.ts +101 -0
- package/dist/types/src/swapRouter.d.ts +51 -0
- package/dist/types/src/utils/calldata.d.ts +20 -0
- package/dist/types/src/utils/computePoolAddress.d.ts +20 -0
- package/dist/types/src/utils/encodeRouteToPath.d.ts +8 -0
- package/dist/types/src/utils/encodeSqrtRatioX96.d.ts +9 -0
- package/dist/types/src/utils/fullMath.d.ts +8 -0
- package/dist/types/src/utils/index.d.ts +18 -0
- package/dist/types/src/utils/isSorted.d.ts +7 -0
- package/dist/types/src/utils/liquidityMath.d.ts +8 -0
- package/dist/types/src/utils/maxLiquidityForAmounts.d.ts +14 -0
- package/dist/types/src/utils/mostSignificantBit.d.ts +2 -0
- package/dist/types/src/utils/nearestUsableTick.d.ts +6 -0
- package/dist/types/src/utils/position.d.ts +8 -0
- package/dist/types/src/utils/priceTickConversions.d.ts +15 -0
- package/dist/types/src/utils/sqrtPriceMath.d.ts +13 -0
- package/dist/types/src/utils/swapMath.d.ts +9 -0
- package/dist/types/src/utils/tickLibrary.d.ts +14 -0
- package/dist/types/src/utils/tickList.d.ts +23 -0
- package/dist/types/src/utils/tickMath.d.ts +34 -0
- package/dist/types/src/utils/v3swap.d.ts +8 -0
- package/package.json +72 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { defaultAbiCoder } from '@ethersproject/abi';
|
|
2
|
+
import { getCreate2Address } from '@ethersproject/address';
|
|
3
|
+
import { keccak256 } from '@ethersproject/solidity';
|
|
4
|
+
import { ChainId, computeZksyncCreate2Address } from '@centurion_project/sdk-core';
|
|
5
|
+
import { poolInitCodeHash } from '../constants';
|
|
6
|
+
/**
|
|
7
|
+
* Computes a pool address
|
|
8
|
+
* @param factoryAddress The Uniswap V3 factory address
|
|
9
|
+
* @param tokenA The first token of the pair, irrespective of sort order
|
|
10
|
+
* @param tokenB The second token of the pair, irrespective of sort order
|
|
11
|
+
* @param fee The fee tier of the pool
|
|
12
|
+
* @param initCodeHashManualOverride Override the init code hash used to compute the pool address if necessary
|
|
13
|
+
* @param chainId
|
|
14
|
+
* @returns The pool address
|
|
15
|
+
*/
|
|
16
|
+
export function computePoolAddress({ factoryAddress, tokenA, tokenB, fee, initCodeHashManualOverride, chainId, }) {
|
|
17
|
+
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA]; // does safety checks
|
|
18
|
+
const salt = keccak256(['bytes'], [defaultAbiCoder.encode(['address', 'address', 'uint24'], [token0.address, token1.address, fee])]);
|
|
19
|
+
const initCodeHash = initCodeHashManualOverride !== null && initCodeHashManualOverride !== void 0 ? initCodeHashManualOverride : poolInitCodeHash(chainId);
|
|
20
|
+
// ZKSync uses a different create2 address computation
|
|
21
|
+
// Most likely all ZKEVM chains will use the different computation from standard create2
|
|
22
|
+
switch (chainId) {
|
|
23
|
+
case ChainId.ZKSYNC:
|
|
24
|
+
return computeZksyncCreate2Address(factoryAddress, initCodeHash, salt);
|
|
25
|
+
default:
|
|
26
|
+
return getCreate2Address(factoryAddress, salt, initCodeHash);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=computePoolAddress.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"computePoolAddress.js","sourceRoot":"","sources":["../../../../src/utils/computePoolAddress.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,OAAO,EAAE,2BAA2B,EAAS,MAAM,6BAA6B,CAAA;AACzF,OAAO,EAAa,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAE1D;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB,CAAC,EACjC,cAAc,EACd,MAAM,EACN,MAAM,EACN,GAAG,EACH,0BAA0B,EAC1B,OAAO,GAQR;IACC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA,CAAC,qBAAqB;IAC/G,MAAM,IAAI,GAAG,SAAS,CACpB,CAAC,OAAO,CAAC,EACT,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,SAAS,EAAE,QAAQ,CAAC,EAAE,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,CAClG,CAAA;IACD,MAAM,YAAY,GAAG,0BAA0B,aAA1B,0BAA0B,cAA1B,0BAA0B,GAAI,gBAAgB,CAAC,OAAO,CAAC,CAAA;IAE5E,sDAAsD;IACtD,wFAAwF;IACxF,QAAQ,OAAO,EAAE;QACf,KAAK,OAAO,CAAC,MAAM;YACjB,OAAO,2BAA2B,CAAC,cAAc,EAAE,YAAY,EAAE,IAAI,CAAC,CAAA;QACxE;YACE,OAAO,iBAAiB,CAAC,cAAc,EAAE,IAAI,EAAE,YAAY,CAAC,CAAA;KAC/D;AACH,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Currency } from '@centurion_project/sdk-core';
|
|
2
|
+
import { Route } from '../entities/route';
|
|
3
|
+
/**
|
|
4
|
+
* Converts a route to a hex encoded path
|
|
5
|
+
* @param route the v3 path to convert to an encoded path
|
|
6
|
+
* @param exactOutput whether the route should be encoded in reverse, for making exact output swaps
|
|
7
|
+
*/
|
|
8
|
+
export declare function encodeRouteToPath(route: Route<Currency, Currency>, exactOutput: boolean): string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { pack } from '@ethersproject/solidity';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a route to a hex encoded path
|
|
4
|
+
* @param route the v3 path to convert to an encoded path
|
|
5
|
+
* @param exactOutput whether the route should be encoded in reverse, for making exact output swaps
|
|
6
|
+
*/
|
|
7
|
+
export function encodeRouteToPath(route, exactOutput) {
|
|
8
|
+
const firstInputToken = route.input.wrapped;
|
|
9
|
+
const { path, types } = route.pools.reduce(({ inputToken, path, types }, pool, index) => {
|
|
10
|
+
const outputToken = pool.token0.equals(inputToken) ? pool.token1 : pool.token0;
|
|
11
|
+
if (index === 0) {
|
|
12
|
+
return {
|
|
13
|
+
inputToken: outputToken,
|
|
14
|
+
types: ['address', 'uint24', 'address'],
|
|
15
|
+
path: [inputToken.address, pool.fee, outputToken.address],
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return {
|
|
20
|
+
inputToken: outputToken,
|
|
21
|
+
types: [...types, 'uint24', 'address'],
|
|
22
|
+
path: [...path, pool.fee, outputToken.address],
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}, { inputToken: firstInputToken, path: [], types: [] });
|
|
26
|
+
return exactOutput ? pack(types.reverse(), path.reverse()) : pack(types, path);
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=encodeRouteToPath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encodeRouteToPath.js","sourceRoot":"","sources":["../../../../src/utils/encodeRouteToPath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAA;AAK9C;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAgC,EAAE,WAAoB;IACtF,MAAM,eAAe,GAAU,KAAK,CAAC,KAAK,CAAC,OAAO,CAAA;IAElD,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,CACxC,CACE,EAAE,UAAU,EAAE,IAAI,EAAE,KAAK,EAAqE,EAC9F,IAAU,EACV,KAAK,EAC8D,EAAE;QACrE,MAAM,WAAW,GAAU,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAA;QACrF,IAAI,KAAK,KAAK,CAAC,EAAE;YACf,OAAO;gBACL,UAAU,EAAE,WAAW;gBACvB,KAAK,EAAE,CAAC,SAAS,EAAE,QAAQ,EAAE,SAAS,CAAC;gBACvC,IAAI,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,OAAO,CAAC;aAC1D,CAAA;SACF;aAAM;YACL,OAAO;gBACL,UAAU,EAAE,WAAW;gBACvB,KAAK,EAAE,CAAC,GAAG,KAAK,EAAE,QAAQ,EAAE,SAAS,CAAC;gBACtC,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,OAAO,CAAC;aAC/C,CAAA;SACF;IACH,CAAC,EACD,EAAE,UAAU,EAAE,eAAe,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,CACrD,CAAA;IAED,OAAO,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;AAChF,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import JSBI from 'jsbi';
|
|
2
|
+
import { BigintIsh } from '@centurion_project/sdk-core';
|
|
3
|
+
/**
|
|
4
|
+
* Returns the sqrt ratio as a Q64.96 corresponding to a given ratio of amount1 and amount0
|
|
5
|
+
* @param amount1 The numerator amount i.e., the amount of token1
|
|
6
|
+
* @param amount0 The denominator amount i.e., the amount of token0
|
|
7
|
+
* @returns The sqrt ratio
|
|
8
|
+
*/
|
|
9
|
+
export declare function encodeSqrtRatioX96(amount1: BigintIsh, amount0: BigintIsh): JSBI;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import JSBI from 'jsbi';
|
|
2
|
+
import { sqrt } from '@centurion_project/sdk-core';
|
|
3
|
+
/**
|
|
4
|
+
* Returns the sqrt ratio as a Q64.96 corresponding to a given ratio of amount1 and amount0
|
|
5
|
+
* @param amount1 The numerator amount i.e., the amount of token1
|
|
6
|
+
* @param amount0 The denominator amount i.e., the amount of token0
|
|
7
|
+
* @returns The sqrt ratio
|
|
8
|
+
*/
|
|
9
|
+
export function encodeSqrtRatioX96(amount1, amount0) {
|
|
10
|
+
const numerator = JSBI.leftShift(JSBI.BigInt(amount1), JSBI.BigInt(192));
|
|
11
|
+
const denominator = JSBI.BigInt(amount0);
|
|
12
|
+
const ratioX192 = JSBI.divide(numerator, denominator);
|
|
13
|
+
return sqrt(ratioX192);
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=encodeSqrtRatioX96.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encodeSqrtRatioX96.js","sourceRoot":"","sources":["../../../../src/utils/encodeSqrtRatioX96.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAa,IAAI,EAAE,MAAM,6BAA6B,CAAA;AAE7D;;;;;GAKG;AAEH,MAAM,UAAU,kBAAkB,CAAC,OAAkB,EAAE,OAAkB;IACvE,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAA;IACxE,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;IACxC,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,WAAW,CAAC,CAAA;IACrD,OAAO,IAAI,CAAC,SAAS,CAAC,CAAA;AACxB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import JSBI from 'jsbi';
|
|
2
|
+
import { ONE, ZERO } from '../internalConstants';
|
|
3
|
+
export class FullMath {
|
|
4
|
+
/**
|
|
5
|
+
* Cannot be constructed.
|
|
6
|
+
*/
|
|
7
|
+
constructor() { }
|
|
8
|
+
static mulDivRoundingUp(a, b, denominator) {
|
|
9
|
+
const product = JSBI.multiply(a, b);
|
|
10
|
+
let result = JSBI.divide(product, denominator);
|
|
11
|
+
if (JSBI.notEqual(JSBI.remainder(product, denominator), ZERO))
|
|
12
|
+
result = JSBI.add(result, ONE);
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=fullMath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fullMath.js","sourceRoot":"","sources":["../../../../src/utils/fullMath.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAEhD,MAAM,OAAgB,QAAQ;IAC5B;;OAEG;IACH,gBAAuB,CAAC;IAEjB,MAAM,CAAC,gBAAgB,CAAC,CAAO,EAAE,CAAO,EAAE,WAAiB;QAChE,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;QACnC,IAAI,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;QAC9C,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,WAAW,CAAC,EAAE,IAAI,CAAC;YAAE,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;QAC7F,OAAO,MAAM,CAAA;IACf,CAAC;CACF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from './calldata';
|
|
2
|
+
export * from './computePoolAddress';
|
|
3
|
+
export * from './encodeRouteToPath';
|
|
4
|
+
export * from './encodeSqrtRatioX96';
|
|
5
|
+
export * from './fullMath';
|
|
6
|
+
export * from './isSorted';
|
|
7
|
+
export * from './liquidityMath';
|
|
8
|
+
export * from './maxLiquidityForAmounts';
|
|
9
|
+
export * from './mostSignificantBit';
|
|
10
|
+
export * from './nearestUsableTick';
|
|
11
|
+
export * from './position';
|
|
12
|
+
export * from './priceTickConversions';
|
|
13
|
+
export * from './sqrtPriceMath';
|
|
14
|
+
export * from './v3swap';
|
|
15
|
+
export * from './swapMath';
|
|
16
|
+
export * from './tickLibrary';
|
|
17
|
+
export * from './tickList';
|
|
18
|
+
export * from './tickMath';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export * from './calldata';
|
|
2
|
+
export * from './computePoolAddress';
|
|
3
|
+
export * from './encodeRouteToPath';
|
|
4
|
+
export * from './encodeSqrtRatioX96';
|
|
5
|
+
export * from './fullMath';
|
|
6
|
+
export * from './isSorted';
|
|
7
|
+
export * from './liquidityMath';
|
|
8
|
+
export * from './maxLiquidityForAmounts';
|
|
9
|
+
export * from './mostSignificantBit';
|
|
10
|
+
export * from './nearestUsableTick';
|
|
11
|
+
export * from './position';
|
|
12
|
+
export * from './priceTickConversions';
|
|
13
|
+
export * from './sqrtPriceMath';
|
|
14
|
+
export * from './v3swap';
|
|
15
|
+
export * from './swapMath';
|
|
16
|
+
export * from './tickLibrary';
|
|
17
|
+
export * from './tickList';
|
|
18
|
+
export * from './tickMath';
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,sBAAsB,CAAA;AACpC,cAAc,qBAAqB,CAAA;AACnC,cAAc,sBAAsB,CAAA;AACpC,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,0BAA0B,CAAA;AACxC,cAAc,sBAAsB,CAAA;AACpC,cAAc,qBAAqB,CAAA;AACnC,cAAc,YAAY,CAAA;AAC1B,cAAc,wBAAwB,CAAA;AACtC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,UAAU,CAAA;AACxB,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines if a tick list is sorted
|
|
3
|
+
* @param list The tick list
|
|
4
|
+
* @param comparator The comparator
|
|
5
|
+
* @returns true if sorted
|
|
6
|
+
*/
|
|
7
|
+
export function isSorted(list, comparator) {
|
|
8
|
+
for (let i = 0; i < list.length - 1; i++) {
|
|
9
|
+
if (comparator(list[i], list[i + 1]) > 0) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=isSorted.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isSorted.js","sourceRoot":"","sources":["../../../../src/utils/isSorted.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAI,IAAc,EAAE,UAAkC;IAC5E,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE;QACxC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE;YACxC,OAAO,KAAK,CAAA;SACb;KACF;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import JSBI from 'jsbi';
|
|
2
|
+
import { NEGATIVE_ONE, ZERO } from '../internalConstants';
|
|
3
|
+
export class LiquidityMath {
|
|
4
|
+
/**
|
|
5
|
+
* Cannot be constructed.
|
|
6
|
+
*/
|
|
7
|
+
constructor() { }
|
|
8
|
+
static addDelta(x, y) {
|
|
9
|
+
if (JSBI.lessThan(y, ZERO)) {
|
|
10
|
+
return JSBI.subtract(x, JSBI.multiply(y, NEGATIVE_ONE));
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return JSBI.add(x, y);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=liquidityMath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"liquidityMath.js","sourceRoot":"","sources":["../../../../src/utils/liquidityMath.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAEzD,MAAM,OAAgB,aAAa;IACjC;;OAEG;IACH,gBAAuB,CAAC;IAEjB,MAAM,CAAC,QAAQ,CAAC,CAAO,EAAE,CAAO;QACrC,IAAI,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE;YAC1B,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC,CAAA;SACxD;aAAM;YACL,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;SACtB;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BigintIsh } from '@centurion_project/sdk-core';
|
|
2
|
+
import JSBI from 'jsbi';
|
|
3
|
+
/**
|
|
4
|
+
* Computes the maximum amount of liquidity received for a given amount of token0, token1,
|
|
5
|
+
* and the prices at the tick boundaries.
|
|
6
|
+
* @param sqrtRatioCurrentX96 the current price
|
|
7
|
+
* @param sqrtRatioAX96 price at lower boundary
|
|
8
|
+
* @param sqrtRatioBX96 price at upper boundary
|
|
9
|
+
* @param amount0 token0 amount
|
|
10
|
+
* @param amount1 token1 amount
|
|
11
|
+
* @param useFullPrecision if false, liquidity will be maximized according to what the router can calculate,
|
|
12
|
+
* not what core can theoretically support
|
|
13
|
+
*/
|
|
14
|
+
export declare function maxLiquidityForAmounts(sqrtRatioCurrentX96: JSBI, sqrtRatioAX96: JSBI, sqrtRatioBX96: JSBI, amount0: BigintIsh, amount1: BigintIsh, useFullPrecision: boolean): JSBI;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import JSBI from 'jsbi';
|
|
2
|
+
import { Q96 } from '../internalConstants';
|
|
3
|
+
/**
|
|
4
|
+
* Returns an imprecise maximum amount of liquidity received for a given amount of token 0.
|
|
5
|
+
* This function is available to accommodate LiquidityAmounts#getLiquidityForAmount0 in the v3 periphery,
|
|
6
|
+
* which could be more precise by at least 32 bits by dividing by Q64 instead of Q96 in the intermediate step,
|
|
7
|
+
* and shifting the subtracted ratio left by 32 bits. This imprecise calculation will likely be replaced in a future
|
|
8
|
+
* v3 router contract.
|
|
9
|
+
* @param sqrtRatioAX96 The price at the lower boundary
|
|
10
|
+
* @param sqrtRatioBX96 The price at the upper boundary
|
|
11
|
+
* @param amount0 The token0 amount
|
|
12
|
+
* @returns liquidity for amount0, imprecise
|
|
13
|
+
*/
|
|
14
|
+
function maxLiquidityForAmount0Imprecise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
15
|
+
if (JSBI.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
|
|
16
|
+
;
|
|
17
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
18
|
+
}
|
|
19
|
+
const intermediate = JSBI.divide(JSBI.multiply(sqrtRatioAX96, sqrtRatioBX96), Q96);
|
|
20
|
+
return JSBI.divide(JSBI.multiply(JSBI.BigInt(amount0), intermediate), JSBI.subtract(sqrtRatioBX96, sqrtRatioAX96));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns a precise maximum amount of liquidity received for a given amount of token 0 by dividing by Q64 instead of Q96 in the intermediate step,
|
|
24
|
+
* and shifting the subtracted ratio left by 32 bits.
|
|
25
|
+
* @param sqrtRatioAX96 The price at the lower boundary
|
|
26
|
+
* @param sqrtRatioBX96 The price at the upper boundary
|
|
27
|
+
* @param amount0 The token0 amount
|
|
28
|
+
* @returns liquidity for amount0, precise
|
|
29
|
+
*/
|
|
30
|
+
function maxLiquidityForAmount0Precise(sqrtRatioAX96, sqrtRatioBX96, amount0) {
|
|
31
|
+
if (JSBI.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
|
|
32
|
+
;
|
|
33
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
34
|
+
}
|
|
35
|
+
const numerator = JSBI.multiply(JSBI.multiply(JSBI.BigInt(amount0), sqrtRatioAX96), sqrtRatioBX96);
|
|
36
|
+
const denominator = JSBI.multiply(Q96, JSBI.subtract(sqrtRatioBX96, sqrtRatioAX96));
|
|
37
|
+
return JSBI.divide(numerator, denominator);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Computes the maximum amount of liquidity received for a given amount of token1
|
|
41
|
+
* @param sqrtRatioAX96 The price at the lower tick boundary
|
|
42
|
+
* @param sqrtRatioBX96 The price at the upper tick boundary
|
|
43
|
+
* @param amount1 The token1 amount
|
|
44
|
+
* @returns liquidity for amount1
|
|
45
|
+
*/
|
|
46
|
+
function maxLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1) {
|
|
47
|
+
if (JSBI.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
|
|
48
|
+
;
|
|
49
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
50
|
+
}
|
|
51
|
+
return JSBI.divide(JSBI.multiply(JSBI.BigInt(amount1), Q96), JSBI.subtract(sqrtRatioBX96, sqrtRatioAX96));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Computes the maximum amount of liquidity received for a given amount of token0, token1,
|
|
55
|
+
* and the prices at the tick boundaries.
|
|
56
|
+
* @param sqrtRatioCurrentX96 the current price
|
|
57
|
+
* @param sqrtRatioAX96 price at lower boundary
|
|
58
|
+
* @param sqrtRatioBX96 price at upper boundary
|
|
59
|
+
* @param amount0 token0 amount
|
|
60
|
+
* @param amount1 token1 amount
|
|
61
|
+
* @param useFullPrecision if false, liquidity will be maximized according to what the router can calculate,
|
|
62
|
+
* not what core can theoretically support
|
|
63
|
+
*/
|
|
64
|
+
export function maxLiquidityForAmounts(sqrtRatioCurrentX96, sqrtRatioAX96, sqrtRatioBX96, amount0, amount1, useFullPrecision) {
|
|
65
|
+
if (JSBI.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
|
|
66
|
+
;
|
|
67
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
68
|
+
}
|
|
69
|
+
const maxLiquidityForAmount0 = useFullPrecision ? maxLiquidityForAmount0Precise : maxLiquidityForAmount0Imprecise;
|
|
70
|
+
if (JSBI.lessThanOrEqual(sqrtRatioCurrentX96, sqrtRatioAX96)) {
|
|
71
|
+
return maxLiquidityForAmount0(sqrtRatioAX96, sqrtRatioBX96, amount0);
|
|
72
|
+
}
|
|
73
|
+
else if (JSBI.lessThan(sqrtRatioCurrentX96, sqrtRatioBX96)) {
|
|
74
|
+
const liquidity0 = maxLiquidityForAmount0(sqrtRatioCurrentX96, sqrtRatioBX96, amount0);
|
|
75
|
+
const liquidity1 = maxLiquidityForAmount1(sqrtRatioAX96, sqrtRatioCurrentX96, amount1);
|
|
76
|
+
return JSBI.lessThan(liquidity0, liquidity1) ? liquidity0 : liquidity1;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
return maxLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=maxLiquidityForAmounts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"maxLiquidityForAmounts.js","sourceRoot":"","sources":["../../../../src/utils/maxLiquidityForAmounts.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MaxUint256 } from '@centurion_project/sdk-core';
|
|
2
|
+
import JSBI from 'jsbi';
|
|
3
|
+
import invariant from 'tiny-invariant';
|
|
4
|
+
import { ZERO } from '../internalConstants';
|
|
5
|
+
const TWO = JSBI.BigInt(2);
|
|
6
|
+
const POWERS_OF_2 = [128, 64, 32, 16, 8, 4, 2, 1].map((pow) => [
|
|
7
|
+
pow,
|
|
8
|
+
JSBI.exponentiate(TWO, JSBI.BigInt(pow)),
|
|
9
|
+
]);
|
|
10
|
+
export function mostSignificantBit(x) {
|
|
11
|
+
invariant(JSBI.greaterThan(x, ZERO), 'ZERO');
|
|
12
|
+
invariant(JSBI.lessThanOrEqual(x, MaxUint256), 'MAX');
|
|
13
|
+
let msb = 0;
|
|
14
|
+
for (const [power, min] of POWERS_OF_2) {
|
|
15
|
+
if (JSBI.greaterThanOrEqual(x, min)) {
|
|
16
|
+
x = JSBI.signedRightShift(x, JSBI.BigInt(power));
|
|
17
|
+
msb += power;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return msb;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=mostSignificantBit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mostSignificantBit.js","sourceRoot":"","sources":["../../../../src/utils/mostSignificantBit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAA;AACxD,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,SAAS,MAAM,gBAAgB,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAE3C,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AAC1B,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAW,EAAkB,EAAE,CAAC;IACrF,GAAG;IACH,IAAI,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;CACzC,CAAC,CAAA;AAEF,MAAM,UAAU,kBAAkB,CAAC,CAAO;IACxC,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,CAAA;IAC5C,SAAS,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,EAAE,UAAU,CAAC,EAAE,KAAK,CAAC,CAAA;IAErD,IAAI,GAAG,GAAW,CAAC,CAAA;IACnB,KAAK,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,IAAI,WAAW,EAAE;QACtC,IAAI,IAAI,CAAC,kBAAkB,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE;YACnC,CAAC,GAAG,IAAI,CAAC,gBAAgB,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;YAChD,GAAG,IAAI,KAAK,CAAA;SACb;KACF;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the closest tick that is nearest a given tick and usable for the given tick spacing
|
|
3
|
+
* @param tick the target tick
|
|
4
|
+
* @param tickSpacing the spacing of the pool
|
|
5
|
+
*/
|
|
6
|
+
export declare function nearestUsableTick(tick: number, tickSpacing: number): number;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import invariant from 'tiny-invariant';
|
|
2
|
+
import { TickMath } from './tickMath';
|
|
3
|
+
/**
|
|
4
|
+
* Returns the closest tick that is nearest a given tick and usable for the given tick spacing
|
|
5
|
+
* @param tick the target tick
|
|
6
|
+
* @param tickSpacing the spacing of the pool
|
|
7
|
+
*/
|
|
8
|
+
export function nearestUsableTick(tick, tickSpacing) {
|
|
9
|
+
invariant(Number.isInteger(tick) && Number.isInteger(tickSpacing), 'INTEGERS');
|
|
10
|
+
invariant(tickSpacing > 0, 'TICK_SPACING');
|
|
11
|
+
invariant(tick >= TickMath.MIN_TICK && tick <= TickMath.MAX_TICK, 'TICK_BOUND');
|
|
12
|
+
const rounded = Math.round(tick / tickSpacing) * tickSpacing;
|
|
13
|
+
if (rounded < TickMath.MIN_TICK)
|
|
14
|
+
return rounded + tickSpacing;
|
|
15
|
+
else if (rounded > TickMath.MAX_TICK)
|
|
16
|
+
return rounded - tickSpacing;
|
|
17
|
+
else
|
|
18
|
+
return rounded;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=nearestUsableTick.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nearestUsableTick.js","sourceRoot":"","sources":["../../../../src/utils/nearestUsableTick.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAA;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAErC;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY,EAAE,WAAmB;IACjE,SAAS,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,EAAE,UAAU,CAAC,CAAA;IAC9E,SAAS,CAAC,WAAW,GAAG,CAAC,EAAE,cAAc,CAAC,CAAA;IAC1C,SAAS,CAAC,IAAI,IAAI,QAAQ,CAAC,QAAQ,IAAI,IAAI,IAAI,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;IAC/E,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,WAAW,CAAC,GAAG,WAAW,CAAA;IAC5D,IAAI,OAAO,GAAG,QAAQ,CAAC,QAAQ;QAAE,OAAO,OAAO,GAAG,WAAW,CAAA;SACxD,IAAI,OAAO,GAAG,QAAQ,CAAC,QAAQ;QAAE,OAAO,OAAO,GAAG,WAAW,CAAA;;QAC7D,OAAO,OAAO,CAAA;AACrB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import JSBI from 'jsbi';
|
|
2
|
+
export declare abstract class PositionLibrary {
|
|
3
|
+
/**
|
|
4
|
+
* Cannot be constructed.
|
|
5
|
+
*/
|
|
6
|
+
private constructor();
|
|
7
|
+
static getTokensOwed(feeGrowthInside0LastX128: JSBI, feeGrowthInside1LastX128: JSBI, liquidity: JSBI, feeGrowthInside0X128: JSBI, feeGrowthInside1X128: JSBI): JSBI[];
|
|
8
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import JSBI from 'jsbi';
|
|
2
|
+
import { subIn256 } from '.';
|
|
3
|
+
const Q128 = JSBI.exponentiate(JSBI.BigInt(2), JSBI.BigInt(128));
|
|
4
|
+
export class PositionLibrary {
|
|
5
|
+
/**
|
|
6
|
+
* Cannot be constructed.
|
|
7
|
+
*/
|
|
8
|
+
constructor() { }
|
|
9
|
+
// replicates the portions of Position#update required to compute unaccounted fees
|
|
10
|
+
static getTokensOwed(feeGrowthInside0LastX128, feeGrowthInside1LastX128, liquidity, feeGrowthInside0X128, feeGrowthInside1X128) {
|
|
11
|
+
const tokensOwed0 = JSBI.divide(JSBI.multiply(subIn256(feeGrowthInside0X128, feeGrowthInside0LastX128), liquidity), Q128);
|
|
12
|
+
const tokensOwed1 = JSBI.divide(JSBI.multiply(subIn256(feeGrowthInside1X128, feeGrowthInside1LastX128), liquidity), Q128);
|
|
13
|
+
return [tokensOwed0, tokensOwed1];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=position.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"position.js","sourceRoot":"","sources":["../../../../src/utils/position.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,CAAA;AAE5B,MAAM,IAAI,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAA;AAEhE,MAAM,OAAgB,eAAe;IACnC;;OAEG;IACH,gBAAuB,CAAC;IAExB,kFAAkF;IAC3E,MAAM,CAAC,aAAa,CACzB,wBAA8B,EAC9B,wBAA8B,EAC9B,SAAe,EACf,oBAA0B,EAC1B,oBAA0B;QAE1B,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAC7B,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,oBAAoB,EAAE,wBAAwB,CAAC,EAAE,SAAS,CAAC,EAClF,IAAI,CACL,CAAA;QAED,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAC7B,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,oBAAoB,EAAE,wBAAwB,CAAC,EAAE,SAAS,CAAC,EAClF,IAAI,CACL,CAAA;QAED,OAAO,CAAC,WAAW,EAAE,WAAW,CAAC,CAAA;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Price, Token } from '@centurion_project/sdk-core';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a price object corresponding to the input tick and the base/quote token
|
|
4
|
+
* Inputs must be tokens because the address order is used to interpret the price represented by the tick
|
|
5
|
+
* @param baseToken the base token of the price
|
|
6
|
+
* @param quoteToken the quote token of the price
|
|
7
|
+
* @param tick the tick for which to return the price
|
|
8
|
+
*/
|
|
9
|
+
export declare function tickToPrice(baseToken: Token, quoteToken: Token, tick: number): Price<Token, Token>;
|
|
10
|
+
/**
|
|
11
|
+
* Returns the first tick for which the given price is greater than or equal to the tick price
|
|
12
|
+
* @param price for which to return the closest tick that represents a price less than or equal to the input price,
|
|
13
|
+
* i.e. the price of the returned tick is less than or equal to the input price
|
|
14
|
+
*/
|
|
15
|
+
export declare function priceToClosestTick(price: Price<Token, Token>): number;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Price } from '@centurion_project/sdk-core';
|
|
2
|
+
import JSBI from 'jsbi';
|
|
3
|
+
import { Q192 } from '../internalConstants';
|
|
4
|
+
import { encodeSqrtRatioX96 } from './encodeSqrtRatioX96';
|
|
5
|
+
import { TickMath } from './tickMath';
|
|
6
|
+
/**
|
|
7
|
+
* Returns a price object corresponding to the input tick and the base/quote token
|
|
8
|
+
* Inputs must be tokens because the address order is used to interpret the price represented by the tick
|
|
9
|
+
* @param baseToken the base token of the price
|
|
10
|
+
* @param quoteToken the quote token of the price
|
|
11
|
+
* @param tick the tick for which to return the price
|
|
12
|
+
*/
|
|
13
|
+
export function tickToPrice(baseToken, quoteToken, tick) {
|
|
14
|
+
const sqrtRatioX96 = TickMath.getSqrtRatioAtTick(tick);
|
|
15
|
+
const ratioX192 = JSBI.multiply(sqrtRatioX96, sqrtRatioX96);
|
|
16
|
+
return baseToken.sortsBefore(quoteToken)
|
|
17
|
+
? new Price(baseToken, quoteToken, Q192, ratioX192)
|
|
18
|
+
: new Price(baseToken, quoteToken, ratioX192, Q192);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns the first tick for which the given price is greater than or equal to the tick price
|
|
22
|
+
* @param price for which to return the closest tick that represents a price less than or equal to the input price,
|
|
23
|
+
* i.e. the price of the returned tick is less than or equal to the input price
|
|
24
|
+
*/
|
|
25
|
+
export function priceToClosestTick(price) {
|
|
26
|
+
const sorted = price.baseCurrency.sortsBefore(price.quoteCurrency);
|
|
27
|
+
const sqrtRatioX96 = sorted
|
|
28
|
+
? encodeSqrtRatioX96(price.numerator, price.denominator)
|
|
29
|
+
: encodeSqrtRatioX96(price.denominator, price.numerator);
|
|
30
|
+
let tick = TickMath.getTickAtSqrtRatio(sqrtRatioX96);
|
|
31
|
+
const nextTickPrice = tickToPrice(price.baseCurrency, price.quoteCurrency, tick + 1);
|
|
32
|
+
if (sorted) {
|
|
33
|
+
if (!price.lessThan(nextTickPrice)) {
|
|
34
|
+
tick++;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
if (!price.greaterThan(nextTickPrice)) {
|
|
39
|
+
tick++;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return tick;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=priceTickConversions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"priceTickConversions.js","sourceRoot":"","sources":["../../../../src/utils/priceTickConversions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAS,MAAM,6BAA6B,CAAA;AAC1D,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAErC;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,SAAgB,EAAE,UAAiB,EAAE,IAAY;IAC3E,MAAM,YAAY,GAAG,QAAQ,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAA;IAEtD,MAAM,SAAS,GAAG,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE,YAAY,CAAC,CAAA;IAE3D,OAAO,SAAS,CAAC,WAAW,CAAC,UAAU,CAAC;QACtC,CAAC,CAAC,IAAI,KAAK,CAAC,SAAS,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,CAAC;QACnD,CAAC,CAAC,IAAI,KAAK,CAAC,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,CAAC,CAAA;AACvD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAA0B;IAC3D,MAAM,MAAM,GAAG,KAAK,CAAC,YAAY,CAAC,WAAW,CAAC,KAAK,CAAC,aAAa,CAAC,CAAA;IAElE,MAAM,YAAY,GAAG,MAAM;QACzB,CAAC,CAAC,kBAAkB,CAAC,KAAK,CAAC,SAAS,EAAE,KAAK,CAAC,WAAW,CAAC;QACxD,CAAC,CAAC,kBAAkB,CAAC,KAAK,CAAC,WAAW,EAAE,KAAK,CAAC,SAAS,CAAC,CAAA;IAE1D,IAAI,IAAI,GAAG,QAAQ,CAAC,kBAAkB,CAAC,YAAY,CAAC,CAAA;IACpD,MAAM,aAAa,GAAG,WAAW,CAAC,KAAK,CAAC,YAAY,EAAE,KAAK,CAAC,aAAa,EAAE,IAAI,GAAG,CAAC,CAAC,CAAA;IACpF,IAAI,MAAM,EAAE;QACV,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE;YAClC,IAAI,EAAE,CAAA;SACP;KACF;SAAM;QACL,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,aAAa,CAAC,EAAE;YACrC,IAAI,EAAE,CAAA;SACP;KACF;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import JSBI from 'jsbi';
|
|
2
|
+
export declare abstract class SqrtPriceMath {
|
|
3
|
+
/**
|
|
4
|
+
* Cannot be constructed.
|
|
5
|
+
*/
|
|
6
|
+
private constructor();
|
|
7
|
+
static getAmount0Delta(sqrtRatioAX96: JSBI, sqrtRatioBX96: JSBI, liquidity: JSBI, roundUp: boolean): JSBI;
|
|
8
|
+
static getAmount1Delta(sqrtRatioAX96: JSBI, sqrtRatioBX96: JSBI, liquidity: JSBI, roundUp: boolean): JSBI;
|
|
9
|
+
static getNextSqrtPriceFromInput(sqrtPX96: JSBI, liquidity: JSBI, amountIn: JSBI, zeroForOne: boolean): JSBI;
|
|
10
|
+
static getNextSqrtPriceFromOutput(sqrtPX96: JSBI, liquidity: JSBI, amountOut: JSBI, zeroForOne: boolean): JSBI;
|
|
11
|
+
private static getNextSqrtPriceFromAmount0RoundingUp;
|
|
12
|
+
private static getNextSqrtPriceFromAmount1RoundingDown;
|
|
13
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { MaxUint256 } from '@centurion_project/sdk-core';
|
|
2
|
+
import JSBI from 'jsbi';
|
|
3
|
+
import invariant from 'tiny-invariant';
|
|
4
|
+
import { ONE, ZERO, Q96 } from '../internalConstants';
|
|
5
|
+
import { FullMath } from './fullMath';
|
|
6
|
+
const MaxUint160 = JSBI.subtract(JSBI.exponentiate(JSBI.BigInt(2), JSBI.BigInt(160)), ONE);
|
|
7
|
+
function multiplyIn256(x, y) {
|
|
8
|
+
const product = JSBI.multiply(x, y);
|
|
9
|
+
return JSBI.bitwiseAnd(product, MaxUint256);
|
|
10
|
+
}
|
|
11
|
+
function addIn256(x, y) {
|
|
12
|
+
const sum = JSBI.add(x, y);
|
|
13
|
+
return JSBI.bitwiseAnd(sum, MaxUint256);
|
|
14
|
+
}
|
|
15
|
+
export class SqrtPriceMath {
|
|
16
|
+
/**
|
|
17
|
+
* Cannot be constructed.
|
|
18
|
+
*/
|
|
19
|
+
constructor() { }
|
|
20
|
+
static getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
21
|
+
if (JSBI.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
|
|
22
|
+
;
|
|
23
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
24
|
+
}
|
|
25
|
+
const numerator1 = JSBI.leftShift(liquidity, JSBI.BigInt(96));
|
|
26
|
+
const numerator2 = JSBI.subtract(sqrtRatioBX96, sqrtRatioAX96);
|
|
27
|
+
return roundUp
|
|
28
|
+
? FullMath.mulDivRoundingUp(FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96), ONE, sqrtRatioAX96)
|
|
29
|
+
: JSBI.divide(JSBI.divide(JSBI.multiply(numerator1, numerator2), sqrtRatioBX96), sqrtRatioAX96);
|
|
30
|
+
}
|
|
31
|
+
static getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity, roundUp) {
|
|
32
|
+
if (JSBI.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
|
|
33
|
+
;
|
|
34
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
35
|
+
}
|
|
36
|
+
return roundUp
|
|
37
|
+
? FullMath.mulDivRoundingUp(liquidity, JSBI.subtract(sqrtRatioBX96, sqrtRatioAX96), Q96)
|
|
38
|
+
: JSBI.divide(JSBI.multiply(liquidity, JSBI.subtract(sqrtRatioBX96, sqrtRatioAX96)), Q96);
|
|
39
|
+
}
|
|
40
|
+
static getNextSqrtPriceFromInput(sqrtPX96, liquidity, amountIn, zeroForOne) {
|
|
41
|
+
invariant(JSBI.greaterThan(sqrtPX96, ZERO));
|
|
42
|
+
invariant(JSBI.greaterThan(liquidity, ZERO));
|
|
43
|
+
return zeroForOne
|
|
44
|
+
? this.getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true)
|
|
45
|
+
: this.getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true);
|
|
46
|
+
}
|
|
47
|
+
static getNextSqrtPriceFromOutput(sqrtPX96, liquidity, amountOut, zeroForOne) {
|
|
48
|
+
invariant(JSBI.greaterThan(sqrtPX96, ZERO));
|
|
49
|
+
invariant(JSBI.greaterThan(liquidity, ZERO));
|
|
50
|
+
return zeroForOne
|
|
51
|
+
? this.getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false)
|
|
52
|
+
: this.getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false);
|
|
53
|
+
}
|
|
54
|
+
static getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amount, add) {
|
|
55
|
+
if (JSBI.equal(amount, ZERO))
|
|
56
|
+
return sqrtPX96;
|
|
57
|
+
const numerator1 = JSBI.leftShift(liquidity, JSBI.BigInt(96));
|
|
58
|
+
if (add) {
|
|
59
|
+
let product = multiplyIn256(amount, sqrtPX96);
|
|
60
|
+
if (JSBI.equal(JSBI.divide(product, amount), sqrtPX96)) {
|
|
61
|
+
const denominator = addIn256(numerator1, product);
|
|
62
|
+
if (JSBI.greaterThanOrEqual(denominator, numerator1)) {
|
|
63
|
+
return FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return FullMath.mulDivRoundingUp(numerator1, ONE, JSBI.add(JSBI.divide(numerator1, sqrtPX96), amount));
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
let product = multiplyIn256(amount, sqrtPX96);
|
|
70
|
+
invariant(JSBI.equal(JSBI.divide(product, amount), sqrtPX96));
|
|
71
|
+
invariant(JSBI.greaterThan(numerator1, product));
|
|
72
|
+
const denominator = JSBI.subtract(numerator1, product);
|
|
73
|
+
return FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
static getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amount, add) {
|
|
77
|
+
if (add) {
|
|
78
|
+
const quotient = JSBI.lessThanOrEqual(amount, MaxUint160)
|
|
79
|
+
? JSBI.divide(JSBI.leftShift(amount, JSBI.BigInt(96)), liquidity)
|
|
80
|
+
: JSBI.divide(JSBI.multiply(amount, Q96), liquidity);
|
|
81
|
+
return JSBI.add(sqrtPX96, quotient);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const quotient = FullMath.mulDivRoundingUp(amount, Q96, liquidity);
|
|
85
|
+
invariant(JSBI.greaterThan(sqrtPX96, quotient));
|
|
86
|
+
return JSBI.subtract(sqrtPX96, quotient);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=sqrtPriceMath.js.map
|