Skip to content

Commit 5b48519

Browse files
authored
rewrite the function getNextSqrtPriceFromAmount0RoundingUp for small gas optimization/readability (#388)
* rewrite the function for small gas optimization * remove abicoder v2 from mock time pair as well
1 parent 4d3036f commit 5b48519

File tree

4 files changed

+42
-39
lines changed

4 files changed

+42
-39
lines changed

contracts/libraries/SqrtPriceMath.sol

+15-12
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,27 @@ library SqrtPriceMath {
3535
if (amount == 0) return sqrtPX96;
3636
uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
3737

38-
uint256 product = amount * sqrtPX96;
39-
if (product / amount == sqrtPX96) {
40-
if (add) {
38+
if (add) {
39+
uint256 product;
40+
if ((product = amount * sqrtPX96) / amount == sqrtPX96) {
4141
uint256 denominator = numerator1 + product;
4242
if (denominator >= numerator1)
4343
// always fits in 160 bits
4444
return uint160(FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator));
45-
} else {
46-
uint256 denominator = numerator1 - product;
47-
if (denominator <= numerator1 && denominator != 0)
48-
return FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator).toUint160();
4945
}
50-
}
51-
52-
uint256 denominator1 = add ? (numerator1 / sqrtPX96).add(amount) : (numerator1 / sqrtPX96).sub(amount);
53-
require(denominator1 != 0);
5446

55-
return UnsafeMath.divRoundingUp(numerator1, denominator1).toUint160();
47+
uint256 denominator1 = (numerator1 / sqrtPX96).add(amount);
48+
require(denominator1 != 0);
49+
50+
return UnsafeMath.divRoundingUp(numerator1, denominator1).toUint160();
51+
} else {
52+
uint256 product;
53+
// if the product overflows, we know the denominator underflows
54+
// in addition, we must check that the denominator does not underflow
55+
require((product = amount * sqrtPX96) / amount == sqrtPX96 && numerator1 > product);
56+
uint256 denominator = numerator1 - product;
57+
return FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator).toUint160();
58+
}
5659
}
5760

5861
/// @notice Get the next sqrt price given a delta of token1

test/__snapshots__/SqrtPriceMath.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ exports[`SqrtPriceMath #getNextSqrtPriceFromInput zeroForOne = false gas 1`] = `
1212

1313
exports[`SqrtPriceMath #getNextSqrtPriceFromInput zeroForOne = true gas 1`] = `747`;
1414

15-
exports[`SqrtPriceMath #getNextSqrtPriceFromOutput zeroForOne = false gas 1`] = `872`;
15+
exports[`SqrtPriceMath #getNextSqrtPriceFromOutput zeroForOne = false gas 1`] = `842`;
1616

1717
exports[`SqrtPriceMath #getNextSqrtPriceFromOutput zeroForOne = true gas 1`] = `641`;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`UniswapV3Factory #createPair gas 1`] = `4306397`;
3+
exports[`UniswapV3Factory #createPair gas 1`] = `4299974`;
44

5-
exports[`UniswapV3Factory factory bytecode size 1`] = `23249`;
5+
exports[`UniswapV3Factory factory bytecode size 1`] = `23217`;
66

7-
exports[`UniswapV3Factory pair bytecode size 1`] = `20880`;
7+
exports[`UniswapV3Factory pair bytecode size 1`] = `20848`;

test/__snapshots__/UniswapV3Pair.gas.spec.ts.snap

+23-23
Original file line numberDiff line numberDiff line change
@@ -30,53 +30,53 @@ exports[`UniswapV3Pair gas tests fee is off #increaseObservationCardinalityNext
3030

3131
exports[`UniswapV3Pair gas tests fee is off #increaseObservationCardinalityNext no op 1`] = `28299`;
3232

33-
exports[`UniswapV3Pair gas tests fee is off #mint above current price add to position after some time passes 1`] = `102498`;
33+
exports[`UniswapV3Pair gas tests fee is off #mint above current price add to position after some time passes 1`] = `102486`;
3434

35-
exports[`UniswapV3Pair gas tests fee is off #mint above current price add to position existing 1`] = `102498`;
35+
exports[`UniswapV3Pair gas tests fee is off #mint above current price add to position existing 1`] = `102486`;
3636

37-
exports[`UniswapV3Pair gas tests fee is off #mint above current price new position mint first in range 1`] = `170880`;
37+
exports[`UniswapV3Pair gas tests fee is off #mint above current price new position mint first in range 1`] = `170868`;
3838

39-
exports[`UniswapV3Pair gas tests fee is off #mint above current price second position in same range 1`] = `117498`;
39+
exports[`UniswapV3Pair gas tests fee is off #mint above current price second position in same range 1`] = `117486`;
4040

41-
exports[`UniswapV3Pair gas tests fee is off #mint around current price add to position after some time passes 1`] = `148721`;
41+
exports[`UniswapV3Pair gas tests fee is off #mint around current price add to position after some time passes 1`] = `148709`;
4242

43-
exports[`UniswapV3Pair gas tests fee is off #mint around current price add to position existing 1`] = `137895`;
43+
exports[`UniswapV3Pair gas tests fee is off #mint around current price add to position existing 1`] = `137883`;
4444

45-
exports[`UniswapV3Pair gas tests fee is off #mint around current price new position mint first in range 1`] = `297536`;
45+
exports[`UniswapV3Pair gas tests fee is off #mint around current price new position mint first in range 1`] = `297524`;
4646

47-
exports[`UniswapV3Pair gas tests fee is off #mint around current price second position in same range 1`] = `152895`;
47+
exports[`UniswapV3Pair gas tests fee is off #mint around current price second position in same range 1`] = `152883`;
4848

49-
exports[`UniswapV3Pair gas tests fee is off #mint below current price add to position after some time passes 1`] = `103027`;
49+
exports[`UniswapV3Pair gas tests fee is off #mint below current price add to position after some time passes 1`] = `103015`;
5050

51-
exports[`UniswapV3Pair gas tests fee is off #mint below current price add to position existing 1`] = `103027`;
51+
exports[`UniswapV3Pair gas tests fee is off #mint below current price add to position existing 1`] = `103015`;
5252

53-
exports[`UniswapV3Pair gas tests fee is off #mint below current price new position mint first in range 1`] = `274675`;
53+
exports[`UniswapV3Pair gas tests fee is off #mint below current price new position mint first in range 1`] = `274663`;
5454

55-
exports[`UniswapV3Pair gas tests fee is off #mint below current price second position in same range 1`] = `118027`;
55+
exports[`UniswapV3Pair gas tests fee is off #mint below current price second position in same range 1`] = `118015`;
5656

5757
exports[`UniswapV3Pair gas tests fee is off #poke best case 1`] = `43339`;
5858

59-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 first swap in block moves tick, no initialized crossings 1`] = `101320`;
59+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 first swap in block moves tick, no initialized crossings 1`] = `101308`;
6060

61-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 first swap in block with no tick movement 1`] = `83974`;
61+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 first swap in block with no tick movement 1`] = `83962`;
6262

63-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 first swap in block, large swap crossing a single initialized tick 1`] = `103625`;
63+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 first swap in block, large swap crossing a single initialized tick 1`] = `103613`;
6464

65-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 first swap in block, large swap crossing several initialized ticks 1`] = `119242`;
65+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 first swap in block, large swap crossing several initialized ticks 1`] = `119230`;
6666

67-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 first swap in block, large swap, no initialized crossings 1`] = `113336`;
67+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 first swap in block, large swap, no initialized crossings 1`] = `113324`;
6868

69-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 large swap crossing several initialized ticks after some time passes (seconds outside is set) 1`] = `134242`;
69+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 large swap crossing several initialized ticks after some time passes (seconds outside is set) 1`] = `134230`;
7070

71-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 large swap crossing several initialized ticks second time after some time passes 1`] = `194242`;
71+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 large swap crossing several initialized ticks second time after some time passes 1`] = `194230`;
7272

73-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 second swap in block moves tick, no initialized crossings 1`] = `101320`;
73+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 second swap in block moves tick, no initialized crossings 1`] = `101308`;
7474

75-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 second swap in block with no tick movement 1`] = `84085`;
75+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 second swap in block with no tick movement 1`] = `84073`;
7676

77-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 second swap in block, large swap crossing a single initialized tick 1`] = `91199`;
77+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 second swap in block, large swap crossing a single initialized tick 1`] = `91187`;
7878

79-
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 second swap in block, large swap crossing several initialized ticks 1`] = `106795`;
79+
exports[`UniswapV3Pair gas tests fee is off #swapExact0For1 second swap in block, large swap crossing several initialized ticks 1`] = `106783`;
8080

8181
exports[`UniswapV3Pair gas tests fee is on #burn above current price burn entire position after some time passes 1`] = `55605`;
8282

0 commit comments

Comments
 (0)