Option to print only unspent notes and utxos

This commit is contained in:
Aditya Kulkarni
2019-09-12 20:47:23 -07:00
parent d71e37152b
commit 4febeda516
2 changed files with 40 additions and 16 deletions

View File

@@ -176,7 +176,6 @@ impl Command for TransactionsCommand {
let txns = lightclient.do_list_transactions(); let txns = lightclient.do_list_transactions();
println!("{}", txns.pretty(2)); println!("{}", txns.pretty(2));
} }
} }
@@ -190,11 +189,27 @@ impl Command for NotesCommand {
"List all sapling notes in the wallet".to_string() "List all sapling notes in the wallet".to_string()
} }
fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { fn exec(&self, args: &[&str], lightclient: &mut LightClient) {
let txns = lightclient.do_list_notes(); // Parse the args.
if args.len() > 1 {
self.help();
return;
}
// Make sure we can parse the amount
let all_notes = if args.len() == 1 {
match args[0] {
"all" => true,
_ => { println!("Invalid argument. Specify 'all' to include unspent notes");
return; }
}
} else {
false
};
let txns = lightclient.do_list_notes(all_notes);
println!("{}", txns.pretty(2)); println!("{}", txns.pretty(2));
} }
} }

View File

@@ -157,7 +157,7 @@ impl LightClient {
} }
// Return a list of all notes, spent and unspent // Return a list of all notes, spent and unspent
pub fn do_list_notes(&self) -> JsonValue { pub fn do_list_notes(&self, all_notes: bool) -> JsonValue {
let mut unspent_notes: Vec<JsonValue> = vec![]; let mut unspent_notes: Vec<JsonValue> = vec![];
let mut spent_notes : Vec<JsonValue> = vec![]; let mut spent_notes : Vec<JsonValue> = vec![];
let mut pending_notes: Vec<JsonValue> = vec![]; let mut pending_notes: Vec<JsonValue> = vec![];
@@ -165,15 +165,19 @@ impl LightClient {
// Collect Sapling notes // Collect Sapling notes
self.wallet.txs.read().unwrap().iter() self.wallet.txs.read().unwrap().iter()
.flat_map( |(txid, wtx)| { .flat_map( |(txid, wtx)| {
wtx.notes.iter().map(move |nd| wtx.notes.iter().filter_map(move |nd|
object!{ if !all_notes && nd.spent.is_some() {
"created_in_block" => wtx.block, None
"created_in_txid" => format!("{}", txid), } else {
"value" => nd.note.value, Some(object!{
"is_change" => nd.is_change, "created_in_block" => wtx.block,
"address" => LightWallet::address_from_extfvk(&nd.extfvk, nd.diversifier), "created_in_txid" => format!("{}", txid),
"spent" => nd.spent.map(|spent_txid| format!("{}", spent_txid)), "value" => nd.note.value,
"unconfirmed_spent" => nd.unconfirmed_spent.map(|spent_txid| format!("{}", spent_txid)), "is_change" => nd.is_change,
"address" => LightWallet::address_from_extfvk(&nd.extfvk, nd.diversifier),
"spent" => nd.spent.map(|spent_txid| format!("{}", spent_txid)),
"unconfirmed_spent" => nd.unconfirmed_spent.map(|spent_txid| format!("{}", spent_txid)),
})
} }
) )
}) })
@@ -205,12 +209,17 @@ impl LightClient {
}) })
.collect::<Vec<JsonValue>>(); .collect::<Vec<JsonValue>>();
object!{ let mut res = object!{
"spent_notes" => spent_notes,
"unspent_notes" => unspent_notes, "unspent_notes" => unspent_notes,
"pending_notes" => pending_notes, "pending_notes" => pending_notes,
"utxos" => utxos, "utxos" => utxos,
};
if all_notes {
res["spent_notes"] = JsonValue::Array(spent_notes);
} }
res
} }
pub fn do_list_transactions(&self) -> JsonValue { pub fn do_list_transactions(&self) -> JsonValue {