@@ -10,14 +10,17 @@ use ink_e2e::{
10
10
PolkadotConfig ,
11
11
Weight ,
12
12
} ;
13
- use std:: process:: {
14
- Command ,
15
- Stdio ,
13
+ use std:: {
14
+ error:: Error ,
15
+ process:: {
16
+ Command ,
17
+ Stdio ,
18
+ } ,
16
19
} ;
17
20
18
21
const DEFAULT_GAS : Weight = Weight :: from_parts ( 100_000_000_000 , 1024 * 1024 ) ;
19
22
const DEFAULT_STORAGE_DEPOSIT_LIMIT : u128 = 10_000_000_000_000 ;
20
- type E2EResult < T > = std :: result :: Result < T , Box < dyn std :: error :: Error > > ;
23
+ type E2EResult < T > = Result < T , Box < dyn Error > > ;
21
24
22
25
#[ ink_e2e:: test]
23
26
async fn solidity_calls_ink_works < Client : E2EBackend > (
@@ -42,7 +45,7 @@ async fn solidity_calls_ink_works<Client: E2EBackend>(
42
45
43
46
// deploy ink! flipper (RLP encoded)
44
47
client. api . map_account ( & signer) . await ;
45
- let res = client
48
+ let ink_addr = client
46
49
. exec_instantiate (
47
50
& signer,
48
51
client. contracts . load_code ( "flipper" ) ,
@@ -51,28 +54,23 @@ async fn solidity_calls_ink_works<Client: E2EBackend>(
51
54
DEFAULT_GAS . into ( ) ,
52
55
DEFAULT_STORAGE_DEPOSIT_LIMIT ,
53
56
)
54
- . await ?;
55
-
56
- let ink_addr = res. addr ;
57
+ . await ?
58
+ . addr ;
57
59
58
60
let get_selector = keccak_selector ( b"get" ) ;
59
61
let value: bool = call_ink ( & mut client, ink_addr, get_selector. clone ( ) ) . await ;
60
62
assert_eq ! ( value, false ) ;
61
63
62
- let sol_dir = "./sol" . to_string ( ) ;
63
-
64
- let mut sol_handler = SolidityHandler :: new ( sol_dir, client. url ( ) . to_string ( ) ) ;
64
+ let mut sol_handler = SolidityHandler :: new ( "./sol" . into ( ) , client. url ( ) . to_string ( ) ) ;
65
65
sol_handler. start_eth_rpc ( ) ?;
66
66
sol_handler. setup ( ) ?;
67
67
let sol_addr = sol_handler. deploy ( ink_addr) ?;
68
- let _ = sol_handler. call ( sol_addr. clone ( ) , "callFlip" ) ?;
69
68
70
- // check if flip worked
69
+ let _ = sol_handler . call ( sol_addr . clone ( ) , "callFlip" ) ? ;
71
70
let value: bool = call_ink ( & mut client, ink_addr, get_selector. clone ( ) ) . await ;
72
71
assert_eq ! ( value, true ) ;
73
72
74
73
let _ = sol_handler. call ( sol_addr. clone ( ) , "callFlip2" ) ?;
75
-
76
74
let value: bool = call_ink ( & mut client, ink_addr, get_selector) . await ;
77
75
assert_eq ! ( value, false ) ;
78
76
@@ -122,7 +120,7 @@ impl SolidityHandler {
122
120
}
123
121
}
124
122
125
- fn start_eth_rpc ( & mut self ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
123
+ fn start_eth_rpc ( & mut self ) -> Result < ( ) , Box < dyn Error > > {
126
124
let eth_rpc = Command :: new ( "eth-rpc" )
127
125
. arg ( "--dev" )
128
126
. arg ( "--node-rpc-url" )
@@ -132,7 +130,7 @@ impl SolidityHandler {
132
130
Ok ( ( ) )
133
131
}
134
132
135
- fn stop_eth_rpc ( & mut self ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
133
+ fn stop_eth_rpc ( & mut self ) -> Result < ( ) , Box < dyn Error > > {
136
134
if self . eth_rpc_process_id . is_none ( ) {
137
135
return Ok ( ( ) ) ;
138
136
}
@@ -143,7 +141,7 @@ impl SolidityHandler {
143
141
Ok ( ( ) )
144
142
}
145
143
146
- fn setup ( & mut self ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
144
+ fn setup ( & mut self ) -> Result < ( ) , Box < dyn Error > > {
147
145
let install_status = Command :: new ( "npm" )
148
146
. current_dir ( & self . sol_dir )
149
147
. arg ( "install" )
@@ -165,59 +163,64 @@ impl SolidityHandler {
165
163
Ok ( ( ) )
166
164
}
167
165
168
- fn deploy ( & self , ink_addr : H160 ) -> Result < String , Box < dyn std:: error:: Error > > {
169
- let deploy_process = Command :: new ( "npx" )
170
- . current_dir ( & self . sol_dir )
171
- . arg ( "hardhat" )
172
- . arg ( "run" )
173
- . arg ( "01-deploy.js" )
174
- . arg ( "--network" )
175
- . arg ( "localhost" )
176
- . arg ( "--no-compile" )
177
- . arg ( "--config" )
178
- . arg ( "hardhat.config.js" )
179
- . env ( "INK_ADDRESS" , format ! ( "{:?}" , ink_addr) )
180
- . stdout ( Stdio :: piped ( ) ) // capture stdout
181
- . stderr ( Stdio :: inherit ( ) ) // print stderr
182
- . spawn ( ) ?;
183
- let output = deploy_process. wait_with_output ( ) ?;
184
- assert ! ( output. status. success( ) , "solidity deployment failed" ) ;
185
-
186
- Ok ( String :: from_utf8 ( output. stdout ) ?
187
- . lines ( )
188
- . last ( )
189
- . ok_or ( "Failed to retrieve contract address" ) ?
190
- . trim ( )
191
- . to_string ( ) )
192
- }
193
-
194
- fn call (
166
+ fn run_hardhat_script (
195
167
& self ,
196
- sol_addr : String ,
197
- message : & str ,
198
- ) -> Result < Option < String > , Box < dyn std:: error:: Error > > {
199
- let call_process = Command :: new ( "npx" )
168
+ script : & str ,
169
+ env_vars : & [ ( & str , String ) ] ,
170
+ ) -> Result < Vec < u8 > , Box < dyn Error > > {
171
+ let mut command = Command :: new ( "npx" ) ;
172
+ command
200
173
. current_dir ( & self . sol_dir )
201
174
. arg ( "hardhat" )
202
175
. arg ( "run" )
203
- . arg ( "02-call-flip.js" )
176
+ . arg ( script )
204
177
. arg ( "--network" )
205
178
. arg ( "localhost" )
206
179
. arg ( "--no-compile" )
207
180
. arg ( "--config" )
208
181
. arg ( "hardhat.config.js" )
209
- . env ( "SOL_ADDRESS" , sol_addr)
210
- . env ( "MESSAGE" , message)
211
- . stdout ( Stdio :: piped ( ) )
212
- . stderr ( Stdio :: inherit ( ) )
213
- . spawn ( ) ?;
214
- let output = call_process. wait_with_output ( ) ?;
182
+ . stdout ( Stdio :: piped ( ) ) // Capture stdout
183
+ . stderr ( Stdio :: inherit ( ) ) ; // Print stderr
184
+
185
+ // Add environment variables
186
+ for ( key, value) in env_vars {
187
+ command. env ( key, value) ;
188
+ }
189
+
190
+ let output = command. spawn ( ) ?. wait_with_output ( ) ?;
215
191
assert ! (
216
192
output. status. success( ) ,
217
- "solidity call failed on {}" ,
218
- message
193
+ "{} execution failed with env: {:?}" ,
194
+ script,
195
+ env_vars
219
196
) ;
220
- Ok ( String :: from_utf8 ( output. stdout )
197
+
198
+ Ok ( output. stdout )
199
+ }
200
+
201
+ fn deploy ( & self , ink_addr : H160 ) -> Result < String , Box < dyn Error > > {
202
+ let output = self . run_hardhat_script (
203
+ "01-deploy.js" ,
204
+ & [ ( "INK_ADDRESS" , format ! ( "{:?}" , ink_addr) ) ] ,
205
+ ) ?;
206
+ Ok ( String :: from_utf8 ( output) ?
207
+ . lines ( )
208
+ . last ( )
209
+ . ok_or ( "Failed to retrieve contract address" ) ?
210
+ . trim ( )
211
+ . to_string ( ) )
212
+ }
213
+
214
+ fn call (
215
+ & self ,
216
+ sol_addr : String ,
217
+ message : & str ,
218
+ ) -> Result < Option < String > , Box < dyn Error > > {
219
+ let output = self . run_hardhat_script (
220
+ "02-call-flip.js" ,
221
+ & [ ( "SOL_ADDRESS" , sol_addr) , ( "MESSAGE" , message. to_string ( ) ) ] ,
222
+ ) ?;
223
+ Ok ( String :: from_utf8 ( output)
221
224
. ok ( )
222
225
. and_then ( |s| Some ( s. lines ( ) . last ( ) ?. to_string ( ) ) )
223
226
. map ( |s| s. trim ( ) . to_string ( ) ) )
0 commit comments