Skip to content

Commit

Permalink
test(e2e): refactor hardhat command + general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwht committed Feb 11, 2025
1 parent 2fbd8df commit e69d711
Showing 1 changed file with 61 additions and 58 deletions.
119 changes: 61 additions & 58 deletions integration-tests/public/solidity-calls-flipper/e2e_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ use ink_e2e::{
PolkadotConfig,
Weight,
};
use std::process::{
Command,
Stdio,
use std::{
error::Error,
process::{
Command,
Stdio,
},
};

const DEFAULT_GAS: Weight = Weight::from_parts(100_000_000_000, 1024 * 1024);
const DEFAULT_STORAGE_DEPOSIT_LIMIT: u128 = 10_000_000_000_000;
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
type E2EResult<T> = Result<T, Box<dyn Error>>;

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

// deploy ink! flipper (RLP encoded)
client.api.map_account(&signer).await;
let res = client
let ink_addr = client
.exec_instantiate(
&signer,
client.contracts.load_code("flipper"),
Expand All @@ -51,28 +54,23 @@ async fn solidity_calls_ink_works<Client: E2EBackend>(
DEFAULT_GAS.into(),
DEFAULT_STORAGE_DEPOSIT_LIMIT,
)
.await?;

let ink_addr = res.addr;
.await?
.addr;

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

let sol_dir = "./sol".to_string();

let mut sol_handler = SolidityHandler::new(sol_dir, client.url().to_string());
let mut sol_handler = SolidityHandler::new("./sol".into(), client.url().to_string());
sol_handler.start_eth_rpc()?;
sol_handler.setup()?;
let sol_addr = sol_handler.deploy(ink_addr)?;
let _ = sol_handler.call(sol_addr.clone(), "callFlip")?;

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

let _ = sol_handler.call(sol_addr.clone(), "callFlip2")?;

let value: bool = call_ink(&mut client, ink_addr, get_selector).await;
assert_eq!(value, false);

Expand Down Expand Up @@ -122,7 +120,7 @@ impl SolidityHandler {
}
}

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

fn stop_eth_rpc(&mut self) -> Result<(), Box<dyn std::error::Error>> {
fn stop_eth_rpc(&mut self) -> Result<(), Box<dyn Error>> {
if self.eth_rpc_process_id.is_none() {
return Ok(());
}
Expand All @@ -143,7 +141,7 @@ impl SolidityHandler {
Ok(())
}

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

fn deploy(&self, ink_addr: H160) -> Result<String, Box<dyn std::error::Error>> {
let deploy_process = Command::new("npx")
.current_dir(&self.sol_dir)
.arg("hardhat")
.arg("run")
.arg("01-deploy.js")
.arg("--network")
.arg("localhost")
.arg("--no-compile")
.arg("--config")
.arg("hardhat.config.js")
.env("INK_ADDRESS", format!("{:?}", ink_addr))
.stdout(Stdio::piped()) // capture stdout
.stderr(Stdio::inherit()) // print stderr
.spawn()?;
let output = deploy_process.wait_with_output()?;
assert!(output.status.success(), "solidity deployment failed");

Ok(String::from_utf8(output.stdout)?
.lines()
.last()
.ok_or("Failed to retrieve contract address")?
.trim()
.to_string())
}

fn call(
fn run_hardhat_script(
&self,
sol_addr: String,
message: &str,
) -> Result<Option<String>, Box<dyn std::error::Error>> {
let call_process = Command::new("npx")
script: &str,
env_vars: &[(&str, String)],
) -> Result<Vec<u8>, Box<dyn Error>> {
let mut command = Command::new("npx");
command
.current_dir(&self.sol_dir)
.arg("hardhat")
.arg("run")
.arg("02-call-flip.js")
.arg(script)
.arg("--network")
.arg("localhost")
.arg("--no-compile")
.arg("--config")
.arg("hardhat.config.js")
.env("SOL_ADDRESS", sol_addr)
.env("MESSAGE", message)
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()?;
let output = call_process.wait_with_output()?;
.stdout(Stdio::piped()) // Capture stdout
.stderr(Stdio::inherit()); // Print stderr

// Add environment variables
for (key, value) in env_vars {
command.env(key, value);
}

let output = command.spawn()?.wait_with_output()?;
assert!(
output.status.success(),
"solidity call failed on {}",
message
"{} execution failed with env: {:?}",
script,
env_vars
);
Ok(String::from_utf8(output.stdout)

Ok(output.stdout)
}

fn deploy(&self, ink_addr: H160) -> Result<String, Box<dyn Error>> {
let output = self.run_hardhat_script(
"01-deploy.js",
&[("INK_ADDRESS", format!("{:?}", ink_addr))],
)?;
Ok(String::from_utf8(output)?
.lines()
.last()
.ok_or("Failed to retrieve contract address")?
.trim()
.to_string())
}

fn call(
&self,
sol_addr: String,
message: &str,
) -> Result<Option<String>, Box<dyn Error>> {
let output = self.run_hardhat_script(
"02-call-flip.js",
&[("SOL_ADDRESS", sol_addr), ("MESSAGE", message.to_string())],
)?;
Ok(String::from_utf8(output)
.ok()
.and_then(|s| Some(s.lines().last()?.to_string()))
.map(|s| s.trim().to_string()))
Expand Down

0 comments on commit e69d711

Please sign in to comment.