From 840287c72dfe00618581df7b45cd63fbb0d96c91 Mon Sep 17 00:00:00 2001 From: fossephate Date: Tue, 24 Sep 2024 12:57:12 -0700 Subject: [PATCH] review fixes, additional cleanup --- cw_bitcoin/lib/electrum_balance.dart | 7 +- cw_bitcoin/lib/electrum_wallet.dart | 8 +-- cw_bitcoin/lib/litecoin_wallet.dart | 70 +++++++------------ .../screens/receive/widgets/address_list.dart | 2 +- .../dashboard/balance_view_model.dart | 11 --- lib/view_model/send/send_view_model.dart | 11 ++- .../wallet_address_list_view_model.dart | 20 ++++-- 7 files changed, 54 insertions(+), 75 deletions(-) diff --git a/cw_bitcoin/lib/electrum_balance.dart b/cw_bitcoin/lib/electrum_balance.dart index fb0f059d8..3367511e7 100644 --- a/cw_bitcoin/lib/electrum_balance.dart +++ b/cw_bitcoin/lib/electrum_balance.dart @@ -48,15 +48,14 @@ class ElectrumBalance extends Balance { } @override - String get formattedSecondAvailableBalance => bitcoinAmountToString(amount: secondConfirmed ?? 0); + String get formattedSecondAvailableBalance => bitcoinAmountToString(amount: secondConfirmed); @override - String get formattedSecondAdditionalBalance => - bitcoinAmountToString(amount: secondUnconfirmed ?? 0); + String get formattedSecondAdditionalBalance => bitcoinAmountToString(amount: secondUnconfirmed); @override String get formattedFullAvailableBalance => - bitcoinAmountToString(amount: confirmed + (secondConfirmed ?? 0) - frozen); + bitcoinAmountToString(amount: confirmed + secondConfirmed - frozen); String toJSON() => json.encode({ 'confirmed': confirmed, diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index d726d1c05..cf46ae2c8 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1357,13 +1357,7 @@ abstract class ElectrumWalletBase List> unspents = []; List updatedUnspentCoins = []; - try { - unspents = await electrumClient.getListUnspent(address.getScriptHash(network)); - } catch (e, s) { - print(e); - print(s); - return []; - } + unspents = await electrumClient.getListUnspent(address.getScriptHash(network)); await Future.wait(unspents.map((unspent) async { try { diff --git a/cw_bitcoin/lib/litecoin_wallet.dart b/cw_bitcoin/lib/litecoin_wallet.dart index a4ac7258b..1152603e5 100644 --- a/cw_bitcoin/lib/litecoin_wallet.dart +++ b/cw_bitcoin/lib/litecoin_wallet.dart @@ -97,7 +97,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { late final Bip32Slip10Secp256k1 mwebHd; late final Box mwebUtxosBox; Timer? _syncTimer; - Timer? _stuckSyncTimer; Timer? _feeRatesTimer; StreamSubscription? _utxoStream; late RpcClient _stub; @@ -233,6 +232,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { Future waitForMwebAddresses() async { // ensure that we have the full 1000 mweb addresses generated before continuing: + // should no longer be needed, but leaving here just in case final mwebAddrs = (walletAddresses as LitecoinWalletAddresses).mwebAddrs; while (mwebAddrs.length < 1000) { print("waiting for mweb addresses to finish generating..."); @@ -243,31 +243,35 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { @action @override Future startSync() async { + print("STARTING SYNC @@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); + if (syncStatus is SyncronizingSyncStatus) { + return; + } print("STARTING SYNC - MWEB ENABLED: $mwebEnabled"); - syncStatus = SyncronizingSyncStatus(); - await subscribeForUpdates(); + try { + syncStatus = SyncronizingSyncStatus(); + await subscribeForUpdates(); - await updateFeeRates(); - _feeRatesTimer?.cancel(); - _feeRatesTimer = - Timer.periodic(const Duration(minutes: 1), (timer) async => await updateFeeRates()); + await updateFeeRates(); + _feeRatesTimer?.cancel(); + _feeRatesTimer = + Timer.periodic(const Duration(minutes: 1), (timer) async => await updateFeeRates()); - if (!mwebEnabled) { - try { - await updateAllUnspents(); - await updateTransactions(); - await updateBalance(); - syncStatus = SyncedSyncStatus(); - } catch (e, s) { - print(e); - print(s); - syncStatus = FailedSyncStatus(); + if (!mwebEnabled) { + try { + await updateAllUnspents(); + await updateTransactions(); + await updateBalance(); + syncStatus = SyncedSyncStatus(); + } catch (e, s) { + print(e); + print(s); + syncStatus = FailedSyncStatus(); + } + return; } - return; - } - await waitForMwebAddresses(); - try { + await waitForMwebAddresses(); await getStub(); await processMwebUtxos(); await updateTransactions(); @@ -318,26 +322,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { return; } }); - - // setup a watch dog to restart the sync process if it gets stuck: - List lastFewProgresses = []; - _stuckSyncTimer?.cancel(); - _stuckSyncTimer = Timer.periodic(const Duration(seconds: 10), (timer) async { - if (syncStatus is! SyncingSyncStatus) return; - if (syncStatus.progress() > 0.98) return; // don't check if we're close to synced - lastFewProgresses.add(syncStatus.progress()); - if (lastFewProgresses.length < 10) return; - // limit list size to 10: - while (lastFewProgresses.length > 10) { - lastFewProgresses.removeAt(0); - } - // if the progress is the same over the last 100 seconds, restart the sync: - if (lastFewProgresses.every((p) => p == lastFewProgresses.first)) { - print("mweb syncing is stuck, restarting..."); - syncStatus = LostConnectionSyncStatus(); - await stopSync(); - } - }); } @action @@ -345,7 +329,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { Future stopSync() async { _syncTimer?.cancel(); _utxoStream?.cancel(); - _stuckSyncTimer?.cancel(); _feeRatesTimer?.cancel(); await CwMweb.stop(); } @@ -913,7 +896,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { final addresses = {}; transaction.inputAddresses?.forEach((id) async { final utxo = mwebUtxosBox.get(id); - // await mwebUtxosBox.delete(id); + // await mwebUtxosBox.delete(id);// gets deleted in checkMwebUtxosSpent if (utxo == null) return; final addressRecord = walletAddresses.allAddresses .firstWhere((addressRecord) => addressRecord.address == utxo.address); @@ -946,7 +929,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { @override Future close() async { _utxoStream?.cancel(); - _stuckSyncTimer?.cancel(); _feeRatesTimer?.cancel(); _syncTimer?.cancel(); await stopSync(); diff --git a/lib/src/screens/receive/widgets/address_list.dart b/lib/src/screens/receive/widgets/address_list.dart index de01f6879..27ec8c33a 100644 --- a/lib/src/screens/receive/widgets/address_list.dart +++ b/lib/src/screens/receive/widgets/address_list.dart @@ -38,7 +38,7 @@ class AddressList extends StatelessWidget { separatorBuilder: (context, _) => const HorizontalSectionDivider(), shrinkWrap: true, physics: NeverScrollableScrollPhysics(), - itemCount: min(addressListViewModel.items.length, 100),// TODO: don't show all 1000 mweb addresses + itemCount: addressListViewModel.items.length, itemBuilder: (context, index) { final item = addressListViewModel.items[index]; Widget cell = Container(); diff --git a/lib/view_model/dashboard/balance_view_model.dart b/lib/view_model/dashboard/balance_view_model.dart index 255bcc511..c3fb5718a 100644 --- a/lib/view_model/dashboard/balance_view_model.dart +++ b/lib/view_model/dashboard/balance_view_model.dart @@ -235,17 +235,6 @@ abstract class BalanceViewModelBase with Store { return walletBalance.formattedAdditionalBalance; } - @computed - String get secondAdditionalBalance { - final walletBalance = _walletBalance; - - if (displayMode == BalanceDisplayMode.hiddenBalance) { - return '---'; - } - - return walletBalance.formattedSecondAdditionalBalance; - } - @computed String get availableFiatBalance { final walletBalance = _walletBalance; diff --git a/lib/view_model/send/send_view_model.dart b/lib/view_model/send/send_view_model.dart index 79f473eb3..4edaad872 100644 --- a/lib/view_model/send/send_view_model.dart +++ b/lib/view_model/send/send_view_model.dart @@ -217,7 +217,16 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor PendingTransaction? pendingTransaction; @computed - String get balance => wallet.balance[selectedCryptoCurrency]!.formattedFullAvailableBalance; + String get balance { + String fullFormattedBalance = + wallet.balance[selectedCryptoCurrency]!.formattedFullAvailableBalance; + String formattedAvailableBalance = + wallet.balance[selectedCryptoCurrency]!.formattedAvailableBalance; + if (fullFormattedBalance.isNotEmpty) { + return fullFormattedBalance; + } + return formattedAvailableBalance; + } @computed bool get isFiatDisabled => balanceViewModel.isFiatDisabled; diff --git a/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart b/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart index 919d7baad..b8274ce7c 100644 --- a/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart +++ b/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:cake_wallet/bitcoin/bitcoin.dart'; import 'package:cake_wallet/core/wallet_change_listener_view_model.dart'; import 'package:cake_wallet/entities/auto_generate_subaddress_status.dart'; @@ -217,8 +219,9 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo }) : _baseItems = [], selectedCurrency = walletTypeToCryptoCurrency(appStore.wallet!.type), _cryptoNumberFormat = NumberFormat(_cryptoNumberPattern), - hasAccounts = - appStore.wallet!.type == WalletType.monero || appStore.wallet!.type == WalletType.wownero || appStore.wallet!.type == WalletType.haven, + hasAccounts = appStore.wallet!.type == WalletType.monero || + appStore.wallet!.type == WalletType.wownero || + appStore.wallet!.type == WalletType.haven, amount = '', _settingsStore = appStore.settingsStore, super(appStore: appStore) { @@ -230,7 +233,9 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo _init(); selectedCurrency = walletTypeToCryptoCurrency(wallet.type); - hasAccounts = wallet.type == WalletType.monero || wallet.type == WalletType.wownero || wallet.type == WalletType.haven; + hasAccounts = wallet.type == WalletType.monero || + wallet.type == WalletType.wownero || + wallet.type == WalletType.haven; } static const String _cryptoNumberPattern = '0.00000000'; @@ -446,7 +451,7 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo address: wallet.walletAddresses.address, )); } - + if (wallet.type == WalletType.tron) { final primaryAddress = tron!.getAddress(wallet); @@ -519,8 +524,7 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo @action Future setAddressType(dynamic option) async { - if (wallet.type == WalletType.bitcoin || - wallet.type == WalletType.litecoin) { + if (wallet.type == WalletType.bitcoin || wallet.type == WalletType.litecoin) { await bitcoin!.setAddressType(wallet, option); } } @@ -528,7 +532,9 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo void _init() { _baseItems = []; - if (wallet.type == WalletType.monero || wallet.type == WalletType.wownero || wallet.type == WalletType.haven) { + if (wallet.type == WalletType.monero || + wallet.type == WalletType.wownero || + wallet.type == WalletType.haven) { _baseItems.add(WalletAccountListHeader()); }