Skip to content

Commit

Permalink
Integrated data transformer with sncast commands
Browse files Browse the repository at this point in the history
commit-id:9557f1ee
  • Loading branch information
integraledelebesgue committed Oct 4, 2024
1 parent ee87fc4 commit a8be152
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 44 deletions.
61 changes: 53 additions & 8 deletions crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::starknet_commands::{
account, call::Call, declare::Declare, deploy::Deploy, invoke::Invoke, multicall::Multicall,
script::Script, tx_status::TxStatus,
};
use anyhow::{Context, Result};
use anyhow::{bail, 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};

Expand All @@ -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;
Expand Down Expand Up @@ -249,9 +250,20 @@ async fn run_async_command(
.try_into_fee_settings(&provider, account.block_id())
.await?;

// safe to unwrap because "constructor" is a standardized name
let selector = get_selector_from_name("constructor").unwrap();

// Causes issue #2554
let contract_class = get_contract_class(deploy.class_hash, &provider).await?;

let serialized_calldata = constructor_calldata
.map(|ref data| transform(data, contract_class, &selector))
.transpose()?
.unwrap_or_default();

let result = starknet_commands::deploy::deploy(
deploy.class_hash,
&constructor_calldata,
&serialized_calldata,
deploy.salt,
deploy.unique,
fee_settings,
Expand Down Expand Up @@ -284,13 +296,30 @@ async fn run_async_command(

let block_id = get_block_id(&block_id)?;

let entry_point_selector = get_selector_from_name(&function)
// Causes issue #2554
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}"
)
})?;

// Causes issue #2554
let contract_class = get_contract_class(class_hash, &provider).await?;

let selector = get_selector_from_name(&function)
.context("Failed to convert entry point selector to FieldElement")?;

let serialized_calldata = calldata
.map(|ref data| transform(data, contract_class, &selector))
.transpose()?
.unwrap_or_default();

let result = starknet_commands::call::call(
contract_address,
entry_point_selector,
calldata,
selector,
serialized_calldata,
&provider,
block_id.as_ref(),
)
Expand Down Expand Up @@ -331,9 +360,25 @@ async fn run_async_command(
let selector = get_selector_from_name(&function)
.context("Failed to convert entry point selector to FieldElement")?;

// Causes issue #2554
let Ok(Some(class_hash)) = get_class_hash_by_address(&provider, contract_address).await
else {
bail!(
"Couldn't retrieve class hash of a contract with address {contract_address:#x}"
)
};

// Causes issue #2554
let contract_class = get_contract_class(class_hash, &provider).await?;

let serialized_calldata = calldata
.map(|ref data| transform(data, contract_class, &selector))
.transpose()?
.unwrap_or_default();

let result = starknet_commands::invoke::invoke(
contract_address,
calldata,
serialized_calldata,
nonce,
fee_args,
selector,
Expand Down
4 changes: 2 additions & 2 deletions crates/sncast/src/starknet_commands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub struct Call {
#[clap(short, 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(short, long, value_delimiter = ' ', num_args = 1..)]
pub calldata: Vec<Felt>,
pub calldata: Option<Vec<String>>,

/// Block identifier on which call should be performed.
/// Possible values: pending, latest, block hash (0x prefixed string)
Expand Down
4 changes: 2 additions & 2 deletions crates/sncast/src/starknet_commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(short, long, value_delimiter = ' ', num_args = 1..)]
pub constructor_calldata: Vec<Felt>,
pub constructor_calldata: Option<Vec<String>>,

/// Salt for the address
#[clap(short, long)]
Expand Down
4 changes: 2 additions & 2 deletions crates/sncast/src/starknet_commands/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub struct Invoke {
#[clap(short, 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(short, long, value_delimiter = ' ', num_args = 1..)]
pub calldata: Vec<Felt>,
pub calldata: Option<Vec<String>>,

#[clap(flatten)]
pub fee_args: FeeArgs,
Expand Down
7 changes: 2 additions & 5 deletions crates/sncast/tests/e2e/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", // Issue #2554
);
}

Expand Down
11 changes: 4 additions & 7 deletions crates/sncast/tests/e2e/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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')[..]
"},
);
}
Expand All @@ -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", // Issue #2554
);
}

Expand Down
13 changes: 5 additions & 8 deletions crates/sncast/tests/e2e/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,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", // Issue #2554
);
}

Expand All @@ -312,9 +309,9 @@ fn test_wrong_function_name() {

assert_stderr_contains(
output,
indoc! {r"
command: invoke
error: An error occurred in the called contract[..]Entry point[..]not found in contract[..]
indoc! {"
command: invoke
error: An error occurred in the called contract[..]Entry point[..]not found in contract[..]
"},
);
}
Expand Down
14 changes: 4 additions & 10 deletions crates/sncast/tests/e2e/main_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", // Issue #2554
);
}

Expand All @@ -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", // Issue #2554
);
}

Expand Down

0 comments on commit a8be152

Please sign in to comment.