From 2f7b5a839d23740c107ea923cb15f85d8e287f1d Mon Sep 17 00:00:00 2001 From: ndkazu Date: Sat, 21 Dec 2024 17:09:32 +0900 Subject: [PATCH] pop up contract implementation --- crates/pop-cli/src/commands/up/contract.rs | 28 ++++++++++++++++++---- crates/pop-cli/tests/contract.rs | 19 ++++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/crates/pop-cli/src/commands/up/contract.rs b/crates/pop-cli/src/commands/up/contract.rs index f46c0776..1bbdb8df 100644 --- a/crates/pop-cli/src/commands/up/contract.rs +++ b/crates/pop-cli/src/commands/up/contract.rs @@ -34,6 +34,9 @@ pub struct UpContractCommand { /// Path to the contract build directory. #[arg(short, long)] path: Option, + /// Directory path without flag for your project [default: current directory] + #[arg(value_name = "PATH", index = 1, conflicts_with = "path")] + pub path_pos: Option, /// The name of the contract constructor to call. #[clap(short, long, default_value = "new")] constructor: String, @@ -92,13 +95,19 @@ impl UpContractCommand { pub(crate) async fn execute(mut self) -> anyhow::Result<()> { Cli.intro("Deploy a smart contract")?; + let path_flag = self.path.clone(); + let project_path = if let Some(ref path_pos) = self.path_pos { + Some(path_pos) // Use positional path if present + } else { + path_flag.as_ref() // Otherwise, use the named path + }; // Check if build exists in the specified "Contract build directory" - if !has_contract_been_built(self.path.as_deref()) { + if !has_contract_been_built(project_path.as_deref().map(|v| &**v)) { // Build the contract in release mode Cli.warning("NOTE: contract has not yet been built.")?; let spinner = spinner(); spinner.start("Building contract in RELEASE mode..."); - let result = match build_smart_contract(self.path.as_deref(), true, Verbosity::Quiet) { + let result = match build_smart_contract(project_path.as_deref().map(|v| &**v), true, Verbosity::Quiet) { Ok(result) => result, Err(e) => { Cli.outro_cancel(format!("🚫 An error occurred building your contract: {e}\nUse `pop build` to retry with build output."))?; @@ -360,7 +369,13 @@ impl UpContractCommand { // get the call data and contract code hash async fn get_contract_data(&self) -> anyhow::Result<(Vec, [u8; 32])> { - let contract_code = get_contract_code(self.path.as_ref())?; + let path_flag = self.path.clone(); + let project_path = if let Some(ref path_pos) = self.path_pos { + Some(path_pos) // Use positional path if present + } else { + path_flag.as_ref() // Otherwise, use the named path + }; + let contract_code = get_contract_code(project_path)?; let hash = contract_code.code_hash(); if self.upload_only { let call_data = get_upload_payload(contract_code, self.url.as_str()).await?; @@ -435,6 +450,7 @@ mod tests { fn default_up_contract_command() -> UpContractCommand { UpContractCommand { path: None, + path_pos: None, constructor: "new".to_string(), args: vec![], value: "0".to_string(), @@ -510,7 +526,8 @@ mod tests { let localhost_url = format!("ws://127.0.0.1:{}", port); let up_contract_opts = UpContractCommand { - path: Some(temp_dir), + path: Some(temp_dir.clone()), + path_pos: Some(temp_dir), constructor: "new".to_string(), args: vec![], value: "0".to_string(), @@ -561,7 +578,8 @@ mod tests { let localhost_url = format!("ws://127.0.0.1:{}", port); let up_contract_opts = UpContractCommand { - path: Some(temp_dir), + path: Some(temp_dir.clone()), + path_pos: Some(temp_dir), constructor: "new".to_string(), args: vec!["false".to_string()], value: "0".to_string(), diff --git a/crates/pop-cli/tests/contract.rs b/crates/pop-cli/tests/contract.rs index 6b8a79b2..962dd330 100644 --- a/crates/pop-cli/tests/contract.rs +++ b/crates/pop-cli/tests/contract.rs @@ -67,22 +67,22 @@ async fn contract_lifecycle() -> Result<()> { .success(); assert!(temp_dir.join("test_contract").exists()); - // pop build --path ./test_contract --release +/* // pop build --path ./test_contract --release Command::cargo_bin("pop") .unwrap() .current_dir(&temp_dir) .args(&["build", "--path", "./test_contract", "--release"]) .assert() .success(); - +*/ // pop build ./test_contract --release - /*Command::cargo_bin("pop") + Command::cargo_bin("pop") .unwrap() .current_dir(&temp_dir) .args(&["build", "./test_contract", "--release"]) .assert() .success(); - */ + println!("Contract built!!"); // Verify that the directory target has been created @@ -99,13 +99,20 @@ async fn contract_lifecycle() -> Result<()> { sleep(Duration::from_secs(5)).await; // Only upload the contract - // pop up contract --upload-only + // pop up contract --path ./test_contract --upload-only Command::cargo_bin("pop") .unwrap() .current_dir(&temp_dir.join("test_contract")) - .args(&["up", "contract", "--upload-only", "--url", default_endpoint]) + .args(&["up", "contract", "--path", "./test_contract","--upload-only", "--url", default_endpoint]) .assert() .success(); + /*// pop up contract --upload-only + Command::cargo_bin("pop") + .unwrap() + .current_dir(&temp_dir.join("test_contract")) + .args(&["up", "contract", "--upload-only", "--url", default_endpoint]) + .assert() + .success();*/ // Instantiate contract, only dry-run Command::cargo_bin("pop") .unwrap()