Skip to content

Commit

Permalink
Improve JSON Output for Upload and Remove Commands (use-ink#1389)
Browse files Browse the repository at this point in the history
Fix an issue where the JSON output for both the upload and remove commands in cargo contract was in an invalid format.

Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser authored Nov 24, 2023
1 parent c9786ba commit 83b941d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Do not allow to execute calls on immutable contract messages - [#1397](https://github.com/paritytech/cargo-contract/pull/1397)
- Improve JSON Output for Upload and Remove Commands - [#1389](https://github.com/paritytech/cargo-contract/pull/1389)

## [4.0.0-alpha]

Expand Down
12 changes: 9 additions & 3 deletions crates/cargo-contract/src/cmd/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,27 @@ impl RemoveCommand {
.await?;
let remove_result = remove_exec.remove_code().await?;
let display_events = remove_result.display_events;
let output = if self.output_json() {
let output_events = if self.output_json() {
display_events.to_json()?
} else {
display_events.display_events(
self.extrinsic_cli_opts.verbosity().unwrap(),
remove_exec.token_metadata(),
)?
};
println!("{output}");
if let Some(code_removed) = remove_result.code_removed {
let remove_result = code_removed.code_hash;

if self.output_json() {
println!("{}", &remove_result);
// Create a JSON object with the events and the removed code hash.
let json_object = serde_json::json!({
"events": serde_json::from_str::<serde_json::Value>(&output_events)?,
"code_hash": remove_result,
});
let json_object = serde_json::to_string_pretty(&json_object)?;
println!("{}", json_object);
} else {
println!("{}", output_events);
name_value_println!("Code hash", format!("{remove_result:?}"));
}
Result::<(), ErrorVariant>::Ok(())
Expand Down
32 changes: 10 additions & 22 deletions crates/cargo-contract/src/cmd/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,26 @@ impl UploadCommand {
} else {
let upload_result = upload_exec.upload_code().await?;
let display_events = upload_result.display_events;
let output = if self.output_json() {
let output_events = if self.output_json() {
display_events.to_json()?
} else {
display_events.display_events(
self.extrinsic_cli_opts.verbosity()?,
upload_exec.token_metadata(),
)?
};
println!("{output}");
if let Some(code_stored) = upload_result.code_stored {
let upload_result = CodeHashResult {
code_hash: format!("{:?}", code_stored.code_hash),
};
let code_hash = code_stored.code_hash;
if self.output_json() {
println!("{}", upload_result.to_json()?);
// Create a JSON object with the events and the code hash.
let json_object = serde_json::json!({
"events": serde_json::from_str::<serde_json::Value>(&output_events)?,
"code_hash": code_hash,
});
println!("{}", serde_json::to_string_pretty(&json_object)?);
} else {
upload_result.print();
println!("{}", output_events);
name_value_println!("Code hash", format!("{:?}", code_hash));
}
} else {
let code_hash = hex::encode(code_hash);
Expand All @@ -118,21 +121,6 @@ impl UploadCommand {
}
}

#[derive(serde::Serialize)]
pub struct CodeHashResult {
pub code_hash: String,
}

impl CodeHashResult {
pub fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string_pretty(self)?)
}

pub fn print(&self) {
name_value_println!("Code hash", format!("{:?}", self.code_hash));
}
}

#[derive(serde::Serialize)]
pub struct UploadDryRunResult {
pub result: String,
Expand Down

0 comments on commit 83b941d

Please sign in to comment.