Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix: Fixed pending tx update in different chains & tx history mix in …
Browse files Browse the repository at this point in the history
…different chains
  • Loading branch information
reasje committed Nov 10, 2023
1 parent 94fef9b commit c8c3a47
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,7 +24,25 @@ class TransactionsHistoryUseCase extends ReactiveUseCase {

late final ValueStream<bool> shouldUpdateBalances = reactive(false);

List<String> updatingTxList = [];
int? currentChainId;

Map<String, StreamSubscription<TransactionReceipt?>> updatingTransactions =
{};

initTransactionHistoryListening() {
transactionsHistory.listen(listenToTransactionsHistory);
}

listenToTransactionsHistory(List<TransactionModel> event) {
final selectedNetwork = _chainConfigurationUseCase.selectedNetwork.value;
if (selectedNetwork != null) {
final chainId = selectedNetwork.chainId;
checkChainChange(chainId);
if (!Config.isMxcChains(chainId)) {
checkForPendingTransactions();
}
}
}

void updateItem(
TransactionModel item,
Expand Down Expand Up @@ -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();
Expand All @@ -68,32 +95,31 @@ class TransactionsHistoryUseCase extends ReactiveUseCase {
updateItem(
updatedItem,
);
updatingTxList.remove(item.hash);
updatingTransactions.remove(item.hash);
update(shouldUpdateBalances, true);

stream.cancel();
}
});
}
}

/// 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
Expand All @@ -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;
}
}
1 change: 0 additions & 1 deletion lib/core/src/providers/providers_use_cases.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ final Provider<TransactionsHistoryUseCase> transactionHistoryUseCaseProvider =
(ref) => TransactionsHistoryUseCase(
ref.watch(datadashCacheProvider).transactionsHistoryRepository,
ref.watch(web3RepositoryProvider),
ref.watch(globalCacheProvider).chainConfigurationRepository,
ref.watch(chainConfigurationUseCaseProvider),
),
);
Expand Down
6 changes: 0 additions & 6 deletions lib/features/wallet/presentation/wallet_page_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class WalletPresenter extends CompletePresenter<WalletState> {
super.initState();

getMXCTweets();
checkForPendingTx();

listen(_accountUserCase.account, (value) {
if (value != null) {
Expand Down Expand Up @@ -431,11 +430,6 @@ class WalletPresenter extends CompletePresenter<WalletState> {
}
}

void checkForPendingTx() {
_transactionHistoryUseCase.checkForPendingTransactions(
_chainConfigurationUseCase.getCurrentNetworkWithoutRefresh().chainId);
}

void initBalanceUpdateStream() {
state.balancesUpdateSubscription ??=
listen(_transactionHistoryUseCase.shouldUpdateBalances, (value) {
Expand Down

0 comments on commit c8c3a47

Please sign in to comment.