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

Commit

Permalink
fix: Balance update bug in other chains
Browse files Browse the repository at this point in the history
  • Loading branch information
reasje committed Oct 27, 2023
1 parent f031d70 commit 40da684
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class TransactionsHistoryUseCase extends ReactiveUseCase {

List<TransactionModel> getTransactionsHistory() => _repository.items;

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

List<String> updatingTxList = [];

void updateItem(
Expand Down Expand Up @@ -52,13 +54,15 @@ class TransactionsHistoryUseCase extends ReactiveUseCase {
if (!updatingTxList.contains(item.hash)) {
updatingTxList.add(item.hash);
final stream = _web3Repository.tokenContract.spyTransaction(item.hash);

stream.onData((succeeded) {
if (succeeded) {
final updatedItem = item.copyWith(status: TransactionStatus.done);
updateItem(
updatedItem,
);
updatingTxList.remove(item.hash);
update(shouldUpdateBalances, true);
stream.cancel();
}
});
Expand Down
39 changes: 28 additions & 11 deletions lib/features/wallet/presentation/wallet_page_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class WalletPresenter extends CompletePresenter<WalletState> {
super.initState();

getMXCTweets();
_transactionHistoryUseCase.checkForPendingTransactions(
_chainConfigurationUseCase.getCurrentNetworkWithoutRefresh().chainId);
checkForPendingTx();

listen(_accountUserCase.account, (value) {
if (value != null) {
Expand All @@ -52,14 +51,15 @@ class WalletPresenter extends CompletePresenter<WalletState> {
state.network = value;
connectAndSubscribe();
getTransactions();
resetBalanceUpdateStream();
}
});

listen(_transactionHistoryUseCase.transactionsHistory, (value) {
if (state.network != null) {
if (!Config.isMxcChains(state.network!.chainId)) {
getCustomChainsTransactions(value);
}
if (state.network != null &&
!Config.isMxcChains(state.network!.chainId)) {
getCustomChainsTransactions(value);
initBalanceUpdateStream();
}
});

Expand Down Expand Up @@ -230,12 +230,9 @@ class WalletPresenter extends CompletePresenter<WalletState> {
void getCustomChainsTransactions(List<TransactionModel>? txHistory) {
txHistory =
txHistory ?? _transactionHistoryUseCase.getTransactionsHistory();
final chainTxHistory = txHistory;

if (state.network != null) {
final chainTxHistory = txHistory;

notify(() => state.txList = chainTxHistory);
}
notify(() => state.txList = chainTxHistory);
}

void getMXCTransactions() async {
Expand Down Expand Up @@ -460,4 +457,24 @@ class WalletPresenter extends CompletePresenter<WalletState> {
notify(() => state.maxTweetViewHeight = height + 120);
}
}

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

void initBalanceUpdateStream() {
state.balancesUpdateSubscription ??=
listen(_transactionHistoryUseCase.shouldUpdateBalances, (value) {
if (value) initializeBalancePanelAndTokens();
});
}

void resetBalanceUpdateStream() {
if (Config.isMxcChains(state.network!.chainId) &&
state.balancesUpdateSubscription != null) {
state.balancesUpdateSubscription!.cancel();
state.balancesUpdateSubscription = null;
}
}
}
3 changes: 3 additions & 0 deletions lib/features/wallet/presentation/wallet_page_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class WalletState with EquatableMixin {

StreamSubscription<dynamic>? subscription;

/// This stream is only used for chains other than MXC
StreamSubscription<void>? balancesUpdateSubscription;

Network? network;

double maxTweetViewHeight = 620;
Expand Down

0 comments on commit 40da684

Please sign in to comment.