Browse Source

Add unconfirmed_zbalance to the balance command.

danger
DenioD 4 years ago
parent
commit
6d583f6135
  1. 6
      lib/src/lightclient.rs
  2. 38
      lib/src/lightwallet.rs

6
lib/src/lightclient.rs

@ -627,8 +627,9 @@ impl LightClient {
object!{
"address" => zaddress.clone(),
"zbalance" => wallet.zbalance(Some(zaddress.clone())),
"verified_zbalance" => wallet.verified_zbalance(Some(zaddress.clone())),
"spendable_zbalance" => wallet.spendable_zbalance(Some(zaddress.clone()))
"verified_zbalance" => wallet.verified_zbalance(Some(zaddress.clone())),
"spendable_zbalance" => wallet.spendable_zbalance(Some(zaddress.clone())),
"unconfirmed_zbalance" => wallet.unconfirmed_zbalance(Some(zaddress.clone()))
}
}).collect::<Vec<JsonValue>>();
@ -647,6 +648,7 @@ impl LightClient {
"zbalance" => wallet.zbalance(None),
"verified_zbalance" => wallet.verified_zbalance(None),
"spendable_zbalance" => wallet.spendable_zbalance(None),
"unconfirmed_zbalance" => wallet.unconfirmed_zbalance(None),
"tbalance" => wallet.tbalance(None),
"z_addresses" => z_addresses,
"t_addresses" => t_addresses,

38
lib/src/lightwallet.rs

@ -1099,6 +1099,44 @@ impl LightWallet {
.sum::<u64>() as u64
}
pub fn unconfirmed_zbalance(&self, addr: Option<String>) -> u64 {
let anchor_height = match self.get_target_height_and_anchor_offset() {
Some((height, anchor_offset)) => height - anchor_offset as u32 - 1,
None => return 0,
};
self.txs
.read()
.unwrap()
.values()
.map(|tx| {
tx.notes
.iter()
.filter(|nd| nd.spent.is_none() && nd.unconfirmed_spent.is_none())
.filter(|nd| { // TODO, this whole section is shared with verified_balance. Refactor it.
match addr.clone() {
Some(a) => a == encode_payment_address(
self.config.hrp_sapling_address(),
&nd.extfvk.fvk.vk
.into_payment_address(nd.diversifier, &JUBJUB).unwrap()
),
None => true
}
})
.map(|nd| {
if tx.block as u32 <= anchor_height {
// If confirmed, then unconfirmed is 0
0
} else {
// If confirmed but dont have anchor yet, it is unconfirmed
nd.note.value
}
})
.sum::<u64>()
})
.sum::<u64>()
}
pub fn verified_zbalance(&self, addr: Option<String>) -> u64 {
let anchor_height = match self.get_target_height_and_anchor_offset() {
Some((height, anchor_offset)) => height - anchor_offset as u32 ,

Loading…
Cancel
Save