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

chore(avm-transpiler): better error messages #7217

Merged
merged 1 commit into from
Jun 27, 2024
Merged
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
27 changes: 17 additions & 10 deletions avm-transpiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,36 @@ fn main() {
let args: Vec<String> = env::args().collect();
let in_contract_artifact_path = &args[1];
let out_transpiled_artifact_path = &args[2];
let json_parse_error = format!(
"Unable to parse json for: {in_contract_artifact_path}
This is probably a stale json file with a different wire format.
You might need to recompile the contract or delete the json file"
);

// Parse original (pre-transpile) contract.
let contract_json =
fs::read_to_string(Path::new(in_contract_artifact_path)).expect("Unable to read file");
let contract_json = fs::read_to_string(Path::new(in_contract_artifact_path))
.expect(&format!("Unable to read file: {in_contract_artifact_path}"));
let raw_json_obj: serde_json::Value =
serde_json::from_str(&contract_json).expect("Unable to parse json");
serde_json::from_str(&contract_json).expect(&json_parse_error);

// Skip if contract has "transpiled: true" flag!
if let Some(serde_json::Value::Bool(true)) = raw_json_obj.get("transpiled") {
warn!("Contract already transpiled. Skipping.");
return;
}

// Backup the original file.
std::fs::copy(
Path::new(in_contract_artifact_path),
Path::new(&(in_contract_artifact_path.clone() + ".bak")),
)
.expect("Unable to backup file");
// Backup the output file if it already exists.
if Path::new(out_transpiled_artifact_path).exists() {
std::fs::copy(
Path::new(out_transpiled_artifact_path),
Path::new(&(out_transpiled_artifact_path.clone() + ".bak")),
)
.expect(&format!("Unable to backup file: {out_transpiled_artifact_path}"));
}

// Parse json into contract object
let contract: CompiledAcirContractArtifact =
serde_json::from_str(&contract_json).expect("Unable to parse json");
serde_json::from_str(&contract_json).expect(&json_parse_error);

// Transpile contract to AVM bytecode
let transpiled_contract = TranspiledContractArtifact::from(contract);
Expand Down
Loading