Skip to content

Commit

Permalink
Merge pull request #14 from ergoplatform/wallet-status-change-none
Browse files Browse the repository at this point in the history
parse change address in wallet status as None if empty string;
  • Loading branch information
greenhat authored Mar 14, 2023
2 parents e500abb + f7cf90b commit 9fbb87c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ thiserror = "1.0.22"
blake2b_simd = "0.5.11"
base16 = "0.2.1"
yaml-rust = "0.4.4"
serde_with = { version = "1.14", features = ["json"] }
5 changes: 1 addition & 4 deletions src/local_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ pub fn acquire_node_interface_from_local_config() -> NodeInterface {
}
// Error checking reading the local node interface yaml
if let Err(e) = new_interface_from_local_config() {
println!(
"Could not parse local `node-interface.yaml` file.\nError: {:?}",
e
);
println!("Could not parse local `node-interface.yaml` file.\nError: {e:?}");
std::process::exit(0);
}
// Create `NodeInterface`
Expand Down
26 changes: 22 additions & 4 deletions src/node_interface.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/// The `NodeInterface` struct is defined which allows for interacting with an
/// Ergo Node via Rust.
//! The `NodeInterface` struct is defined which allows for interacting with an Ergo Node via Rust.
use crate::{BlockHeight, NanoErg, P2PKAddressString, P2SAddressString};
use ergo_lib::ergotree_ir::chain::ergo_box::ErgoBox;
use reqwest::Url;
use serde_json::from_str;
use serde_with::serde_as;
use serde_with::NoneAsEmptyString;
use thiserror::Error;

pub type Result<T> = std::result::Result<T, NodeError>;
Expand Down Expand Up @@ -105,7 +107,7 @@ impl NodeInterface {
let mut n = 0;
for address in &address_list {
n += 1;
println!("{}. {}", n, address);
println!("{n}. {address}");
}
println!("Which address would you like to select?");
let mut input = String::new();
Expand Down Expand Up @@ -365,13 +367,15 @@ impl NodeInterface {
}
}

#[serde_as]
#[derive(serde::Deserialize, serde::Serialize)]
pub struct WalletStatus {
#[serde(rename = "isInitialized")]
pub initialized: bool,
#[serde(rename = "isUnlocked")]
pub unlocked: bool,
#[serde(rename = "changeAddress")]
#[serde_as(as = "NoneAsEmptyString")]
pub change_address: Option<P2PKAddressString>,
#[serde(rename = "walletHeight")]
pub height: BlockHeight,
Expand All @@ -384,7 +388,7 @@ mod tests {
use super::*;

#[test]
fn test_name() {
fn test_parsing_wallet_status_unlocked() {
let node_response_json_str = r#"{
"isInitialized": true,
"isUnlocked": true,
Expand All @@ -395,4 +399,18 @@ mod tests {
let t: WalletStatus = serde_json::from_str(node_response_json_str).unwrap();
assert_eq!(t.height, 251965);
}

#[test]
fn test_parsing_wallet_status_locked() {
let node_response_json_str = r#"{
"isInitialized": true,
"isUnlocked": false,
"changeAddress": "",
"walletHeight": 251965,
"error": ""
}"#;
let t: WalletStatus = serde_json::from_str(node_response_json_str).unwrap();
assert_eq!(t.change_address, None);
assert_eq!(t.height, 251965);
}
}
2 changes: 1 addition & 1 deletion src/scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl NodeInterface {
if let Ok(ergo_box) = res_ergo_box {
box_list.push(ergo_box);
} else if let Err(e) = res_ergo_box {
let mess = format!("Box Json: {}\nError: {:?}", box_json, e);
let mess = format!("Box Json: {box_json}\nError: {e:?}");
return Err(NodeError::FailedParsingBox(mess));
}
}
Expand Down

0 comments on commit 9fbb87c

Please sign in to comment.