Skip to content

Commit e69d711

Browse files
committed
test(e2e): refactor hardhat command + general cleanup
1 parent 2fbd8df commit e69d711

File tree

1 file changed

+61
-58
lines changed

1 file changed

+61
-58
lines changed

integration-tests/public/solidity-calls-flipper/e2e_tests.rs

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ use ink_e2e::{
1010
PolkadotConfig,
1111
Weight,
1212
};
13-
use std::process::{
14-
Command,
15-
Stdio,
13+
use std::{
14+
error::Error,
15+
process::{
16+
Command,
17+
Stdio,
18+
},
1619
};
1720

1821
const DEFAULT_GAS: Weight = Weight::from_parts(100_000_000_000, 1024 * 1024);
1922
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>>;
2124

2225
#[ink_e2e::test]
2326
async fn solidity_calls_ink_works<Client: E2EBackend>(
@@ -42,7 +45,7 @@ async fn solidity_calls_ink_works<Client: E2EBackend>(
4245

4346
// deploy ink! flipper (RLP encoded)
4447
client.api.map_account(&signer).await;
45-
let res = client
48+
let ink_addr = client
4649
.exec_instantiate(
4750
&signer,
4851
client.contracts.load_code("flipper"),
@@ -51,28 +54,23 @@ async fn solidity_calls_ink_works<Client: E2EBackend>(
5154
DEFAULT_GAS.into(),
5255
DEFAULT_STORAGE_DEPOSIT_LIMIT,
5356
)
54-
.await?;
55-
56-
let ink_addr = res.addr;
57+
.await?
58+
.addr;
5759

5860
let get_selector = keccak_selector(b"get");
5961
let value: bool = call_ink(&mut client, ink_addr, get_selector.clone()).await;
6062
assert_eq!(value, false);
6163

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());
6565
sol_handler.start_eth_rpc()?;
6666
sol_handler.setup()?;
6767
let sol_addr = sol_handler.deploy(ink_addr)?;
68-
let _ = sol_handler.call(sol_addr.clone(), "callFlip")?;
6968

70-
// check if flip worked
69+
let _ = sol_handler.call(sol_addr.clone(), "callFlip")?;
7170
let value: bool = call_ink(&mut client, ink_addr, get_selector.clone()).await;
7271
assert_eq!(value, true);
7372

7473
let _ = sol_handler.call(sol_addr.clone(), "callFlip2")?;
75-
7674
let value: bool = call_ink(&mut client, ink_addr, get_selector).await;
7775
assert_eq!(value, false);
7876

@@ -122,7 +120,7 @@ impl SolidityHandler {
122120
}
123121
}
124122

125-
fn start_eth_rpc(&mut self) -> Result<(), Box<dyn std::error::Error>> {
123+
fn start_eth_rpc(&mut self) -> Result<(), Box<dyn Error>> {
126124
let eth_rpc = Command::new("eth-rpc")
127125
.arg("--dev")
128126
.arg("--node-rpc-url")
@@ -132,7 +130,7 @@ impl SolidityHandler {
132130
Ok(())
133131
}
134132

135-
fn stop_eth_rpc(&mut self) -> Result<(), Box<dyn std::error::Error>> {
133+
fn stop_eth_rpc(&mut self) -> Result<(), Box<dyn Error>> {
136134
if self.eth_rpc_process_id.is_none() {
137135
return Ok(());
138136
}
@@ -143,7 +141,7 @@ impl SolidityHandler {
143141
Ok(())
144142
}
145143

146-
fn setup(&mut self) -> Result<(), Box<dyn std::error::Error>> {
144+
fn setup(&mut self) -> Result<(), Box<dyn Error>> {
147145
let install_status = Command::new("npm")
148146
.current_dir(&self.sol_dir)
149147
.arg("install")
@@ -165,59 +163,64 @@ impl SolidityHandler {
165163
Ok(())
166164
}
167165

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(
195167
&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
200173
.current_dir(&self.sol_dir)
201174
.arg("hardhat")
202175
.arg("run")
203-
.arg("02-call-flip.js")
176+
.arg(script)
204177
.arg("--network")
205178
.arg("localhost")
206179
.arg("--no-compile")
207180
.arg("--config")
208181
.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()?;
215191
assert!(
216192
output.status.success(),
217-
"solidity call failed on {}",
218-
message
193+
"{} execution failed with env: {:?}",
194+
script,
195+
env_vars
219196
);
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)
221224
.ok()
222225
.and_then(|s| Some(s.lines().last()?.to_string()))
223226
.map(|s| s.trim().to_string()))

0 commit comments

Comments
 (0)