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

Improve JSON Output for Upload and Remove Commands #1389

Merged
merged 3 commits into from
Nov 24, 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
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}");
smiasojed marked this conversation as resolved.
Show resolved Hide resolved
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
Loading