Skip to content

Commit

Permalink
Added block explorer output to declare-deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
integraledelebesgue committed Aug 27, 2024
1 parent f085c3e commit 7b77cd3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 17 deletions.
28 changes: 11 additions & 17 deletions crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use sncast::response::print::{print_command_result, OutputFormat};

use camino::Utf8PathBuf;
use clap::{Parser, Subcommand};
use configuration::load_global_config;
use sncast::helpers::configuration::CastConfig;
use sncast::helpers::constants::{DEFAULT_ACCOUNTS_FILE, DEFAULT_MULTICALL_CONTENTS};
use sncast::helpers::fee::PayableTransaction;
Expand All @@ -20,7 +19,6 @@ use sncast::helpers::scarb_utils::{
get_scarb_metadata_with_deps, read_manifest_and_build_artifacts, BuildConfig,
};
use sncast::response::errors::handle_starknet_command_error;
use sncast::response::print::{print_command_result, OutputFormat};
use sncast::response::structs::DeclareDeployResponse;
use sncast::{
chain_id_to_network_name, get_account, get_block_id, get_chain_id, get_default_state_file_name,
Expand Down Expand Up @@ -229,7 +227,7 @@ async fn run_async_command(
} else {
let contract =
deploy.build_artifacts_and_get_compiled_contract(cli.json, &cli.profile)?;
let class_hash = contract.class_hash;
let class_hash = contract.sierra_class_hash;

if !contract.is_declared(&provider).await? {
bail!("Contract with class hash {:x} is not declared", class_hash);
Expand All @@ -244,7 +242,9 @@ async fn run_async_command(
.await
.map_err(handle_starknet_command_error);

print_command_result("deploy", &result, numbers_format, output_format)
print_command_result("deploy", &result, numbers_format, output_format)?;
print_block_explorer_link_if_allowed(&result, output_format, config.block_explorer);
Ok(())
}

Commands::DeclareDeploy(declare_deploy) => {
Expand All @@ -263,7 +263,7 @@ async fn run_async_command(

let contract =
deploy.build_artifacts_and_get_compiled_contract(cli.json, &cli.profile)?;
let class_hash = contract.class_hash;
let class_hash = contract.sierra_class_hash;

let needs_declaration = !contract.is_declared(&provider).await?;

Expand Down Expand Up @@ -294,17 +294,16 @@ async fn run_async_command(
let deploy = deploy.resolved_with_class_hash(class_hash);
deploy.validate()?;

let mut deploy_result =
starknet_commands::deploy::deploy(deploy, &account, wait_config)
.await
.map_err(handle_starknet_command_error);
let deploy_result = starknet_commands::deploy::deploy(deploy, &account, wait_config)
.await
.map_err(handle_starknet_command_error);

if deploy_result.is_err() {
return print_command_result(
"declare-deploy",
&mut deploy_result,
&deploy_result,
numbers_format,
&output_format,
output_format,
);
}

Expand All @@ -313,12 +312,7 @@ async fn run_async_command(
deploy_result.unwrap(),
));

print_command_result(
"declare-deploy",
&result,
numbers_format,
output_format,
)
print_command_result("declare-deploy", &result, numbers_format, output_format)?;
print_block_explorer_link_if_allowed(&result, output_format, config.block_explorer);
Ok(())
}
Expand Down
32 changes: 32 additions & 0 deletions crates/sncast/src/response/structs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use camino::Utf8PathBuf;
use conversions::serde::serialize::CairoSerialize;
use indoc::formatdoc;
use itertools::Itertools;
use serde::{Deserialize, Serialize, Serializer};
use starknet::core::types::FieldElement;

Expand Down Expand Up @@ -228,6 +229,37 @@ impl OutputLink for DeclareResponse {
}
}

impl OutputLink for DeclareDeployResponse {
const TITLE: &'static str = "declaration and deployment";

fn format_links(&self, provider: Box<dyn LinkProvider>) -> String {
let mut links = vec![];

if let Some(ref class_hash) = self.class_hash {
links.push(format!("class: {}", provider.class(class_hash.0)));
}

links.push(format!(
"contract: {}",
provider.contract(self.contract_address.0)
));

if let Some(ref transaction_hash) = self.declare_transaction_hash {
links.push(format!(
"declaration transaction: {}",
provider.class(transaction_hash.0)
));
}

links.push(format!(
"deployment transaction: {}",
provider.transaction(self.deploy_transaction_hash.0)
));

links.iter().join("\n")
}
}

impl OutputLink for AccountCreateResponse {
const TITLE: &'static str = "account creation";

Expand Down
55 changes: 55 additions & 0 deletions crates/sncast/tests/e2e/declare_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,55 @@ fn test_happy_case_strk(account: &str) {
}

#[test]
fn test_happy_case_human_readable() {
let tempdir = duplicate_contract_directory_with_salt(
CONTRACTS_DIR.to_string() + "/map",
"put",
"_human_readable",
);

let accounts_file = &get_accounts_path(ACCOUNT_FILE_PATH)[..];

let args = vec![
"--accounts-file",
accounts_file,
"--account",
"user17",
"--int-format",
"declare-deploy",
"--url",
URL,
"--contract-name",
MAP_CONTRACT_NAME,
"--fee-token",
"strk",
];

let snapbox = runner(&args).current_dir(tempdir.path());
let output = snapbox.assert().success();

let expected = indoc!(
"
[..]
command: [..]
class_hash: [..]
contract_address: [..]
declare_transaction_hash: [..]
deploy_transaction_hash: [..]
To see declaration and deployment details, visit:
class: [..]
contract: [..]
declaration transaction: [..]
deployment transaction: [..]
"
);

assert_stdout_contains(output, expected);
}

#[test]
#[ignore = "Expand the contract's code to more complex or wait for fix: https://github.com/xJonathanLEI/starknet-rs/issues/649#issue-2469861847"]
fn test_happy_case_specify_package() {
let tempdir = duplicate_contract_directory_with_salt(
CONTRACTS_DIR.to_string() + "/multiple_packages",
Expand Down Expand Up @@ -342,6 +391,12 @@ fn test_no_scarb_profile() {
contract_address: [..]
declare_transaction_hash: [..]
deploy_transaction_hash: [..]
To see declaration and deployment details, visit:
class: [..]
contract: [..]
declaration transaction: [..]
deployment transaction: [..]
"
);

Expand Down

0 comments on commit 7b77cd3

Please sign in to comment.