@paraswap/dex-lib 2.42.28 → 2.42.29-wombat.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/abi/{algebra/AlgebraPool.abi.json → solidly-v3/SolidlyV3Pool.abi.json} +293 -282
- package/build/abi/solidly-v3/SolidlyV3StateMulticall.abi.json +375 -0
- package/build/abi/trader-joe-v2_1/FactoryABI.json +0 -0
- package/build/abi/trader-joe-v2_1/PairABI.json +1493 -0
- package/build/abi/trader-joe-v2_1/RouterABI.json +1489 -0
- package/build/abi/wombat/pool-v2.json +1289 -0
- package/build/abi/wombat/pool-v3.json +2013 -0
- package/build/config.js +2 -2
- package/build/dex/index.js +4 -1
- package/build/dex/index.js.map +1 -1
- package/build/dex/solidly-v3/config.d.ts +9 -0
- package/build/dex/solidly-v3/config.js +58 -0
- package/build/dex/solidly-v3/config.js.map +1 -0
- package/build/dex/solidly-v3/constants.d.ts +27 -0
- package/build/dex/solidly-v3/constants.js +34 -0
- package/build/dex/solidly-v3/constants.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/BitMath.d.ts +4 -0
- package/build/dex/solidly-v3/contract-math/BitMath.js +93 -0
- package/build/dex/solidly-v3/contract-math/BitMath.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/FixedPoint128.d.ts +3 -0
- package/build/dex/solidly-v3/contract-math/FixedPoint128.js +8 -0
- package/build/dex/solidly-v3/contract-math/FixedPoint128.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/FixedPoint96.d.ts +4 -0
- package/build/dex/solidly-v3/contract-math/FixedPoint96.js +9 -0
- package/build/dex/solidly-v3/contract-math/FixedPoint96.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/FullMath.d.ts +4 -0
- package/build/dex/solidly-v3/contract-math/FullMath.js +19 -0
- package/build/dex/solidly-v3/contract-math/FullMath.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/LiquidityMath.d.ts +3 -0
- package/build/dex/solidly-v3/contract-math/LiquidityMath.js +22 -0
- package/build/dex/solidly-v3/contract-math/LiquidityMath.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/SqrtPriceMath.d.ts +10 -0
- package/build/dex/solidly-v3/contract-math/SqrtPriceMath.js +91 -0
- package/build/dex/solidly-v3/contract-math/SqrtPriceMath.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/SwapMath.d.ts +8 -0
- package/build/dex/solidly-v3/contract-math/SwapMath.js +70 -0
- package/build/dex/solidly-v3/contract-math/SwapMath.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/Tick.d.ts +7 -0
- package/build/dex/solidly-v3/contract-math/Tick.js +36 -0
- package/build/dex/solidly-v3/contract-math/Tick.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/TickBitMap.d.ts +7 -0
- package/build/dex/solidly-v3/contract-math/TickBitMap.js +84 -0
- package/build/dex/solidly-v3/contract-math/TickBitMap.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/TickMath.d.ts +8 -0
- package/build/dex/solidly-v3/contract-math/TickMath.js +162 -0
- package/build/dex/solidly-v3/contract-math/TickMath.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/UnsafeMath.d.ts +3 -0
- package/build/dex/solidly-v3/contract-math/UnsafeMath.js +10 -0
- package/build/dex/solidly-v3/contract-math/UnsafeMath.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/uniswap-v3-math.d.ts +31 -0
- package/build/dex/solidly-v3/contract-math/uniswap-v3-math.js +363 -0
- package/build/dex/solidly-v3/contract-math/uniswap-v3-math.js.map +1 -0
- package/build/dex/solidly-v3/contract-math/utils.d.ts +7 -0
- package/build/dex/solidly-v3/contract-math/utils.js +39 -0
- package/build/dex/solidly-v3/contract-math/utils.js.map +1 -0
- package/build/dex/solidly-v3/solidly-v3-factory.d.ts +26 -0
- package/build/dex/solidly-v3/solidly-v3-factory.js +44 -0
- package/build/dex/solidly-v3/solidly-v3-factory.js.map +1 -0
- package/build/dex/{algebra/algebra-pool.d.ts → solidly-v3/solidly-v3-pool.d.ts} +14 -16
- package/build/dex/{algebra/algebra-pool.js → solidly-v3/solidly-v3-pool.js} +127 -165
- package/build/dex/solidly-v3/solidly-v3-pool.js.map +1 -0
- package/build/dex/solidly-v3/solidly-v3.d.ts +67 -0
- package/build/dex/solidly-v3/solidly-v3.js +590 -0
- package/build/dex/solidly-v3/solidly-v3.js.map +1 -0
- package/build/dex/solidly-v3/types.d.ts +132 -0
- package/build/dex/solidly-v3/types.js +9 -0
- package/build/dex/solidly-v3/types.js.map +1 -0
- package/build/dex/solidly-v3/utils.d.ts +6 -0
- package/build/dex/solidly-v3/utils.js +53 -0
- package/build/dex/solidly-v3/utils.js.map +1 -0
- package/build/dex/trader-joe-v2-1-2/config.d.ts +4 -0
- package/build/dex/trader-joe-v2-1-2/config.js +43 -0
- package/build/dex/trader-joe-v2-1-2/config.js.map +1 -0
- package/build/dex/trader-joe-v2-1-2/constants.d.ts +17 -0
- package/build/dex/trader-joe-v2-1-2/constants.js +19 -0
- package/build/dex/trader-joe-v2-1-2/constants.js.map +1 -0
- package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.d.ts +60 -0
- package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.js +199 -0
- package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.js.map +1 -0
- package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.d.ts +49 -0
- package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.js +269 -0
- package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.js.map +1 -0
- package/build/dex/trader-joe-v2-1-2/types.d.ts +81 -0
- package/build/dex/trader-joe-v2-1-2/types.js +9 -0
- package/build/dex/trader-joe-v2-1-2/types.js.map +1 -0
- package/build/dex/trader-joe-v2_1/config.d.ts +4 -0
- package/build/dex/trader-joe-v2_1/config.js +43 -0
- package/build/dex/trader-joe-v2_1/config.js.map +1 -0
- package/build/dex/trader-joe-v2_1/constants.d.ts +16 -0
- package/build/dex/trader-joe-v2_1/constants.js +18 -0
- package/build/dex/trader-joe-v2_1/constants.js.map +1 -0
- package/build/dex/trader-joe-v2_1/trader-joe-v2.1.d.ts +0 -0
- package/build/dex/trader-joe-v2_1/trader-joe-v2.1.js +384 -0
- package/build/dex/trader-joe-v2_1/trader-joe-v2.1.js.map +1 -0
- package/build/dex/trader-joe-v2_1/types.d.ts +39 -0
- package/build/dex/trader-joe-v2_1/types.js +3 -0
- package/build/dex/trader-joe-v2_1/types.js.map +1 -0
- package/build/dex/wombat/types.d.ts +2 -0
- package/build/dex/wombat/utils.d.ts +1 -0
- package/build/dex/wombat/utils.js +14 -1
- package/build/dex/wombat/utils.js.map +1 -1
- package/build/dex/wombat/wombat-bmw.d.ts +2 -2
- package/build/dex/wombat/wombat-bmw.js +8 -25
- package/build/dex/wombat/wombat-bmw.js.map +1 -1
- package/build/dex/wombat/wombat-pool-poller.d.ts +22 -0
- package/build/dex/wombat/wombat-pool-poller.js +164 -0
- package/build/dex/wombat/wombat-pool-poller.js.map +1 -0
- package/build/dex/wombat/wombat-pool.d.ts +51 -17
- package/build/dex/wombat/wombat-pool.js +393 -112
- package/build/dex/wombat/wombat-pool.js.map +1 -1
- package/build/dex/wombat/wombat.d.ts +1 -1
- package/build/dex/wombat/wombat.js +12 -18
- package/build/dex/wombat/wombat.js.map +1 -1
- package/package.json +1 -1
- package/src/abi/solidly-v3/SolidlyV3Pool.abi.json +738 -0
- package/src/abi/solidly-v3/SolidlyV3StateMulticall.abi.json +375 -0
- package/src/abi/wombat/pool-v3.json +2013 -0
- package/src/config.ts +2 -2
- package/src/dex/index.ts +4 -1
- package/src/dex/solidly-v3/config.ts +64 -0
- package/src/dex/solidly-v3/constants.ts +42 -0
- package/src/dex/solidly-v3/contract-math/BitMath.ts +90 -0
- package/src/dex/solidly-v3/contract-math/FixedPoint128.ts +3 -0
- package/src/dex/solidly-v3/contract-math/FixedPoint96.ts +4 -0
- package/src/dex/solidly-v3/contract-math/FullMath.ts +30 -0
- package/src/dex/solidly-v3/contract-math/LiquidityMath.ts +17 -0
- package/src/dex/solidly-v3/contract-math/SqrtPriceMath.ts +226 -0
- package/src/dex/solidly-v3/contract-math/SwapMath.ts +139 -0
- package/src/dex/solidly-v3/contract-math/Tick.ts +66 -0
- package/src/dex/solidly-v3/contract-math/TickBitMap.ts +123 -0
- package/src/dex/solidly-v3/contract-math/TickMath.ts +211 -0
- package/src/dex/solidly-v3/contract-math/UnsafeMath.ts +5 -0
- package/src/dex/solidly-v3/contract-math/uniswap-v3-math.ts +570 -0
- package/src/dex/solidly-v3/contract-math/utils.ts +50 -0
- package/src/dex/solidly-v3/solidly-v3-e2e.test.ts +124 -0
- package/src/dex/solidly-v3/solidly-v3-events.test.ts +144 -0
- package/src/dex/solidly-v3/solidly-v3-factory.ts +73 -0
- package/src/dex/solidly-v3/solidly-v3-integration.test.ts +358 -0
- package/src/dex/solidly-v3/solidly-v3-pool.ts +494 -0
- package/src/dex/solidly-v3/solidly-v3.ts +988 -0
- package/src/dex/solidly-v3/types.ts +158 -0
- package/src/dex/solidly-v3/utils.ts +71 -0
- package/src/dex/uniswap-v3/uniswap-v3-events.test.ts +3 -0
- package/src/dex/wombat/types.ts +5 -0
- package/src/dex/wombat/utils.ts +20 -0
- package/src/dex/wombat/wombat-bmw.ts +8 -31
- package/src/dex/wombat/wombat-e2e.test.ts +13 -1
- package/src/dex/wombat/wombat-events.test.ts +147 -0
- package/src/dex/wombat/wombat-integration.test.ts +101 -111
- package/src/dex/wombat/wombat-pool.ts +620 -171
- package/src/dex/wombat/wombat.ts +13 -23
- package/tests/constants-e2e.ts +11 -1
- package/build/abi/uniswap-v3/UniswapV3Quoter.abi.json +0 -193
- package/build/dex/algebra/algebra-pool.js.map +0 -1
- package/build/dex/quickswap/camelot-v3.d.ts +0 -6
- package/build/dex/quickswap/camelot-v3.js +0 -19
- package/build/dex/quickswap/camelot-v3.js.map +0 -1
- package/build/dex/quickswap/zyberswap-v3.d.ts +0 -6
- package/build/dex/quickswap/zyberswap-v3.js +0 -19
- package/build/dex/quickswap/zyberswap-v3.js.map +0 -1
- package/build/dex/uniswap-v2/rebase-tokens.json +0 -7
- /package/src/abi/wombat/{pool.json → pool-v2.json} +0 -0
package/src/config.ts
CHANGED
|
@@ -53,9 +53,9 @@ const baseConfigs: { [network: number]: BaseConfig } = {
|
|
|
53
53
|
Adapter01: '0x9bE264469eF954c139Da4A45Cf76CbCC5e3A6A73',
|
|
54
54
|
Adapter02: '0xFC2Ba6E830a04C25e207B8214b26d8C713F6881F',
|
|
55
55
|
Adapter03: '0xBAEeb4540f59d30E567a5B563CC0c4587eDd9366',
|
|
56
|
-
Adapter04: '
|
|
56
|
+
Adapter04: '0x369A2FDb910d432f0a07381a5E3d27572c876713',
|
|
57
57
|
BuyAdapter: '0x84bEF12C9931cE12662cc9F2366b6a5029E4BD29',
|
|
58
|
-
BuyAdapter02: '
|
|
58
|
+
BuyAdapter02: '0xe53d24CD81cC81bbf271AD7B02D0d67f851D727c',
|
|
59
59
|
},
|
|
60
60
|
uniswapV2ExchangeRouterAddress:
|
|
61
61
|
'0xF9234CB08edb93c0d4a4d4c70cC3FfD070e78e07',
|
package/src/dex/index.ts
CHANGED
|
@@ -44,6 +44,7 @@ import { WooFiV2 } from './woo-fi-v2/woo-fi-v2';
|
|
|
44
44
|
import { ParaSwapLimitOrders } from './paraswap-limit-orders/paraswap-limit-orders';
|
|
45
45
|
import { AugustusRFQOrder } from './augustus-rfq';
|
|
46
46
|
import { Solidly } from './solidly/solidly';
|
|
47
|
+
import { SolidlyV3 } from './solidly-v3/solidly-v3';
|
|
47
48
|
import { Ramses } from './solidly/forks-override/ramses';
|
|
48
49
|
import { Thena } from './solidly/forks-override/thena';
|
|
49
50
|
import { Chronos } from './solidly/forks-override/chronos';
|
|
@@ -82,6 +83,7 @@ import { QuickPerps } from './quick-perps/quick-perps';
|
|
|
82
83
|
import { NomiswapV2 } from './uniswap-v2/nomiswap-v2';
|
|
83
84
|
import { Dexalot } from './dexalot/dexalot';
|
|
84
85
|
import { Smardex } from './smardex/smardex';
|
|
86
|
+
import { Wombat } from './wombat/wombat';
|
|
85
87
|
|
|
86
88
|
const LegacyDexes = [
|
|
87
89
|
CurveV2,
|
|
@@ -159,8 +161,9 @@ const Dexes = [
|
|
|
159
161
|
SwaapV2,
|
|
160
162
|
QuickPerps,
|
|
161
163
|
NomiswapV2,
|
|
164
|
+
SolidlyV3,
|
|
162
165
|
Smardex,
|
|
163
|
-
|
|
166
|
+
Wombat,
|
|
164
167
|
];
|
|
165
168
|
|
|
166
169
|
export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { DexParams } from './types';
|
|
2
|
+
import { DexConfigMap, AdapterMappings } from '../../types';
|
|
3
|
+
import { Network, SwapSide } from '../../constants';
|
|
4
|
+
import { Address } from '../../types';
|
|
5
|
+
|
|
6
|
+
const SUPPORTED_TICK_SPACINGS = [1n, 10n, 50n, 100n];
|
|
7
|
+
|
|
8
|
+
// Pools that will be initialized on app startup
|
|
9
|
+
// They are added for testing
|
|
10
|
+
export const PoolsToPreload: DexConfigMap<
|
|
11
|
+
{ token0: Address; token1: Address }[]
|
|
12
|
+
> = {
|
|
13
|
+
SolidlyV3: {
|
|
14
|
+
[Network.MAINNET]: [
|
|
15
|
+
{
|
|
16
|
+
token0: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'.toLowerCase(),
|
|
17
|
+
token1: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'.toLowerCase(),
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
token0: '0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0'.toLowerCase(),
|
|
21
|
+
token1: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'.toLowerCase(),
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
token0: '0x514910771AF9Ca656af840dff83E8264EcF986CA'.toLowerCase(),
|
|
25
|
+
token1: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'.toLowerCase(),
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
token0: '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599'.toLowerCase(),
|
|
29
|
+
token1: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'.toLowerCase(),
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
token0: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'.toLowerCase(),
|
|
33
|
+
token1: '0xdAC17F958D2ee523a2206206994597C13D831ec7'.toLowerCase(),
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
token0: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'.toLowerCase(),
|
|
37
|
+
token1: '0xdAC17F958D2ee523a2206206994597C13D831ec7'.toLowerCase(),
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const SolidlyV3Config: DexConfigMap<DexParams> = {
|
|
44
|
+
SolidlyV3: {
|
|
45
|
+
[Network.MAINNET]: {
|
|
46
|
+
factory: '0x70Fe4a44EA505cFa3A57b95cF2862D4fd5F0f687',
|
|
47
|
+
quoter: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e',
|
|
48
|
+
supportedTickSpacings: SUPPORTED_TICK_SPACINGS,
|
|
49
|
+
stateMulticall: '0xb229563028302AA693EEaD62F80CC331aEDE4e26',
|
|
50
|
+
chunksCount: 10,
|
|
51
|
+
initRetryFrequency: 10,
|
|
52
|
+
initHash: `0xe9b68c5f77858eecac2e651646e208175e9b1359d68d0e14fc69f8c54e5010bf`,
|
|
53
|
+
subgraphURL:
|
|
54
|
+
'https://api.thegraph.com/subgraphs/name/solidlylabs/solidly-v3',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const Adapters: Record<number, AdapterMappings> = {
|
|
60
|
+
[Network.MAINNET]: {
|
|
61
|
+
[SwapSide.SELL]: [{ name: 'Adapter04', index: 7 }],
|
|
62
|
+
[SwapSide.BUY]: [{ name: 'BuyAdapter02', index: 3 }],
|
|
63
|
+
},
|
|
64
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export const UNISWAPV3_TICK_GAS_COST = 10_000; // Ceiled
|
|
2
|
+
export const UNISWAPV3_TICK_BASE_OVERHEAD = 50_000;
|
|
3
|
+
export const UNISWAPV3_POOL_SEARCH_OVERHEAD = 0;
|
|
4
|
+
|
|
5
|
+
// This is used for price calculation. If out of scope, return 0n
|
|
6
|
+
export const TICK_BITMAP_TO_USE = 4n;
|
|
7
|
+
|
|
8
|
+
// This is used to check if the state is still valid.
|
|
9
|
+
export const TICK_BITMAP_BUFFER = 8n;
|
|
10
|
+
|
|
11
|
+
export const MAX_PRICING_COMPUTATION_STEPS_ALLOWED = 128;
|
|
12
|
+
|
|
13
|
+
export const UNISWAPV3_SUBGRAPH_URL =
|
|
14
|
+
'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3';
|
|
15
|
+
|
|
16
|
+
export const UNISWAPV3_EFFICIENCY_FACTOR = 3;
|
|
17
|
+
|
|
18
|
+
export const ZERO_TICK_INFO = {
|
|
19
|
+
liquidityGross: 0n,
|
|
20
|
+
liquidityNet: 0n,
|
|
21
|
+
initialized: false,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const ZERO_ORACLE_OBSERVATION = {
|
|
25
|
+
blockTimestamp: 0n,
|
|
26
|
+
tickCumulative: 0n,
|
|
27
|
+
secondsPerLiquidityCumulativeX128: 0n,
|
|
28
|
+
initialized: false,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const OUT_OF_RANGE_ERROR_POSTFIX = `INVALID_TICK_BIT_MAP_RANGES`;
|
|
32
|
+
|
|
33
|
+
export const DEFAULT_POOL_INIT_CODE_HASH = `0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54`;
|
|
34
|
+
|
|
35
|
+
export enum DirectMethods {
|
|
36
|
+
directSell = 'directUniV3Swap',
|
|
37
|
+
directBuy = 'directUniV3Buy',
|
|
38
|
+
}
|
|
39
|
+
export const MIN_SQRT_RATIO = BigInt('4295128739');
|
|
40
|
+
export const MAX_SQRT_RATIO = BigInt(
|
|
41
|
+
'1461446703485210103287273052203988822378723970342',
|
|
42
|
+
);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BI_MAX_UINT128,
|
|
3
|
+
BI_MAX_UINT16,
|
|
4
|
+
BI_MAX_UINT32,
|
|
5
|
+
BI_MAX_UINT64,
|
|
6
|
+
BI_MAX_UINT8,
|
|
7
|
+
} from '../../../bigint-constants';
|
|
8
|
+
import { _require } from '../../../utils';
|
|
9
|
+
|
|
10
|
+
export class BitMath {
|
|
11
|
+
static mostSignificantBit(x: bigint): bigint {
|
|
12
|
+
_require(x > 0, '', { x }, 'x > 0');
|
|
13
|
+
let r = 0n;
|
|
14
|
+
|
|
15
|
+
if (x >= 0x100000000000000000000000000000000n) {
|
|
16
|
+
x >>= 128n;
|
|
17
|
+
r += 128n;
|
|
18
|
+
}
|
|
19
|
+
if (x >= 0x10000000000000000n) {
|
|
20
|
+
x >>= 64n;
|
|
21
|
+
r += 64n;
|
|
22
|
+
}
|
|
23
|
+
if (x >= 0x100000000n) {
|
|
24
|
+
x >>= 32n;
|
|
25
|
+
r += 32n;
|
|
26
|
+
}
|
|
27
|
+
if (x >= 0x10000n) {
|
|
28
|
+
x >>= 16n;
|
|
29
|
+
r += 16n;
|
|
30
|
+
}
|
|
31
|
+
if (x >= 0x100n) {
|
|
32
|
+
x >>= 8n;
|
|
33
|
+
r += 8n;
|
|
34
|
+
}
|
|
35
|
+
if (x >= 0x10n) {
|
|
36
|
+
x >>= 4n;
|
|
37
|
+
r += 4n;
|
|
38
|
+
}
|
|
39
|
+
if (x >= 0x4n) {
|
|
40
|
+
x >>= 2n;
|
|
41
|
+
r += 2n;
|
|
42
|
+
}
|
|
43
|
+
if (x >= 0x2n) r += 1n;
|
|
44
|
+
|
|
45
|
+
return r;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static leastSignificantBit(x: bigint): bigint {
|
|
49
|
+
_require(x > 0, '', { x }, 'x > 0');
|
|
50
|
+
|
|
51
|
+
let r = 255n;
|
|
52
|
+
if ((x & BI_MAX_UINT128) > 0n) {
|
|
53
|
+
r -= 128n;
|
|
54
|
+
} else {
|
|
55
|
+
x >>= 128n;
|
|
56
|
+
}
|
|
57
|
+
if ((x & BI_MAX_UINT64) > 0n) {
|
|
58
|
+
r -= 64n;
|
|
59
|
+
} else {
|
|
60
|
+
x >>= 64n;
|
|
61
|
+
}
|
|
62
|
+
if ((x & BI_MAX_UINT32) > 0n) {
|
|
63
|
+
r -= 32n;
|
|
64
|
+
} else {
|
|
65
|
+
x >>= 32n;
|
|
66
|
+
}
|
|
67
|
+
if ((x & BI_MAX_UINT16) > 0n) {
|
|
68
|
+
r -= 16n;
|
|
69
|
+
} else {
|
|
70
|
+
x >>= 16n;
|
|
71
|
+
}
|
|
72
|
+
if ((x & BI_MAX_UINT8) > 0n) {
|
|
73
|
+
r -= 8n;
|
|
74
|
+
} else {
|
|
75
|
+
x >>= 8n;
|
|
76
|
+
}
|
|
77
|
+
if ((x & 0xfn) > 0n) {
|
|
78
|
+
r -= 4n;
|
|
79
|
+
} else {
|
|
80
|
+
x >>= 4n;
|
|
81
|
+
}
|
|
82
|
+
if ((x & 0x3n) > 0n) {
|
|
83
|
+
r -= 2n;
|
|
84
|
+
} else {
|
|
85
|
+
x >>= 2n;
|
|
86
|
+
}
|
|
87
|
+
if ((x & 0x1n) > 0n) r -= 1n;
|
|
88
|
+
return r;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { BI_MAX_UINT256 } from '../../../bigint-constants';
|
|
2
|
+
import { _require } from '../../../utils';
|
|
3
|
+
|
|
4
|
+
export class FullMath {
|
|
5
|
+
static mulDiv(a: bigint, b: bigint, denominator: bigint) {
|
|
6
|
+
const result = (a * b) / denominator;
|
|
7
|
+
|
|
8
|
+
_require(
|
|
9
|
+
result <= BI_MAX_UINT256,
|
|
10
|
+
'',
|
|
11
|
+
{ result, BI_MAX_UINT: BI_MAX_UINT256 },
|
|
12
|
+
'result <= BI_MAX_UINT',
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static mulDivRoundingUp(a: bigint, b: bigint, denominator: bigint) {
|
|
19
|
+
const result = (a * b + denominator - 1n) / denominator;
|
|
20
|
+
|
|
21
|
+
_require(
|
|
22
|
+
result <= BI_MAX_UINT256,
|
|
23
|
+
'',
|
|
24
|
+
{ result, BI_MAX_UINT: BI_MAX_UINT256 },
|
|
25
|
+
'result <= BI_MAX_UINT',
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { _require } from '../../../utils';
|
|
2
|
+
|
|
3
|
+
export class LiquidityMath {
|
|
4
|
+
static addDelta(x: bigint, y: bigint): bigint {
|
|
5
|
+
let z;
|
|
6
|
+
if (y < 0) {
|
|
7
|
+
const _y = BigInt.asUintN(128, -y);
|
|
8
|
+
z = x - _y;
|
|
9
|
+
_require(z < x, 'LS', { z, x, y, _y }, 'z < x');
|
|
10
|
+
} else {
|
|
11
|
+
const _y = BigInt.asUintN(128, y);
|
|
12
|
+
z = x + _y;
|
|
13
|
+
_require(z >= x, 'LA', { z, x, y, _y }, 'z >= x');
|
|
14
|
+
}
|
|
15
|
+
return z;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { BI_MAX_UINT160 } from '../../../bigint-constants';
|
|
2
|
+
import { FixedPoint96 } from './FixedPoint96';
|
|
3
|
+
import { FullMath } from './FullMath';
|
|
4
|
+
import { UnsafeMath } from './UnsafeMath';
|
|
5
|
+
import { _require } from '../../../utils';
|
|
6
|
+
|
|
7
|
+
export class SqrtPriceMath {
|
|
8
|
+
static getNextSqrtPriceFromAmount0RoundingUp(
|
|
9
|
+
sqrtPX96: bigint,
|
|
10
|
+
liquidity: bigint,
|
|
11
|
+
amount: bigint,
|
|
12
|
+
add: boolean,
|
|
13
|
+
): bigint {
|
|
14
|
+
if (amount === 0n) return sqrtPX96;
|
|
15
|
+
const numerator1 =
|
|
16
|
+
BigInt.asUintN(256, liquidity) << FixedPoint96.RESOLUTION;
|
|
17
|
+
|
|
18
|
+
const product = amount * sqrtPX96;
|
|
19
|
+
if (add) {
|
|
20
|
+
if (product / amount === sqrtPX96) {
|
|
21
|
+
const denominator = numerator1 + product;
|
|
22
|
+
if (denominator >= numerator1) {
|
|
23
|
+
return BigInt.asUintN(
|
|
24
|
+
160,
|
|
25
|
+
FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator),
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return BigInt.asUintN(
|
|
30
|
+
160,
|
|
31
|
+
UnsafeMath.divRoundingUp(numerator1, numerator1 / sqrtPX96 + amount),
|
|
32
|
+
);
|
|
33
|
+
} else {
|
|
34
|
+
_require(
|
|
35
|
+
product / amount === sqrtPX96 && numerator1 > product,
|
|
36
|
+
'',
|
|
37
|
+
{ product, amount, sqrtPX96, numerator1 },
|
|
38
|
+
'product / amount === sqrtPX96 && numerator1 > product',
|
|
39
|
+
);
|
|
40
|
+
const denominator = numerator1 - product;
|
|
41
|
+
return BigInt.asUintN(
|
|
42
|
+
160,
|
|
43
|
+
FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static getNextSqrtPriceFromAmount1RoundingDown(
|
|
49
|
+
sqrtPX96: bigint,
|
|
50
|
+
liquidity: bigint,
|
|
51
|
+
amount: bigint,
|
|
52
|
+
add: boolean,
|
|
53
|
+
): bigint {
|
|
54
|
+
if (add) {
|
|
55
|
+
const quotient =
|
|
56
|
+
amount <= BI_MAX_UINT160
|
|
57
|
+
? (amount << FixedPoint96.RESOLUTION) / liquidity
|
|
58
|
+
: FullMath.mulDiv(amount, FixedPoint96.Q96, liquidity);
|
|
59
|
+
return BigInt.asUintN(160, BigInt.asUintN(256, sqrtPX96) + quotient);
|
|
60
|
+
} else {
|
|
61
|
+
const quotient =
|
|
62
|
+
amount <= BI_MAX_UINT160
|
|
63
|
+
? UnsafeMath.divRoundingUp(
|
|
64
|
+
amount << FixedPoint96.RESOLUTION,
|
|
65
|
+
liquidity,
|
|
66
|
+
)
|
|
67
|
+
: FullMath.mulDivRoundingUp(amount, FixedPoint96.Q96, liquidity);
|
|
68
|
+
|
|
69
|
+
_require(
|
|
70
|
+
sqrtPX96 > quotient,
|
|
71
|
+
'',
|
|
72
|
+
{ sqrtPX96, quotient },
|
|
73
|
+
'sqrtPX96 > quotient',
|
|
74
|
+
);
|
|
75
|
+
return BigInt.asUintN(160, sqrtPX96 - quotient);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static getNextSqrtPriceFromInput(
|
|
80
|
+
sqrtPX96: bigint,
|
|
81
|
+
liquidity: bigint,
|
|
82
|
+
amountIn: bigint,
|
|
83
|
+
zeroForOne: boolean,
|
|
84
|
+
): bigint {
|
|
85
|
+
_require(sqrtPX96 > 0n, '', { sqrtPX96 }, 'sqrtPX96 > 0n');
|
|
86
|
+
_require(liquidity > 0n, '', { liquidity }, 'liquidity > 0n');
|
|
87
|
+
|
|
88
|
+
return zeroForOne
|
|
89
|
+
? SqrtPriceMath.getNextSqrtPriceFromAmount0RoundingUp(
|
|
90
|
+
sqrtPX96,
|
|
91
|
+
liquidity,
|
|
92
|
+
amountIn,
|
|
93
|
+
true,
|
|
94
|
+
)
|
|
95
|
+
: SqrtPriceMath.getNextSqrtPriceFromAmount1RoundingDown(
|
|
96
|
+
sqrtPX96,
|
|
97
|
+
liquidity,
|
|
98
|
+
amountIn,
|
|
99
|
+
true,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static getNextSqrtPriceFromOutput(
|
|
104
|
+
sqrtPX96: bigint,
|
|
105
|
+
liquidity: bigint,
|
|
106
|
+
amountOut: bigint,
|
|
107
|
+
zeroForOne: boolean,
|
|
108
|
+
): bigint {
|
|
109
|
+
_require(sqrtPX96 > 0n, '', { sqrtPX96 }, 'sqrtPX96 > 0n');
|
|
110
|
+
_require(liquidity > 0n, '', { liquidity }, 'liquidity > 0n');
|
|
111
|
+
|
|
112
|
+
return zeroForOne
|
|
113
|
+
? SqrtPriceMath.getNextSqrtPriceFromAmount1RoundingDown(
|
|
114
|
+
sqrtPX96,
|
|
115
|
+
liquidity,
|
|
116
|
+
amountOut,
|
|
117
|
+
false,
|
|
118
|
+
)
|
|
119
|
+
: SqrtPriceMath.getNextSqrtPriceFromAmount0RoundingUp(
|
|
120
|
+
sqrtPX96,
|
|
121
|
+
liquidity,
|
|
122
|
+
amountOut,
|
|
123
|
+
false,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static getAmount0Delta(
|
|
128
|
+
sqrtRatioAX96: bigint,
|
|
129
|
+
sqrtRatioBX96: bigint,
|
|
130
|
+
liquidity: bigint,
|
|
131
|
+
roundUp: boolean,
|
|
132
|
+
) {
|
|
133
|
+
if (sqrtRatioAX96 > sqrtRatioBX96) {
|
|
134
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const numerator1 =
|
|
138
|
+
BigInt.asUintN(256, liquidity) << FixedPoint96.RESOLUTION;
|
|
139
|
+
const numerator2 = sqrtRatioBX96 - sqrtRatioAX96;
|
|
140
|
+
|
|
141
|
+
_require(sqrtRatioAX96 > 0, '', { sqrtRatioAX96 }, 'sqrtRatioAX96 > 0');
|
|
142
|
+
|
|
143
|
+
return roundUp
|
|
144
|
+
? UnsafeMath.divRoundingUp(
|
|
145
|
+
FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96),
|
|
146
|
+
sqrtRatioAX96,
|
|
147
|
+
)
|
|
148
|
+
: FullMath.mulDiv(numerator1, numerator2, sqrtRatioBX96) / sqrtRatioAX96;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static getAmount1Delta(
|
|
152
|
+
sqrtRatioAX96: bigint,
|
|
153
|
+
sqrtRatioBX96: bigint,
|
|
154
|
+
liquidity: bigint,
|
|
155
|
+
roundUp: boolean,
|
|
156
|
+
) {
|
|
157
|
+
if (sqrtRatioAX96 > sqrtRatioBX96)
|
|
158
|
+
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
|
|
159
|
+
|
|
160
|
+
return roundUp
|
|
161
|
+
? FullMath.mulDivRoundingUp(
|
|
162
|
+
liquidity,
|
|
163
|
+
sqrtRatioBX96 - sqrtRatioAX96,
|
|
164
|
+
FixedPoint96.Q96,
|
|
165
|
+
)
|
|
166
|
+
: FullMath.mulDiv(
|
|
167
|
+
liquidity,
|
|
168
|
+
sqrtRatioBX96 - sqrtRatioAX96,
|
|
169
|
+
FixedPoint96.Q96,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Overloaded with different argument numbers
|
|
174
|
+
static _getAmount0DeltaO(
|
|
175
|
+
sqrtRatioAX96: bigint,
|
|
176
|
+
sqrtRatioBX96: bigint,
|
|
177
|
+
liquidity: bigint,
|
|
178
|
+
) {
|
|
179
|
+
return liquidity < 0
|
|
180
|
+
? -BigInt.asIntN(
|
|
181
|
+
256,
|
|
182
|
+
SqrtPriceMath.getAmount0Delta(
|
|
183
|
+
sqrtRatioAX96,
|
|
184
|
+
sqrtRatioBX96,
|
|
185
|
+
BigInt.asUintN(128, -liquidity),
|
|
186
|
+
false,
|
|
187
|
+
),
|
|
188
|
+
)
|
|
189
|
+
: BigInt.asIntN(
|
|
190
|
+
256,
|
|
191
|
+
SqrtPriceMath.getAmount0Delta(
|
|
192
|
+
sqrtRatioAX96,
|
|
193
|
+
sqrtRatioBX96,
|
|
194
|
+
BigInt.asUintN(128, liquidity),
|
|
195
|
+
true,
|
|
196
|
+
),
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Overloaded with different argument numbers
|
|
201
|
+
static _getAmount1DeltaO(
|
|
202
|
+
sqrtRatioAX96: bigint,
|
|
203
|
+
sqrtRatioBX96: bigint,
|
|
204
|
+
liquidity: bigint,
|
|
205
|
+
) {
|
|
206
|
+
return liquidity < 0
|
|
207
|
+
? -BigInt.asIntN(
|
|
208
|
+
256,
|
|
209
|
+
SqrtPriceMath.getAmount1Delta(
|
|
210
|
+
sqrtRatioAX96,
|
|
211
|
+
sqrtRatioBX96,
|
|
212
|
+
BigInt.asUintN(128, -liquidity),
|
|
213
|
+
false,
|
|
214
|
+
),
|
|
215
|
+
)
|
|
216
|
+
: BigInt.asIntN(
|
|
217
|
+
256,
|
|
218
|
+
SqrtPriceMath.getAmount1Delta(
|
|
219
|
+
sqrtRatioAX96,
|
|
220
|
+
sqrtRatioBX96,
|
|
221
|
+
BigInt.asUintN(128, liquidity),
|
|
222
|
+
true,
|
|
223
|
+
),
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { BI_POWS } from '../../../bigint-constants';
|
|
2
|
+
import { FullMath } from './FullMath';
|
|
3
|
+
import { SqrtPriceMath } from './SqrtPriceMath';
|
|
4
|
+
|
|
5
|
+
export class SwapMath {
|
|
6
|
+
static computeSwapStep(
|
|
7
|
+
sqrtRatioCurrentX96: bigint,
|
|
8
|
+
sqrtRatioTargetX96: bigint,
|
|
9
|
+
liquidity: bigint,
|
|
10
|
+
amountRemaining: bigint,
|
|
11
|
+
feePips: bigint,
|
|
12
|
+
): {
|
|
13
|
+
sqrtRatioNextX96: bigint;
|
|
14
|
+
amountIn: bigint;
|
|
15
|
+
amountOut: bigint;
|
|
16
|
+
feeAmount: bigint;
|
|
17
|
+
} {
|
|
18
|
+
const zeroForOne = sqrtRatioCurrentX96 >= sqrtRatioTargetX96;
|
|
19
|
+
const exactIn = amountRemaining >= 0n;
|
|
20
|
+
|
|
21
|
+
let sqrtRatioNextX96 = 0n;
|
|
22
|
+
let amountIn = 0n;
|
|
23
|
+
let amountOut = 0n;
|
|
24
|
+
let feeAmount = 0n;
|
|
25
|
+
|
|
26
|
+
if (exactIn) {
|
|
27
|
+
const amountRemainingLessFee = FullMath.mulDiv(
|
|
28
|
+
BigInt.asUintN(256, amountRemaining),
|
|
29
|
+
BI_POWS[6] - feePips,
|
|
30
|
+
BI_POWS[6],
|
|
31
|
+
);
|
|
32
|
+
amountIn = zeroForOne
|
|
33
|
+
? SqrtPriceMath.getAmount0Delta(
|
|
34
|
+
sqrtRatioTargetX96,
|
|
35
|
+
sqrtRatioCurrentX96,
|
|
36
|
+
liquidity,
|
|
37
|
+
true,
|
|
38
|
+
)
|
|
39
|
+
: SqrtPriceMath.getAmount1Delta(
|
|
40
|
+
sqrtRatioCurrentX96,
|
|
41
|
+
sqrtRatioTargetX96,
|
|
42
|
+
liquidity,
|
|
43
|
+
true,
|
|
44
|
+
);
|
|
45
|
+
if (amountRemainingLessFee >= amountIn)
|
|
46
|
+
sqrtRatioNextX96 = sqrtRatioTargetX96;
|
|
47
|
+
else
|
|
48
|
+
sqrtRatioNextX96 = SqrtPriceMath.getNextSqrtPriceFromInput(
|
|
49
|
+
sqrtRatioCurrentX96,
|
|
50
|
+
liquidity,
|
|
51
|
+
amountRemainingLessFee,
|
|
52
|
+
zeroForOne,
|
|
53
|
+
);
|
|
54
|
+
} else {
|
|
55
|
+
amountOut = zeroForOne
|
|
56
|
+
? SqrtPriceMath.getAmount1Delta(
|
|
57
|
+
sqrtRatioTargetX96,
|
|
58
|
+
sqrtRatioCurrentX96,
|
|
59
|
+
liquidity,
|
|
60
|
+
false,
|
|
61
|
+
)
|
|
62
|
+
: SqrtPriceMath.getAmount0Delta(
|
|
63
|
+
sqrtRatioCurrentX96,
|
|
64
|
+
sqrtRatioTargetX96,
|
|
65
|
+
liquidity,
|
|
66
|
+
false,
|
|
67
|
+
);
|
|
68
|
+
if (BigInt.asUintN(256, -amountRemaining) >= amountOut)
|
|
69
|
+
sqrtRatioNextX96 = sqrtRatioTargetX96;
|
|
70
|
+
else
|
|
71
|
+
sqrtRatioNextX96 = SqrtPriceMath.getNextSqrtPriceFromOutput(
|
|
72
|
+
sqrtRatioCurrentX96,
|
|
73
|
+
liquidity,
|
|
74
|
+
BigInt.asUintN(256, -amountRemaining),
|
|
75
|
+
zeroForOne,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const max = sqrtRatioTargetX96 == sqrtRatioNextX96;
|
|
80
|
+
|
|
81
|
+
if (zeroForOne) {
|
|
82
|
+
amountIn =
|
|
83
|
+
max && exactIn
|
|
84
|
+
? amountIn
|
|
85
|
+
: SqrtPriceMath.getAmount0Delta(
|
|
86
|
+
sqrtRatioNextX96,
|
|
87
|
+
sqrtRatioCurrentX96,
|
|
88
|
+
liquidity,
|
|
89
|
+
true,
|
|
90
|
+
);
|
|
91
|
+
amountOut =
|
|
92
|
+
max && !exactIn
|
|
93
|
+
? amountOut
|
|
94
|
+
: SqrtPriceMath.getAmount1Delta(
|
|
95
|
+
sqrtRatioNextX96,
|
|
96
|
+
sqrtRatioCurrentX96,
|
|
97
|
+
liquidity,
|
|
98
|
+
false,
|
|
99
|
+
);
|
|
100
|
+
} else {
|
|
101
|
+
amountIn =
|
|
102
|
+
max && exactIn
|
|
103
|
+
? amountIn
|
|
104
|
+
: SqrtPriceMath.getAmount1Delta(
|
|
105
|
+
sqrtRatioCurrentX96,
|
|
106
|
+
sqrtRatioNextX96,
|
|
107
|
+
liquidity,
|
|
108
|
+
true,
|
|
109
|
+
);
|
|
110
|
+
amountOut =
|
|
111
|
+
max && !exactIn
|
|
112
|
+
? amountOut
|
|
113
|
+
: SqrtPriceMath.getAmount0Delta(
|
|
114
|
+
sqrtRatioCurrentX96,
|
|
115
|
+
sqrtRatioNextX96,
|
|
116
|
+
liquidity,
|
|
117
|
+
false,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// cap the output amount to not exceed the remaining output amount
|
|
122
|
+
if (!exactIn && amountOut > BigInt.asUintN(256, -amountRemaining)) {
|
|
123
|
+
amountOut = BigInt.asUintN(256, -amountRemaining);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (exactIn && sqrtRatioNextX96 != sqrtRatioTargetX96) {
|
|
127
|
+
// we didn't reach the target, so take the remainder of the maximum input as fee
|
|
128
|
+
feeAmount = BigInt.asUintN(256, amountRemaining) - amountIn;
|
|
129
|
+
} else {
|
|
130
|
+
feeAmount = FullMath.mulDivRoundingUp(
|
|
131
|
+
amountIn,
|
|
132
|
+
feePips,
|
|
133
|
+
BI_POWS[6] - feePips,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return { sqrtRatioNextX96, amountIn, amountOut, feeAmount };
|
|
138
|
+
}
|
|
139
|
+
}
|