diff --git a/rust-lightclient/src/lightclient.rs b/rust-lightclient/src/lightclient.rs index 42022da..1f9720d 100644 --- a/rust-lightclient/src/lightclient.rs +++ b/rust-lightclient/src/lightclient.rs @@ -96,8 +96,14 @@ impl LightClient { // Collect t addresses let t_addresses = self.wallet.tkeys.iter().map( |pk| { + let address = LightWallet::address_from_pk(&pk); + + // Get the balance for this address + let balance = self.wallet.tbalance(Some(address.clone())); + object!{ - "address" => LightWallet::address_from_pk(&pk), + "address" => address, + "balance" => balance, } }).collect::>(); diff --git a/rust-lightclient/src/lightwallet.rs b/rust-lightclient/src/lightwallet.rs index 6cad9cb..b064529 100644 --- a/rust-lightclient/src/lightwallet.rs +++ b/rust-lightclient/src/lightwallet.rs @@ -25,8 +25,8 @@ use zcash_primitives::{ serialize::{Vector, Optional}, transaction::{ builder::{Builder}, - components::Amount, components::amount::DEFAULT_FEE, - TxId, Transaction + components::{Amount, OutPoint}, components::amount::DEFAULT_FEE, + TxId, Transaction, }, legacy::{TransparentAddress::PublicKey}, note_encryption::{Memo, try_sapling_note_decryption}, @@ -338,6 +338,12 @@ pub struct Utxo { pub unconfirmed_spent: Option, // If this utxo was spent in a send, but has not yet been confirmed. } +impl Into for Utxo { + fn into(self) -> OutPoint { + OutPoint { hash: self.txid.0, n: self.output_index as u32 } + } +} + pub struct WalletTx { pub block: i32, @@ -654,13 +660,10 @@ impl LightWallet { } pub fn balance(&self, addr: Option) -> u64 { - self.txs - .read() - .unwrap() + self.txs.read().unwrap() .values() .map(|tx| { - tx.notes - .iter() + tx.notes.iter() .filter(|nd| { // TODO, this whole section is shared with verified_balance. Refactor it. match addr.clone() { Some(a) => a == encode_payment_address( @@ -677,6 +680,23 @@ impl LightWallet { .sum::() } + pub fn tbalance(&self, addr: Option) -> u64 { + self.txs.read().unwrap() + .values() + .map( |tx| { + tx.utxos.iter() + .filter( |utxo| { + match addr.clone() { + Some(a) => a == utxo.address, + None => true + } + }) + .map (|utxo| if utxo.spent.is_none() {utxo.value} else {0}) + .sum::() + }) + .sum::() + } + pub fn verified_balance(&self, addr: Option) -> u64 { let anchor_height = match self.get_target_height_and_anchor_offset() { Some((height, anchor_offset)) => height - anchor_offset as u32,