@paraswap/dex-lib 2.42.3 → 2.42.4-baseswap
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/dex/balancer-v1/balancer-v1.js +21 -10
- package/build/dex/balancer-v1/balancer-v1.js.map +1 -1
- package/build/dex/balancer-v1/config.d.ts +2 -1
- package/build/dex/balancer-v1/config.js +3 -2
- package/build/dex/balancer-v1/config.js.map +1 -1
- package/build/dex/baseswap-v3/baseswap-v3-factory.d.ts +26 -0
- package/build/dex/baseswap-v3/baseswap-v3-factory.js +44 -0
- package/build/dex/baseswap-v3/baseswap-v3-factory.js.map +1 -0
- package/build/dex/baseswap-v3/baseswap-v3-pool.d.ts +46 -0
- package/build/dex/baseswap-v3/baseswap-v3-pool.js +312 -0
- package/build/dex/baseswap-v3/baseswap-v3-pool.js.map +1 -0
- package/build/dex/baseswap-v3/baseswap-v3.d.ts +71 -0
- package/build/dex/baseswap-v3/baseswap-v3.js +752 -0
- package/build/dex/baseswap-v3/baseswap-v3.js.map +1 -0
- package/build/dex/baseswap-v3/config.d.ts +9 -0
- package/build/dex/baseswap-v3/config.js +48 -0
- package/build/dex/baseswap-v3/config.js.map +1 -0
- package/build/dex/baseswap-v3/constants.d.ts +28 -0
- package/build/dex/baseswap-v3/constants.js +35 -0
- package/build/dex/baseswap-v3/constants.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/BitMath.d.ts +4 -0
- package/build/dex/baseswap-v3/contract-math/BitMath.js +93 -0
- package/build/dex/baseswap-v3/contract-math/BitMath.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/FixedPoint128.d.ts +3 -0
- package/build/dex/baseswap-v3/contract-math/FixedPoint128.js +8 -0
- package/build/dex/baseswap-v3/contract-math/FixedPoint128.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/FixedPoint96.d.ts +4 -0
- package/build/dex/baseswap-v3/contract-math/FixedPoint96.js +9 -0
- package/build/dex/baseswap-v3/contract-math/FixedPoint96.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/FullMath.d.ts +4 -0
- package/build/dex/baseswap-v3/contract-math/FullMath.js +19 -0
- package/build/dex/baseswap-v3/contract-math/FullMath.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/LiquidityMath.d.ts +3 -0
- package/build/dex/baseswap-v3/contract-math/LiquidityMath.js +22 -0
- package/build/dex/baseswap-v3/contract-math/LiquidityMath.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/Oracle.d.ts +10 -0
- package/build/dex/baseswap-v3/contract-math/Oracle.js +133 -0
- package/build/dex/baseswap-v3/contract-math/Oracle.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.d.ts +10 -0
- package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.js +91 -0
- package/build/dex/baseswap-v3/contract-math/SqrtPriceMath.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/SwapMath.d.ts +8 -0
- package/build/dex/baseswap-v3/contract-math/SwapMath.js +70 -0
- package/build/dex/baseswap-v3/contract-math/SwapMath.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/Tick.d.ts +7 -0
- package/build/dex/baseswap-v3/contract-math/Tick.js +45 -0
- package/build/dex/baseswap-v3/contract-math/Tick.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/TickBitMap.d.ts +7 -0
- package/build/dex/baseswap-v3/contract-math/TickBitMap.js +84 -0
- package/build/dex/baseswap-v3/contract-math/TickBitMap.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/TickMath.d.ts +8 -0
- package/build/dex/baseswap-v3/contract-math/TickMath.js +162 -0
- package/build/dex/baseswap-v3/contract-math/TickMath.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/UnsafeMath.d.ts +3 -0
- package/build/dex/baseswap-v3/contract-math/UnsafeMath.js +10 -0
- package/build/dex/baseswap-v3/contract-math/UnsafeMath.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.d.ts +37 -0
- package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.js +393 -0
- package/build/dex/baseswap-v3/contract-math/uniswap-v3-math.js.map +1 -0
- package/build/dex/baseswap-v3/contract-math/utils.d.ts +7 -0
- package/build/dex/baseswap-v3/contract-math/utils.js +42 -0
- package/build/dex/baseswap-v3/contract-math/utils.js.map +1 -0
- package/build/dex/baseswap-v3/types.d.ts +164 -0
- package/build/dex/baseswap-v3/types.js +9 -0
- package/build/dex/baseswap-v3/types.js.map +1 -0
- package/build/dex/baseswap-v3/utils.d.ts +6 -0
- package/build/dex/baseswap-v3/utils.js +65 -0
- package/build/dex/baseswap-v3/utils.js.map +1 -0
- package/build/dex/index.js +2 -0
- package/build/dex/index.js.map +1 -1
- package/package.json +1 -1
- package/src/dex/balancer-v1/balancer-v1.ts +32 -13
- package/src/dex/balancer-v1/config.ts +2 -1
- package/src/dex/baseswap-v3/baseswap-v3-e2e.test.ts +129 -0
- package/src/dex/baseswap-v3/baseswap-v3-events.test.ts +141 -0
- package/src/dex/baseswap-v3/baseswap-v3-factory.ts +73 -0
- package/src/dex/baseswap-v3/baseswap-v3-integration.test.ts +472 -0
- package/src/dex/baseswap-v3/baseswap-v3-pool.ts +519 -0
- package/src/dex/baseswap-v3/baseswap-v3.ts +1243 -0
- package/src/dex/baseswap-v3/config.ts +53 -0
- package/src/dex/baseswap-v3/constants.ts +41 -0
- package/src/dex/baseswap-v3/contract-math/BitMath.ts +90 -0
- package/src/dex/baseswap-v3/contract-math/FixedPoint128.ts +3 -0
- package/src/dex/baseswap-v3/contract-math/FixedPoint96.ts +4 -0
- package/src/dex/baseswap-v3/contract-math/FullMath.ts +30 -0
- package/src/dex/baseswap-v3/contract-math/LiquidityMath.ts +17 -0
- package/src/dex/baseswap-v3/contract-math/Oracle.ts +226 -0
- package/src/dex/baseswap-v3/contract-math/SqrtPriceMath.ts +226 -0
- package/src/dex/baseswap-v3/contract-math/SwapMath.ts +139 -0
- package/src/dex/baseswap-v3/contract-math/Tick.ts +82 -0
- package/src/dex/baseswap-v3/contract-math/TickBitMap.ts +123 -0
- package/src/dex/baseswap-v3/contract-math/TickMath.ts +211 -0
- package/src/dex/baseswap-v3/contract-math/UnsafeMath.ts +5 -0
- package/src/dex/baseswap-v3/contract-math/uniswap-v3-math.ts +666 -0
- package/src/dex/baseswap-v3/contract-math/utils.ts +55 -0
- package/src/dex/baseswap-v3/types.ts +191 -0
- package/src/dex/baseswap-v3/utils.ts +83 -0
- package/src/dex/index.ts +2 -0
- package/tests/constants-e2e.ts +1 -1
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { DexParams } from './types';
|
|
2
|
+
import { DexConfigMap, AdapterMappings } from '../../types';
|
|
3
|
+
import { Network, SwapSide } from '../../constants';
|
|
4
|
+
import { Address } from '../../types';
|
|
5
|
+
// import RamsesV2StateMulticallABI from '../../abi/RamsesV2StateMulticall.abi.json';
|
|
6
|
+
// import { AbiItem } from 'web3-utils';
|
|
7
|
+
// import { decodeStateMultiCallResultWithRelativeBitmaps } from './forks/ramses-v2/utils';
|
|
8
|
+
// import { RamsesV2EventPool } from './forks/ramses-v2/ramses-v2-pool';
|
|
9
|
+
|
|
10
|
+
const SUPPORTED_FEES = [10000n, 2500n, 450n, 80n];
|
|
11
|
+
|
|
12
|
+
// Pools that will be initialized on app startup
|
|
13
|
+
// They are added for testing
|
|
14
|
+
export const PoolsToPreload: DexConfigMap<
|
|
15
|
+
{ token0: Address; token1: Address }[]
|
|
16
|
+
> = {
|
|
17
|
+
BaseswapV3: {
|
|
18
|
+
[Network.BASE]: [
|
|
19
|
+
{
|
|
20
|
+
token0: '0x4200000000000000000000000000000000000006'.toLowerCase(),
|
|
21
|
+
token1: '0xd9aAEc86B65D86f6A7B5B1b0c42FFA531710b6CA'.toLowerCase(), // USDbC
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
token0: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'.toLowerCase(), // USDC
|
|
25
|
+
token1: '0xd9aAEc86B65D86f6A7B5B1b0c42FFA531710b6CA'.toLowerCase(), // USDbC
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const BaseswapV3Config: DexConfigMap<DexParams> = {
|
|
32
|
+
BaseswapV3: {
|
|
33
|
+
[Network.BASE]: {
|
|
34
|
+
factory: '0x38015D05f4fEC8AFe15D7cc0386a126574e8077B',
|
|
35
|
+
quoter: '0x4fDBD73aD4B1DDde594BF05497C15f76308eFfb9',
|
|
36
|
+
router: '0x1B8eea9315bE495187D873DA7773a874545D9D48',
|
|
37
|
+
supportedFees: SUPPORTED_FEES,
|
|
38
|
+
stateMulticall: '0x7160f736c52e1e78e92FD4eE4D73e21A7Cf4F950',
|
|
39
|
+
uniswapMulticall: '0x091e99cb1C49331a94dD62755D168E941AbD0693',
|
|
40
|
+
chunksCount: 10,
|
|
41
|
+
initRetryFrequency: 10,
|
|
42
|
+
initHash: `0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54`,
|
|
43
|
+
subgraphURL: 'https://api.thegraph.com/subgraphs/name/baseswapfi/v3-base',
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const Adapters: Record<number, AdapterMappings> = {
|
|
49
|
+
[Network.BASE]: {
|
|
50
|
+
[SwapSide.SELL]: [{ name: 'BaseAdapter01', index: 1 }],
|
|
51
|
+
[SwapSide.BUY]: [{ name: 'BaseBuyAdapter', index: 1 }],
|
|
52
|
+
},
|
|
53
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export const UNISWAPV3_TICK_GAS_COST = 24_000; // Ceiled
|
|
2
|
+
export const UNISWAPV3_TICK_BASE_OVERHEAD = 75_000;
|
|
3
|
+
export const UNISWAPV3_POOL_SEARCH_OVERHEAD = 10_000;
|
|
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/baseswapfi/v3-base';
|
|
15
|
+
|
|
16
|
+
export const UNISWAPV3_EFFICIENCY_FACTOR = 3;
|
|
17
|
+
|
|
18
|
+
export const ZERO_TICK_INFO = {
|
|
19
|
+
liquidityGross: 0n,
|
|
20
|
+
liquidityNet: 0n,
|
|
21
|
+
tickCumulativeOutside: 0n,
|
|
22
|
+
secondsPerLiquidityOutsideX128: 0n,
|
|
23
|
+
secondsOutside: 0n,
|
|
24
|
+
initialized: false,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const ZERO_ORACLE_OBSERVATION = {
|
|
28
|
+
blockTimestamp: 0n,
|
|
29
|
+
tickCumulative: 0n,
|
|
30
|
+
secondsPerLiquidityCumulativeX128: 0n,
|
|
31
|
+
initialized: false,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const OUT_OF_RANGE_ERROR_POSTFIX = `INVALID_TICK_BIT_MAP_RANGES`;
|
|
35
|
+
|
|
36
|
+
export const DEFAULT_POOL_INIT_CODE_HASH = `0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54`;
|
|
37
|
+
|
|
38
|
+
export enum DirectMethods {
|
|
39
|
+
directSell = 'directUniV3Swap',
|
|
40
|
+
directBuy = 'directUniV3Buy',
|
|
41
|
+
}
|
|
@@ -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 {
|
|
2
|
+
OracleObservation,
|
|
3
|
+
OracleObservationCandidates,
|
|
4
|
+
PoolState,
|
|
5
|
+
} from '../types';
|
|
6
|
+
import { _require } from '../../../utils';
|
|
7
|
+
import { DeepReadonly } from 'ts-essentials';
|
|
8
|
+
import { ZERO_ORACLE_OBSERVATION } from '../constants';
|
|
9
|
+
|
|
10
|
+
function replaceUndefinedObservationWithZero(state: PoolState, index: number) {
|
|
11
|
+
if (state.observations[index] === undefined) {
|
|
12
|
+
state.observations[index] = { ...ZERO_ORACLE_OBSERVATION };
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class Oracle {
|
|
17
|
+
static transform(
|
|
18
|
+
state: DeepReadonly<PoolState>,
|
|
19
|
+
last: OracleObservation,
|
|
20
|
+
blockTimestamp: bigint,
|
|
21
|
+
tick: bigint,
|
|
22
|
+
liquidity: bigint,
|
|
23
|
+
): OracleObservation {
|
|
24
|
+
const delta = blockTimestamp - last.blockTimestamp;
|
|
25
|
+
return {
|
|
26
|
+
blockTimestamp: state.blockTimestamp,
|
|
27
|
+
tickCumulative: last.tickCumulative + BigInt.asIntN(56, tick) * delta,
|
|
28
|
+
secondsPerLiquidityCumulativeX128:
|
|
29
|
+
last.secondsPerLiquidityCumulativeX128 +
|
|
30
|
+
(BigInt.asUintN(160, delta) << 128n) /
|
|
31
|
+
(liquidity > 0n ? liquidity : 1n),
|
|
32
|
+
initialized: true,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static write(
|
|
37
|
+
state: PoolState,
|
|
38
|
+
index: number,
|
|
39
|
+
blockTimestamp: bigint,
|
|
40
|
+
tick: bigint,
|
|
41
|
+
liquidity: bigint,
|
|
42
|
+
cardinality: number,
|
|
43
|
+
cardinalityNext: number,
|
|
44
|
+
): [number, number] {
|
|
45
|
+
const last = state.observations[index];
|
|
46
|
+
|
|
47
|
+
if (last.blockTimestamp == state.blockTimestamp)
|
|
48
|
+
return [index, cardinality];
|
|
49
|
+
|
|
50
|
+
let indexUpdated = 0;
|
|
51
|
+
let cardinalityUpdated = 0;
|
|
52
|
+
|
|
53
|
+
if (cardinalityNext > cardinality && index == cardinality - 1) {
|
|
54
|
+
cardinalityUpdated = cardinalityNext;
|
|
55
|
+
} else {
|
|
56
|
+
cardinalityUpdated = cardinality;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
indexUpdated = (index + 1) % cardinalityUpdated;
|
|
60
|
+
|
|
61
|
+
state.observations[indexUpdated] = Oracle.transform(
|
|
62
|
+
state,
|
|
63
|
+
last,
|
|
64
|
+
blockTimestamp,
|
|
65
|
+
tick,
|
|
66
|
+
liquidity,
|
|
67
|
+
);
|
|
68
|
+
if (indexUpdated !== index) {
|
|
69
|
+
delete state.observations[index];
|
|
70
|
+
}
|
|
71
|
+
return [indexUpdated, cardinalityUpdated];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static lte(time: bigint, a: bigint, b: bigint): boolean {
|
|
75
|
+
if (a <= time && b <= time) return a <= b;
|
|
76
|
+
|
|
77
|
+
const aAdjusted = a > time ? a : a + 2n ** 32n;
|
|
78
|
+
const bAdjusted = b > time ? b : b + 2n ** 32n;
|
|
79
|
+
return aAdjusted <= bAdjusted;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static binarySearch(
|
|
83
|
+
state: DeepReadonly<PoolState>,
|
|
84
|
+
time: bigint,
|
|
85
|
+
target: bigint,
|
|
86
|
+
index: number,
|
|
87
|
+
cardinality: number,
|
|
88
|
+
): OracleObservationCandidates {
|
|
89
|
+
let l = (index + 1) % cardinality;
|
|
90
|
+
let r = l + cardinality - 1;
|
|
91
|
+
let i;
|
|
92
|
+
|
|
93
|
+
let beforeOrAt;
|
|
94
|
+
let atOrAfter;
|
|
95
|
+
while (true) {
|
|
96
|
+
i = (l + r) / 2;
|
|
97
|
+
|
|
98
|
+
beforeOrAt = state.observations[i % cardinality];
|
|
99
|
+
|
|
100
|
+
// we've landed on an uninitialized tick, keep searching higher (more recently)
|
|
101
|
+
if (!beforeOrAt.initialized) {
|
|
102
|
+
l = i + 1;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
atOrAfter = state.observations[(i + 1) % cardinality];
|
|
107
|
+
|
|
108
|
+
const targetAtOrAfter = Oracle.lte(
|
|
109
|
+
time,
|
|
110
|
+
beforeOrAt.blockTimestamp,
|
|
111
|
+
target,
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
// check if we've found the answer!
|
|
115
|
+
if (targetAtOrAfter && Oracle.lte(time, target, atOrAfter.blockTimestamp))
|
|
116
|
+
break;
|
|
117
|
+
|
|
118
|
+
if (!targetAtOrAfter) r = i - 1;
|
|
119
|
+
else l = i + 1;
|
|
120
|
+
}
|
|
121
|
+
return { beforeOrAt, atOrAfter };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static getSurroundingObservations(
|
|
125
|
+
state: DeepReadonly<PoolState>,
|
|
126
|
+
time: bigint,
|
|
127
|
+
target: bigint,
|
|
128
|
+
tick: bigint,
|
|
129
|
+
index: number,
|
|
130
|
+
liquidity: bigint,
|
|
131
|
+
cardinality: number,
|
|
132
|
+
): OracleObservationCandidates {
|
|
133
|
+
let beforeOrAt = state.observations[index];
|
|
134
|
+
|
|
135
|
+
if (Oracle.lte(time, beforeOrAt.blockTimestamp, target)) {
|
|
136
|
+
if (beforeOrAt.blockTimestamp === target) {
|
|
137
|
+
return { beforeOrAt, atOrAfter: beforeOrAt };
|
|
138
|
+
} else {
|
|
139
|
+
return {
|
|
140
|
+
beforeOrAt,
|
|
141
|
+
atOrAfter: Oracle.transform(
|
|
142
|
+
state,
|
|
143
|
+
beforeOrAt,
|
|
144
|
+
target,
|
|
145
|
+
tick,
|
|
146
|
+
liquidity,
|
|
147
|
+
),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
beforeOrAt = state.observations[(index + 1) % cardinality];
|
|
153
|
+
if (!beforeOrAt.initialized) beforeOrAt = state.observations[0];
|
|
154
|
+
|
|
155
|
+
_require(
|
|
156
|
+
Oracle.lte(time, beforeOrAt.blockTimestamp, target),
|
|
157
|
+
'OLD',
|
|
158
|
+
{ time, beforeOrAtBlockTimestamp: beforeOrAt.blockTimestamp, target },
|
|
159
|
+
'Oracle.lte(time, beforeOrAt.blockTimestamp, target)',
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
return Oracle.binarySearch(state, time, target, index, cardinality);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static observeSingle(
|
|
166
|
+
state: DeepReadonly<PoolState>,
|
|
167
|
+
time: bigint,
|
|
168
|
+
secondsAgo: bigint,
|
|
169
|
+
tick: bigint,
|
|
170
|
+
index: number,
|
|
171
|
+
liquidity: bigint,
|
|
172
|
+
cardinality: number,
|
|
173
|
+
): [bigint, bigint] {
|
|
174
|
+
if (secondsAgo == 0n) {
|
|
175
|
+
let last = state.observations[index];
|
|
176
|
+
if (last.blockTimestamp != time)
|
|
177
|
+
last = Oracle.transform(state, last, time, tick, liquidity);
|
|
178
|
+
return [last.tickCumulative, last.secondsPerLiquidityCumulativeX128];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const target = time - secondsAgo;
|
|
182
|
+
|
|
183
|
+
const { beforeOrAt, atOrAfter } = Oracle.getSurroundingObservations(
|
|
184
|
+
state,
|
|
185
|
+
time,
|
|
186
|
+
target,
|
|
187
|
+
tick,
|
|
188
|
+
index,
|
|
189
|
+
liquidity,
|
|
190
|
+
cardinality,
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
if (target === beforeOrAt.blockTimestamp) {
|
|
194
|
+
return [
|
|
195
|
+
beforeOrAt.tickCumulative,
|
|
196
|
+
beforeOrAt.secondsPerLiquidityCumulativeX128,
|
|
197
|
+
];
|
|
198
|
+
} else if (target === atOrAfter.blockTimestamp) {
|
|
199
|
+
return [
|
|
200
|
+
atOrAfter.tickCumulative,
|
|
201
|
+
atOrAfter.secondsPerLiquidityCumulativeX128,
|
|
202
|
+
];
|
|
203
|
+
} else {
|
|
204
|
+
const observationTimeDelta =
|
|
205
|
+
atOrAfter.blockTimestamp - beforeOrAt.blockTimestamp;
|
|
206
|
+
const targetDelta = target - beforeOrAt.blockTimestamp;
|
|
207
|
+
return [
|
|
208
|
+
beforeOrAt.tickCumulative +
|
|
209
|
+
((atOrAfter.tickCumulative - beforeOrAt.tickCumulative) /
|
|
210
|
+
observationTimeDelta) *
|
|
211
|
+
targetDelta,
|
|
212
|
+
beforeOrAt.secondsPerLiquidityCumulativeX128 +
|
|
213
|
+
BigInt.asUintN(
|
|
214
|
+
160,
|
|
215
|
+
(BigInt.asUintN(
|
|
216
|
+
256,
|
|
217
|
+
atOrAfter.secondsPerLiquidityCumulativeX128 -
|
|
218
|
+
beforeOrAt.secondsPerLiquidityCumulativeX128,
|
|
219
|
+
) *
|
|
220
|
+
targetDelta) /
|
|
221
|
+
observationTimeDelta,
|
|
222
|
+
),
|
|
223
|
+
];
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -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
|
+
}
|