From 1170f742e962f5b0f42c441753d8bd4dbda8d54b Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 12 Jan 2023 13:21:03 -0600 Subject: [PATCH] use balance caching in all wallets --- lib/hive/db.dart | 1 + .../coins/bitcoin/bitcoin_wallet.dart | 3 ++- .../coins/dogecoin/dogecoin_wallet.dart | 3 ++- .../coins/epiccash/epiccash_wallet.dart | 4 ++- lib/services/coins/firo/firo_wallet.dart | 7 +++-- .../coins/litecoin/litecoin_wallet.dart | 3 ++- lib/services/coins/monero/monero_wallet.dart | 3 ++- .../coins/namecoin/namecoin_wallet.dart | 3 ++- .../coins/particl/particl_wallet.dart | 3 ++- .../coins/wownero/wownero_wallet.dart | 3 ++- lib/services/mixins/wallet_cache.dart | 26 +++++++++++++++++++ 11 files changed, 49 insertions(+), 10 deletions(-) diff --git a/lib/hive/db.dart b/lib/hive/db.dart index 962f56405..f5e031972 100644 --- a/lib/hive/db.dart +++ b/lib/hive/db.dart @@ -248,6 +248,7 @@ class DB { abstract class DBKeys { static const String cachedBalance = "cachedBalance"; + static const String cachedBalanceSecondary = "cachedBalanceSecondary"; static const String isFavorite = "isFavorite"; static const String id = "id"; static const String storedChainHeight = "storedChainHeight"; diff --git a/lib/services/coins/bitcoin/bitcoin_wallet.dart b/lib/services/coins/bitcoin/bitcoin_wallet.dart index 392728835..5dae3eb58 100644 --- a/lib/services/coins/bitcoin/bitcoin_wallet.dart +++ b/lib/services/coins/bitcoin/bitcoin_wallet.dart @@ -1810,6 +1810,7 @@ class BitcoinWallet extends CoinServiceAPI with WalletCache, WalletDB { blockedTotal: satoshiBalanceBlocked, pendingSpendable: satoshiBalancePending, ); + await updateCachedBalance(walletId, _balance!); } catch (e, s) { Logging.instance .log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error); @@ -1817,7 +1818,7 @@ class BitcoinWallet extends CoinServiceAPI with WalletCache, WalletDB { } @override - Balance get balance => _balance!; + Balance get balance => _balance ??= getCachedBalance(walletId, coin); Balance? _balance; // /// Takes in a list of UtxoObjects and adds a name (dependent on object index within list) diff --git a/lib/services/coins/dogecoin/dogecoin_wallet.dart b/lib/services/coins/dogecoin/dogecoin_wallet.dart index f7b8209b8..4a787bd7f 100644 --- a/lib/services/coins/dogecoin/dogecoin_wallet.dart +++ b/lib/services/coins/dogecoin/dogecoin_wallet.dart @@ -1552,6 +1552,7 @@ class DogecoinWallet extends CoinServiceAPI with WalletCache, WalletDB { blockedTotal: satoshiBalanceBlocked, pendingSpendable: satoshiBalancePending, ); + await updateCachedBalance(walletId, _balance!); } catch (e, s) { Logging.instance .log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error); @@ -1559,7 +1560,7 @@ class DogecoinWallet extends CoinServiceAPI with WalletCache, WalletDB { } @override - Balance get balance => _balance!; + Balance get balance => _balance ??= getCachedBalance(walletId, coin); Balance? _balance; // /// Takes in a list of UtxoObjects and adds a name (dependent on object index within list) diff --git a/lib/services/coins/epiccash/epiccash_wallet.dart b/lib/services/coins/epiccash/epiccash_wallet.dart index a29d1fa49..32344d623 100644 --- a/lib/services/coins/epiccash/epiccash_wallet.dart +++ b/lib/services/coins/epiccash/epiccash_wallet.dart @@ -2284,10 +2284,12 @@ class EpicCashWallet extends CoinServiceAPI with WalletCache, WalletDB { coin, ), ); + + await updateCachedBalance(walletId, _balance!); } @override - Balance get balance => _balance!; + Balance get balance => _balance ??= getCachedBalance(walletId, coin); Balance? _balance; @override diff --git a/lib/services/coins/firo/firo_wallet.dart b/lib/services/coins/firo/firo_wallet.dart index 17a905384..e3ecfd787 100644 --- a/lib/services/coins/firo/firo_wallet.dart +++ b/lib/services/coins/firo/firo_wallet.dart @@ -2459,6 +2459,7 @@ class FiroWallet extends CoinServiceAPI with WalletCache, WalletDB { blockedTotal: 0, pendingSpendable: unconfirmedLelantusBalance + balance.total, ); + await updateCachedBalanceSecondary(walletId, _balancePrivate!); // _balance = Balance( // coin: coin, // total: utxos.satoshiBalance, @@ -3660,6 +3661,7 @@ class FiroWallet extends CoinServiceAPI with WalletCache, WalletDB { blockedTotal: satoshiBalanceBlocked, pendingSpendable: satoshiBalancePending, ); + await updateCachedBalance(walletId, _balance!); } catch (e, s) { Logging.instance .log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error); @@ -4843,10 +4845,11 @@ class FiroWallet extends CoinServiceAPI with WalletCache, WalletDB { } @override - Balance get balance => _balance!; + Balance get balance => _balance ??= getCachedBalance(walletId, coin); Balance? _balance; - Balance get balancePrivate => _balancePrivate!; + Balance get balancePrivate => + _balancePrivate ??= getCachedBalanceSecondary(walletId, coin); Balance? _balancePrivate; @override diff --git a/lib/services/coins/litecoin/litecoin_wallet.dart b/lib/services/coins/litecoin/litecoin_wallet.dart index c99c9f285..a19ea6eed 100644 --- a/lib/services/coins/litecoin/litecoin_wallet.dart +++ b/lib/services/coins/litecoin/litecoin_wallet.dart @@ -1796,6 +1796,7 @@ class LitecoinWallet extends CoinServiceAPI with WalletCache, WalletDB { blockedTotal: satoshiBalanceBlocked, pendingSpendable: satoshiBalancePending, ); + await updateCachedBalance(walletId, _balance!); } catch (e, s) { Logging.instance .log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error); @@ -1803,7 +1804,7 @@ class LitecoinWallet extends CoinServiceAPI with WalletCache, WalletDB { } @override - Balance get balance => _balance!; + Balance get balance => _balance ??= getCachedBalance(walletId, coin); Balance? _balance; // /// Takes in a list of UtxoObjects and adds a name (dependent on object index within list) diff --git a/lib/services/coins/monero/monero_wallet.dart b/lib/services/coins/monero/monero_wallet.dart index af3acfb82..a606e7f6a 100644 --- a/lib/services/coins/monero/monero_wallet.dart +++ b/lib/services/coins/monero/monero_wallet.dart @@ -734,6 +734,7 @@ class MoneroWallet extends CoinServiceAPI with WalletCache, WalletDB { blockedTotal: 0, pendingSpendable: total - available, ); + await updateCachedBalance(walletId, _balance!); } Future get _availableBalance async { @@ -1214,7 +1215,7 @@ class MoneroWallet extends CoinServiceAPI with WalletCache, WalletDB { int get storedChainHeight => throw UnimplementedError(); @override - Balance get balance => _balance!; + Balance get balance => _balance ??= getCachedBalance(walletId, coin); Balance? _balance; @override diff --git a/lib/services/coins/namecoin/namecoin_wallet.dart b/lib/services/coins/namecoin/namecoin_wallet.dart index d9c62b303..94bcc2ff6 100644 --- a/lib/services/coins/namecoin/namecoin_wallet.dart +++ b/lib/services/coins/namecoin/namecoin_wallet.dart @@ -1776,6 +1776,7 @@ class NamecoinWallet extends CoinServiceAPI with WalletCache, WalletDB { blockedTotal: satoshiBalanceBlocked, pendingSpendable: satoshiBalancePending, ); + await updateCachedBalance(walletId, _balance!); } catch (e, s) { Logging.instance .log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error); @@ -1783,7 +1784,7 @@ class NamecoinWallet extends CoinServiceAPI with WalletCache, WalletDB { } @override - Balance get balance => _balance!; + Balance get balance => _balance ??= getCachedBalance(walletId, coin); Balance? _balance; // /// Takes in a list of UtxoObjects and adds a name (dependent on object index within list) diff --git a/lib/services/coins/particl/particl_wallet.dart b/lib/services/coins/particl/particl_wallet.dart index 435450610..c315e7c12 100644 --- a/lib/services/coins/particl/particl_wallet.dart +++ b/lib/services/coins/particl/particl_wallet.dart @@ -1665,6 +1665,7 @@ class ParticlWallet extends CoinServiceAPI with WalletCache, WalletDB { blockedTotal: satoshiBalanceBlocked, pendingSpendable: satoshiBalancePending, ); + await updateCachedBalance(walletId, _balance!); } catch (e, s) { Logging.instance .log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error); @@ -1672,7 +1673,7 @@ class ParticlWallet extends CoinServiceAPI with WalletCache, WalletDB { } @override - Balance get balance => _balance!; + Balance get balance => _balance ??= getCachedBalance(walletId, coin); Balance? _balance; // /// Takes in a list of UtxoObjects and adds a name (dependent on object index within list) diff --git a/lib/services/coins/wownero/wownero_wallet.dart b/lib/services/coins/wownero/wownero_wallet.dart index f7d254b01..ac71df11d 100644 --- a/lib/services/coins/wownero/wownero_wallet.dart +++ b/lib/services/coins/wownero/wownero_wallet.dart @@ -744,6 +744,7 @@ class WowneroWallet extends CoinServiceAPI with WalletCache, WalletDB { blockedTotal: 0, pendingSpendable: total - available, ); + await updateCachedBalance(walletId, _balance!); } Future get _availableBalance async { @@ -1284,7 +1285,7 @@ class WowneroWallet extends CoinServiceAPI with WalletCache, WalletDB { int get storedChainHeight => throw UnimplementedError(); @override - Balance get balance => _balance!; + Balance get balance => _balance ??= getCachedBalance(walletId, coin); Balance? _balance; @override diff --git a/lib/services/mixins/wallet_cache.dart b/lib/services/mixins/wallet_cache.dart index 6d085c734..c838dc684 100644 --- a/lib/services/mixins/wallet_cache.dart +++ b/lib/services/mixins/wallet_cache.dart @@ -27,4 +27,30 @@ mixin WalletCache { value: balance.toJsonIgnoreCoin(), ); } + + Balance getCachedBalanceSecondary(String walletId, Coin coin) { + final jsonString = DB.instance.get( + boxName: walletId, + key: DBKeys.cachedBalanceSecondary, + ) as String?; + if (jsonString == null) { + return Balance( + coin: coin, + total: 0, + spendable: 0, + blockedTotal: 0, + pendingSpendable: 0, + ); + } + return Balance.fromJson(jsonString, coin); + } + + Future updateCachedBalanceSecondary( + String walletId, Balance balance) async { + await DB.instance.put( + boxName: walletId, + key: DBKeys.cachedBalanceSecondary, + value: balance.toJsonIgnoreCoin(), + ); + } }