From 281bab5cacced2139235892ceb2e8d2a65310bcf Mon Sep 17 00:00:00 2001 From: adrian Date: Wed, 3 Sep 2025 10:32:54 +0200 Subject: [PATCH] feat: add adapter builder --- protocol-testing/src/adapter_builder.rs | 79 +++++++++++++++++++++++++ protocol-testing/src/main.rs | 1 + 2 files changed, 80 insertions(+) create mode 100644 protocol-testing/src/adapter_builder.rs diff --git a/protocol-testing/src/adapter_builder.rs b/protocol-testing/src/adapter_builder.rs new file mode 100644 index 0000000..f0a8dcd --- /dev/null +++ b/protocol-testing/src/adapter_builder.rs @@ -0,0 +1,79 @@ +use std::{ + path::{Path, PathBuf}, + process::{Command, Output}, +}; + +use miette::{miette, IntoDiagnostic, WrapErr}; + +pub struct AdapterContractBuilder { + src_path: String, +} + +impl AdapterContractBuilder { + pub fn new(src_path: String) -> Self { + Self { src_path } + } + + /// Finds the contract file in the provided source path. + /// + /// # Parameters + /// - `adapter_contract`: The contract name to be passed to the script. + /// + /// # Returns + /// The path to the contract file. + pub fn find_contract(&self, adapter_contract: &str) -> miette::Result { + let contract_path = Path::new(&self.src_path) + .join("out") + .join(format!("{adapter_contract}.sol")) + .join(format!("{adapter_contract}.evm.runtime")); + if !contract_path.exists() { + return Err(miette!("Contract {adapter_contract} not found.")); + } + Ok(contract_path) + } + + /// Runs the buildRuntime Bash script in a subprocess with the provided arguments. + /// + /// # Parameters + /// - `adapter_contract`: The contract name to be passed to the script. + /// - `signature`: The constructor signature to be passed to the script (optional). + /// - `args`: The constructor arguments to be passed to the script (optional). + /// + /// # Returns + /// The path to the contract file. + pub fn build_target( + &self, + adapter_contract: &str, + signature: Option<&str>, + args: Option<&str>, + ) -> miette::Result { + let script_path = "scripts/buildRuntime.sh"; + let mut cmd = Command::new(script_path); + cmd.current_dir(&self.src_path) + .arg("-c") + .arg(adapter_contract); + + if let (Some(sig), Some(arg)) = (signature, args) { + cmd.arg("-s") + .arg(sig) + .arg("-a") + .arg(arg); + } + + let output: Output = cmd + .output() + .into_diagnostic() + .wrap_err(miette!("Error running '{script_path}'"))?; + + println!("Output:\n{}", String::from_utf8_lossy(&output.stdout)); + if !output.stderr.is_empty() { + println!("Errors:\n{}", String::from_utf8_lossy(&output.stderr)); + } + + if !output.status.success() { + return Err(miette!("An error occurred: {}", String::from_utf8_lossy(&output.stderr))); + } + + self.find_contract(adapter_contract) + } +} diff --git a/protocol-testing/src/main.rs b/protocol-testing/src/main.rs index 2d3c144..597c850 100644 --- a/protocol-testing/src/main.rs +++ b/protocol-testing/src/main.rs @@ -1,3 +1,4 @@ +mod adapter_builder; mod config; mod rpc; mod test_runner;