From 57add19e885a335398984656bbff0a4b3a199514 Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 2 Jan 2023 09:13:00 -0600 Subject: [PATCH] do not partially open monero wallets at the same time on startup --- .../my_stack_view/coin_wallets_table.dart | 8 +++ lib/services/coins/monero/monero_wallet.dart | 55 +++++++++++++------ .../coins/wownero/wownero_wallet.dart | 55 +++++++++++++------ lib/services/wallets.dart | 4 +- 4 files changed, 86 insertions(+), 36 deletions(-) diff --git a/lib/pages_desktop_specific/my_stack_view/coin_wallets_table.dart b/lib/pages_desktop_specific/my_stack_view/coin_wallets_table.dart index a0c561e3c..073ea73b4 100644 --- a/lib/pages_desktop_specific/my_stack_view/coin_wallets_table.dart +++ b/lib/pages_desktop_specific/my_stack_view/coin_wallets_table.dart @@ -60,6 +60,14 @@ class CoinWalletsTable extends ConsumerWidget { ref.read(currentWalletIdProvider.state).state = walletIds[i]; + final manager = ref + .read(walletsChangeNotifierProvider) + .getManager(walletIds[i]); + if (manager.coin == Coin.monero || + manager.coin == Coin.wownero) { + await manager.initializeExisting(); + } + await Navigator.of(context).pushNamed( DesktopWalletView.routeName, arguments: walletIds[i], diff --git a/lib/services/coins/monero/monero_wallet.dart b/lib/services/coins/monero/monero_wallet.dart index caf41c34d..f0869259b 100644 --- a/lib/services/coins/monero/monero_wallet.dart +++ b/lib/services/coins/monero/monero_wallet.dart @@ -792,27 +792,48 @@ class MoneroWallet extends CoinServiceAPI { } }; + Future _updateCachedBalance(int sats) async { + await DB.instance.put( + boxName: walletId, + key: "cachedMoneroBalanceSats", + value: sats, + ); + } + + int _getCachedBalance() => + DB.instance.get( + boxName: walletId, + key: "cachedMoneroBalanceSats", + ) as int? ?? + 0; + @override Future get totalBalance async { - final balanceEntries = walletBase?.balance?.entries; - if (balanceEntries != null) { - int bal = 0; - for (var element in balanceEntries) { - bal = bal + element.value.fullBalance; - } - return Format.satoshisToAmount(bal, coin: coin); - } else { - final transactions = walletBase!.transactionHistory!.transactions; - int transactionBalance = 0; - for (var tx in transactions!.entries) { - if (tx.value.direction == TransactionDirection.incoming) { - transactionBalance += tx.value.amount!; - } else { - transactionBalance += -tx.value.amount! - tx.value.fee!; + try { + final balanceEntries = walletBase?.balance?.entries; + if (balanceEntries != null) { + int bal = 0; + for (var element in balanceEntries) { + bal = bal + element.value.fullBalance; + } + await _updateCachedBalance(bal); + return Format.satoshisToAmount(bal, coin: coin); + } else { + final transactions = walletBase!.transactionHistory!.transactions; + int transactionBalance = 0; + for (var tx in transactions!.entries) { + if (tx.value.direction == TransactionDirection.incoming) { + transactionBalance += tx.value.amount!; + } else { + transactionBalance += -tx.value.amount! - tx.value.fee!; + } } - } - return Format.satoshisToAmount(transactionBalance, coin: coin); + await _updateCachedBalance(transactionBalance); + return Format.satoshisToAmount(transactionBalance, coin: coin); + } + } catch (_) { + return Format.satoshisToAmount(_getCachedBalance(), coin: coin); } } diff --git a/lib/services/coins/wownero/wownero_wallet.dart b/lib/services/coins/wownero/wownero_wallet.dart index 470df05e3..d21ebab8c 100644 --- a/lib/services/coins/wownero/wownero_wallet.dart +++ b/lib/services/coins/wownero/wownero_wallet.dart @@ -820,27 +820,48 @@ class WowneroWallet extends CoinServiceAPI { } }; + Future _updateCachedBalance(int sats) async { + await DB.instance.put( + boxName: walletId, + key: "cachedWowneroBalanceSats", + value: sats, + ); + } + + int _getCachedBalance() => + DB.instance.get( + boxName: walletId, + key: "cachedWowneroBalanceSats", + ) as int? ?? + 0; + @override Future get totalBalance async { - final balanceEntries = walletBase?.balance?.entries; - if (balanceEntries != null) { - int bal = 0; - for (var element in balanceEntries) { - bal = bal + element.value.fullBalance; - } - return Format.satoshisToAmount(bal, coin: coin); - } else { - final transactions = walletBase!.transactionHistory!.transactions; - int transactionBalance = 0; - for (var tx in transactions!.entries) { - if (tx.value.direction == TransactionDirection.incoming) { - transactionBalance += tx.value.amount!; - } else { - transactionBalance += -tx.value.amount! - tx.value.fee!; + try { + final balanceEntries = walletBase?.balance?.entries; + if (balanceEntries != null) { + int bal = 0; + for (var element in balanceEntries) { + bal = bal + element.value.fullBalance; + } + await _updateCachedBalance(bal); + return Format.satoshisToAmount(bal, coin: coin); + } else { + final transactions = walletBase!.transactionHistory!.transactions; + int transactionBalance = 0; + for (var tx in transactions!.entries) { + if (tx.value.direction == TransactionDirection.incoming) { + transactionBalance += tx.value.amount!; + } else { + transactionBalance += -tx.value.amount! - tx.value.fee!; + } } - } - return Format.satoshisToAmount(transactionBalance, coin: coin); + await _updateCachedBalance(transactionBalance); + return Format.satoshisToAmount(transactionBalance, coin: coin); + } + } catch (_) { + return Format.satoshisToAmount(_getCachedBalance(), coin: coin); } } diff --git a/lib/services/wallets.dart b/lib/services/wallets.dart index faed86f75..9d1b97cee 100644 --- a/lib/services/wallets.dart +++ b/lib/services/wallets.dart @@ -238,7 +238,7 @@ class Wallets extends ChangeNotifier { walletIdsToEnableAutoSync.contains(manager.walletId); if (manager.coin == Coin.monero || manager.coin == Coin.wownero) { - walletsToInitLinearly.add(Tuple2(manager, shouldSetAutoSync)); + // walletsToInitLinearly.add(Tuple2(manager, shouldSetAutoSync)); } else { walletInitFutures.add(manager.initializeExisting().then((value) { if (shouldSetAutoSync) { @@ -328,7 +328,7 @@ class Wallets extends ChangeNotifier { walletIdsToEnableAutoSync.contains(manager.walletId); if (manager.coin == Coin.monero || manager.coin == Coin.wownero) { - walletsToInitLinearly.add(Tuple2(manager, shouldSetAutoSync)); + // walletsToInitLinearly.add(Tuple2(manager, shouldSetAutoSync)); } else { walletInitFutures.add(manager.initializeExisting().then((value) { if (shouldSetAutoSync) {