Skip to content

Commit

Permalink
[CLI] format code (#2179)
Browse files Browse the repository at this point in the history
### Checklist
- [ ] Relevant issue is linked
- [ ] Docs updated/issue for docs created
- [ ] Added relevant tests
  • Loading branch information
THenry14 authored Jul 5, 2023
1 parent 6a4e92b commit c038b03
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 48 deletions.
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

0 comments on commit c038b03

Please sign in to comment.