From 4c7472f41d8941ec6892d7f12bee4b40ccaa09d7 Mon Sep 17 00:00:00 2001 From: reasje Date: Thu, 7 Dec 2023 13:03:45 +0330 Subject: [PATCH] feat: Added speed up & cancel to wallet page --- .../presentation/wallet_page_presenter.dart | 91 +++++++++++++++++++ packages/shared | 2 +- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/lib/features/wallet/presentation/wallet_page_presenter.dart b/lib/features/wallet/presentation/wallet_page_presenter.dart index 25cfedfd..415b5e9a 100644 --- a/lib/features/wallet/presentation/wallet_page_presenter.dart +++ b/lib/features/wallet/presentation/wallet_page_presenter.dart @@ -1,4 +1,5 @@ import 'package:datadashwallet/common/common.dart'; +import 'package:datadashwallet/common/components/recent_transactions/widgets/widgets.dart'; import 'package:datadashwallet/core/core.dart'; import 'package:datadashwallet/features/wallet/wallet.dart'; import 'package:fl_chart/fl_chart.dart'; @@ -384,4 +385,94 @@ class WalletPresenter extends CompletePresenter { state.balancesUpdateSubscription = null; } } + + // TODO: Dismiss bottom sheet when tx is done or maybe not since we check for It's receipt + + void cancelTransaction(TransactionModel transaction) async { + double totalFeeDouble = + MXCGas.calculateTotalFee(transaction.feePerGas!, transaction.gasLimit!); + String totalFee = totalFeeDouble.toString(); + totalFee = Formatter.checkExpoNumber(totalFee); + + double newGasPriceDouble = MXCGas.addExtraFeeForTxReplacement( + transaction.feePerGas!, + ); + + final totalMaxFeeDouble = + MXCGas.calculateTotalMaxFee(newGasPriceDouble, transaction.gasLimit!); + String totalMaxFee = totalMaxFeeDouble.toString(); + totalMaxFee = Formatter.checkExpoNumber(totalMaxFee); + + final result = await showCancelDialog(context!, + estimatedFee: totalFee, + maxFee: totalMaxFee, + symbol: state.network!.symbol); + + if (result ?? false) { + final result = await _tokenContractUseCase.cancelTransaction( + transaction, state.account!); + + // Find the index of transaction in list & change It's action + final transactionIndex = state.txList! + .indexWhere((element) => element.hash == transaction.hash); + + if (transactionIndex != -1) { + switch (transaction.action) { + case null: + notify(() => state.txList![transactionIndex] = + transaction.copyWith(action: TransactionActions.cancel)); + break; + case TransactionActions.speedUp: + notify(() => state.txList![transactionIndex] = + transaction.copyWith(action: TransactionActions.cancelSpeedUp)); + break; + default: + throw 'Unsupported transaction action'; + } + } + } + } + + void speedUpTransaction(TransactionModel transaction) async { + final totalFeeDouble = + MXCGas.calculateTotalFee(transaction.feePerGas!, transaction.gasLimit!); + String totalFee = totalFeeDouble.toString(); + totalFee = Formatter.checkExpoNumber(totalFee); + + final totalMaxFeeDouble = MXCGas.calculateTotalMaxFee( + transaction.feePerGas!, transaction.gasLimit!); + String totalMaxFee = totalMaxFeeDouble.toString(); + totalMaxFee = Formatter.checkExpoNumber(totalMaxFee); + + final result = await showSpeedUpDialog(context!, + estimatedFee: totalFee, + maxFee: totalMaxFee, + symbol: state.network!.symbol); + + if (result ?? false) { + final result = await _tokenContractUseCase.speedUpTransaction( + transaction, state.account!); + + // Find the index of transaction in list & change It's action + final transactionIndex = state.txList! + .indexWhere((element) => element.hash == transaction.hash); + + if (transactionIndex != -1) { + switch (transaction.action) { + case null: + notify(() => state.txList![transactionIndex] = + transaction.copyWith(action: TransactionActions.speedUp)); + break; + case TransactionActions.cancel: + notify(() => state.txList![transactionIndex] = + transaction.copyWith(action: TransactionActions.speedUpCancel)); + break; + default: + throw 'Unsupported transaction action'; + } + state.txList![transactionIndex] = + transaction.copyWith(action: TransactionActions.speedUp); + } + } + } } diff --git a/packages/shared b/packages/shared index ea1db74a..f18f152c 160000 --- a/packages/shared +++ b/packages/shared @@ -1 +1 @@ -Subproject commit ea1db74afb06d909712d5f65130bd8f549fd0d59 +Subproject commit f18f152cdd41a57c1f1d0d94e2298606ef05938e