-
Notifications
You must be signed in to change notification settings - Fork 62
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
rusk-wallet: Add stdout_format flag to binary #2700
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ use rusk_wallet::{ | |
}, | ||
Address, Error, Profile, Wallet, EPOCH, MAX_PROFILES, | ||
}; | ||
use serde_json::json; | ||
use wallet_core::BalanceInfo; | ||
|
||
use crate::io::prompt; | ||
|
@@ -638,7 +639,7 @@ impl Command { | |
let contract_id = | ||
wallet.get_contract_id(profile_idx, code, deploy_nonce)?; | ||
|
||
Ok(RunResult::ContractId(contract_id)) | ||
Ok(RunResult::ContractId((contract_id, deploy_nonce))) | ||
} | ||
Command::Create { .. } => Ok(RunResult::Create()), | ||
Command::Restore { .. } => Ok(RunResult::Restore()), | ||
|
@@ -655,17 +656,109 @@ pub enum RunResult<'a> { | |
StakeInfo(StakeData, bool), | ||
Profile((u8, &'a Profile)), | ||
Profiles(&'a Vec<Profile>), | ||
ContractId([u8; CONTRACT_ID_BYTES]), | ||
ContractId(([u8; CONTRACT_ID_BYTES], u64)), | ||
ExportedKeys(PathBuf, PathBuf), | ||
Create(), | ||
Restore(), | ||
Settings(), | ||
PhoenixHistory(Vec<TransactionHistory>), | ||
} | ||
|
||
impl fmt::Display for RunResult<'_> { | ||
impl<'a> RunResult<'a> { | ||
/// Convert the ran result into json | ||
pub fn to_json(&self) -> String { | ||
use RunResult::*; | ||
|
||
match self { | ||
PhoenixBalance(bal, _) => { | ||
json!({ | ||
"total": bal.value, | ||
"spendable": bal.spendable | ||
}) | ||
} | ||
MoonlightBalance(bal) => { | ||
let bal = bal.to_string(); | ||
|
||
json!({ | ||
"total": bal, | ||
}) | ||
} | ||
Profile((_, profile)) => profile.to_json(), | ||
Profiles(addrs) => { | ||
let addrs = addrs.iter().map(|a| a.to_json()).collect(); | ||
|
||
serde_json::Value::Array(addrs) | ||
} | ||
Tx(hash) => { | ||
let hex = hex::encode(hash.to_bytes()); | ||
|
||
json!({ | ||
"hash": hex | ||
}) | ||
} | ||
StakeInfo(data, _) => { | ||
let faults = data.faults.to_string(); | ||
let hard_faults = data.hard_faults.to_string(); | ||
let rewards = Dusk::from(data.reward).to_string(); | ||
|
||
match data.amount { | ||
Some(amt) => { | ||
let amount = Dusk::from(amt.value).to_string(); | ||
let locked = Dusk::from(amt.locked).to_string(); | ||
let eligibility = amt.eligibility.to_string(); | ||
let epoch = (amt.eligibility / EPOCH).to_string(); | ||
|
||
json!({ | ||
"rewards": rewards, | ||
"amount": amount, | ||
"locked": locked, | ||
"eligibility": eligibility, | ||
"epoch": epoch, | ||
"hard_faults": hard_faults, | ||
"faults": faults, | ||
}) | ||
} | ||
None => { | ||
json!({ | ||
"rewards": rewards, | ||
"faults": faults, | ||
"hard_faults": hard_faults | ||
}) | ||
} | ||
} | ||
} | ||
ExportedKeys(pk, kp) => { | ||
let pk = pk.as_os_str(); | ||
let kp = kp.as_os_str(); | ||
|
||
json!({ | ||
"pk_export_location": pk, | ||
"key_pair_export_location": kp | ||
}) | ||
} | ||
PhoenixHistory(txns) => { | ||
let txns = txns.iter().map(|his| his.to_json()).collect(); | ||
|
||
serde_json::Value::Array(txns) | ||
} | ||
ContractId((bytes, nonce)) => { | ||
let contract_id = hex::encode(bytes); | ||
|
||
json!({ | ||
"contract_id": contract_id, | ||
"deploy_nonce": nonce | ||
}) | ||
} | ||
Create() | Restore() | Settings() => serde_json::Value::Null, | ||
} | ||
.to_string() | ||
} | ||
} | ||
|
||
impl<'a> fmt::Display for RunResult<'a> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
use RunResult::*; | ||
|
||
match self { | ||
PhoenixBalance(balance, _) => { | ||
let total = Dusk::from(balance.value); | ||
|
@@ -719,8 +812,6 @@ impl fmt::Display for RunResult<'_> { | |
writeln!(f, "> Eligible stake: {amount} DUSK")?; | ||
writeln!(f, "> Reclaimable slashed stake: {locked} DUSK")?; | ||
writeln!(f, "> Stake active from block #{eligibility} (Epoch {epoch})")?; | ||
} else { | ||
writeln!(f, "> No active stake found for this key")?; | ||
Comment on lines
-722
to
-723
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this has been removed? |
||
} | ||
let faults = data.faults; | ||
let hard_faults = data.hard_faults; | ||
|
@@ -730,8 +821,9 @@ impl fmt::Display for RunResult<'_> { | |
writeln!(f, "> Hard Slashes: {hard_faults}")?; | ||
write!(f, "> Accumulated rewards is: {rewards} DUSK") | ||
} | ||
ContractId(bytes) => { | ||
write!(f, "> Contract ID: {}", hex::encode(bytes)) | ||
ContractId((bytes, nonce)) => { | ||
write!(f, "> Contract ID: {}", hex::encode(bytes))?; | ||
write!(f, "> Deploy nonce: {}", nonce) | ||
} | ||
ExportedKeys(pk, kp) => { | ||
let pk = pk.display(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,9 @@ use inquire::InquireError; | |
use rocksdb::ErrorKind; | ||
use tracing::{error, info, warn, Level}; | ||
|
||
use crate::command::TransactionHistory; | ||
use crate::settings::{LogFormat, Settings}; | ||
|
||
use rusk_wallet::{ | ||
currency::Dusk, | ||
dat::{self, LATEST_VERSION}, | ||
Error, GraphQL, Profile, SecureWalletFile, Wallet, WalletPath, EPOCH, | ||
}; | ||
|
@@ -318,93 +316,20 @@ async fn exec() -> anyhow::Result<()> { | |
match cmd { | ||
// if there is no command we are in interactive mode and need to run the | ||
// interactive loop | ||
None => { | ||
wallet.register_sync().await?; | ||
interactive::run_loop(&mut wallet, &settings).await?; | ||
} | ||
// else we run the given command and print the result | ||
Some(cmd) => { | ||
match cmd.run(&mut wallet, &settings).await? { | ||
RunResult::PhoenixBalance(balance, spendable) => { | ||
if spendable { | ||
println!("{}", Dusk::from(balance.spendable)); | ||
} else { | ||
println!("{}", Dusk::from(balance.value)); | ||
} | ||
} | ||
RunResult::MoonlightBalance(balance) => { | ||
println!("Total: {}", balance); | ||
} | ||
RunResult::Profile((profile_idx, profile)) => { | ||
println!( | ||
"> {}\n> {}\n> {}\n", | ||
Profile::index_string(profile_idx), | ||
profile.shielded_address_string(), | ||
profile.public_account_string(), | ||
); | ||
} | ||
RunResult::Profiles(addrs) => { | ||
for (profile_idx, profile) in addrs.iter().enumerate() { | ||
println!( | ||
"> {}\n> {}\n> {}\n\n", | ||
Profile::index_string(profile_idx as u8), | ||
profile.shielded_address_string(), | ||
profile.public_account_string(), | ||
); | ||
} | ||
} | ||
RunResult::Tx(hash) => { | ||
let tx_id = hex::encode(hash.to_bytes()); | ||
let run_res = cmd.run(&mut wallet, &settings).await?; | ||
|
||
// Wait for transaction confirmation from network | ||
let gql = GraphQL::new(settings.state, status::headless)?; | ||
gql.wait_for(&tx_id).await?; | ||
let mut stdout_res = run_res.to_string(); | ||
|
||
println!("{tx_id}"); | ||
} | ||
RunResult::StakeInfo(info, reward) => { | ||
let rewards = Dusk::from(info.reward); | ||
if reward { | ||
println!("{rewards}"); | ||
} else { | ||
if let Some(amt) = info.amount { | ||
let amount = Dusk::from(amt.value); | ||
let locked = Dusk::from(amt.locked); | ||
let eligibility = amt.eligibility; | ||
let epoch = amt.eligibility / EPOCH; | ||
|
||
println!("Eligible stake: {amount} DUSK"); | ||
println!( | ||
"Reclaimable slashed stake: {locked} DUSK" | ||
); | ||
println!("Stake active from block #{eligibility} (Epoch {epoch})"); | ||
} else { | ||
println!("No active stake found for this key"); | ||
} | ||
let faults = info.faults; | ||
let hard_faults = info.hard_faults; | ||
let rewards = Dusk::from(info.reward); | ||
|
||
println!("Slashes: {faults}"); | ||
println!("Hard Slashes: {hard_faults}"); | ||
println!("Accumulated rewards is: {rewards} DUSK"); | ||
} | ||
} | ||
RunResult::ExportedKeys(pub_key, key_pair) => { | ||
println!("{},{}", pub_key.display(), key_pair.display()) | ||
} | ||
RunResult::PhoenixHistory(transactions) => { | ||
println!("{}", TransactionHistory::header()); | ||
for th in transactions { | ||
println!("{th}"); | ||
} | ||
} | ||
RunResult::ContractId(id) => { | ||
println!("Contract ID: {:?}", id); | ||
} | ||
RunResult::Settings() => {} | ||
RunResult::Create() | RunResult::Restore() => {} | ||
if want_json { | ||
stdout_res = run_res.to_json(); | ||
Comment on lines
+324
to
+325
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put an if else and avoid to "run_res.to_string()"? |
||
} | ||
|
||
println!("{stdout_res}"); | ||
} | ||
None => { | ||
wallet.register_sync().await?; | ||
interactive::run_loop(&mut wallet, &settings).await?; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you implement serde::Serialize for RunResult?