From b3495fe714e7cd10b1247073cdd48cc2e81913ca Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Wed, 18 Sep 2024 12:39:25 -0700 Subject: [PATCH] stopsync monero (TODO) --- cw_monero/lib/monero_wallet.dart | 205 +++++++++++++------------------ 1 file changed, 87 insertions(+), 118 deletions(-) diff --git a/cw_monero/lib/monero_wallet.dart b/cw_monero/lib/monero_wallet.dart index 8773d694d..97ff1b432 100644 --- a/cw_monero/lib/monero_wallet.dart +++ b/cw_monero/lib/monero_wallet.dart @@ -46,8 +46,8 @@ const MIN_RESTORE_HEIGHT = 1000; class MoneroWallet = MoneroWalletBase with _$MoneroWallet; -abstract class MoneroWalletBase extends WalletBase with Store { +abstract class MoneroWalletBase + extends WalletBase with Store { MoneroWalletBase( {required WalletInfo walletInfo, required Box unspentCoinsInfo, @@ -68,16 +68,13 @@ abstract class MoneroWalletBase extends WalletBase walletAddresses.account, (Account? account) { + _onAccountChangeReaction = reaction((_) => walletAddresses.account, (Account? account) { if (account == null) return; - balance = ObservableMap.of({ + balance = ObservableMap.of({ currency: MoneroBalance( fullBalance: monero_wallet.getFullBalance(accountIndex: account.id), - unlockedBalance: - monero_wallet.getUnlockedBalance(accountIndex: account.id)) + unlockedBalance: monero_wallet.getUnlockedBalance(accountIndex: account.id)) }); _updateSubAddress(isEnabledAutoGenerateSubaddress, account: account); _askForUpdateTransactionHistory(); @@ -123,8 +120,7 @@ abstract class MoneroWalletBase extends WalletBase - transactionHistory.transactions.values.firstOrNull?.height; + int? get restoreHeight => transactionHistory.transactions.values.firstOrNull?.height; monero_wallet.SyncListener? _listener; ReactionDisposer? _onAccountChangeReaction; @@ -136,13 +132,11 @@ abstract class MoneroWalletBase extends WalletBase init() async { await walletAddresses.init(); - balance = ObservableMap.of({ + balance = ObservableMap.of({ currency: MoneroBalance( - fullBalance: monero_wallet.getFullBalance( - accountIndex: walletAddresses.account!.id), - unlockedBalance: monero_wallet.getUnlockedBalance( - accountIndex: walletAddresses.account!.id)) + fullBalance: monero_wallet.getFullBalance(accountIndex: walletAddresses.account!.id), + unlockedBalance: + monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account!.id)) }); _setListeners(); await updateTransactions(); @@ -151,13 +145,12 @@ abstract class MoneroWalletBase extends WalletBase await save()); + _autoSaveTimer = + Timer.periodic(Duration(seconds: _autoSaveInterval), (_) async => await save()); } @override @@ -223,14 +216,22 @@ abstract class MoneroWalletBase extends WalletBase stopSync() async { + syncStatus = NotConnectedSyncStatus(); + _listener?.stop(); + // TODO: find a better way to stop syncing than setting an invalid address: + monero_wallet.setupNode(address: ""); + } + @override Future createTransaction(Object credentials) async { final _credentials = credentials as MoneroTransactionCreationCredentials; final inputs = []; final outputs = _credentials.outputs; final hasMultiDestination = outputs.length > 1; - final unlockedBalance = monero_wallet.getUnlockedBalance( - accountIndex: walletAddresses.account!.id); + final unlockedBalance = + monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account!.id); var allInputsAmount = 0; PendingTransactionDescription pendingTransactionDescription; @@ -252,20 +253,16 @@ abstract class MoneroWalletBase extends WalletBase item.sendAll || (item.formattedCryptoAmount ?? 0) <= 0)) { - throw MoneroTransactionCreationException( - 'You do not have enough XMR to send this amount.'); + if (outputs.any((item) => item.sendAll || (item.formattedCryptoAmount ?? 0) <= 0)) { + throw MoneroTransactionCreationException('You do not have enough XMR to send this amount.'); } - final int totalAmount = outputs.fold( - 0, (acc, value) => acc + (value.formattedCryptoAmount ?? 0)); + final int totalAmount = + outputs.fold(0, (acc, value) => acc + (value.formattedCryptoAmount ?? 0)); - final estimatedFee = - calculateEstimatedFee(_credentials.priority, totalAmount); + final estimatedFee = calculateEstimatedFee(_credentials.priority, totalAmount); if (unlockedBalance < totalAmount) { - throw MoneroTransactionCreationException( - 'You do not have enough XMR to send this amount.'); + throw MoneroTransactionCreationException('You do not have enough XMR to send this amount.'); } if (!spendAllCoins && (allInputsAmount < totalAmount + estimatedFee)) { @@ -273,28 +270,22 @@ abstract class MoneroWalletBase extends WalletBase changePassword(String password) async => - monero_wallet.setPasswordSync(password); + Future changePassword(String password) async => monero_wallet.setPasswordSync(password); Future getNodeHeight() async => monero_wallet.getNodeHeight(); @@ -547,15 +530,13 @@ abstract class MoneroWalletBase extends WalletBase _refreshUnspentCoinsInfo() async { try { final List keys = []; - final currentWalletUnspentCoins = unspentCoinsInfo.values.where( - (element) => - element.walletId.contains(id) && - element.accountIndex == walletAddresses.account!.id); + final currentWalletUnspentCoins = unspentCoinsInfo.values.where((element) => + element.walletId.contains(id) && element.accountIndex == walletAddresses.account!.id); if (currentWalletUnspentCoins.isNotEmpty) { currentWalletUnspentCoins.forEach((element) { - final existUnspentCoins = unspentCoins - .where((coin) => element.keyImage!.contains(coin.keyImage!)); + final existUnspentCoins = + unspentCoins.where((coin) => element.keyImage!.contains(coin.keyImage!)); if (existUnspentCoins.isEmpty) { keys.add(element.key); @@ -572,15 +553,13 @@ abstract class MoneroWalletBase extends WalletBase - monero_wallet.getAddress( - accountIndex: accountIndex, addressIndex: addressIndex); + monero_wallet.getAddress(accountIndex: accountIndex, addressIndex: addressIndex); @override Future> fetchTransactions() async { transaction_history.refreshTransactions(); return _getAllTransactionsOfAccount(walletAddresses.account?.id) - .fold>( - {}, + .fold>({}, (Map acc, MoneroTransactionInfo tx) { acc[tx.id] = tx; return acc; @@ -608,31 +587,28 @@ abstract class MoneroWalletBase extends WalletBase monero_wallet.getSubaddressLabel(accountIndex, addressIndex); - List _getAllTransactionsOfAccount(int? accountIndex) => - transaction_history - .getAllTransactions() - .map( - (row) => MoneroTransactionInfo( - row.hash, - row.blockheight, - row.isSpend - ? TransactionDirection.outgoing - : TransactionDirection.incoming, - row.timeStamp, - row.isPending, - row.amount, - row.accountIndex, - 0, - row.fee, - row.confirmations, - )..additionalInfo = { - 'key': row.key, - 'accountIndex': row.accountIndex, - 'addressIndex': row.addressIndex - }, - ) - .where((element) => element.accountIndex == (accountIndex ?? 0)) - .toList(); + List _getAllTransactionsOfAccount(int? accountIndex) => transaction_history + .getAllTransactions() + .map( + (row) => MoneroTransactionInfo( + row.hash, + row.blockheight, + row.isSpend ? TransactionDirection.outgoing : TransactionDirection.incoming, + row.timeStamp, + row.isPending, + row.amount, + row.accountIndex, + 0, + row.fee, + row.confirmations, + )..additionalInfo = { + 'key': row.key, + 'accountIndex': row.accountIndex, + 'addressIndex': row.addressIndex + }, + ) + .where((element) => element.accountIndex == (accountIndex ?? 0)) + .toList(); void _setListeners() { _listener?.stop(); @@ -666,8 +642,7 @@ abstract class MoneroWalletBase extends WalletBase _askForUpdateTransactionHistory() async => - await updateTransactions(); + Future _askForUpdateTransactionHistory() async => await updateTransactions(); - int _getFullBalance() => - monero_wallet.getFullBalance(accountIndex: walletAddresses.account!.id); + int _getFullBalance() => monero_wallet.getFullBalance(accountIndex: walletAddresses.account!.id); - int _getUnlockedBalance() => monero_wallet.getUnlockedBalance( - accountIndex: walletAddresses.account!.id); + int _getUnlockedBalance() => + monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account!.id); int _getFrozenBalance() { var frozenBalance = 0; for (var coin in unspentCoinsInfo.values.where((element) => - element.walletId == id && - element.accountIndex == walletAddresses.account!.id)) { + element.walletId == id && element.accountIndex == walletAddresses.account!.id)) { if (coin.isFrozen) frozenBalance += coin.value; } @@ -788,5 +758,4 @@ abstract class MoneroWalletBase extends WalletBase