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 3, 2024
1 parent 54c1372 commit 96e909a
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 48 deletions.
41 changes: 36 additions & 5 deletions crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

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,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,
Expand Down Expand Up @@ -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(),
)
Expand Down Expand Up @@ -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,
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(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<Felt>,
pub calldata: 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(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(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<Felt>,
pub calldata: Vec<String>,

#[clap(flatten)]
pub fee_args: FeeArgs,
Expand Down
14 changes: 4 additions & 10 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",
);
}

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

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",
);
}

Expand Down
14 changes: 4 additions & 10 deletions crates/sncast/tests/e2e/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
}

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

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",
);
}

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",
);
}

Expand Down

0 comments on commit 96e909a

Please sign in to comment.