From e0fcbbea3a63271ddf4d70c514ebff9b32bf870b Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sat, 27 Jan 2024 02:21:08 +0000 Subject: [PATCH] Refresh wallet transparent balance flow from `WalletSummary` Previously we only showed balance updates for the default address; this could potentially undercount transparent balance in some cases. We also now use the total zero-conf transparent balance for both "total" and "available", because we only allow transparent balance to be shielded, and we use a zero-conf transaction to do so. --- .../block/processor/CompactBlockProcessor.kt | 20 +++++++++---------- .../sdk/internal/model/AccountBalance.kt | 12 +++++++++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/block/processor/CompactBlockProcessor.kt b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/block/processor/CompactBlockProcessor.kt index 60bd4c29..4b47820c 100644 --- a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/block/processor/CompactBlockProcessor.kt +++ b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/block/processor/CompactBlockProcessor.kt @@ -715,15 +715,6 @@ class CompactBlockProcessor internal constructor( transactionStorage.invalidate() } - /** - * Calculate the latest Transparent balance, based on the blocks that have been scanned and transmit this - * information into the internal [transparentBalances] flow. - */ - internal suspend fun checkTransparentBalance() { - Twig.debug { "Checking Transparent balance" } - transparentBalances.value = getUtxoCacheBalance(getTransparentAddress(backend, Account.DEFAULT)) - } - /** * Update the latest balances using the given wallet summary, and transmit this information * into the related internal flows. Note that the Orchard balance is not supported. @@ -732,10 +723,17 @@ class CompactBlockProcessor internal constructor( summary.accountBalances[Account.DEFAULT]?.let { Twig.debug { "Updating Sapling balance" } saplingBalances.value = it.sapling - // TODO [#682]: refresh orchard balance + // TODO [#682]: Uncomment this once we have Orchard support. // TODO [#682]: https://github.com/zcash/zcash-android-wallet-sdk/issues/682 + // orchardBalances.value = it.orchard + // We only allow stored transparent balance to be shielded, and we do so with + // a zero-conf transaction, so treat all unshielded balance as available. + transparentBalances.value = + WalletBalance( + it.unshielded, + it.unshielded + ) } - checkTransparentBalance() } /** diff --git a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/model/AccountBalance.kt b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/model/AccountBalance.kt index 0a54ccd1..cbdece42 100644 --- a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/model/AccountBalance.kt +++ b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/model/AccountBalance.kt @@ -4,7 +4,9 @@ import cash.z.ecc.android.sdk.model.WalletBalance import cash.z.ecc.android.sdk.model.Zatoshi internal data class AccountBalance( - val sapling: WalletBalance + val sapling: WalletBalance, + val orchard: WalletBalance, + val unshielded: Zatoshi ) { companion object { fun new(jni: JniAccountBalance): AccountBalance { @@ -13,7 +15,13 @@ internal data class AccountBalance( WalletBalance( Zatoshi(jni.saplingTotalBalance), Zatoshi(jni.saplingVerifiedBalance) - ) + ), + orchard = + WalletBalance( + Zatoshi(jni.orchardTotalBalance), + Zatoshi(jni.orchardVerifiedBalance) + ), + unshielded = Zatoshi(jni.unshieldedBalance) ) } }