-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
TickLens.sol
42 lines (37 loc) · 1.49 KB
/
TickLens.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
pragma abicoder v2;
import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol';
import '../interfaces/ITickLens.sol';
/// @title Tick Lens contract
contract TickLens is ITickLens {
/// @inheritdoc ITickLens
function getPopulatedTicksInWord(address pool, int16 tickBitmapIndex)
public
view
override
returns (PopulatedTick[] memory populatedTicks)
{
// fetch bitmap
uint256 bitmap = IUniswapV3Pool(pool).tickBitmap(tickBitmapIndex);
// calculate the number of populated ticks
uint256 numberOfPopulatedTicks;
for (uint256 i = 0; i < 256; i++) {
if (bitmap & (1 << i) > 0) numberOfPopulatedTicks++;
}
// fetch populated tick data
int24 tickSpacing = IUniswapV3Pool(pool).tickSpacing();
populatedTicks = new PopulatedTick[](numberOfPopulatedTicks);
for (uint256 i = 0; i < 256; i++) {
if (bitmap & (1 << i) > 0) {
int24 populatedTick = ((int24(tickBitmapIndex) << 8) + int24(i)) * tickSpacing;
(uint128 liquidityGross, int128 liquidityNet, , , , , , ) = IUniswapV3Pool(pool).ticks(populatedTick);
populatedTicks[--numberOfPopulatedTicks] = PopulatedTick({
tick: populatedTick,
liquidityNet: liquidityNet,
liquidityGross: liquidityGross
});
}
}
}
}