diff --git a/rust-lightclient/src/lightclient.rs b/rust-lightclient/src/lightclient.rs index cb72ae4..f7cede6 100644 --- a/rust-lightclient/src/lightclient.rs +++ b/rust-lightclient/src/lightclient.rs @@ -193,20 +193,18 @@ impl LightClient { }); // Collect UTXOs - let utxos = self.wallet.txs.read().unwrap().iter() - .flat_map( |(_, wtx)| { - wtx.utxos.iter().map(move |utxo| { - object!{ - "created_in_block" => wtx.block, - "created_in_txid" => format!("{}", utxo.txid), - "value" => utxo.value, - "scriptkey" => hex::encode(utxo.script.clone()), - "is_change" => false, // TODO: Identify notes as change - "address" => utxo.address.clone(), - "spent" => utxo.spent.map(|spent_txid| format!("{}", spent_txid)), - "unconfirmed_spent" => utxo.unconfirmed_spent.map(|spent_txid| format!("{}", spent_txid)), - } - }) + let utxos = self.wallet.utxos.read().unwrap().iter() + .map(|utxo| { + object!{ + "created_in_block" => utxo.height, + "created_in_txid" => format!("{}", utxo.txid), + "value" => utxo.value, + "scriptkey" => hex::encode(utxo.script.clone()), + "is_change" => false, // TODO: Identify notes as change + "address" => utxo.address.clone(), + "spent" => utxo.spent.map(|spent_txid| format!("{}", spent_txid)), + "unconfirmed_spent" => utxo.unconfirmed_spent.map(|spent_txid| format!("{}", spent_txid)), + } }) .collect::>(); @@ -250,7 +248,7 @@ impl LightClient { }); } - // For each note that is not a change, add a Tx. + // For each sapling note that is not a change, add a Tx. txns.extend(v.notes.iter() .filter( |nd| !nd.is_change ) .map ( |nd| @@ -271,6 +269,19 @@ impl LightClient { }) ); + // Get the total transparent recieved + let total_transparent_received = v.utxos.iter().map(|u| u.value).sum::(); + if total_transparent_received > v.total_transparent_value_spent { + // Create a input transaction for the transparent value as well. + txns.push(object!{ + "block_height" => v.block, + "txid" => format!("{}", v.txid), + "amount" => total_transparent_received as i64 - v.total_transparent_value_spent as i64, + "address" => v.utxos.iter().map(|u| u.address.clone()).collect::>().join(","), + "memo" => None:: + }) + } + txns }) .collect::>(); diff --git a/rust-lightclient/src/lightwallet.rs b/rust-lightclient/src/lightwallet.rs index 0d7d24b..84989c8 100644 --- a/rust-lightclient/src/lightwallet.rs +++ b/rust-lightclient/src/lightwallet.rs @@ -538,7 +538,7 @@ pub struct LightWallet { pub tkeys: Vec, // Current UTXOs that can be spent - utxos: Arc>>, + pub utxos: Arc>>, blocks: Arc>>, pub txs: Arc>>, @@ -801,19 +801,14 @@ impl LightWallet { } 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::() + self.utxos.read().unwrap().iter() + .filter(|utxo| { + match addr.clone() { + Some(a) => utxo.address == a, + None => true, + } }) + .map(|utxo| utxo.value ) .sum::() } @@ -1206,19 +1201,18 @@ impl LightWallet { // Specifically, if you send an outgoing transaction that is sent to a shielded address, // ZecWallet will add all your t-address funds into that transaction, and send them to your shielded // address as change. - let tinputs = self.txs.read().unwrap().iter() - .flat_map(|(_, wtx)| { - wtx.utxos.iter().map(|utxo| { - let outpoint: OutPoint = utxo.to_outpoint(); + let tinputs = self.utxos.read().unwrap().iter() + .map(|utxo| { + let outpoint: OutPoint = utxo.to_outpoint(); - let coin = TxOut { - value: Amount::from_u64(utxo.value).unwrap(), - script_pubkey: Script { 0: utxo.script.clone() }, - }; + let coin = TxOut { + value: Amount::from_u64(utxo.value).unwrap(), + script_pubkey: Script { 0: utxo.script.clone() }, + }; - (outpoint, coin) - }) - }).collect::>(); + (outpoint, coin) + }) + .collect::>(); if let Err(e) = match to { address::RecipientAddress::Shielded(_) => {