Skip to content

Commit

Permalink
pop up contract implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Dec 21, 2024
1 parent aac2984 commit 2f7b5a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
28 changes: 23 additions & 5 deletions crates/pop-cli/src/commands/up/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub struct UpContractCommand {
/// Path to the contract build directory.
#[arg(short, long)]
path: Option<PathBuf>,
/// Directory path without flag for your project [default: current directory]
#[arg(value_name = "PATH", index = 1, conflicts_with = "path")]
pub path_pos: Option<PathBuf>,
/// The name of the contract constructor to call.
#[clap(short, long, default_value = "new")]
constructor: String,
Expand Down Expand Up @@ -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."))?;
Expand Down Expand Up @@ -360,7 +369,13 @@ impl UpContractCommand {

// get the call data and contract code hash
async fn get_contract_data(&self) -> anyhow::Result<(Vec<u8>, [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?;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
19 changes: 13 additions & 6 deletions crates/pop-cli/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 2f7b5a8

Please sign in to comment.