From c5ccf4d28a73fde90f0bb9ea3fd299d7d2bcdf83 Mon Sep 17 00:00:00 2001 From: Emily Williams Date: Thu, 16 Mar 2023 15:21:38 -0500 Subject: [PATCH] Fix code style issues with Prettier (#629) Co-authored-by: Lint Action --- .../crytic/echidna/E2E_mint_burn.sol | 17 +- .../tob/contracts/crytic/echidna/E2E_swap.sol | 28 +-- contracts/UniswapV3Pool.sol | 163 +++++++++--------- contracts/libraries/Oracle.sol | 11 +- contracts/libraries/Position.sol | 22 ++- contracts/libraries/SqrtPriceMath.sol | 22 +-- contracts/libraries/TransferHelper.sol | 5 +- contracts/test/OracleEchidnaTest.sol | 25 ++- contracts/test/SqrtPriceMathEchidnaTest.sol | 7 +- contracts/test/SwapMathEchidnaTest.sol | 9 +- contracts/test/TestUniswapV3Router.sol | 5 +- .../test/TickOverflowSafetyEchidnaTest.sol | 50 +++--- test/Oracle.spec.ts | 8 +- test/TickMath.spec.ts | 15 +- test/UniswapV3Pool.arbitrage.spec.ts | 37 ++-- test/UniswapV3Pool.spec.ts | 98 ++++++++--- test/UniswapV3Pool.swaps.spec.ts | 10 +- test/shared/fixtures.ts | 3 +- 18 files changed, 296 insertions(+), 239 deletions(-) diff --git a/audits/tob/contracts/crytic/echidna/E2E_mint_burn.sol b/audits/tob/contracts/crytic/echidna/E2E_mint_burn.sol index e33d6a221..4cd3b0321 100644 --- a/audits/tob/contracts/crytic/echidna/E2E_mint_burn.sol +++ b/audits/tob/contracts/crytic/echidna/E2E_mint_burn.sol @@ -382,18 +382,11 @@ contract E2E_mint_burn { function test_mint(uint128 _amount) public { if (!inited) _init(_amount); - (int24 _tL, int24 _tU) = forgePosition( - _amount, - poolParams.tickSpacing, - poolParams.tickCount, - poolParams.maxTick - ); - - (UniswapMinter.MinterStats memory bfre, UniswapMinter.MinterStats memory aftr) = minter.doMint( - _tL, - _tU, - _amount - ); + (int24 _tL, int24 _tU) = + forgePosition(_amount, poolParams.tickSpacing, poolParams.tickCount, poolParams.maxTick); + + (UniswapMinter.MinterStats memory bfre, UniswapMinter.MinterStats memory aftr) = + minter.doMint(_tL, _tU, _amount); storeUsedTicks(_tL, _tU); check_mint_invariants(_tL, _tU, bfre, aftr); diff --git a/audits/tob/contracts/crytic/echidna/E2E_swap.sol b/audits/tob/contracts/crytic/echidna/E2E_swap.sol index 863c5ddf1..289ccb977 100644 --- a/audits/tob/contracts/crytic/echidna/E2E_swap.sol +++ b/audits/tob/contracts/crytic/echidna/E2E_swap.sol @@ -360,11 +360,8 @@ contract E2E_swap { uint160 sqrtPriceLimitX96 = get_random_zeroForOne_priceLimit(_amount); // console.log('sqrtPriceLimitX96 = %s', sqrtPriceLimitX96); - (UniswapSwapper.SwapperStats memory bfre, UniswapSwapper.SwapperStats memory aftr) = swapper.doSwap( - true, - _amountSpecified, - sqrtPriceLimitX96 - ); + (UniswapSwapper.SwapperStats memory bfre, UniswapSwapper.SwapperStats memory aftr) = + swapper.doSwap(true, _amountSpecified, sqrtPriceLimitX96); check_swap_invariants( bfre.tick, @@ -397,11 +394,8 @@ contract E2E_swap { uint160 sqrtPriceLimitX96 = get_random_oneForZero_priceLimit(_amount); // console.log('sqrtPriceLimitX96 = %s', sqrtPriceLimitX96); - (UniswapSwapper.SwapperStats memory bfre, UniswapSwapper.SwapperStats memory aftr) = swapper.doSwap( - false, - _amountSpecified, - sqrtPriceLimitX96 - ); + (UniswapSwapper.SwapperStats memory bfre, UniswapSwapper.SwapperStats memory aftr) = + swapper.doSwap(false, _amountSpecified, sqrtPriceLimitX96); check_swap_invariants( bfre.tick, @@ -434,11 +428,8 @@ contract E2E_swap { uint160 sqrtPriceLimitX96 = get_random_zeroForOne_priceLimit(_amount); // console.log('sqrtPriceLimitX96 = %s', sqrtPriceLimitX96); - (UniswapSwapper.SwapperStats memory bfre, UniswapSwapper.SwapperStats memory aftr) = swapper.doSwap( - true, - _amountSpecified, - sqrtPriceLimitX96 - ); + (UniswapSwapper.SwapperStats memory bfre, UniswapSwapper.SwapperStats memory aftr) = + swapper.doSwap(true, _amountSpecified, sqrtPriceLimitX96); check_swap_invariants( bfre.tick, @@ -471,11 +462,8 @@ contract E2E_swap { uint160 sqrtPriceLimitX96 = get_random_oneForZero_priceLimit(_amount); // console.log('sqrtPriceLimitX96 = %s', sqrtPriceLimitX96); - (UniswapSwapper.SwapperStats memory bfre, UniswapSwapper.SwapperStats memory aftr) = swapper.doSwap( - false, - _amountSpecified, - sqrtPriceLimitX96 - ); + (UniswapSwapper.SwapperStats memory bfre, UniswapSwapper.SwapperStats memory aftr) = + swapper.doSwap(false, _amountSpecified, sqrtPriceLimitX96); check_swap_invariants( bfre.tick, diff --git a/contracts/UniswapV3Pool.sol b/contracts/UniswapV3Pool.sol index ca7c59f82..9e0982127 100644 --- a/contracts/UniswapV3Pool.sol +++ b/contracts/UniswapV3Pool.sol @@ -138,9 +138,8 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { /// @dev This function is gas optimized to avoid a redundant extcodesize check in addition to the returndatasize /// check function balance0() private view returns (uint256) { - (bool success, bytes memory data) = token0.staticcall( - abi.encodeWithSelector(IERC20Minimal.balanceOf.selector, address(this)) - ); + (bool success, bytes memory data) = + token0.staticcall(abi.encodeWithSelector(IERC20Minimal.balanceOf.selector, address(this))); require(success && data.length >= 32); return abi.decode(data, (uint256)); } @@ -149,9 +148,8 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { /// @dev This function is gas optimized to avoid a redundant extcodesize check in addition to the returndatasize /// check function balance1() private view returns (uint256) { - (bool success, bytes memory data) = token1.staticcall( - abi.encodeWithSelector(IERC20Minimal.balanceOf.selector, address(this)) - ); + (bool success, bytes memory data) = + token1.staticcall(abi.encodeWithSelector(IERC20Minimal.balanceOf.selector, address(this))); require(success && data.length >= 32); return abi.decode(data, (uint256)); } @@ -209,14 +207,15 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { ); } else if (_slot0.tick < tickUpper) { uint32 time = _blockTimestamp(); - (int56 tickCumulative, uint160 secondsPerLiquidityCumulativeX128) = observations.observeSingle( - time, - 0, - _slot0.tick, - _slot0.observationIndex, - liquidity, - _slot0.observationCardinality - ); + (int56 tickCumulative, uint160 secondsPerLiquidityCumulativeX128) = + observations.observeSingle( + time, + 0, + _slot0.tick, + _slot0.observationIndex, + liquidity, + _slot0.observationCardinality + ); return ( tickCumulative - tickCumulativeLower - tickCumulativeUpper, secondsPerLiquidityCumulativeX128 - @@ -260,10 +259,8 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { noDelegateCall { uint16 observationCardinalityNextOld = slot0.observationCardinalityNext; // for the event - uint16 observationCardinalityNextNew = observations.grow( - observationCardinalityNextOld, - observationCardinalityNext - ); + uint16 observationCardinalityNextNew = + observations.grow(observationCardinalityNextOld, observationCardinalityNext); slot0.observationCardinalityNext = observationCardinalityNextNew; if (observationCardinalityNextOld != observationCardinalityNextNew) emit IncreaseObservationCardinalityNext(observationCardinalityNextOld, observationCardinalityNextNew); @@ -396,14 +393,15 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { bool flippedUpper; if (liquidityDelta != 0) { uint32 time = _blockTimestamp(); - (int56 tickCumulative, uint160 secondsPerLiquidityCumulativeX128) = observations.observeSingle( - time, - 0, - slot0.tick, - slot0.observationIndex, - liquidity, - slot0.observationCardinality - ); + (int56 tickCumulative, uint160 secondsPerLiquidityCumulativeX128) = + observations.observeSingle( + time, + 0, + slot0.tick, + slot0.observationIndex, + liquidity, + slot0.observationCardinality + ); flippedLower = ticks.update( tickLower, @@ -438,13 +436,8 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { } } - (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) = ticks.getFeeGrowthInside( - tickLower, - tickUpper, - tick, - _feeGrowthGlobal0X128, - _feeGrowthGlobal1X128 - ); + (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) = + ticks.getFeeGrowthInside(tickLower, tickUpper, tick, _feeGrowthGlobal0X128, _feeGrowthGlobal1X128); position.update(liquidityDelta, feeGrowthInside0X128, feeGrowthInside1X128); @@ -469,14 +462,15 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { bytes calldata data ) external override lock returns (uint256 amount0, uint256 amount1) { require(amount > 0); - (, int256 amount0Int, int256 amount1Int) = _modifyPosition( - ModifyPositionParams({ - owner: recipient, - tickLower: tickLower, - tickUpper: tickUpper, - liquidityDelta: int256(amount).toInt128() - }) - ); + (, int256 amount0Int, int256 amount1Int) = + _modifyPosition( + ModifyPositionParams({ + owner: recipient, + tickLower: tickLower, + tickUpper: tickUpper, + liquidityDelta: int256(amount).toInt128() + }) + ); amount0 = uint256(amount0Int); amount1 = uint256(amount1Int); @@ -525,14 +519,15 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { int24 tickUpper, uint128 amount ) external override lock returns (uint256 amount0, uint256 amount1) { - (Position.Info storage position, int256 amount0Int, int256 amount1Int) = _modifyPosition( - ModifyPositionParams({ - owner: msg.sender, - tickLower: tickLower, - tickUpper: tickUpper, - liquidityDelta: -int256(amount).toInt128() - }) - ); + (Position.Info storage position, int256 amount0Int, int256 amount1Int) = + _modifyPosition( + ModifyPositionParams({ + owner: msg.sender, + tickLower: tickLower, + tickUpper: tickUpper, + liquidityDelta: -int256(amount).toInt128() + }) + ); amount0 = uint256(-amount0Int); amount1 = uint256(-amount1Int); @@ -619,26 +614,28 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { slot0.unlocked = false; - SwapCache memory cache = SwapCache({ - liquidityStart: liquidity, - blockTimestamp: _blockTimestamp(), - feeProtocol: zeroForOne ? (slot0Start.feeProtocol % 16) : (slot0Start.feeProtocol >> 4), - secondsPerLiquidityCumulativeX128: 0, - tickCumulative: 0, - computedLatestObservation: false - }); + SwapCache memory cache = + SwapCache({ + liquidityStart: liquidity, + blockTimestamp: _blockTimestamp(), + feeProtocol: zeroForOne ? (slot0Start.feeProtocol % 16) : (slot0Start.feeProtocol >> 4), + secondsPerLiquidityCumulativeX128: 0, + tickCumulative: 0, + computedLatestObservation: false + }); bool exactInput = amountSpecified > 0; - SwapState memory state = SwapState({ - amountSpecifiedRemaining: amountSpecified, - amountCalculated: 0, - sqrtPriceX96: slot0Start.sqrtPriceX96, - tick: slot0Start.tick, - feeGrowthGlobalX128: zeroForOne ? feeGrowthGlobal0X128 : feeGrowthGlobal1X128, - protocolFee: 0, - liquidity: cache.liquidityStart - }); + SwapState memory state = + SwapState({ + amountSpecifiedRemaining: amountSpecified, + amountCalculated: 0, + sqrtPriceX96: slot0Start.sqrtPriceX96, + tick: slot0Start.tick, + feeGrowthGlobalX128: zeroForOne ? feeGrowthGlobal0X128 : feeGrowthGlobal1X128, + protocolFee: 0, + liquidity: cache.liquidityStart + }); // continue swapping as long as we haven't used the entire input/output and haven't reached the price limit while (state.amountSpecifiedRemaining != 0 && state.sqrtPriceX96 != sqrtPriceLimitX96) { @@ -709,14 +706,15 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { ); cache.computedLatestObservation = true; } - int128 liquidityNet = ticks.cross( - step.tickNext, - (zeroForOne ? state.feeGrowthGlobalX128 : feeGrowthGlobal0X128), - (zeroForOne ? feeGrowthGlobal1X128 : state.feeGrowthGlobalX128), - cache.secondsPerLiquidityCumulativeX128, - cache.tickCumulative, - cache.blockTimestamp - ); + int128 liquidityNet = + ticks.cross( + step.tickNext, + (zeroForOne ? state.feeGrowthGlobalX128 : feeGrowthGlobal0X128), + (zeroForOne ? feeGrowthGlobal1X128 : state.feeGrowthGlobalX128), + cache.secondsPerLiquidityCumulativeX128, + cache.tickCumulative, + cache.blockTimestamp + ); // if we're moving leftward, we interpret liquidityNet as the opposite sign // safe because liquidityNet cannot be type(int128).min if (zeroForOne) liquidityNet = -liquidityNet; @@ -733,14 +731,15 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall { // update tick and write an oracle entry if the tick change if (state.tick != slot0Start.tick) { - (uint16 observationIndex, uint16 observationCardinality) = observations.write( - slot0Start.observationIndex, - cache.blockTimestamp, - slot0Start.tick, - cache.liquidityStart, - slot0Start.observationCardinality, - slot0Start.observationCardinalityNext - ); + (uint16 observationIndex, uint16 observationCardinality) = + observations.write( + slot0Start.observationIndex, + cache.blockTimestamp, + slot0Start.tick, + cache.liquidityStart, + slot0Start.observationCardinality, + slot0Start.observationCardinalityNext + ); (slot0.sqrtPriceX96, slot0.tick, slot0.observationIndex, slot0.observationCardinality) = ( state.sqrtPriceX96, state.tick, diff --git a/contracts/libraries/Oracle.sol b/contracts/libraries/Oracle.sol index 05671650e..d386328ef 100644 --- a/contracts/libraries/Oracle.sol +++ b/contracts/libraries/Oracle.sol @@ -259,15 +259,8 @@ library Oracle { uint32 target = time - secondsAgo; - (Observation memory beforeOrAt, Observation memory atOrAfter) = getSurroundingObservations( - self, - time, - target, - tick, - index, - liquidity, - cardinality - ); + (Observation memory beforeOrAt, Observation memory atOrAfter) = + getSurroundingObservations(self, time, target, tick, index, liquidity, cardinality); if (target == beforeOrAt.blockTimestamp) { // we're at the left boundary diff --git a/contracts/libraries/Position.sol b/contracts/libraries/Position.sol index f75a40bc8..41829b003 100644 --- a/contracts/libraries/Position.sol +++ b/contracts/libraries/Position.sol @@ -58,12 +58,22 @@ library Position { } // calculate accumulated fees - uint128 tokensOwed0 = uint128( - FullMath.mulDiv(feeGrowthInside0X128 - _self.feeGrowthInside0LastX128, _self.liquidity, FixedPoint128.Q128) - ); - uint128 tokensOwed1 = uint128( - FullMath.mulDiv(feeGrowthInside1X128 - _self.feeGrowthInside1LastX128, _self.liquidity, FixedPoint128.Q128) - ); + uint128 tokensOwed0 = + uint128( + FullMath.mulDiv( + feeGrowthInside0X128 - _self.feeGrowthInside0LastX128, + _self.liquidity, + FixedPoint128.Q128 + ) + ); + uint128 tokensOwed1 = + uint128( + FullMath.mulDiv( + feeGrowthInside1X128 - _self.feeGrowthInside1LastX128, + _self.liquidity, + FixedPoint128.Q128 + ) + ); // update the position if (liquidityDelta != 0) self.liquidity = liquidityNext; diff --git a/contracts/libraries/SqrtPriceMath.sol b/contracts/libraries/SqrtPriceMath.sol index 169ac9d91..685f485da 100644 --- a/contracts/libraries/SqrtPriceMath.sol +++ b/contracts/libraries/SqrtPriceMath.sol @@ -74,19 +74,21 @@ library SqrtPriceMath { // if we're adding (subtracting), rounding down requires rounding the quotient down (up) // in both cases, avoid a mulDiv for most inputs if (add) { - uint256 quotient = ( - amount <= type(uint160).max - ? (amount << FixedPoint96.RESOLUTION) / liquidity - : FullMath.mulDiv(amount, FixedPoint96.Q96, liquidity) - ); + uint256 quotient = + ( + amount <= type(uint160).max + ? (amount << FixedPoint96.RESOLUTION) / liquidity + : FullMath.mulDiv(amount, FixedPoint96.Q96, liquidity) + ); return uint256(sqrtPX96).add(quotient).toUint160(); } else { - uint256 quotient = ( - amount <= type(uint160).max - ? UnsafeMath.divRoundingUp(amount << FixedPoint96.RESOLUTION, liquidity) - : FullMath.mulDivRoundingUp(amount, FixedPoint96.Q96, liquidity) - ); + uint256 quotient = + ( + amount <= type(uint160).max + ? UnsafeMath.divRoundingUp(amount << FixedPoint96.RESOLUTION, liquidity) + : FullMath.mulDivRoundingUp(amount, FixedPoint96.Q96, liquidity) + ); require(sqrtPX96 > quotient); // always fits 160 bits diff --git a/contracts/libraries/TransferHelper.sol b/contracts/libraries/TransferHelper.sol index 4e217320e..25d630902 100644 --- a/contracts/libraries/TransferHelper.sol +++ b/contracts/libraries/TransferHelper.sol @@ -16,9 +16,8 @@ library TransferHelper { address to, uint256 value ) internal { - (bool success, bytes memory data) = token.call( - abi.encodeWithSelector(IERC20Minimal.transfer.selector, to, value) - ); + (bool success, bytes memory data) = + token.call(abi.encodeWithSelector(IERC20Minimal.transfer.selector, to, value)); require(success && (data.length == 0 || abi.decode(data, (bool))), 'TF'); } } diff --git a/contracts/test/OracleEchidnaTest.sol b/contracts/test/OracleEchidnaTest.sol index 4e0a7dfc4..2cf5f2e6c 100644 --- a/contracts/test/OracleEchidnaTest.sol +++ b/contracts/test/OracleEchidnaTest.sol @@ -59,12 +59,12 @@ contract OracleEchidnaTest { secondsAgos[0] = secondsAgo0; secondsAgos[1] = secondsAgo1; - (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s) = oracle.observe( - secondsAgos - ); + (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s) = + oracle.observe(secondsAgos); int56 timeWeightedTick = (tickCumulatives[1] - tickCumulatives[0]) / timeElapsed; - uint256 timeWeightedHarmonicMeanLiquidity = (uint256(timeElapsed) * type(uint160).max) / - (uint256(secondsPerLiquidityCumulativeX128s[1] - secondsPerLiquidityCumulativeX128s[0]) << 32); + uint256 timeWeightedHarmonicMeanLiquidity = + (uint256(timeElapsed) * type(uint160).max) / + (uint256(secondsPerLiquidityCumulativeX128s[1] - secondsPerLiquidityCumulativeX128s[0]) << 32); assert(timeWeightedHarmonicMeanLiquidity <= type(uint128).max); assert(timeWeightedTick <= type(int24).max); assert(timeWeightedTick >= type(int24).min); @@ -98,9 +98,8 @@ contract OracleEchidnaTest { // check that the observations are initialized, and that the index is not the oldest observation require(index < cardinality && index != (oracle.index() + 1) % cardinality); - (uint32 blockTimestamp0, int56 tickCumulative0, , bool initialized0) = oracle.observations( - index == 0 ? cardinality - 1 : index - 1 - ); + (uint32 blockTimestamp0, int56 tickCumulative0, , bool initialized0) = + oracle.observations(index == 0 ? cardinality - 1 : index - 1); (uint32 blockTimestamp1, int56 tickCumulative1, , bool initialized1) = oracle.observations(index); require(initialized0); @@ -117,9 +116,8 @@ contract OracleEchidnaTest { uint32[] memory secondsAgos = new uint32[](2); secondsAgos[0] = secondsAgo; secondsAgos[1] = 0; - (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s) = oracle.observe( - secondsAgos - ); + (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s) = + oracle.observe(secondsAgos); // compute the time weighted tick, rounded towards negative infinity int56 numerator = tickCumulatives[1] - tickCumulatives[0]; @@ -131,8 +129,9 @@ contract OracleEchidnaTest { // the time weighted averages fit in their respective accumulated types assert(timeWeightedTick <= type(int24).max && timeWeightedTick >= type(int24).min); - uint256 timeWeightedHarmonicMeanLiquidity = (uint256(secondsAgo) * type(uint160).max) / - (uint256(secondsPerLiquidityCumulativeX128s[1] - secondsPerLiquidityCumulativeX128s[0]) << 32); + uint256 timeWeightedHarmonicMeanLiquidity = + (uint256(secondsAgo) * type(uint160).max) / + (uint256(secondsPerLiquidityCumulativeX128s[1] - secondsPerLiquidityCumulativeX128s[0]) << 32); assert(timeWeightedHarmonicMeanLiquidity <= type(uint128).max); } } diff --git a/contracts/test/SqrtPriceMathEchidnaTest.sol b/contracts/test/SqrtPriceMathEchidnaTest.sol index 1e569acb4..48010d9e6 100644 --- a/contracts/test/SqrtPriceMathEchidnaTest.sol +++ b/contracts/test/SqrtPriceMathEchidnaTest.sol @@ -134,9 +134,10 @@ contract SqrtPriceMathEchidnaTest { uint256 numerator2 = sqrtP - sqrtQ; uint256 denominator = uint256(sqrtP) * sqrtQ; - uint256 safeResult = roundUp - ? FullMath.mulDivRoundingUp(numerator1, numerator2, denominator) - : FullMath.mulDiv(numerator1, numerator2, denominator); + uint256 safeResult = + roundUp + ? FullMath.mulDivRoundingUp(numerator1, numerator2, denominator) + : FullMath.mulDiv(numerator1, numerator2, denominator); uint256 fullResult = SqrtPriceMath.getAmount0Delta(sqrtQ, sqrtP, liquidity, roundUp); assert(safeResult == fullResult); diff --git a/contracts/test/SwapMathEchidnaTest.sol b/contracts/test/SwapMathEchidnaTest.sol index 0be6b0b87..67bf86128 100644 --- a/contracts/test/SwapMathEchidnaTest.sol +++ b/contracts/test/SwapMathEchidnaTest.sol @@ -16,13 +16,8 @@ contract SwapMathEchidnaTest { require(feePips > 0); require(feePips < 1e6); - (uint160 sqrtQ, uint256 amountIn, uint256 amountOut, uint256 feeAmount) = SwapMath.computeSwapStep( - sqrtPriceRaw, - sqrtPriceTargetRaw, - liquidity, - amountRemaining, - feePips - ); + (uint160 sqrtQ, uint256 amountIn, uint256 amountOut, uint256 feeAmount) = + SwapMath.computeSwapStep(sqrtPriceRaw, sqrtPriceTargetRaw, liquidity, amountRemaining, feePips); assert(amountIn <= type(uint256).max - feeAmount); diff --git a/contracts/test/TestUniswapV3Router.sol b/contracts/test/TestUniswapV3Router.sol index 60f2e3f2c..8f916ddaf 100644 --- a/contracts/test/TestUniswapV3Router.sol +++ b/contracts/test/TestUniswapV3Router.sol @@ -60,9 +60,8 @@ contract TestUniswapV3Router is IUniswapV3SwapCallback { if (pools.length == 1) { // get the address and amount of the token that we need to pay - address tokenToBePaid = amount0Delta > 0 - ? IUniswapV3Pool(msg.sender).token0() - : IUniswapV3Pool(msg.sender).token1(); + address tokenToBePaid = + amount0Delta > 0 ? IUniswapV3Pool(msg.sender).token0() : IUniswapV3Pool(msg.sender).token1(); int256 amountToBePaid = amount0Delta > 0 ? amount0Delta : amount1Delta; bool zeroForOne = tokenToBePaid == IUniswapV3Pool(pools[0]).token1(); diff --git a/contracts/test/TickOverflowSafetyEchidnaTest.sol b/contracts/test/TickOverflowSafetyEchidnaTest.sol index f1b8ec1a4..fcda269e6 100644 --- a/contracts/test/TickOverflowSafetyEchidnaTest.sol +++ b/contracts/test/TickOverflowSafetyEchidnaTest.sol @@ -42,30 +42,32 @@ contract TickOverflowSafetyEchidnaTest { require(tickLower > MIN_TICK); require(tickUpper < MAX_TICK); require(tickLower < tickUpper); - bool flippedLower = ticks.update( - tickLower, - tick, - liquidityDelta, - feeGrowthGlobal0X128, - feeGrowthGlobal1X128, - 0, - 0, - uint32(block.timestamp), - false, - MAX_LIQUIDITY - ); - bool flippedUpper = ticks.update( - tickUpper, - tick, - liquidityDelta, - feeGrowthGlobal0X128, - feeGrowthGlobal1X128, - 0, - 0, - uint32(block.timestamp), - true, - MAX_LIQUIDITY - ); + bool flippedLower = + ticks.update( + tickLower, + tick, + liquidityDelta, + feeGrowthGlobal0X128, + feeGrowthGlobal1X128, + 0, + 0, + uint32(block.timestamp), + false, + MAX_LIQUIDITY + ); + bool flippedUpper = + ticks.update( + tickUpper, + tick, + liquidityDelta, + feeGrowthGlobal0X128, + feeGrowthGlobal1X128, + 0, + 0, + uint32(block.timestamp), + true, + MAX_LIQUIDITY + ); if (flippedLower) { if (liquidityDelta < 0) { diff --git a/test/Oracle.spec.ts b/test/Oracle.spec.ts index 36e6fd065..1a31d6fd4 100644 --- a/test/Oracle.spec.ts +++ b/test/Oracle.spec.ts @@ -593,7 +593,13 @@ describe('Oracle', () => { it('fetch many values', async () => { await oracle.advanceTime(6) const { tickCumulatives, secondsPerLiquidityCumulativeX128s } = await oracle.observe([ - 20, 17, 13, 10, 5, 1, 0, + 20, + 17, + 13, + 10, + 5, + 1, + 0, ]) expect({ tickCumulatives: tickCumulatives.map((tc) => tc.toNumber()), diff --git a/test/TickMath.spec.ts b/test/TickMath.spec.ts index 0f5f5bbd5..8e03a60ec 100644 --- a/test/TickMath.spec.ts +++ b/test/TickMath.spec.ts @@ -53,7 +53,20 @@ describe('TickMath', () => { }) for (const absTick of [ - 50, 100, 250, 500, 1_000, 2_500, 3_000, 4_000, 5_000, 50_000, 150_000, 250_000, 500_000, 738_203, + 50, + 100, + 250, + 500, + 1_000, + 2_500, + 3_000, + 4_000, + 5_000, + 50_000, + 150_000, + 250_000, + 500_000, + 738_203, ]) { for (const tick of [-absTick, absTick]) { describe(`tick ${tick}`, () => { diff --git a/test/UniswapV3Pool.arbitrage.spec.ts b/test/UniswapV3Pool.arbitrage.spec.ts index 2bd999903..3c3de0061 100644 --- a/test/UniswapV3Pool.arbitrage.spec.ts +++ b/test/UniswapV3Pool.arbitrage.spec.ts @@ -82,13 +82,18 @@ describe('UniswapV3Pool arbitrage tests', () => { await fix.token0.transfer(arbitrageur.address, BigNumber.from(2).pow(254)) await fix.token1.transfer(arbitrageur.address, BigNumber.from(2).pow(254)) - const { swapExact0For1, swapToHigherPrice, swapToLowerPrice, swapExact1For0, mint } = - await createPoolFunctions({ - swapTarget: fix.swapTargetCallee, - token0: fix.token0, - token1: fix.token1, - pool, - }) + const { + swapExact0For1, + swapToHigherPrice, + swapToLowerPrice, + swapExact1For0, + mint, + } = await createPoolFunctions({ + swapTarget: fix.swapTargetCallee, + token0: fix.token0, + token1: fix.token1, + pool, + }) const testerFactory = await ethers.getContractFactory('UniswapV3PoolSwapTest') const tester = (await testerFactory.deploy()) as UniswapV3PoolSwapTest @@ -119,8 +124,16 @@ describe('UniswapV3Pool arbitrage tests', () => { let tickMath: TickMathTest beforeEach('load the fixture', async () => { - ;({ swapExact0For1, pool, mint, swapToHigherPrice, swapToLowerPrice, swapExact1For0, tester, tickMath } = - await loadFixture(arbTestFixture)) + ;({ + swapExact0For1, + pool, + mint, + swapToHigherPrice, + swapToLowerPrice, + swapExact1For0, + tester, + tickMath, + } = await loadFixture(arbTestFixture)) }) async function simulateSwap( @@ -253,8 +266,10 @@ describe('UniswapV3Pool arbitrage tests', () => { arbBalance1 = arbBalance1.add(amount1Burn) // add the fees as well - const { amount0: amount0CollectAndBurn, amount1: amount1CollectAndBurn } = - await pool.callStatic.collect(arbitrageur.address, tickLower, tickUpper, MaxUint128, MaxUint128) + const { + amount0: amount0CollectAndBurn, + amount1: amount1CollectAndBurn, + } = await pool.callStatic.collect(arbitrageur.address, tickLower, tickUpper, MaxUint128, MaxUint128) const [amount0Collect, amount1Collect] = [ amount0CollectAndBurn.sub(amount0Burn), amount1CollectAndBurn.sub(amount1Burn), diff --git a/test/UniswapV3Pool.spec.ts b/test/UniswapV3Pool.spec.ts index dfd70a6ee..a7eb75e07 100644 --- a/test/UniswapV3Pool.spec.ts +++ b/test/UniswapV3Pool.spec.ts @@ -515,8 +515,13 @@ describe('UniswapV3Pool', () => { await expect(pool.burn(minTick + tickSpacing, maxTick - tickSpacing, 0)).to.be.reverted await mint(wallet.address, minTick + tickSpacing, maxTick - tickSpacing, 1) - let { liquidity, feeGrowthInside0LastX128, feeGrowthInside1LastX128, tokensOwed1, tokensOwed0 } = - await pool.positions(getPositionKey(wallet.address, minTick + tickSpacing, maxTick - tickSpacing)) + let { + liquidity, + feeGrowthInside0LastX128, + feeGrowthInside1LastX128, + tokensOwed1, + tokensOwed0, + } = await pool.positions(getPositionKey(wallet.address, minTick + tickSpacing, maxTick - tickSpacing)) expect(liquidity).to.eq(1) expect(feeGrowthInside0LastX128).to.eq('102084710076281216349243831104605583') expect(feeGrowthInside1LastX128).to.eq('10208471007628121634924383110460558') @@ -524,8 +529,13 @@ describe('UniswapV3Pool', () => { expect(tokensOwed1, 'tokens owed 1 before').to.eq(0) await pool.burn(minTick + tickSpacing, maxTick - tickSpacing, 1) - ;({ liquidity, feeGrowthInside0LastX128, feeGrowthInside1LastX128, tokensOwed1, tokensOwed0 } = - await pool.positions(getPositionKey(wallet.address, minTick + tickSpacing, maxTick - tickSpacing))) + ;({ + liquidity, + feeGrowthInside0LastX128, + feeGrowthInside1LastX128, + tokensOwed1, + tokensOwed0, + } = await pool.positions(getPositionKey(wallet.address, minTick + tickSpacing, maxTick - tickSpacing))) expect(liquidity).to.eq(0) expect(feeGrowthInside0LastX128).to.eq('102084710076281216349243831104605583') expect(feeGrowthInside1LastX128).to.eq('10208471007628121634924383110460558') @@ -558,8 +568,13 @@ describe('UniswapV3Pool', () => { await swapExact0For1(expandTo18Decimals(1), wallet.address) await swapExact1For0(expandTo18Decimals(1), wallet.address) await pool.connect(other).burn(minTick, maxTick, expandTo18Decimals(1)) - const { liquidity, tokensOwed0, tokensOwed1, feeGrowthInside0LastX128, feeGrowthInside1LastX128 } = - await pool.positions(getPositionKey(other.address, minTick, maxTick)) + const { + liquidity, + tokensOwed0, + tokensOwed1, + feeGrowthInside0LastX128, + feeGrowthInside1LastX128, + } = await pool.positions(getPositionKey(other.address, minTick, maxTick)) expect(liquidity).to.eq(0) expect(tokensOwed0).to.not.eq(0) expect(tokensOwed1).to.not.eq(0) @@ -1580,8 +1595,12 @@ describe('UniswapV3Pool', () => { expect(observationCardinality).to.eq(1) expect(observationIndex).to.eq(0) expect(observationCardinalityNext).to.eq(1) - const { secondsPerLiquidityCumulativeX128, tickCumulative, initialized, blockTimestamp } = - await pool.observations(0) + const { + secondsPerLiquidityCumulativeX128, + tickCumulative, + initialized, + blockTimestamp, + } = await pool.observations(0) expect(secondsPerLiquidityCumulativeX128).to.eq(0) expect(tickCumulative).to.eq(0) expect(initialized).to.eq(true) @@ -1701,16 +1720,22 @@ describe('UniswapV3Pool', () => { await expect(pool.snapshotCumulativesInside(tickLower, tickUpper + tickSpacing)).to.be.reverted }) it('is zero immediately after initialize', async () => { - const { secondsPerLiquidityInsideX128, tickCumulativeInside, secondsInside } = - await pool.snapshotCumulativesInside(tickLower, tickUpper) + const { + secondsPerLiquidityInsideX128, + tickCumulativeInside, + secondsInside, + } = await pool.snapshotCumulativesInside(tickLower, tickUpper) expect(secondsPerLiquidityInsideX128).to.eq(0) expect(tickCumulativeInside).to.eq(0) expect(secondsInside).to.eq(0) }) it('increases by expected amount when time elapses in the range', async () => { await pool.advanceTime(5) - const { secondsPerLiquidityInsideX128, tickCumulativeInside, secondsInside } = - await pool.snapshotCumulativesInside(tickLower, tickUpper) + const { + secondsPerLiquidityInsideX128, + tickCumulativeInside, + secondsInside, + } = await pool.snapshotCumulativesInside(tickLower, tickUpper) expect(secondsPerLiquidityInsideX128).to.eq(BigNumber.from(5).shl(128).div(10)) expect(tickCumulativeInside, 'tickCumulativeInside').to.eq(0) expect(secondsInside).to.eq(5) @@ -1719,8 +1744,11 @@ describe('UniswapV3Pool', () => { await pool.advanceTime(5) await swapToHigherPrice(encodePriceSqrt(2, 1), wallet.address) await pool.advanceTime(7) - const { secondsPerLiquidityInsideX128, tickCumulativeInside, secondsInside } = - await pool.snapshotCumulativesInside(tickLower, tickUpper) + const { + secondsPerLiquidityInsideX128, + tickCumulativeInside, + secondsInside, + } = await pool.snapshotCumulativesInside(tickLower, tickUpper) expect(secondsPerLiquidityInsideX128).to.eq(BigNumber.from(5).shl(128).div(10)) expect(tickCumulativeInside, 'tickCumulativeInside').to.eq(0) expect(secondsInside).to.eq(5) @@ -1729,8 +1757,11 @@ describe('UniswapV3Pool', () => { await pool.advanceTime(5) await swapToLowerPrice(encodePriceSqrt(1, 2), wallet.address) await pool.advanceTime(7) - const { secondsPerLiquidityInsideX128, tickCumulativeInside, secondsInside } = - await pool.snapshotCumulativesInside(tickLower, tickUpper) + const { + secondsPerLiquidityInsideX128, + tickCumulativeInside, + secondsInside, + } = await pool.snapshotCumulativesInside(tickLower, tickUpper) expect(secondsPerLiquidityInsideX128).to.eq(BigNumber.from(5).shl(128).div(10)) // tick is 0 for 5 seconds, then not in range expect(tickCumulativeInside, 'tickCumulativeInside').to.eq(0) @@ -1741,8 +1772,11 @@ describe('UniswapV3Pool', () => { await pool.advanceTime(5) await swapToHigherPrice(encodePriceSqrt(1, 1), wallet.address) await pool.advanceTime(7) - const { secondsPerLiquidityInsideX128, tickCumulativeInside, secondsInside } = - await pool.snapshotCumulativesInside(tickLower, tickUpper) + const { + secondsPerLiquidityInsideX128, + tickCumulativeInside, + secondsInside, + } = await pool.snapshotCumulativesInside(tickLower, tickUpper) expect(secondsPerLiquidityInsideX128).to.eq(BigNumber.from(7).shl(128).div(10)) // tick is not in range then tick is 0 for 7 seconds expect(tickCumulativeInside, 'tickCumulativeInside').to.eq(0) @@ -1753,8 +1787,11 @@ describe('UniswapV3Pool', () => { await pool.advanceTime(5) await swapToLowerPrice(encodePriceSqrt(1, 1), wallet.address) await pool.advanceTime(7) - const { secondsPerLiquidityInsideX128, tickCumulativeInside, secondsInside } = - await pool.snapshotCumulativesInside(tickLower, tickUpper) + const { + secondsPerLiquidityInsideX128, + tickCumulativeInside, + secondsInside, + } = await pool.snapshotCumulativesInside(tickLower, tickUpper) expect(secondsPerLiquidityInsideX128).to.eq(BigNumber.from(7).shl(128).div(10)) expect((await pool.slot0()).tick).to.eq(-1) // justify the -7 tick cumulative inside value expect(tickCumulativeInside, 'tickCumulativeInside').to.eq(-7) @@ -1765,8 +1802,11 @@ describe('UniswapV3Pool', () => { await mint(wallet.address, tickUpper, getMaxTick(tickSpacing), 15) await swapToHigherPrice(encodePriceSqrt(2, 1), wallet.address) await pool.advanceTime(8) - const { secondsPerLiquidityInsideX128, tickCumulativeInside, secondsInside } = - await pool.snapshotCumulativesInside(tickUpper, getMaxTick(tickSpacing)) + const { + secondsPerLiquidityInsideX128, + tickCumulativeInside, + secondsInside, + } = await pool.snapshotCumulativesInside(tickUpper, getMaxTick(tickSpacing)) expect(secondsPerLiquidityInsideX128).to.eq(BigNumber.from(8).shl(128).div(15)) // the tick of 2/1 is 6931 // 8 seconds * 6931 = 55448 @@ -1778,8 +1818,11 @@ describe('UniswapV3Pool', () => { await pool.advanceTime(5) await swapToHigherPrice(encodePriceSqrt(2, 1), wallet.address) await pool.advanceTime(8) - const { secondsPerLiquidityInsideX128, tickCumulativeInside, secondsInside } = - await pool.snapshotCumulativesInside(tickLower, tickUpper) + const { + secondsPerLiquidityInsideX128, + tickCumulativeInside, + secondsInside, + } = await pool.snapshotCumulativesInside(tickLower, tickUpper) expect(secondsPerLiquidityInsideX128).to.eq(BigNumber.from(5).shl(128).div(25)) expect(tickCumulativeInside, 'tickCumulativeInside').to.eq(0) expect(secondsInside).to.eq(5) @@ -1796,8 +1839,11 @@ describe('UniswapV3Pool', () => { // 13 seconds in starting range, then 3 seconds in newly minted range await swapToLowerPrice(encodePriceSqrt(1, 2), wallet.address) await pool.advanceTime(3) - const { secondsPerLiquidityInsideX128, tickCumulativeInside, secondsInside } = - await pool.snapshotCumulativesInside(getMinTick(tickSpacing), tickLower) + const { + secondsPerLiquidityInsideX128, + tickCumulativeInside, + secondsInside, + } = await pool.snapshotCumulativesInside(getMinTick(tickSpacing), tickLower) const expectedDiffSecondsPerLiquidity = BigNumber.from(3).shl(128).div(15) expect(secondsPerLiquidityInsideX128.sub(secondsPerLiquidityInsideX128Start)).to.eq( expectedDiffSecondsPerLiquidity diff --git a/test/UniswapV3Pool.swaps.spec.ts b/test/UniswapV3Pool.swaps.spec.ts index 03c76ddf4..da4be1940 100644 --- a/test/UniswapV3Pool.swaps.spec.ts +++ b/test/UniswapV3Pool.swaps.spec.ts @@ -461,12 +461,10 @@ describe('UniswapV3Pool swap tests', () => { for (const poolCase of TEST_POOLS) { describe(poolCase.description, () => { const poolCaseFixture = async () => { - const { - createPool, - token0, - token1, - swapTargetCallee: swapTarget, - } = await poolFixture([wallet], waffle.provider) + const { createPool, token0, token1, swapTargetCallee: swapTarget } = await poolFixture( + [wallet], + waffle.provider + ) const pool = await createPool(poolCase.feeAmount, poolCase.tickSpacing) const poolFunctions = createPoolFunctions({ swapTarget, token0, token1, pool }) await pool.initialize(poolCase.startingPrice) diff --git a/test/shared/fixtures.ts b/test/shared/fixtures.ts index 5a768a6e5..11c84bb72 100644 --- a/test/shared/fixtures.ts +++ b/test/shared/fixtures.ts @@ -75,8 +75,7 @@ export const poolFixture: Fixture = async function (): Promise { - const mockTimePoolDeployer = - (await MockTimeUniswapV3PoolDeployerFactory.deploy()) as MockTimeUniswapV3PoolDeployer + const mockTimePoolDeployer = (await MockTimeUniswapV3PoolDeployerFactory.deploy()) as MockTimeUniswapV3PoolDeployer const tx = await mockTimePoolDeployer.deploy( factory.address, firstToken.address,