Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] format code #2179

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,13 @@ fn write_to_output<T: std::fmt::Display>(value: T, error: bool) {
}

pub fn parse_number(number_as_str: &str) -> Result<FieldElement> {
let contract_address = match &number_as_str[..2] {
"0x" => FieldElement::from_hex_be(number_as_str)?,
_ => FieldElement::from_dec_str(number_as_str)?,
};
Ok(contract_address)
let contract_address = match &number_as_str[..2] {
"0x" => FieldElement::from_hex_be(number_as_str)?,
_ => FieldElement::from_dec_str(number_as_str)?,
};
Ok(contract_address)
}


#[cfg(test)]
mod tests {
use crate::{get_account, get_block_id, get_network, get_provider, Network};
Expand Down
9 changes: 4 additions & 5 deletions cast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ enum Commands {
async fn main() -> Result<()> {
let cli = Cli::parse();

let accounts_file_path = Utf8PathBuf::from(shellexpand::tilde(&cli.accounts_file_path).to_string());
let accounts_file_path =
Utf8PathBuf::from(shellexpand::tilde(&cli.accounts_file_path).to_string());
if !&accounts_file_path.exists() {
bail! {"Accounts file {} does not exist! Make sure to supply correct path to accounts file.", cli.accounts_file_path}
}
Expand All @@ -79,8 +80,7 @@ async fn main() -> Result<()> {

match cli.command {
Commands::Declare(declare) => {
let mut account =
get_account(&cli.account, &accounts_file_path, &provider, &network)?;
let mut account = get_account(&cli.account, &accounts_file_path, &provider, &network)?;

let result = starknet_commands::declare::declare(
&declare.contract,
Expand Down Expand Up @@ -190,8 +190,7 @@ async fn main() -> Result<()> {
Ok(())
}
Commands::Invoke(invoke) => {
let mut account =
get_account(&cli.account, &accounts_file_path, &provider, &network)?;
let mut account = get_account(&cli.account, &accounts_file_path, &provider, &network)?;
let result = starknet_commands::invoke::invoke(
&invoke.contract_address,
&invoke.entry_point_name,
Expand Down
4 changes: 1 addition & 3 deletions cast/src/starknet_commands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ pub async fn call(
.context("Failed to convert entry point selector to FieldElement")?,
calldata: calldata
.iter()
.map(|x| {
parse_number(x).context("Failed to convert calldata to FieldElement")
})
.map(|x| parse_number(x).context("Failed to convert calldata to FieldElement"))
.collect::<Result<Vec<_>>>()?,
};
let res = provider.call(function_call, block_id).await;
Expand Down
85 changes: 62 additions & 23 deletions cast/src/starknet_commands/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,21 @@ pub async fn declare(
.stderr(Stdio::piped())
.output()
.context("Failed to start building contracts with Scarb")?;
let result_code = command_result.status.code().context("failed to obtain status code from scarb build")?;
let result_code = command_result
.status
.code()
.context("failed to obtain status code from scarb build")?;
if result_code != 0 {
anyhow::bail!("scarb build returned non-zero exit code: {}", result_code);
}

// TODO #2141 improve handling starknet artifacts
// TODO #2154 consider using `scarb manifest-path` instead of current_dir
let current_dir = std::env::current_dir()
.context("Failed to get current directory")?
.join("target/release");
.context("Failed to get current directory")?
.join("target/release");
let mut paths = std::fs::read_dir(&current_dir)
.context("Failed to read ./target/release, scarb build probably failed")?;
.context("Failed to read ./target/release, scarb build probably failed")?;

let starknet_artifacts = &paths
.find_map(|path| match path {
Expand All @@ -95,34 +98,70 @@ pub async fn declare(
serde_json::from_str(starknet_artifacts.as_str())
.context("Failed to parse starknet_artifacts.json contents")?;

let sierra_path = starknet_artifacts.contracts.iter().find_map(|contract| {
if contract.contract_name == contract_name {
return Some(contract.artifacts.sierra.clone());
}
None
}).unwrap_or_else(|| panic!("Failed to find contract {contract_name} in starknet_artifacts.json"));
let sierra_path = starknet_artifacts
.contracts
.iter()
.find_map(|contract| {
if contract.contract_name == contract_name {
return Some(contract.artifacts.sierra.clone());
}
None
})
.unwrap_or_else(|| {
panic!("Failed to find contract {contract_name} in starknet_artifacts.json")
});
let sierra_contract_path = current_dir.join(sierra_path);

let casm_path = starknet_artifacts.contracts.iter().find_map(|contract| {
if contract.contract_name == contract_name {
return Some(contract.artifacts.casm.clone());
}
None
}).unwrap_or_else(|| panic!("Failed to find contract {contract_name} in starknet_artifacts.json")).unwrap();
let casm_path = starknet_artifacts
.contracts
.iter()
.find_map(|contract| {
if contract.contract_name == contract_name {
return Some(contract.artifacts.casm.clone());
}
None
})
.unwrap_or_else(|| {
panic!("Failed to find contract {contract_name} in starknet_artifacts.json")
})
.unwrap();
let casm_contract_path = current_dir.join(casm_path);

let contract_definition: SierraClass = {
let file_contents = std::fs::read(sierra_contract_path.clone())
.with_context(|| format!("Failed to read contract file: {}", sierra_contract_path.to_str().expect("failed to convert sierra_contract_path to string")))?;
let file_contents = std::fs::read(sierra_contract_path.clone()).with_context(|| {
format!(
"Failed to read contract file: {}",
sierra_contract_path
.to_str()
.expect("failed to convert sierra_contract_path to string")
)
})?;
serde_json::from_slice(&file_contents).with_context(|| {
format!("Failed to parse contract definition: {}", sierra_contract_path.to_str().expect("failed to convert sierra_contract_path to string"))
format!(
"Failed to parse contract definition: {}",
sierra_contract_path
.to_str()
.expect("failed to convert sierra_contract_path to string")
)
})?
};
let casm_contract_definition: CompiledClass = {
let file_contents = std::fs::read(casm_contract_path.clone())
.with_context(|| format!("Failed to read contract file: {}", casm_contract_path.to_str().expect("failed to convert casm_contract_path to string")))?;
serde_json::from_slice(&file_contents)
.with_context(|| format!("Failed to parse contract definition: {}", casm_contract_path.to_str().expect("failed to convert casm_contract_path to string")))?
let file_contents = std::fs::read(casm_contract_path.clone()).with_context(|| {
format!(
"Failed to read contract file: {}",
casm_contract_path
.to_str()
.expect("failed to convert casm_contract_path to string")
)
})?;
serde_json::from_slice(&file_contents).with_context(|| {
format!(
"Failed to parse contract definition: {}",
casm_contract_path
.to_str()
.expect("failed to convert casm_contract_path to string")
)
})?
};

let casm_class_hash = casm_contract_definition.class_hash()?;
Expand Down
4 changes: 1 addition & 3 deletions cast/src/starknet_commands/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ pub async fn invoke(
selector: get_selector_from_name(entry_point_name)?,
calldata: calldata
.iter()
.map(|cd| {
parse_number(cd).context("Failed to convert calldata to FieldElement")
})
.map(|cd| parse_number(cd).context("Failed to convert calldata to FieldElement"))
.collect::<Result<Vec<_>>>()?,
};
let execution = account.execute(vec![call]);
Expand Down
14 changes: 6 additions & 8 deletions cast/tests/e2e/helpers/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ pub async fn declare_deploy_simple_balance_contract() {
.expect("Could not get the account");

let contract_definition: SierraClass = {
let file_contents = std::fs::read(
CONTRACTS_DIR.to_string() + "/map/target/dev/map_Map.sierra.json",
)
.expect("Could not read balance's sierra file");
let file_contents =
std::fs::read(CONTRACTS_DIR.to_string() + "/map/target/dev/map_Map.sierra.json")
.expect("Could not read balance's sierra file");
serde_json::from_slice(&file_contents).expect("Could not cast sierra file to SierraClass")
};
let casm_contract_definition: CompiledClass = {
let file_contents = std::fs::read(
CONTRACTS_DIR.to_string() + "/map/target/dev/map_Map.casm.json",
)
.expect("Could not read balance's casm file");
let file_contents =
std::fs::read(CONTRACTS_DIR.to_string() + "/map/target/dev/map_Map.casm.json")
.expect("Could not read balance's casm file");
serde_json::from_slice(&file_contents).expect("Could not cast casm file to CompiledClass")
};

Expand Down