From 6b35ef24c2b4b20481d15a4f0b99c6e9345bb9a5 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 20 Jun 2019 22:58:58 +0200 Subject: [PATCH] Expose verified (and thus spendable) balance --- demo-www/index.html | 1 + demo-www/index.js | 11 ++++++++--- zcash-client-backend-wasm/src/lib.rs | 27 +++++++++++++++++++++++++++ zcash-client-sdk-js/src/index.js | 2 +- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/demo-www/index.html b/demo-www/index.html index bc1c323..179b9d4 100644 --- a/demo-www/index.html +++ b/demo-www/index.html @@ -14,6 +14,7 @@

That's your Zcash address!

+

diff --git a/demo-www/index.js b/demo-www/index.js index 1faa78b..652fe83 100644 --- a/demo-www/index.js +++ b/demo-www/index.js @@ -2,6 +2,7 @@ import { ZcashClient } from 'zcash-client-sdk' const address = document.getElementById('zcash-client-address') const balance = document.getElementById('zcash-client-balance') +const spendableBalance = document.getElementById('zcash-client-spendable-balance') const yesBalance = document.getElementById('zcash-client-yes-balance') const noBalance = document.getElementById('zcash-client-no-balance') const sendToAddress = document.getElementById('zcash-client-send-to-address') @@ -13,15 +14,19 @@ var zcashClient = new ZcashClient('http://localhost:8081', { setAddress: (newAddress) => { address.textContent = newAddress }, - updateBalance: (newBalance) => { + updateBalance: (newBalance, newVerifiedBalance) => { balance.textContent = `Balance: ${newBalance} TAZ` + spendableBalance.textContent = `Spendable: ${newVerifiedBalance} TAZ` if (newBalance > 0) { - yesBalance.style.display = '' noBalance.style.display = 'none' } else { - yesBalance.style.display = 'none' noBalance.style.display = '' } + if (newVerifiedBalance > 0) { + yesBalance.style.display = '' + } else { + yesBalance.style.display = 'none' + } }, updateSyncStatus: (syncedHeight, latestHeight) => { if (syncedHeight === latestHeight) { diff --git a/zcash-client-backend-wasm/src/lib.rs b/zcash-client-backend-wasm/src/lib.rs index f3589bf..6b08ea0 100644 --- a/zcash-client-backend-wasm/src/lib.rs +++ b/zcash-client-backend-wasm/src/lib.rs @@ -248,6 +248,33 @@ impl Client { .sum::() as u32 } + // TODO: This will be inaccurate if the balance exceeds a u32, but u64 -> JavaScript + // requires BigUint64Array which has limited support across browsers, and is not + // implemented in the LTS version of Node.js. For now, let's assume that no one is + // going to use a web wallet with more than ~21 TAZ. + pub fn verified_balance(&self) -> u32 { + let anchor_height = match self.get_target_height_and_anchor_offset() { + Some((height, anchor_offset)) => height - anchor_offset as u32, + None => return 0, + }; + + self.txs + .read() + .unwrap() + .values() + .map(|tx| { + if tx.block as u32 <= anchor_height { + tx.notes + .iter() + .map(|nd| if nd.spent.is_none() { nd.note.value } else { 0 }) + .sum::() + } else { + 0 + } + }) + .sum::() as u32 + } + pub fn scan_block(&self, block: &[u8]) -> bool { let block: CompactBlock = match parse_from_bytes(block) { Ok(block) => block, diff --git a/zcash-client-sdk-js/src/index.js b/zcash-client-sdk-js/src/index.js index 1bbfd03..0bdaee3 100644 --- a/zcash-client-sdk-js/src/index.js +++ b/zcash-client-sdk-js/src/index.js @@ -58,7 +58,7 @@ export class ZcashClient { } updateUI () { - this.uiHandlers.updateBalance(this.client.balance() / COIN) + this.uiHandlers.updateBalance(this.client.balance() / COIN, this.client.verified_balance() / COIN) } sync () {