@@ -124,8 +124,10 @@ impl InMemoryNode {
124
124
125
125
#[ cfg( test) ]
126
126
mod tests {
127
+ use alloy_dyn_abi:: { DynSolValue , FunctionExt , JsonAbiExt } ;
128
+ use alloy_json_abi:: { Function , Param } ;
129
+ use alloy_primitives:: { Address as AlloyAddress , U256 as AlloyU256 } ;
127
130
use anvil_zksync_config:: constants:: DEFAULT_ACCOUNT_BALANCE ;
128
- use ethers:: abi:: { short_signature, AbiEncode , HumanReadableParser , ParamType , Token } ;
129
131
use zksync_types:: {
130
132
transaction_request:: CallRequestBuilder , utils:: deployed_address_create, Address ,
131
133
K256PrivateKey , L2BlockNumber , Nonce , H160 , U256 ,
@@ -150,12 +152,16 @@ mod tests {
150
152
include_bytes ! ( "../deps/test-contracts/Secondary.json" ) ,
151
153
) ;
152
154
let secondary_deployed_address = deployed_address_create ( from_account, U256 :: zero ( ) ) ;
155
+ let alloy_secondary_address = AlloyAddress :: from ( secondary_deployed_address. 0 ) ;
156
+ let secondary_constructor_calldata =
157
+ DynSolValue :: Uint ( AlloyU256 :: from ( 2 ) , 256 ) . abi_encode ( ) ;
158
+
153
159
testing:: deploy_contract (
154
160
node,
155
161
H256 :: repeat_byte ( 0x1 ) ,
156
162
& private_key,
157
163
secondary_bytecode,
158
- Some ( ( U256 :: from ( 2 ) , ) . encode ( ) ) ,
164
+ Some ( secondary_constructor_calldata ) ,
159
165
Nonce ( 0 ) ,
160
166
)
161
167
. await ;
@@ -166,15 +172,19 @@ mod tests {
166
172
include_bytes ! ( "../deps/test-contracts/Primary.json" ) ,
167
173
) ;
168
174
let primary_deployed_address = deployed_address_create ( from_account, U256 :: one ( ) ) ;
175
+ let primary_constructor_calldata =
176
+ DynSolValue :: Address ( alloy_secondary_address) . abi_encode ( ) ;
177
+
169
178
testing:: deploy_contract (
170
179
node,
171
180
H256 :: repeat_byte ( 0x1 ) ,
172
181
& private_key,
173
182
primary_bytecode,
174
- Some ( ( secondary_deployed_address ) . encode ( ) ) ,
183
+ Some ( primary_constructor_calldata ) ,
175
184
Nonce ( 1 ) ,
176
185
)
177
186
. await ;
187
+
178
188
( primary_deployed_address, secondary_deployed_address)
179
189
}
180
190
@@ -184,9 +194,28 @@ mod tests {
184
194
185
195
let ( primary_deployed_address, secondary_deployed_address) =
186
196
deploy_test_contracts ( & node) . await ;
187
- // trace a call to the primary contract
188
- let func = HumanReadableParser :: parse_function ( "calculate(uint)" ) . unwrap ( ) ;
189
- let calldata = func. encode_input ( & [ Token :: Uint ( U256 :: from ( 42 ) ) ] ) . unwrap ( ) ;
197
+
198
+ let func = Function {
199
+ name : "calculate" . to_string ( ) ,
200
+ inputs : vec ! [ Param {
201
+ name: "value" . to_string( ) ,
202
+ ty: "uint256" . to_string( ) ,
203
+ components: vec![ ] ,
204
+ internal_type: None ,
205
+ } ] ,
206
+ outputs : vec ! [ Param {
207
+ name: "" . to_string( ) ,
208
+ ty: "uint256" . to_string( ) ,
209
+ components: vec![ ] ,
210
+ internal_type: None ,
211
+ } ] ,
212
+ state_mutability : alloy_json_abi:: StateMutability :: NonPayable ,
213
+ } ;
214
+
215
+ let calldata = func
216
+ . abi_encode_input ( & [ DynSolValue :: Uint ( AlloyU256 :: from ( 42 ) , 256 ) ] )
217
+ . expect ( "failed to encode function input" ) ;
218
+
190
219
let request = CallRequestBuilder :: default ( )
191
220
. to ( Some ( primary_deployed_address) )
192
221
. data ( calldata. clone ( ) . into ( ) )
@@ -203,9 +232,14 @@ mod tests {
203
232
assert ! ( trace. revert_reason. is_none( ) ) ;
204
233
205
234
// check that the call was successful
206
- let output =
207
- ethers:: abi:: decode ( & [ ParamType :: Uint ( 256 ) ] , trace. output . 0 . as_slice ( ) ) . unwrap ( ) ;
208
- assert_eq ! ( output[ 0 ] , Token :: Uint ( U256 :: from( 84 ) ) ) ;
235
+ let output = func
236
+ . abi_decode_output ( trace. output . 0 . as_slice ( ) , true )
237
+ . expect ( "failed to decode output" ) ;
238
+ assert_eq ! (
239
+ output[ 0 ] ,
240
+ DynSolValue :: Uint ( AlloyU256 :: from( 84 ) , 256 ) ,
241
+ "unexpected output"
242
+ ) ;
209
243
210
244
// find the call to primary contract in the trace
211
245
let contract_call = trace
@@ -226,7 +260,12 @@ mod tests {
226
260
let subcall = contract_call. calls . first ( ) . unwrap ( ) ;
227
261
assert_eq ! ( subcall. to, secondary_deployed_address) ;
228
262
assert_eq ! ( subcall. from, primary_deployed_address) ;
229
- assert_eq ! ( subcall. output, U256 :: from( 84 ) . encode( ) . into( ) ) ;
263
+ assert_eq ! (
264
+ subcall. output,
265
+ func. abi_encode_output( & [ DynSolValue :: Uint ( AlloyU256 :: from( 84 ) , 256 ) ] )
266
+ . expect( "failed to encode function output" )
267
+ . into( )
268
+ ) ;
230
269
}
231
270
232
271
#[ tokio:: test]
@@ -236,8 +275,22 @@ mod tests {
236
275
let ( primary_deployed_address, _) = deploy_test_contracts ( & node) . await ;
237
276
238
277
// trace a call to the primary contract
239
- let func = HumanReadableParser :: parse_function ( "calculate(uint)" ) . unwrap ( ) ;
240
- let calldata = func. encode_input ( & [ Token :: Uint ( U256 :: from ( 42 ) ) ] ) . unwrap ( ) ;
278
+ let func = Function {
279
+ name : "calculate" . to_string ( ) ,
280
+ inputs : vec ! [ Param {
281
+ name: "value" . to_string( ) ,
282
+ ty: "uint256" . to_string( ) ,
283
+ components: vec![ ] ,
284
+ internal_type: None ,
285
+ } ] ,
286
+ outputs : vec ! [ ] ,
287
+ state_mutability : alloy_json_abi:: StateMutability :: NonPayable ,
288
+ } ;
289
+
290
+ let calldata = func
291
+ . abi_encode_input ( & [ DynSolValue :: Uint ( AlloyU256 :: from ( 42 ) , 256 ) ] )
292
+ . expect ( "failed to encode function input" ) ;
293
+
241
294
let request = CallRequestBuilder :: default ( )
242
295
. to ( Some ( primary_deployed_address) )
243
296
. data ( calldata. into ( ) )
@@ -273,10 +326,17 @@ mod tests {
273
326
274
327
let ( primary_deployed_address, _) = deploy_test_contracts ( & node) . await ;
275
328
329
+ let func = Function {
330
+ name : "shouldRevert" . to_string ( ) ,
331
+ inputs : vec ! [ ] ,
332
+ outputs : vec ! [ ] ,
333
+ state_mutability : alloy_json_abi:: StateMutability :: NonPayable ,
334
+ } ;
335
+
276
336
// trace a call to the primary contract
277
337
let request = CallRequestBuilder :: default ( )
278
338
. to ( Some ( primary_deployed_address) )
279
- . data ( short_signature ( "shouldRevert()" , & [ ] ) . into ( ) )
339
+ . data ( func . selector ( ) . to_vec ( ) . into ( ) )
280
340
. gas ( 80_000_000 . into ( ) )
281
341
. build ( ) ;
282
342
let trace = node
0 commit comments