From 6f681662234ff2d1f387f23e5d9eaeb0424cf92d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sm=C3=B3=C5=82ka?= Date: Tue, 1 Oct 2024 23:13:35 +0200 Subject: [PATCH] Integrated data transformer with `sncast` commands commit-id:9557f1ee --- crates/sncast/src/main.rs | 41 ++++++++++++++++--- crates/sncast/src/starknet_commands/call.rs | 4 +- crates/sncast/src/starknet_commands/deploy.rs | 4 +- crates/sncast/src/starknet_commands/invoke.rs | 4 +- crates/sncast/tests/e2e/call.rs | 14 ++----- crates/sncast/tests/e2e/deploy.rs | 11 ++--- crates/sncast/tests/e2e/invoke.rs | 14 ++----- crates/sncast/tests/e2e/main_tests.rs | 14 ++----- 8 files changed, 58 insertions(+), 48 deletions(-) diff --git a/crates/sncast/src/main.rs b/crates/sncast/src/main.rs index 6245cf8fab..a3c34cc9c1 100644 --- a/crates/sncast/src/main.rs +++ b/crates/sncast/src/main.rs @@ -6,6 +6,7 @@ use crate::starknet_commands::{ }; use anyhow::{Context, Result}; use configuration::load_global_config; +use sncast::helpers::data_transformer::transformer::transform; use sncast::response::explorer_link::print_block_explorer_link_if_allowed; use sncast::response::print::{print_command_result, OutputFormat}; @@ -20,8 +21,8 @@ use sncast::helpers::scarb_utils::{ }; use sncast::response::errors::handle_starknet_command_error; use sncast::{ - chain_id_to_network_name, get_account, get_block_id, get_chain_id, get_default_state_file_name, - NumbersFormat, ValidatedWaitParams, WaitForTx, + chain_id_to_network_name, get_account, get_block_id, get_chain_id, get_class_hash_by_address, + get_contract_class, get_default_state_file_name, NumbersFormat, ValidatedWaitParams, WaitForTx, }; use starknet::accounts::ConnectedAccount; use starknet::core::utils::get_selector_from_name; @@ -249,9 +250,18 @@ async fn run_async_command( .try_into_fee_settings(&provider, account.block_id()) .await?; + let selector = get_selector_from_name("constructor").unwrap(); + + let contract_class = get_contract_class(deploy.class_hash, &provider).await?; + + let serialized_calldata = match constructor_calldata { + Some(ref data) => transform(data, contract_class, &selector)?, + None => vec![], + }; + let result = starknet_commands::deploy::deploy( deploy.class_hash, - &constructor_calldata, + &serialized_calldata, deploy.salt, deploy.unique, fee_settings, @@ -284,13 +294,25 @@ async fn run_async_command( let block_id = get_block_id(&block_id)?; + let class_hash = get_class_hash_by_address(&provider, contract_address) + .await? + .with_context(|| { + format!( + "Couldn't retrieve class hash of a contract with address {contract_address:#x}" + ) + })?; + + let contract_class = get_contract_class(class_hash, &provider).await?; + let entry_point_selector = get_selector_from_name(&function) .context("Failed to convert entry point selector to FieldElement")?; + let serialized_calldata = transform(&calldata, contract_class, &entry_point_selector)?; + let result = starknet_commands::call::call( contract_address, entry_point_selector, - calldata, + serialized_calldata, &provider, block_id.as_ref(), ) @@ -331,9 +353,18 @@ async fn run_async_command( let selector = get_selector_from_name(&function) .context("Failed to convert entry point selector to FieldElement")?; + let class_hash = get_class_hash_by_address(&provider, contract_address) + .await + .with_context(|| format!("Couldn't retrieve class hash of a contract with address {contract_address:#x}"))? + .with_context(|| format!("Couldn't retrieve class hash of a contract with address {contract_address:#x}"))?; + + let contract_class = get_contract_class(class_hash, &provider).await?; + + let serialized_calldata = transform(&calldata, contract_class, &selector)?; + let result = starknet_commands::invoke::invoke( contract_address, - calldata, + serialized_calldata, nonce, fee_args, selector, diff --git a/crates/sncast/src/starknet_commands/call.rs b/crates/sncast/src/starknet_commands/call.rs index 8bddb636b1..7c94b10cbb 100644 --- a/crates/sncast/src/starknet_commands/call.rs +++ b/crates/sncast/src/starknet_commands/call.rs @@ -18,9 +18,9 @@ pub struct Call { #[clap(long)] pub function: String, - /// Arguments of the called function (list of hex) + /// Arguments of the called function (a list of Cairo-like expressions or values serialized as felts) #[clap(long, value_delimiter = ' ', num_args = 1..)] - pub calldata: Vec, + pub calldata: Vec, /// Block identifier on which call should be performed. /// Possible values: pending, latest, block hash (0x prefixed string) diff --git a/crates/sncast/src/starknet_commands/deploy.rs b/crates/sncast/src/starknet_commands/deploy.rs index b4361e60de..3f2c1ab3fe 100644 --- a/crates/sncast/src/starknet_commands/deploy.rs +++ b/crates/sncast/src/starknet_commands/deploy.rs @@ -23,9 +23,9 @@ pub struct Deploy { #[clap(short = 'g', long)] pub class_hash: Felt, - /// Calldata for the contract constructor + /// Calldata for the contract constructor (a list of Cairo-like expressions or values serialized as felts) #[clap(long, value_delimiter = ' ', num_args = 1..)] - pub constructor_calldata: Vec, + pub constructor_calldata: Option>, /// Salt for the address #[clap(short, long)] diff --git a/crates/sncast/src/starknet_commands/invoke.rs b/crates/sncast/src/starknet_commands/invoke.rs index dd492df4b0..6fd676b975 100644 --- a/crates/sncast/src/starknet_commands/invoke.rs +++ b/crates/sncast/src/starknet_commands/invoke.rs @@ -24,9 +24,9 @@ pub struct Invoke { #[clap(long)] pub function: String, - /// Calldata for the invoked function + /// Calldata for the invoked function (a list of Cairo-like expressions or values serialized as felts) #[clap(long, value_delimiter = ' ', num_args = 1..)] - pub calldata: Vec, + pub calldata: Vec, #[clap(flatten)] pub fee_args: FeeArgs, diff --git a/crates/sncast/tests/e2e/call.rs b/crates/sncast/tests/e2e/call.rs index 72f401820f..e202b5eb95 100644 --- a/crates/sncast/tests/e2e/call.rs +++ b/crates/sncast/tests/e2e/call.rs @@ -78,14 +78,11 @@ async fn test_contract_does_not_exist() { ]; let snapbox = runner(&args); - let output = snapbox.assert().success(); + let output = snapbox.assert().failure(); assert_stderr_contains( output, - indoc! {r" - command: call - error: There is no contract at the specified address - "}, + "Error: Couldn't retrieve class hash of a contract with address 0x1", ); } @@ -104,14 +101,11 @@ fn test_wrong_function_name() { ]; let snapbox = runner(&args); - let output = snapbox.assert().success(); + let output = snapbox.assert().failure(); assert_stderr_contains( output, - indoc! {r" - command: call - error: An error occurred [..]Entry point[..]not found in contract[..] - "}, + r#"Error: Function with selector "[..]" not found in ABI of the contract"#, ); } diff --git a/crates/sncast/tests/e2e/deploy.rs b/crates/sncast/tests/e2e/deploy.rs index c95c62da99..dfcd5f3875 100644 --- a/crates/sncast/tests/e2e/deploy.rs +++ b/crates/sncast/tests/e2e/deploy.rs @@ -298,7 +298,7 @@ fn test_wrong_calldata() { "--class-hash", CONSTRUCTOR_WITH_PARAMS_CONTRACT_CLASS_HASH_SEPOLIA, "--constructor-calldata", - "0x1 0x1", + "0x1 0x2 0x3 0x4", ]; let snapbox = runner(&args); @@ -308,7 +308,7 @@ fn test_wrong_calldata() { output, indoc! {r" command: deploy - error: An error occurred in the called contract[..]Failed to deserialize param #2[..] + error: An error occurred in the called contract[..]('Input too long for arguments')[..] "}, ); } @@ -330,14 +330,11 @@ async fn test_contract_not_declared() { ]; let snapbox = runner(&args); - let output = snapbox.assert().success(); + let output = snapbox.assert().failure(); assert_stderr_contains( output, - indoc! {r" - command: deploy - error: An error occurred in the called contract[..]Class with hash[..]is not declared[..] - "}, + "Error: Couldn't retrieve contract class with hash: 0x1", ); } diff --git a/crates/sncast/tests/e2e/invoke.rs b/crates/sncast/tests/e2e/invoke.rs index d62b2e1608..cf12fcfad5 100644 --- a/crates/sncast/tests/e2e/invoke.rs +++ b/crates/sncast/tests/e2e/invoke.rs @@ -310,14 +310,11 @@ async fn test_contract_does_not_exist() { ]; let snapbox = runner(&args); - let output = snapbox.assert().success(); + let output = snapbox.assert().failure(); assert_stderr_contains( output, - indoc! {r" - command: invoke - error: An error occurred in the called contract[..]Requested contract address[..]is not deployed[..] - "}, + "Error: Couldn't retrieve class hash of a contract with address 0x1", ); } @@ -340,14 +337,11 @@ fn test_wrong_function_name() { ]; let snapbox = runner(&args); - let output = snapbox.assert().success(); + let output = snapbox.assert().failure(); assert_stderr_contains( output, - indoc! {r" - command: invoke - error: An error occurred in the called contract[..]Entry point[..]not found in contract[..] - "}, + r#"Error: Function with selector "[..]" not found in ABI of the contract"#, ); } diff --git a/crates/sncast/tests/e2e/main_tests.rs b/crates/sncast/tests/e2e/main_tests.rs index 65fd34651e..d1a3738d35 100644 --- a/crates/sncast/tests/e2e/main_tests.rs +++ b/crates/sncast/tests/e2e/main_tests.rs @@ -27,14 +27,11 @@ async fn test_happy_case_from_sncast_config() { ]; let snapbox = runner(&args).current_dir(tempdir.path()); - let output = snapbox.assert().success(); + let output = snapbox.assert().failure(); assert_stderr_contains( output, - indoc! {r" - command: call - error: There is no contract at the specified address - "}, + "Error: Couldn't retrieve class hash of a contract with address 0x0", ); } @@ -55,14 +52,11 @@ async fn test_happy_case_from_cli_no_scarb() { ]; let snapbox = runner(&args); - let output = snapbox.assert().success(); + let output = snapbox.assert().failure(); assert_stderr_contains( output, - indoc! {r" - command: call - error: There is no contract at the specified address - "}, + "Error: Couldn't retrieve class hash of a contract with address 0x0", ); }