From c8c3a47e1d12d5bae9581bd50a6abb25a7b28ce8 Mon Sep 17 00:00:00 2001 From: reasje Date: Fri, 10 Nov 2023 10:56:29 +0330 Subject: [PATCH] fix: Fixed pending tx update in different chains & tx history mix in different chains --- .../domain/transactions_history_use_case.dart | 76 ++++++++++++++----- .../src/providers/providers_use_cases.dart | 1 - .../presentation/wallet_page_presenter.dart | 6 -- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/lib/common/components/recent_transactions/domain/transactions_history_use_case.dart b/lib/common/components/recent_transactions/domain/transactions_history_use_case.dart index 0ab28540..94ed03bf 100644 --- a/lib/common/components/recent_transactions/domain/transactions_history_use_case.dart +++ b/lib/common/components/recent_transactions/domain/transactions_history_use_case.dart @@ -1,12 +1,19 @@ +import 'dart:async'; + import 'package:datadashwallet/common/common.dart'; import 'package:datadashwallet/core/core.dart'; +import 'package:datadashwallet/features/settings/subfeatures/chain_configuration/domain/chain_configuration_use_case.dart'; import 'package:mxc_logic/mxc_logic.dart'; import 'package:web3dart/web3dart.dart'; class TransactionsHistoryUseCase extends ReactiveUseCase { - TransactionsHistoryUseCase(this._repository, this._web3Repository); + TransactionsHistoryUseCase( + this._repository, this._web3Repository, this._chainConfigurationUseCase) { + initTransactionHistoryListening(); + } final Web3Repository _web3Repository; + final ChainConfigurationUseCase _chainConfigurationUseCase; final TransactionsHistoryRepository _repository; @@ -17,7 +24,25 @@ class TransactionsHistoryUseCase extends ReactiveUseCase { late final ValueStream shouldUpdateBalances = reactive(false); - List updatingTxList = []; + int? currentChainId; + + Map> updatingTransactions = + {}; + + initTransactionHistoryListening() { + transactionsHistory.listen(listenToTransactionsHistory); + } + + listenToTransactionsHistory(List event) { + final selectedNetwork = _chainConfigurationUseCase.selectedNetwork.value; + if (selectedNetwork != null) { + final chainId = selectedNetwork.chainId; + checkChainChange(chainId); + if (!Config.isMxcChains(chainId)) { + checkForPendingTransactions(); + } + } + } void updateItem( TransactionModel item, @@ -52,13 +77,15 @@ class TransactionsHistoryUseCase extends ReactiveUseCase { void spyOnTransaction( TransactionModel item, ) { - if (!updatingTxList.contains(item.hash)) { - updatingTxList.add(item.hash); - final stream = _web3Repository.tokenContract.spyTransaction(item.hash); + if (!updatingTransactions.keys.contains(item.hash)) { + updatingTransactions[item.hash] = + _web3Repository.tokenContract.spyTransaction(item.hash); - stream.onData((receipt) { + updatingTransactions[item.hash]?.onData((receipt) { if (receipt?.status ?? false) { // success + updatingTransactions[item.hash]?.cancel(); + final itemValue = item.value ?? (receipt!.gasUsed! * receipt.effectiveGasPrice!.getInWei) .toString(); @@ -68,10 +95,8 @@ class TransactionsHistoryUseCase extends ReactiveUseCase { updateItem( updatedItem, ); - updatingTxList.remove(item.hash); + updatingTransactions.remove(item.hash); update(shouldUpdateBalances, true); - - stream.cancel(); } }); } @@ -79,21 +104,22 @@ class TransactionsHistoryUseCase extends ReactiveUseCase { /// This function will run through all the transactions and will start spying on /// pending transactions - void checkForPendingTransactions(int chainId) { - if (!Config.isMxcChains(chainId)) { - final txList = transactionsHistory.value; - final pendingTxList = txList - .where((element) => element.status == TransactionStatus.pending); - for (TransactionModel pendingTx in pendingTxList) { - spyOnTransaction( - pendingTx, - ); - } + void checkForPendingTransactions() async { + final txList = transactionsHistory.value; + final pendingTxList = + txList.where((element) => element.status == TransactionStatus.pending); + for (TransactionModel pendingTx in pendingTxList) { + spyOnTransaction( + pendingTx, + ); } } void spyOnUnknownTransaction( - String hash, String address, Token token, int chainId) async { + String hash, + String address, + Token token, + ) async { TransactionInformation? receipt; receipt = await _web3Repository.tokenContract @@ -107,4 +133,14 @@ class TransactionsHistoryUseCase extends ReactiveUseCase { ); } } + + void checkChainChange(int chainId) async { + if (currentChainId != chainId) { + for (String txHash in updatingTransactions.keys) { + await updatingTransactions[txHash]?.cancel(); + } + updatingTransactions.clear(); + } + currentChainId = chainId; + } } diff --git a/lib/core/src/providers/providers_use_cases.dart b/lib/core/src/providers/providers_use_cases.dart index 4590cbe0..96af3901 100644 --- a/lib/core/src/providers/providers_use_cases.dart +++ b/lib/core/src/providers/providers_use_cases.dart @@ -127,7 +127,6 @@ final Provider transactionHistoryUseCaseProvider = (ref) => TransactionsHistoryUseCase( ref.watch(datadashCacheProvider).transactionsHistoryRepository, ref.watch(web3RepositoryProvider), - ref.watch(globalCacheProvider).chainConfigurationRepository, ref.watch(chainConfigurationUseCaseProvider), ), ); diff --git a/lib/features/wallet/presentation/wallet_page_presenter.dart b/lib/features/wallet/presentation/wallet_page_presenter.dart index cb3bcb62..2166976d 100644 --- a/lib/features/wallet/presentation/wallet_page_presenter.dart +++ b/lib/features/wallet/presentation/wallet_page_presenter.dart @@ -31,7 +31,6 @@ class WalletPresenter extends CompletePresenter { super.initState(); getMXCTweets(); - checkForPendingTx(); listen(_accountUserCase.account, (value) { if (value != null) { @@ -431,11 +430,6 @@ class WalletPresenter extends CompletePresenter { } } - void checkForPendingTx() { - _transactionHistoryUseCase.checkForPendingTransactions( - _chainConfigurationUseCase.getCurrentNetworkWithoutRefresh().chainId); - } - void initBalanceUpdateStream() { state.balancesUpdateSubscription ??= listen(_transactionHistoryUseCase.shouldUpdateBalances, (value) {