@@ -6,151 +6,83 @@ async function fixture() {
66 const [ receiver , other ] = await ethers . getSigners ( ) ;
77
88 const mock = await ethers . deployContract ( '$SimulateCall' ) ;
9- const computeSimulatorAddress = ( salt = ethers . ZeroHash ) =>
10- ethers . getCreate2Address (
11- mock . target ,
12- salt ,
13- ethers . keccak256 (
14- ethers . concat ( [
15- '0x602e5f8160095f39f3' ,
16- '0x60133611600a575f5ffd5b6014360360145f375f5f601436035f345f3560601c5af13d5f5f3e3d533d6001015ffd' ,
17- ] ) ,
18- ) ,
19- ) ;
9+ const simulator = ethers . getCreate2Address (
10+ mock . target ,
11+ ethers . ZeroHash ,
12+ ethers . keccak256 (
13+ ethers . concat ( [
14+ '0x60315f8160095f39f3' ,
15+ '0x60333611600a575f5ffd5b6034360360345f375f5f603436035f6014355f3560601c5af13d5f5f3e5f3d91602f57f35bfd' ,
16+ ] ) ,
17+ ) ,
18+ ) ;
2019
2120 const target = await ethers . deployContract ( '$CallReceiverMock' ) ;
2221
23- return { mock, target, receiver, other, computeSimulatorAddress } ;
22+ return { mock, target, receiver, other, simulator } ;
2423}
2524
2625describe ( 'SimulateCall' , function ( ) {
2726 beforeEach ( async function ( ) {
2827 Object . assign ( this , await loadFixture ( fixture ) ) ;
2928 } ) ;
3029
31- describe ( 'default (zero) salt' , function ( ) {
32- beforeEach ( async function ( ) {
33- this . simulator = await this . computeSimulatorAddress ( ) ;
34- } ) ;
35-
36- it ( 'automatic simulator deployment' , async function ( ) {
37- await expect ( ethers . provider . getCode ( this . simulator ) ) . to . eventually . equal ( '0x' ) ;
38-
39- // First call performs deployment
40- await expect ( this . mock . $getSimulator ( ) ) . to . emit ( this . mock , 'return$getSimulator' ) . withArgs ( this . simulator ) ;
30+ it ( 'automatic simulator deployment' , async function ( ) {
31+ await expect ( ethers . provider . getCode ( this . simulator ) ) . to . eventually . equal ( '0x' ) ;
4132
42- await expect ( ethers . provider . getCode ( this . simulator ) ) . to . eventually . not . equal ( '0x' ) ;
33+ // First call performs deployment
34+ await expect ( this . mock . $getSimulator ( ) ) . to . emit ( this . mock , 'return$getSimulator' ) . withArgs ( this . simulator ) ;
4335
44- // Following calls use the same simulator
45- await expect ( this . mock . $getSimulator ( ) ) . to . emit ( this . mock , 'return$getSimulator' ) . withArgs ( this . simulator ) ;
46- } ) ;
36+ await expect ( ethers . provider . getCode ( this . simulator ) ) . to . eventually . not . equal ( '0x' ) ;
4737
48- describe ( 'simulated call' , function ( ) {
49- it ( 'target success' , async function ( ) {
50- const tx = this . mock . $simulateCall (
51- ethers . Typed . address ( this . target ) ,
52- ethers . Typed . bytes ( this . target . interface . encodeFunctionData ( 'mockFunctionWithArgsReturn' , [ 10 , 20 ] ) ) ,
53- ) ;
54- await expect ( tx )
55- . to . emit ( this . mock , 'return$simulateCall_address_bytes' )
56- . withArgs ( true , ethers . AbiCoder . defaultAbiCoder ( ) . encode ( [ 'uint256' , 'uint256' ] , [ 10 , 20 ] ) )
57- . to . not . emit ( this . target , 'MockFunctionCalledWithArgs' ) ;
58- } ) ;
59-
60- it ( 'target success (with value)' , async function ( ) {
61- const value = 42n ;
62-
63- // fund the mock
64- await this . other . sendTransaction ( { to : this . mock . target , value } ) ;
65-
66- // perform simulated call
67- const tx = this . mock . $simulateCall (
68- ethers . Typed . address ( this . receiver ) ,
69- ethers . Typed . uint256 ( value ) ,
70- ethers . Typed . bytes ( '0x' ) ,
71- ) ;
72-
73- await expect ( tx ) . to . changeEtherBalances ( [ this . mock , this . simulator , this . receiver ] , [ 0n , 0n , 0n ] ) ;
74- await expect ( tx ) . to . emit ( this . mock , 'return$simulateCall_address_uint256_bytes' ) . withArgs ( true , '0x' ) ;
75- } ) ;
76-
77- it ( 'target revert' , async function ( ) {
78- const tx = this . mock . $simulateCall (
79- ethers . Typed . address ( this . target ) ,
80- ethers . Typed . bytes ( this . target . interface . encodeFunctionData ( 'mockFunctionRevertsReason' , [ ] ) ) ,
81- ) ;
82-
83- await expect ( tx )
84- . to . emit ( this . mock , 'return$simulateCall_address_bytes' )
85- . withArgs ( false , this . target . interface . encodeErrorResult ( 'Error' , [ 'CallReceiverMock: reverting' ] ) ) ;
86- } ) ;
87- } ) ;
38+ // Following calls use the same simulator
39+ await expect ( this . mock . $getSimulator ( ) ) . to . emit ( this . mock , 'return$getSimulator' ) . withArgs ( this . simulator ) ;
8840 } ) ;
8941
90- describe ( 'random salt' , function ( ) {
91- beforeEach ( async function ( ) {
92- this . salt = ethers . hexlify ( ethers . randomBytes ( 32 ) ) ;
93- this . simulator = await this . computeSimulatorAddress ( this . salt ) ;
42+ describe ( 'simulated call' , function ( ) {
43+ it ( 'target success' , async function ( ) {
44+ const txPromise = this . mock . $simulateCall (
45+ ethers . Typed . address ( this . target ) ,
46+ ethers . Typed . bytes ( this . target . interface . encodeFunctionData ( 'mockFunctionWithArgsReturn' , [ 10 , 20 ] ) ) ,
47+ ) ;
48+
49+ await expect ( txPromise ) . to . changeEtherBalances ( [ this . mock , this . simulator , this . target ] , [ 0n , 0n , 0n ] ) ;
50+ await expect ( txPromise )
51+ . to . emit ( this . mock , 'return$simulateCall_address_bytes' )
52+ . withArgs ( true , ethers . AbiCoder . defaultAbiCoder ( ) . encode ( [ 'uint256' , 'uint256' ] , [ 10 , 20 ] ) )
53+ . to . not . emit ( this . target , 'MockFunctionCalledWithArgs' ) ;
9454 } ) ;
9555
96- it ( 'automatic simulator deployment ' , async function ( ) {
97- await expect ( ethers . provider . getCode ( this . simulator ) ) . to . eventually . equal ( '0x' ) ;
56+ it ( 'target success (with value) ' , async function ( ) {
57+ const value = 42n ;
9858
99- // First call performs deployment
100- await expect ( this . mock . $getSimulator ( ethers . Typed . bytes32 ( this . salt ) ) )
101- . to . emit ( this . mock , 'return$getSimulator_bytes32' )
102- . withArgs ( this . simulator ) ;
59+ // fund the mock
60+ await this . other . sendTransaction ( { to : this . mock . target , value } ) ;
10361
104- await expect ( ethers . provider . getCode ( this . simulator ) ) . to . eventually . not . equal ( '0x' ) ;
62+ // perform simulated call
63+ const txPromise = this . mock . $simulateCall (
64+ ethers . Typed . address ( this . target ) ,
65+ ethers . Typed . uint256 ( value ) ,
66+ ethers . Typed . bytes ( this . target . interface . encodeFunctionData ( 'mockFunctionExtra' ) ) ,
67+ ) ;
10568
106- // Following calls use the same simulator
107- await expect ( this . mock . $getSimulator ( ethers . Typed . bytes32 ( this . salt ) ) )
108- . to . emit ( this . mock , 'return$getSimulator_bytes32' )
109- . withArgs ( this . simulator ) ;
69+ await expect ( txPromise ) . to . changeEtherBalances ( [ this . mock , this . simulator , this . target ] , [ 0n , 0n , 0n ] ) ;
70+ await expect ( txPromise )
71+ . to . emit ( this . mock , 'return$simulateCall_address_uint256_bytes' )
72+ . withArgs ( true , ethers . AbiCoder . defaultAbiCoder ( ) . encode ( [ 'address' , 'uint256' ] , [ this . mock . target , value ] ) )
73+ . to . not . emit ( this . target , 'MockFunctionCalledExtra' ) ;
11074 } ) ;
11175
112- describe ( 'simulated call' , function ( ) {
113- it ( 'target success' , async function ( ) {
114- const tx = this . mock . $simulateCall (
115- ethers . Typed . address ( this . target ) ,
116- ethers . Typed . bytes ( this . target . interface . encodeFunctionData ( 'mockFunctionWithArgsReturn' , [ 10 , 20 ] ) ) ,
117- ethers . Typed . bytes32 ( this . salt ) ,
118- ) ;
119- await expect ( tx )
120- . to . emit ( this . mock , 'return$simulateCall_address_bytes_bytes32' )
121- . withArgs ( true , ethers . AbiCoder . defaultAbiCoder ( ) . encode ( [ 'uint256' , 'uint256' ] , [ 10 , 20 ] ) )
122- . to . not . emit ( this . target , 'MockFunctionCalledWithArgs' ) ;
123- } ) ;
124-
125- it ( 'target success (with value)' , async function ( ) {
126- const value = 42n ;
127-
128- // fund the mock
129- await this . other . sendTransaction ( { to : this . mock . target , value } ) ;
130-
131- // perform simulated call
132- const tx = this . mock . $simulateCall (
133- ethers . Typed . address ( this . receiver ) ,
134- ethers . Typed . uint256 ( value ) ,
135- ethers . Typed . bytes ( '0x' ) ,
136- ethers . Typed . bytes32 ( this . salt ) ,
137- ) ;
138-
139- await expect ( tx ) . to . changeEtherBalances ( [ this . mock , this . simulator , this . receiver ] , [ 0n , 0n , 0n ] ) ;
140- await expect ( tx ) . to . emit ( this . mock , 'return$simulateCall_address_uint256_bytes_bytes32' ) . withArgs ( true , '0x' ) ;
141- } ) ;
142-
143- it ( 'target revert' , async function ( ) {
144- const tx = this . mock . $simulateCall (
145- ethers . Typed . address ( this . target ) ,
146- ethers . Typed . bytes ( this . target . interface . encodeFunctionData ( 'mockFunctionRevertsReason' , [ ] ) ) ,
147- ethers . Typed . bytes32 ( this . salt ) ,
148- ) ;
149-
150- await expect ( tx )
151- . to . emit ( this . mock , 'return$simulateCall_address_bytes_bytes32' )
152- . withArgs ( false , this . target . interface . encodeErrorResult ( 'Error' , [ 'CallReceiverMock: reverting' ] ) ) ;
153- } ) ;
76+ it ( 'target revert' , async function ( ) {
77+ const txPromise = this . mock . $simulateCall (
78+ ethers . Typed . address ( this . target ) ,
79+ ethers . Typed . bytes ( this . target . interface . encodeFunctionData ( 'mockFunctionRevertsReason' ) ) ,
80+ ) ;
81+
82+ await expect ( txPromise ) . to . changeEtherBalances ( [ this . mock , this . simulator , this . target ] , [ 0n , 0n , 0n ] ) ;
83+ await expect ( txPromise )
84+ . to . emit ( this . mock , 'return$simulateCall_address_bytes' )
85+ . withArgs ( false , this . target . interface . encodeErrorResult ( 'Error' , [ 'CallReceiverMock: reverting' ] ) ) ;
15486 } ) ;
15587 } ) ;
15688} ) ;
0 commit comments