Skip to content

Commit ece590b

Browse files
authored
twap tick rounding (#257)
1 parent 8968606 commit ece590b

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

spot-vaults/contracts/_utils/UniswapV3PoolHelpers.sol

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ library UniswapV3PoolHelpers {
3434
/// @notice Retrieves the Time-Weighted Average Price (TWAP) tick from a Uniswap V3 pool over a given duration.
3535
/// @param pool The Uniswap V3 pool.
3636
/// @param twapDuration The TWAP duration.
37-
/// @return The TWAP tick.
37+
/// @return twapTick The TWAP tick.
3838
function getTwapTick(
3939
IUniswapV3Pool pool,
4040
uint32 twapDuration
41-
) internal view returns (int24) {
41+
) internal view returns (int24 twapTick) {
4242
uint32[] memory secondsAgo = new uint32[](2);
4343
secondsAgo[0] = twapDuration;
4444
secondsAgo[1] = 0;
4545
(int56[] memory tickCumulatives, ) = pool.observe(secondsAgo);
46-
return int24((tickCumulatives[1] - tickCumulatives[0]) / twapDuration);
46+
int56 tickCumulativesDelta = tickCumulatives[1] - tickCumulatives[0];
47+
twapTick = int24(tickCumulativesDelta / twapDuration);
48+
if (tickCumulativesDelta < 0 && (tickCumulativesDelta % twapDuration != 0))
49+
twapTick--;
4750
}
4851
}

0 commit comments

Comments
 (0)