Skip to content

Commit

Permalink
chore: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed May 21, 2021
1 parent 92e3298 commit e5c8912
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 109 deletions.
70 changes: 25 additions & 45 deletions contracts/UniswapV3Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,9 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall {
)
{
checkTicks(tickLower, tickUpper);
StateMath.SnapshotArgs memory args = StateMath.SnapshotArgs(
slot0,
liquidity,
_blockTimestamp(),
tickLower,
tickUpper

);
return StateMath.snapshotCumulativesInside(
ticks,
observations,
args
);
StateMath.SnapshotArgs memory args =
StateMath.SnapshotArgs(slot0, liquidity, _blockTimestamp(), tickLower, tickUpper);
return StateMath.snapshotCumulativesInside(ticks, observations, args);
}

/// @inheritdoc IUniswapV3PoolDerivedState
Expand Down Expand Up @@ -296,21 +286,16 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall {
function makeFeeParams() private view returns (Tick.FeeParams memory params) {
uint32 time = _blockTimestamp();
(int56 tickCumulative, uint160 secondsPerLiquidityCumulativeX128) =
observations.observeSingle(
time,
0,
slot0.tick,
slot0.observationIndex,
liquidity,
slot0.observationCardinality
);
observations.observeSingle(
time,
0,
slot0.tick,
slot0.observationIndex,
liquidity,
slot0.observationCardinality
);

params = Tick.FeeParams(
tickCumulative,
secondsPerLiquidityCumulativeX128,
time,
maxLiquidityPerTick
);
params = Tick.FeeParams(tickCumulative, secondsPerLiquidityCumulativeX128, time, maxLiquidityPerTick);
}

/// @dev Gets and updates a position with the given liquidity delta
Expand Down Expand Up @@ -485,27 +470,22 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall {
computedLatestObservation: false
});

StateMath.SwapState memory state;
StateMath.SwapState memory state;
bool exactInput;

// pack the arguments to avoid abi encoder
StateMath.SwapArgs memory args = StateMath.SwapArgs(
cache,
fee,
tickSpacing,
feeGrowthGlobal0X128,
feeGrowthGlobal1X128,
zeroForOne,
amountSpecified,
sqrtPriceLimitX96
);
(state, cache, exactInput) = StateMath.swap(
args,
slot0,
ticks,
observations,
tickBitmap
);
StateMath.SwapArgs memory args =
StateMath.SwapArgs(
cache,
fee,
tickSpacing,
feeGrowthGlobal0X128,
feeGrowthGlobal1X128,
zeroForOne,
amountSpecified,
sqrtPriceLimitX96
);
(state, cache, exactInput) = StateMath.swap(args, slot0, ticks, observations, tickBitmap);

// update liquidity if it changed
if (cache.liquidityStart != state.liquidity) liquidity = state.liquidity;
Expand Down
122 changes: 62 additions & 60 deletions contracts/libraries/StateMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
pragma solidity ^0.7.6;
pragma abicoder v2;

import "./../UniswapV3Pool.sol";
import './../UniswapV3Pool.sol';
import './../interfaces/IERC20Minimal.sol';
import "./../interfaces/callback/IUniswapV3SwapCallback.sol";

import "./TickMath.sol";
import "./Tick.sol";
import "./TickBitmap.sol";
import "./SwapMath.sol";
import "./FullMath.sol";
import "./Oracle.sol";
import "./FixedPoint128.sol";
import "./SafeCast.sol";
import "./LowGasSafeMath.sol";
import './../interfaces/callback/IUniswapV3SwapCallback.sol';

import './TickMath.sol';
import './Tick.sol';
import './TickBitmap.sol';
import './SwapMath.sol';
import './FullMath.sol';
import './Oracle.sol';
import './FixedPoint128.sol';
import './SafeCast.sol';
import './LowGasSafeMath.sol';

library StateMath {
using SafeCast for uint256;
Expand Down Expand Up @@ -59,32 +59,37 @@ library StateMath {
mapping(int24 => Tick.Info) storage ticks,
Oracle.Observation[65535] storage observations,
mapping(int16 => uint256) storage tickBitmap
) public returns (SwapState memory state, SwapCache memory, bool) {
)
public
returns (
SwapState memory state,
SwapCache memory,
bool
)
{
Slot0 memory slot0Start = slot0;
state =
SwapState({
amountSpecifiedRemaining: args.amountSpecified,
amountCalculated: 0,
sqrtPriceX96: slot0Start.sqrtPriceX96,
tick: slot0Start.tick,
feeGrowthGlobalX128: args.zeroForOne ? args.feeGrowthGlobal0X128 : args.feeGrowthGlobal1X128,
protocolFee: 0,
liquidity: args.cache.liquidityStart
});
state = SwapState({
amountSpecifiedRemaining: args.amountSpecified,
amountCalculated: 0,
sqrtPriceX96: slot0Start.sqrtPriceX96,
tick: slot0Start.tick,
feeGrowthGlobalX128: args.zeroForOne ? args.feeGrowthGlobal0X128 : args.feeGrowthGlobal1X128,
protocolFee: 0,
liquidity: args.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 != args.sqrtPriceLimitX96) {
StepComputations memory step = createStep(
tickBitmap,
state,
args.zeroForOne,
args.tickSpacing
);
StepComputations memory step = createStep(tickBitmap, state, args.zeroForOne, args.tickSpacing);

// compute values to swap to the target tick, price limit, or point where input/output amount is exhausted
(state.sqrtPriceX96, step.amountIn, step.amountOut, step.feeAmount) = SwapMath.computeSwapStep(
state.sqrtPriceX96,
(args.zeroForOne ? step.sqrtPriceNextX96 < args.sqrtPriceLimitX96 : step.sqrtPriceNextX96 > args.sqrtPriceLimitX96)
(
args.zeroForOne
? step.sqrtPriceNextX96 < args.sqrtPriceLimitX96
: step.sqrtPriceNextX96 > args.sqrtPriceLimitX96
)
? args.sqrtPriceLimitX96
: step.sqrtPriceNextX96,
state.liquidity,
Expand Down Expand Up @@ -119,15 +124,14 @@ library StateMath {
// update tick and write an oracle entry if the tick change
if (state.tick != slot0Start.tick) {
SwapCache memory cache = args.cache;
(slot0.observationIndex, slot0.observationCardinality) =
observations.write(
slot0Start.observationIndex,
cache.blockTimestamp,
slot0Start.tick,
cache.liquidityStart,
slot0Start.observationCardinality,
slot0Start.observationCardinalityNext
);
(slot0.observationIndex, slot0.observationCardinality) = observations.write(
slot0Start.observationIndex,
cache.blockTimestamp,
slot0Start.tick,
cache.liquidityStart,
slot0Start.observationCardinality,
slot0Start.observationCardinalityNext
);
slot0.sqrtPriceX96 = state.sqrtPriceX96;
slot0.tick = state.tick;
} else {
Expand All @@ -140,9 +144,9 @@ library StateMath {

function shiftTick(
Slot0 memory slot0Start,
SwapState memory state,
StepComputations memory step,
SwapArgs memory args,
SwapState memory state,
StepComputations memory step,
SwapArgs memory args,
mapping(int24 => Tick.Info) storage ticks,
Oracle.Observation[65535] storage observations
) private returns (SwapState memory) {
Expand Down Expand Up @@ -238,11 +242,12 @@ library StateMath {
uint256 feeAmount;
}

function createStep(mapping(int16 => uint256) storage tickBitmap, SwapState memory state, bool zeroForOne, int24 tickSpacing)

public
view returns (StepComputations memory step)
{
function createStep(
mapping(int16 => uint256) storage tickBitmap,
SwapState memory state,
bool zeroForOne,
int24 tickSpacing
) public view returns (StepComputations memory step) {
step.sqrtPriceStartX96 = state.sqrtPriceX96;

(step.tickNext, step.initialized) = tickBitmap.nextInitializedTickWithinOneWord(
Expand Down Expand Up @@ -319,19 +324,16 @@ library StateMath {
secondsOutsideLower - secondsOutsideUpper
);
} else if (args.slot0.tick < args.tickUpper) {
ObsArgs memory args2 = ObsArgs(
tickCumulativeLower,
tickCumulativeUpper,
secondsPerLiquidityOutsideLowerX128,
secondsPerLiquidityOutsideUpperX128,
secondsOutsideLower,
secondsOutsideUpper
);
return observeTick(
observations,
args,
args2
);
ObsArgs memory args2 =
ObsArgs(
tickCumulativeLower,
tickCumulativeUpper,
secondsPerLiquidityOutsideLowerX128,
secondsPerLiquidityOutsideUpperX128,
secondsOutsideLower,
secondsOutsideUpper
);
return observeTick(observations, args, args2);
} else {
return (
tickCumulativeUpper - tickCumulativeLower,
Expand Down
1 change: 0 additions & 1 deletion contracts/libraries/Tick.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ library Tick {
feeGrowthInside1X128 = feeGrowthGlobal1X128 - feeGrowthBelow1X128 - feeGrowthAbove1X128;
}


struct FeeParams {
int56 tickCumulative;
uint160 secondsPerLiquidityCumulativeX128;
Expand Down
4 changes: 1 addition & 3 deletions test/UniswapV3Pool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1355,9 +1355,7 @@ describe('UniswapV3Pool', () => {
const sqrtTickMath = (await (
await ethers.getContractFactory('TickMathTest', { libraries: { TickMath: tickMathLib.address } })
).deploy()) as TickMathTest
const swapMath = (await (
await ethers.getContractFactory('SwapMathTest')
).deploy()) as SwapMathTest
const swapMath = (await (await ethers.getContractFactory('SwapMathTest')).deploy()) as SwapMathTest
const p0 = (await sqrtTickMath.getSqrtRatioAtTick(-24081)).add(1)
// initialize at a price of ~0.3 token1/token0
// meaning if you swap in 2 token0, you should end up getting 0 token1
Expand Down

0 comments on commit e5c8912

Please sign in to comment.