Browse Source

Add taddr balances

checkpoints
Aditya Kulkarni 5 years ago
parent
commit
3f75371500
  1. 8
      rust-lightclient/src/lightclient.rs
  2. 34
      rust-lightclient/src/lightwallet.rs

8
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::<Vec<JsonValue>>();

34
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<TxId>, // If this utxo was spent in a send, but has not yet been confirmed.
}
impl Into<OutPoint> 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<String>) -> 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::<u64>()
}
pub fn tbalance(&self, addr: Option<String>) -> 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::<u64>()
})
.sum::<u64>()
}
pub fn verified_balance(&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