Skip to content

Commit 7689aa8

Browse files
low gas balanceOf (#437)
* low gas balanceOf * inline the balanceOf calls * add a note on why we do staticcall * == to >= Co-authored-by: Noah Zinsmeister <noahwz@gmail.com>
1 parent 864efb5 commit 7689aa8

File tree

4 files changed

+63
-54
lines changed

4 files changed

+63
-54
lines changed

contracts/UniswapV3Pool.sol

+12-2
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,23 @@ contract UniswapV3Pool is IUniswapV3Pool, NoDelegateCall {
139139
}
140140

141141
/// @dev Get the pool's balance of token0
142+
/// @dev This function is gas optimized to avoid a redundant extcodesize check in addition to the returndatasize
143+
/// check
142144
function balance0() private view returns (uint256) {
143-
return IERC20Minimal(token0).balanceOf(address(this));
145+
(bool success, bytes memory data) =
146+
token0.staticcall(abi.encodeWithSelector(IERC20Minimal.balanceOf.selector, address(this)));
147+
require(success && data.length >= 32);
148+
return abi.decode(data, (uint256));
144149
}
145150

146151
/// @dev Get the pool's balance of token1
152+
/// @dev This function is gas optimized to avoid a redundant extcodesize check in addition to the returndatasize
153+
/// check
147154
function balance1() private view returns (uint256) {
148-
return IERC20Minimal(token1).balanceOf(address(this));
155+
(bool success, bytes memory data) =
156+
token1.staticcall(abi.encodeWithSelector(IERC20Minimal.balanceOf.selector, address(this)));
157+
require(success && data.length >= 32);
158+
return abi.decode(data, (uint256));
149159
}
150160

151161
/// @inheritdoc IUniswapV3PoolDerivedState

test/UniswapV3Router.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
} from './shared/utilities'
2020
import { TestUniswapV3Router } from '../typechain/TestUniswapV3Router'
2121
import { TestUniswapV3Callee } from '../typechain/TestUniswapV3Callee'
22-
import { Test } from 'mocha'
2322

2423
const feeAmount = FeeAmount.MEDIUM
2524
const tickSpacing = TICK_SPACINGS[feeAmount]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`UniswapV3Factory #createPool gas 1`] = `4480765`;
3+
exports[`UniswapV3Factory #createPool gas 1`] = `4515505`;
44

5-
exports[`UniswapV3Factory factory bytecode size 1`] = `24172`;
5+
exports[`UniswapV3Factory factory bytecode size 1`] = `24345`;
66

7-
exports[`UniswapV3Factory pool bytecode size 1`] = `21749`;
7+
exports[`UniswapV3Factory pool bytecode size 1`] = `21922`;

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

+48-48
Original file line numberDiff line numberDiff line change
@@ -24,59 +24,59 @@ exports[`UniswapV3Pool gas tests fee is off #burn below current price entire pos
2424

2525
exports[`UniswapV3Pool gas tests fee is off #burn below current price partial position burn 1`] = `92946`;
2626

27-
exports[`UniswapV3Pool gas tests fee is off #collect close to worst case 1`] = `40604`;
27+
exports[`UniswapV3Pool gas tests fee is off #collect close to worst case 1`] = `40616`;
2828

2929
exports[`UniswapV3Pool gas tests fee is off #increaseObservationCardinalityNext grow by 1 slot 1`] = `54798`;
3030

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@@ -102,56 +102,56 @@ exports[`UniswapV3Pool gas tests fee is on #burn below current price entire posi
102102

103103
exports[`UniswapV3Pool gas tests fee is on #burn below current price partial position burn 1`] = `92946`;
104104

105-
exports[`UniswapV3Pool gas tests fee is on #collect close to worst case 1`] = `40604`;
105+
exports[`UniswapV3Pool gas tests fee is on #collect close to worst case 1`] = `40616`;
106106

107107
exports[`UniswapV3Pool gas tests fee is on #increaseObservationCardinalityNext grow by 1 slot 1`] = `54798`;
108108

109109
exports[`UniswapV3Pool gas tests fee is on #increaseObservationCardinalityNext no op 1`] = `28277`;
110110

111-
exports[`UniswapV3Pool gas tests fee is on #mint above current price add to position after some time passes 1`] = `108472`;
111+
exports[`UniswapV3Pool gas tests fee is on #mint above current price add to position after some time passes 1`] = `108096`;
112112

113-
exports[`UniswapV3Pool gas tests fee is on #mint above current price add to position existing 1`] = `108472`;
113+
exports[`UniswapV3Pool gas tests fee is on #mint above current price add to position existing 1`] = `108096`;
114114

115-
exports[`UniswapV3Pool gas tests fee is on #mint above current price new position mint first in range 1`] = `176854`;
115+
exports[`UniswapV3Pool gas tests fee is on #mint above current price new position mint first in range 1`] = `176478`;
116116

117-
exports[`UniswapV3Pool gas tests fee is on #mint above current price second position in same range 1`] = `123472`;
117+
exports[`UniswapV3Pool gas tests fee is on #mint above current price second position in same range 1`] = `123096`;
118118

119-
exports[`UniswapV3Pool gas tests fee is on #mint around current price add to position after some time passes 1`] = `160546`;
119+
exports[`UniswapV3Pool gas tests fee is on #mint around current price add to position after some time passes 1`] = `159795`;
120120

121-
exports[`UniswapV3Pool gas tests fee is on #mint around current price add to position existing 1`] = `149738`;
121+
exports[`UniswapV3Pool gas tests fee is on #mint around current price add to position existing 1`] = `148987`;
122122

123-
exports[`UniswapV3Pool gas tests fee is on #mint around current price new position mint first in range 1`] = `309361`;
123+
exports[`UniswapV3Pool gas tests fee is on #mint around current price new position mint first in range 1`] = `308610`;
124124

125-
exports[`UniswapV3Pool gas tests fee is on #mint around current price second position in same range 1`] = `164738`;
125+
exports[`UniswapV3Pool gas tests fee is on #mint around current price second position in same range 1`] = `163987`;
126126

127-
exports[`UniswapV3Pool gas tests fee is on #mint below current price add to position after some time passes 1`] = `109019`;
127+
exports[`UniswapV3Pool gas tests fee is on #mint below current price add to position after some time passes 1`] = `108641`;
128128

129-
exports[`UniswapV3Pool gas tests fee is on #mint below current price add to position existing 1`] = `109019`;
129+
exports[`UniswapV3Pool gas tests fee is on #mint below current price add to position existing 1`] = `108641`;
130130

131-
exports[`UniswapV3Pool gas tests fee is on #mint below current price new position mint first in range 1`] = `280667`;
131+
exports[`UniswapV3Pool gas tests fee is on #mint below current price new position mint first in range 1`] = `280289`;
132132

133-
exports[`UniswapV3Pool gas tests fee is on #mint below current price second position in same range 1`] = `124019`;
133+
exports[`UniswapV3Pool gas tests fee is on #mint below current price second position in same range 1`] = `123641`;
134134

135135
exports[`UniswapV3Pool gas tests fee is on #poke best case 1`] = `42584`;
136136

137-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block moves tick, no initialized crossings 1`] = `125028`;
137+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block moves tick, no initialized crossings 1`] = `124662`;
138138

139-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block with no tick movement 1`] = `110849`;
139+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block with no tick movement 1`] = `110482`;
140140

141-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap crossing a single initialized tick 1`] = `127539`;
141+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap crossing a single initialized tick 1`] = `127173`;
142142

143-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap crossing several initialized ticks 1`] = `143651`;
143+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap crossing several initialized ticks 1`] = `143285`;
144144

145-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap, no initialized crossings 1`] = `137427`;
145+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 first swap in block, large swap, no initialized crossings 1`] = `137061`;
146146

147-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 large swap crossing several initialized ticks after some time passes (seconds outside is set) 1`] = `158651`;
147+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 large swap crossing several initialized ticks after some time passes (seconds outside is set) 1`] = `158285`;
148148

149-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 large swap crossing several initialized ticks second time after some time passes 1`] = `218651`;
149+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 large swap crossing several initialized ticks second time after some time passes 1`] = `218285`;
150150

151-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block moves tick, no initialized crossings 1`] = `125028`;
151+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block moves tick, no initialized crossings 1`] = `124662`;
152152

153-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block with no tick movement 1`] = `110960`;
153+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block with no tick movement 1`] = `110593`;
154154

155-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block, large swap crossing a single initialized tick 1`] = `115001`;
155+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block, large swap crossing a single initialized tick 1`] = `114634`;
156156

157-
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block, large swap crossing several initialized ticks 1`] = `131092`;
157+
exports[`UniswapV3Pool gas tests fee is on #swapExact0For1 second swap in block, large swap crossing several initialized ticks 1`] = `130725`;

0 commit comments

Comments
 (0)