diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 0a569625e..c220f6d39 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -234,6 +234,11 @@ abstract class ElectrumWalletBase return _currentChainTip ?? await electrumClient.getCurrentBlockChainTip() ?? 0; } + Future getUpdatedChainTip() async { + _currentChainTip = await electrumClient.getCurrentBlockChainTip(); + return _currentChainTip ?? 0; + } + @override BitcoinWalletKeys get keys => BitcoinWalletKeys(wif: hd.wif!, privateKey: hd.privKey!, publicKey: hd.pubKey!); @@ -262,10 +267,10 @@ abstract class ElectrumWalletBase @action Future _setListeners(int height, {int? chainTipParam, bool? doSingleScan}) async { - final chainTip = chainTipParam ?? await getCurrentChainTip(); + final chainTip = chainTipParam ?? await getUpdatedChainTip(); if (chainTip == height) { - syncStatus = NotConnectedSyncStatus(); + syncStatus = SyncedSyncStatus(); return; } @@ -1744,6 +1749,11 @@ abstract class ElectrumWalletBase Future _setInitialHeight() async { if (_chainTipUpdateSubject != null) return; + if ((_currentChainTip == null || _currentChainTip! == 0) && walletInfo.restoreHeight == 0) { + await getUpdatedChainTip(); + await walletInfo.updateRestoreHeight(_currentChainTip!); + } + _chainTipUpdateSubject = electrumClient.chainTipSubscribe(); _chainTipUpdateSubject?.listen((e) async { final event = e as Map; @@ -1751,10 +1761,6 @@ abstract class ElectrumWalletBase _currentChainTip = height; - if (_currentChainTip != null && _currentChainTip! > 0 && walletInfo.restoreHeight == 0) { - await walletInfo.updateRestoreHeight(_currentChainTip!); - } - if (alwaysScan == true && syncStatus is SyncedSyncStatus) { _setListeners(walletInfo.restoreHeight); } @@ -1980,10 +1986,16 @@ Future startRefresh(ScanData scanData) async { ); if (tweakHeight >= scanData.chainTip || scanData.isSingleScan) { - scanData.sendPort.send(SyncResponse( - syncHeight, - SyncedTipSyncStatus(scanData.chainTip), - )); + if (tweakHeight >= scanData.chainTip) + scanData.sendPort.send(SyncResponse( + syncHeight, + SyncedTipSyncStatus(scanData.chainTip), + )); + + if (scanData.isSingleScan) { + scanData.sendPort.send(SyncResponse(syncHeight, SyncedSyncStatus())); + } + await tweaksSubscription!.close(); await electrumClient.close(); } diff --git a/cw_core/lib/get_height_by_date.dart b/cw_core/lib/get_height_by_date.dart index 3d23d24af..a3dd51b68 100644 --- a/cw_core/lib/get_height_by_date.dart +++ b/cw_core/lib/get_height_by_date.dart @@ -268,19 +268,31 @@ int getBitcoinHeightByDate({required DateTime date}) { String dateKey = '${date.year}-${date.month.toString().padLeft(2, '0')}'; final closestKey = bitcoinDates.keys .firstWhere((key) => formatMapKey(key).isBefore(date), orElse: () => bitcoinDates.keys.last); - int startBlock = bitcoinDates[dateKey] ?? bitcoinDates[closestKey]!; + final beginningBlock = bitcoinDates[dateKey] ?? bitcoinDates[closestKey]!; - DateTime startOfMonth = DateTime(date.year, date.month); - int daysDifference = date.difference(startOfMonth).inDays; + final startOfMonth = DateTime(date.year, date.month); + final daysDifference = date.difference(startOfMonth).inDays; // approximately 6 blocks per hour, 24 hours per day int estimatedBlocksSinceStartOfMonth = (daysDifference * 24 * 6); - return startBlock + estimatedBlocksSinceStartOfMonth; + return beginningBlock + estimatedBlocksSinceStartOfMonth; } DateTime getDateByBitcoinHeight(int height) { - final date = bitcoinDates.entries - .lastWhere((entry) => entry.value >= height, orElse: () => bitcoinDates.entries.last); - return formatMapKey(date.key); + final closestEntry = bitcoinDates.entries + .lastWhere((entry) => entry.value >= height, orElse: () => bitcoinDates.entries.first); + final beginningBlock = closestEntry.value; + + final startOfMonth = formatMapKey(closestEntry.key); + final blocksDifference = height - beginningBlock; + final hoursDifference = blocksDifference / 5.5; + + final estimatedDate = startOfMonth.add(Duration(hours: hoursDifference.ceil())); + + if (estimatedDate.isAfter(DateTime.now())) { + return DateTime.now(); + } + + return estimatedDate; } diff --git a/lib/src/widgets/setting_actions.dart b/lib/src/widgets/setting_actions.dart index 895fcd97d..6fbdb6868 100644 --- a/lib/src/widgets/setting_actions.dart +++ b/lib/src/widgets/setting_actions.dart @@ -14,10 +14,10 @@ class SettingActions { }); static List all = [ - silentPaymentsSettingAction, connectionSettingAction, walletSettingAction, addressBookSettingAction, + silentPaymentsSettingAction, securityBackupSettingAction, privacySettingAction, displaySettingAction,