@@ -5,6 +5,8 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
5
5
const value = ethers . parseEther ( '1' ) ;
6
6
const returnValue1 = ethers . id ( 'hello' ) ;
7
7
const returnValue2 = ethers . id ( 'world' ) ;
8
+ const storageSlot = ethers . id ( 'location' ) ;
9
+ const storageValue = ethers . id ( 'data' ) ;
8
10
9
11
async function fixture ( ) {
10
12
const [ account ] = await ethers . getSigners ( ) ;
@@ -23,65 +25,45 @@ describe('LowLevelCall', function () {
23
25
describe ( 'call' , function ( ) {
24
26
describe ( 'without any return' , function ( ) {
25
27
it ( 'calls the requested function and returns true' , async function ( ) {
26
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
27
-
28
28
await expect ( this . mock . $callNoReturn ( this . target , this . target . interface . encodeFunctionData ( 'mockFunction' ) ) )
29
29
. to . emit ( this . target , 'MockFunctionCalled' )
30
30
. to . emit ( this . mock , 'return$callNoReturn_address_bytes' )
31
31
. withArgs ( true ) ;
32
-
33
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 1 ) ;
34
32
} ) ;
35
33
36
34
it ( 'calls the requested function with value and returns true' , async function ( ) {
37
35
await this . account . sendTransaction ( { to : this . mock , value } ) ;
38
36
39
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
40
-
41
37
const tx = this . mock . $callNoReturn (
42
38
this . target ,
43
39
ethers . Typed . uint256 ( value ) ,
44
40
this . target . interface . encodeFunctionData ( 'mockFunction' ) ,
45
41
) ;
46
42
await expect ( tx ) . to . changeEtherBalances ( [ this . mock , this . target ] , [ - value , value ] ) ;
47
43
await expect ( tx ) . to . emit ( this . mock , 'return$callNoReturn_address_uint256_bytes' ) . withArgs ( true ) ;
48
-
49
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 1 ) ;
50
44
} ) ;
51
45
52
46
it ( "calls the requested function and returns false if the caller doesn't have enough balance" , async function ( ) {
53
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
54
-
55
47
const tx = this . mock . $callNoReturn (
56
48
this . target ,
57
49
ethers . Typed . uint256 ( value ) ,
58
50
this . target . interface . encodeFunctionData ( 'mockFunction' ) ,
59
51
) ;
60
52
await expect ( tx ) . to . changeEtherBalances ( [ this . mock , this . target ] , [ 0n , 0n ] ) ;
61
53
await expect ( tx ) . to . emit ( this . mock , 'return$callNoReturn_address_uint256_bytes' ) . withArgs ( false ) ;
62
-
63
- // reverted call do not count
64
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
65
54
} ) ;
66
55
67
56
it ( 'calls the requested function and returns false if the subcall reverts' , async function ( ) {
68
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
69
-
70
57
const tx = this . mock . $callNoReturn (
71
58
this . target ,
72
59
this . target . interface . encodeFunctionData ( 'mockFunctionRevertsNoReason' ) ,
73
60
) ;
74
61
await expect ( tx ) . to . emit ( this . mock , 'return$callNoReturn_address_bytes' ) . withArgs ( false ) ;
75
-
76
- // reverted call do not count
77
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
78
62
} ) ;
79
63
} ) ;
80
64
81
65
describe ( 'with 64 bytes return in the scratch space' , function ( ) {
82
66
it ( 'calls the requested function and returns true' , async function ( ) {
83
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
84
-
85
67
await expect (
86
68
this . mock . $callReturn64Bytes (
87
69
this . target ,
@@ -92,15 +74,11 @@ describe('LowLevelCall', function () {
92
74
. withArgs ( returnValue1 , returnValue2 )
93
75
. to . emit ( this . mock , 'return$callReturn64Bytes_address_bytes' )
94
76
. withArgs ( true , returnValue1 , returnValue2 ) ;
95
-
96
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 1 ) ;
97
77
} ) ;
98
78
99
79
it ( 'calls the requested function with value and returns true' , async function ( ) {
100
80
await this . account . sendTransaction ( { to : this . mock , value } ) ;
101
81
102
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
103
-
104
82
const tx = this . mock . $callReturn64Bytes (
105
83
this . target ,
106
84
ethers . Typed . uint256 ( value ) ,
@@ -110,13 +88,9 @@ describe('LowLevelCall', function () {
110
88
await expect ( tx )
111
89
. to . emit ( this . mock , 'return$callReturn64Bytes_address_uint256_bytes' )
112
90
. withArgs ( true , returnValue1 , returnValue2 ) ;
113
-
114
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 1 ) ;
115
91
} ) ;
116
92
117
93
it ( "calls the requested function and returns false if the caller doesn't have enough balance" , async function ( ) {
118
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
119
-
120
94
const tx = this . mock . $callReturn64Bytes (
121
95
this . target ,
122
96
ethers . Typed . uint256 ( value ) ,
@@ -126,24 +100,16 @@ describe('LowLevelCall', function () {
126
100
await expect ( tx )
127
101
. to . emit ( this . mock , 'return$callReturn64Bytes_address_uint256_bytes' )
128
102
. withArgs ( false , ethers . ZeroHash , ethers . ZeroHash ) ;
129
-
130
- // reverted call do not count
131
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
132
103
} ) ;
133
104
134
105
it ( 'calls the requested function and returns false if the subcall reverts' , async function ( ) {
135
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
136
-
137
106
const tx = this . mock . $callReturn64Bytes (
138
107
this . target ,
139
108
this . target . interface . encodeFunctionData ( 'mockFunctionRevertsNoReason' ) ,
140
109
) ;
141
110
await expect ( tx )
142
111
. to . emit ( this . mock , 'return$callReturn64Bytes_address_bytes' )
143
112
. withArgs ( false , ethers . ZeroHash , ethers . ZeroHash ) ;
144
-
145
- // reverted call do not count
146
- await expect ( this . target . nbCalls ( ) ) . to . eventually . equal ( 0 ) ;
147
113
} ) ;
148
114
} ) ;
149
115
} ) ;
@@ -190,20 +156,22 @@ describe('LowLevelCall', function () {
190
156
describe ( 'delegate' , function ( ) {
191
157
describe ( 'without any return' , function ( ) {
192
158
it ( 'calls the requested function and returns true' , async function ( ) {
193
- await expect ( ethers . provider . getStorage ( this . mock , 0 ) ) . to . eventually . equal ( ethers . toBeHex ( 0 , 32 ) ) ;
159
+ await expect ( ethers . provider . getStorage ( this . mock , storageSlot ) ) . to . eventually . equal ( ethers . ZeroHash ) ;
194
160
195
161
await expect (
196
- this . mock . $delegatecallNoReturn ( this . target , this . target . interface . encodeFunctionData ( 'mockFunction' ) ) ,
162
+ this . mock . $delegatecallNoReturn (
163
+ this . target ,
164
+ this . target . interface . encodeFunctionData ( 'mockFunctionWritesStorage' , [ storageSlot , storageValue ] ) ,
165
+ ) ,
197
166
)
198
167
. to . emit ( this . mock , 'return$delegatecallNoReturn' )
199
168
. withArgs ( true ) ;
200
169
201
- await expect ( ethers . provider . getStorage ( this . mock , 0 ) ) . to . eventually . equal ( ethers . toBeHex ( 1 , 32 ) ) ;
170
+ // check delegate call set storage correctly
171
+ await expect ( ethers . provider . getStorage ( this . mock , storageSlot ) ) . to . eventually . equal ( storageValue ) ;
202
172
} ) ;
203
173
204
174
it ( 'calls the requested function and returns false if the subcall reverts' , async function ( ) {
205
- await expect ( ethers . provider . getStorage ( this . mock , 0 ) ) . to . eventually . equal ( ethers . toBeHex ( 0 , 32 ) ) ;
206
-
207
175
await expect (
208
176
this . mock . $delegatecallNoReturn (
209
177
this . target ,
@@ -212,31 +180,32 @@ describe('LowLevelCall', function () {
212
180
)
213
181
. to . emit ( this . mock , 'return$delegatecallNoReturn' )
214
182
. withArgs ( false ) ;
215
-
216
- // reverted call do not count
217
- await expect ( ethers . provider . getStorage ( this . mock , 0 ) ) . to . eventually . equal ( ethers . toBeHex ( 0 , 32 ) ) ;
218
183
} ) ;
219
184
} ) ;
220
185
221
186
describe ( 'with 64 bytes return in the scratch space' , function ( ) {
222
187
it ( 'calls the requested function and returns true' , async function ( ) {
223
- await expect ( ethers . provider . getStorage ( this . mock , 0 ) ) . to . eventually . equal ( ethers . toBeHex ( 0 , 32 ) ) ;
188
+ await expect ( ethers . provider . getStorage ( this . mock , storageSlot ) ) . to . eventually . equal ( ethers . ZeroHash ) ;
224
189
225
190
await expect (
226
191
this . mock . $delegatecallReturn64Bytes (
227
192
this . target ,
228
- this . target . interface . encodeFunctionData ( 'mockFunctionWithArgsReturn' , [ returnValue1 , returnValue2 ] ) ,
193
+ this . target . interface . encodeFunctionData ( 'mockFunctionWithArgsReturnWritesStorage' , [
194
+ storageSlot ,
195
+ storageValue ,
196
+ returnValue1 ,
197
+ returnValue2 ,
198
+ ] ) ,
229
199
) ,
230
200
)
231
201
. to . emit ( this . mock , 'return$delegatecallReturn64Bytes' )
232
202
. withArgs ( true , returnValue1 , returnValue2 ) ;
233
203
234
- await expect ( ethers . provider . getStorage ( this . mock , 0 ) ) . to . eventually . equal ( ethers . toBeHex ( 1 , 32 ) ) ;
204
+ // check delegate call set storage correctly
205
+ await expect ( ethers . provider . getStorage ( this . mock , storageSlot ) ) . to . eventually . equal ( storageValue ) ;
235
206
} ) ;
236
207
237
208
it ( 'calls the requested function and returns false if the subcall reverts' , async function ( ) {
238
- await expect ( ethers . provider . getStorage ( this . mock , 0 ) ) . to . eventually . equal ( ethers . toBeHex ( 0 , 32 ) ) ;
239
-
240
209
await expect (
241
210
this . mock . $delegatecallReturn64Bytes (
242
211
this . target ,
@@ -245,9 +214,6 @@ describe('LowLevelCall', function () {
245
214
)
246
215
. to . emit ( this . mock , 'return$delegatecallReturn64Bytes' )
247
216
. withArgs ( false , ethers . ZeroHash , ethers . ZeroHash ) ;
248
-
249
- // reverted call do not count
250
- await expect ( ethers . provider . getStorage ( this . mock , 0 ) ) . to . eventually . equal ( ethers . toBeHex ( 0 , 32 ) ) ;
251
217
} ) ;
252
218
} ) ;
253
219
} ) ;
0 commit comments