Skip to content

Commit

Permalink
prettier + solhint
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Oct 14, 2020
1 parent 52e931f commit 27ea16b
Show file tree
Hide file tree
Showing 28 changed files with 1,078 additions and 377 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- master
pull_request:
pull_request_target:

jobs:
run-linters:
Expand All @@ -27,6 +27,5 @@ jobs:
uses: wearerequired/lint-action@v1
with:
github_token: ${{ secrets.github_token }}
# Enable your linters heren
prettier: true
auto_fix: true
6 changes: 6 additions & 0 deletions .solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}
4 changes: 2 additions & 2 deletions contracts/UniswapV3Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract UniswapV3Factory is IUniswapV3Factory {
feeToSetter = _feeToSetter;
}

function allPairsLength() external override view returns (uint) {
function allPairsLength() external view override returns (uint256) {
return allPairs.length;
}

Expand All @@ -25,7 +25,7 @@ contract UniswapV3Factory is IUniswapV3Factory {
require(token0 != address(0), 'UniswapV3: ZERO_ADDRESS');
require(getPair[token0][token1] == address(0), 'UniswapV3: PAIR_EXISTS'); // single check is sufficient
// CREATE2 salt is 0 since token0 and token1 are included as constructor arguments
pair = address(new UniswapV3Pair{ salt: bytes32(0) }(address(this), token0, token1));
pair = address(new UniswapV3Pair{salt: bytes32(0)}(address(this), token0, token1));
getPair[token0][token1] = pair;
getPair[token1][token0] = pair; // populate mapping in the reverse direction
allPairs.push(pair);
Expand Down
414 changes: 241 additions & 173 deletions contracts/UniswapV3Pair.sol

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion contracts/interfaces/IUniswapV3Callee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
pragma solidity >=0.5.0;

interface IUniswapV3Callee {
function uniswapV3Call(address sender, uint amount0, uint amount1, bytes calldata data) external;
function uniswapV3Call(
address sender,
uint256 amount0,
uint256 amount1,
bytes calldata data
) external;
}
10 changes: 7 additions & 3 deletions contracts/interfaces/IUniswapV3Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
pragma solidity >=0.5.0;

interface IUniswapV3Factory {
event PairCreated(address indexed token0, address indexed token1, address pair, uint);
event PairCreated(address indexed token0, address indexed token1, address pair, uint256);

function feeTo() external view returns (address);

function feeToSetter() external view returns (address);

function getPair(address tokenA, address tokenB) external view returns (address pair);
function allPairs(uint) external view returns (address pair);
function allPairsLength() external view returns (uint);

function allPairs(uint256) external view returns (address pair);

function allPairsLength() external view returns (uint256);

function createPair(address tokenA, address tokenB) external returns (address pair);

function setFeeTo(address) external;

function setFeeToSetter(address) external;
}
16 changes: 14 additions & 2 deletions contracts/interfaces/IUniswapV3Pair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,43 @@
pragma solidity >=0.5.0;

interface IUniswapV3Pair {
event Initialized(uint amount0, uint amount1, int16 tick, uint8 feeVote);
event Initialized(uint256 amount0, uint256 amount1, int16 tick, uint8 feeVote);
// todo: liquidityDelta or liquidity?
event PositionSet(address owner, int16 tickLower, int16 tickUpper, uint8 feeVote, int112 liquidityDelta);

// constants
function NUM_FEE_OPTIONS() external pure returns (uint8);

function LIQUIDITY_MIN() external pure returns (uint112);

function TOKEN_MIN() external pure returns (uint8);

// immutables
function factory() external view returns (address);

function token0() external view returns (address);

function token1() external view returns (address);

// variables/state
function reserve0Virtual() external view returns (uint112);

function reserve1Virtual() external view returns (uint112);

function blockTimestampLast() external view returns (uint32);

function tickCurrent() external view returns (int16);
function virtualSupplies(uint) external view returns (uint112);

function virtualSupplies(uint256) external view returns (uint112);

function price0CumulativeLast() external view returns (uint256);

function price1CumulativeLast() external view returns (uint256);

// derived state
function getFee() external view returns (uint24 fee);

function getVirtualSupply() external view returns (uint112 virtualSupply);

function getCumulativePrices() external view returns (uint256 price0Cumulative, uint256 price1Cumulative);
}
38 changes: 15 additions & 23 deletions contracts/libraries/FixedPointExtra.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import './SafeMath.sol';
library FixedPointExtra {
// multiply a UQ112x112 by an int and decode, returning an int
// reverts on overflow
function muli(FixedPoint.uq112x112 memory self, int other) internal pure returns (int) {
uint144 z = FixedPoint.decode144(FixedPoint.mul(self, uint(other < 0 ? -other : other)));
return other < 0 ? -int(z) : z;
function muli(FixedPoint.uq112x112 memory self, int256 other) internal pure returns (int256) {
uint144 z = FixedPoint.decode144(FixedPoint.mul(self, uint256(other < 0 ? -other : other)));
return other < 0 ? -int256(z) : z;
}

// lower 112 bits, representing decimal portion of the number, i.e. 14 bytes
uint224 public constant LOWER_MASK = 0xffff_ffff_ffff_ffff_ffff_ffff_ffff;

// multiply a UQ112x112 by a UQ112x112, returning a UQ112x112
function muluq(FixedPoint.uq112x112 memory self, FixedPoint.uq112x112 memory other)
internal
pure
Expand All @@ -26,33 +25,26 @@ library FixedPointExtra {
if (self._x == 0 || other._x == 0) {
return FixedPoint.uq112x112(0);
}
uint112 upper_self = uint112(self._x >> 112); // * 2^0
uint112 lower_self = uint112(self._x & LOWER_MASK); // * 2^-112
uint112 upper_other = uint112(other._x >> 112); // * 2^0
uint112 lower_other = uint112(other._x & LOWER_MASK); // * 2^-112
uint112 upper_self = uint112(self._x >> 112);
uint112 lower_self = uint112(self._x & LOWER_MASK);
uint112 upper_other = uint112(other._x >> 112);
uint112 lower_other = uint112(other._x & LOWER_MASK);

// partial products
uint224 uppers = uint224(upper_self) * upper_other; // * 2^0
uint224 lowers = uint224(lower_self) * lower_other; // * 2^-224
uint224 uppers_lowero = uint224(upper_self) * lower_other; // * 2^-112
uint224 uppero_lowers = uint224(upper_other) * lower_self; // * 2^-112
uint224 uppers = uint224(upper_self) * upper_other;
uint224 lowers = uint224(lower_self) * lower_other;
uint224 uppers_lowero = uint224(upper_self) * lower_other;
uint224 uppero_lowers = uint224(upper_other) * lower_self;

// so the bit shift does not overflow
require(uppers <= uint112(-1), "FixedPointExtra: MULTIPLICATION_OVERFLOW");
require(uppers <= uint112(-1), 'FixedPointExtra: MULTIPLICATION_OVERFLOW');

// this cannot exceed 256 bits, all values are 224 bits
uint sum = uint(uppers << 112) + uppers_lowero + uppero_lowers + (lowers >> 112);
uint256 sum = uint256(uppers << 112) + uppers_lowero + uppero_lowers + (lowers >> 112);

// between 224 bits and 256 bits
require(sum <= uint224(-1), "FixedPointExtra: MULTIPLICATION_OVERFLOW");
require(sum <= uint224(-1), 'FixedPointExtra: MULTIPLICATION_OVERFLOW');

// the multiplication results in a number too small to be represented in Q112.112
require(sum > 0, "FixedPointExtra: MULTIPLICATION_UNDERFLOW");
require(sum > 0, 'FixedPointExtra: MULTIPLICATION_UNDERFLOW');
return FixedPoint.uq112x112(uint224(sum));
}

// divide a UQ112x112 by a UQ112x112, returning a UQ112x112
// TODO improve the precision of this? would be great to avoid hard-coding
function divuq(FixedPoint.uq112x112 memory self, FixedPoint.uq112x112 memory other)
internal
pure
Expand Down
104 changes: 62 additions & 42 deletions contracts/libraries/PriceMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,15 @@ library PriceMath {
uint112 reserveOut,
uint24 lpFee,
FixedPoint.uq112x112 memory inOutRatio
)
internal
pure
returns (uint112 amountIn)
{
) internal pure returns (uint112 amountIn) {
FixedPoint.uq112x112 memory reserveRatio = FixedPoint.fraction(reserveIn, reserveOut);
if (reserveRatio._x >= inOutRatio._x) return 0; // short-circuit if the ratios are equal

uint inputToRatio = getInputToRatioUQ128x128(reserveIn, reserveOut, lpFee, inOutRatio._x);
uint256 inputToRatio = getInputToRatioUQ128x128(reserveIn, reserveOut, lpFee, inOutRatio._x);
require(inputToRatio >> 112 <= type(uint112).max, 'PriceMath: TODO');
return uint112(inputToRatio >> 112);
}


/**
* Calculate (y(g - 2) + sqrt (g^2 * y^2 + 4xyr(1 - g))) / 2(1 - g) * 2^112, where
* y = reserveIn,
Expand All @@ -35,87 +30,112 @@ library PriceMath {
* r = inOutRatio * 2^-112.
* Throw on overflow.
*/
function getInputToRatioUQ128x128 (
uint112 reserveIn, uint112 reserveOut,
uint24 lpFee, uint224 inOutRatio)
internal pure returns (uint256 amountIn) {
function getInputToRatioUQ128x128(
uint112 reserveIn,
uint112 reserveOut,
uint24 lpFee,
uint224 inOutRatio
) internal pure returns (uint256 amountIn) {
// g2y2 = g^2 * y^2 * 1e6 (max value: ~2^236)
uint256 g2y2 = (uint256 (lpFee) * uint256 (lpFee) * uint256 (reserveIn) * uint256 (reserveIn) + 999999) / 1e6;
uint256 g2y2 = (uint256(lpFee) * uint256(lpFee) * uint256(reserveIn) * uint256(reserveIn) + 999999) / 1e6;

// xyr4g1 = 4 * x * y * (1 - g) * 1e6 (max value: ~2^246)
uint256 xy41g = 4 * uint256 (reserveIn) * uint256 (reserveOut) * (1e6 - uint256 (lpFee));
uint256 xy41g = 4 * uint256(reserveIn) * uint256(reserveOut) * (1e6 - uint256(lpFee));

// xyr41g = 4 * x * y * r * (1 - g) * 1e6 (max value: ~2^246)
uint256 xyr41g = mulshift (xy41g, uint256 (inOutRatio), 112);
require (xyr41g < 2**254);
uint256 xyr41g = mulshift(xy41g, uint256(inOutRatio), 112);
require(xyr41g < 2**254);

// sr = sqrt (g^2 * y^2 + 4 * x * y * r * (1 - g)) * 2^128
uint256 sr = (sqrt (g2y2 + xyr41g) + 999) / 1000;
uint256 sr = (sqrt(g2y2 + xyr41g) + 999) / 1000;

// y2g = y(2 - g) * 2^128
uint256 y2g = uint256 (reserveIn) * (2e6 - uint256 (lpFee)) * 0x10c6f7a0b5ed8d36b4c7f3493858;
uint256 y2g = uint256(reserveIn) * (2e6 - uint256(lpFee)) * 0x10c6f7a0b5ed8d36b4c7f3493858;

// Make sure numerator is non-negative
require (sr >= y2g);
require(sr >= y2g);

// num = (sqrt (g^2 * y^2 + 4 * x * y * r * (1 - g)) - y(2 - g)) * 2^128
uint256 num = sr - y2g;

// den = 2 * (1 - g) * 1e6
uint256 den = 2 * (1e6 - uint256 (lpFee));
uint256 den = 2 * (1e6 - uint256(lpFee));

return ((num + den - 1) / den * 1e6 + 0xffff) >> 16;
return (((num + den - 1) / den) * 1e6 + 0xffff) >> 16;
}

/**
* Calculate x * y >> s rounding up. Throw on overflow.
*/
function mulshift (uint256 x, uint256 y, uint8 s)
internal pure returns (uint256 result) {
function mulshift(
uint256 x,
uint256 y,
uint8 s
) internal pure returns (uint256 result) {
uint256 l = x * y;
uint256 m = mulmod (x, y, uint256 (-1));
uint256 m = mulmod(x, y, uint256(-1));
uint256 h = m - l;
if (m < l) h -= 1;

uint256 ss = 256 - s;

require (h >> s == 0);
require(h >> s == 0);
result = (h << ss) | (l >> s);
if (l << ss > 0) {
require (result < uint256 (-1));
require(result < uint256(-1));
result += 1;
}
}

/**
* Calculate sqrt (x) * 2^128 rounding up. Throw on overflow.
*/
function sqrt (uint256 x)
internal pure returns (uint256 result) {
function sqrt(uint256 x) internal pure returns (uint256 result) {
if (x == 0) return 0;
else {
uint256 s = 128;

if (x < 2**128) { x <<= 128; s -= 64; }
if (x < 2**192) { x <<= 64; s -= 32; }
if (x < 2**224) { x <<= 32; s -= 16; }
if (x < 2**240) { x <<= 16; s -= 8; }
if (x < 2**248) { x <<= 8; s -= 4; }
if (x < 2**252) { x <<= 4; s -= 2; }
if (x < 2**254) { x <<= 2; s -= 1; }
if (x < 2**128) {
x <<= 128;
s -= 64;
}
if (x < 2**192) {
x <<= 64;
s -= 32;
}
if (x < 2**224) {
x <<= 32;
s -= 16;
}
if (x < 2**240) {
x <<= 16;
s -= 8;
}
if (x < 2**248) {
x <<= 8;
s -= 4;
}
if (x < 2**252) {
x <<= 4;
s -= 2;
}
if (x < 2**254) {
x <<= 2;
s -= 1;
}

result = 2**127;
result = x / result + result >> 1;
result = x / result + result >> 1;
result = x / result + result >> 1;
result = x / result + result >> 1;
result = x / result + result >> 1;
result = x / result + result >> 1;
result = x / result + result >> 1; // 7 iterations should be enough
result = (x / result + result) >> 1;
result = (x / result + result) >> 1;
result = (x / result + result) >> 1;
result = (x / result + result) >> 1;
result = (x / result + result) >> 1;
result = (x / result + result) >> 1;
result = (x / result + result) >> 1; // 7 iterations should be enough

if (result * result < x) result = x / result + 1;

require (result <= uint256 (-1) >> s);
require(result <= uint256(-1) >> s);
result <<= s;
}
}
Expand Down
Loading

0 comments on commit 27ea16b

Please sign in to comment.