diff --git a/cw_core/lib/get_height_by_date.dart b/cw_core/lib/get_height_by_date.dart index 768a5534f..819990e0a 100644 --- a/cw_core/lib/get_height_by_date.dart +++ b/cw_core/lib/get_height_by_date.dart @@ -122,6 +122,8 @@ int getMoneroHeigthByDate({required DateTime date}) { } const havenDates = { + "2023-05": 1352995, + "2023-04": 1331460, "2023-03": 1309180, "2023-01": 1266810, "2022-12": 1244510, diff --git a/lib/view_model/wallet_keys_view_model.dart b/lib/view_model/wallet_keys_view_model.dart index 0a9ae60a7..d5a19832c 100644 --- a/lib/view_model/wallet_keys_view_model.dart +++ b/lib/view_model/wallet_keys_view_model.dart @@ -1,4 +1,6 @@ import 'package:cake_wallet/store/app_store.dart'; +import 'package:cw_core/transaction_direction.dart'; +import 'package:cw_core/transaction_info.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:mobx/mobx.dart'; import 'package:cake_wallet/generated/i18n.dart'; @@ -19,12 +21,26 @@ abstract class WalletKeysViewModelBase with Store { ? S.current.wallet_seed : S.current.wallet_keys, _restoreHeight = _appStore.wallet!.walletInfo.restoreHeight, + _restoreHeightByTransactions = 0, items = ObservableList() { _populateItems(); reaction((_) => _appStore.wallet, (WalletBase? _wallet) { _populateItems(); }); + + if (_appStore.wallet!.type == WalletType.monero || _appStore.wallet!.type == WalletType.haven) { + final accountTransactions = _getWalletTransactions(_appStore.wallet!); + if (accountTransactions.isNotEmpty) { + final incomingAccountTransactions = + accountTransactions.where((tx) => tx.direction == TransactionDirection.incoming); + if (incomingAccountTransactions.isNotEmpty) { + incomingAccountTransactions.toList().sort((a, b) => a.date.compareTo(b.date)); + _restoreHeightByTransactions = _getRestoreHeightByTransactions( + _appStore.wallet!.type, incomingAccountTransactions.first.date); + } + } + } } final ObservableList items; @@ -35,6 +51,8 @@ abstract class WalletKeysViewModelBase with Store { final int _restoreHeight; + int _restoreHeightByTransactions; + void _populateItems() { items.clear(); @@ -78,7 +96,7 @@ abstract class WalletKeysViewModelBase with Store { } } - Future currentHeight() async { + Future _currentHeight() async { if (_appStore.wallet!.type == WalletType.haven) { return await haven!.getCurrentHeight(); } @@ -88,7 +106,6 @@ abstract class WalletKeysViewModelBase with Store { return null; } - String get _scheme { switch (_appStore.wallet!.type) { case WalletType.monero: @@ -105,14 +122,14 @@ abstract class WalletKeysViewModelBase with Store { } Future get restoreHeight async { - if (_restoreHeight != 0) { - return _restoreHeight.toString(); - } - final _currentHeight = await currentHeight(); - if (_currentHeight == null) { - return null; - } - return ((_currentHeight / 1000).floor() * 1000).toString(); + if (_restoreHeightByTransactions != 0) + return getRoundedRestoreHeight(_restoreHeightByTransactions); + if (_restoreHeight != 0) return _restoreHeight.toString(); + + final currentHeight = await _currentHeight(); + if (currentHeight == null) return null; + + return getRoundedRestoreHeight(currentHeight); } Future> get _queryParams async { @@ -123,10 +140,28 @@ abstract class WalletKeysViewModelBase with Store { }; } - Future get url async { - return Uri( - scheme: _scheme, - queryParameters: await _queryParams, - ); + Future get url async => Uri( + scheme: _scheme, + queryParameters: await _queryParams, + ); + + List _getWalletTransactions(WalletBase wallet) { + if (wallet.type == WalletType.monero) { + return monero!.getTransactionHistory(wallet).transactions.values.toList(); + } else if (wallet.type == WalletType.haven) { + return haven!.getTransactionHistory(wallet).transactions.values.toList(); + } + return []; } + + int _getRestoreHeightByTransactions(WalletType type, DateTime date) { + if (type == WalletType.monero) { + return monero!.getHeigthByDate(date: date); + } else if (type == WalletType.haven) { + return haven!.getHeightByDate(date: date); + } + return 0; + } + + String getRoundedRestoreHeight(int height) => ((height / 1000).floor() * 1000).toString(); }