From 505ff537e6c21a25e1230c68a2a911437aa12d60 Mon Sep 17 00:00:00 2001 From: Nuo Xu Date: Wed, 11 Dec 2024 17:27:39 -0500 Subject: [PATCH] determine the nature of a transaction: none/original, cancel, speed up --- .../Stores/TransactionConfirmationStore.swift | 64 ++++++++++++++----- .../Stores/TransactionStatusStore.swift | 2 +- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/ios/brave-ios/Sources/BraveWallet/Crypto/Stores/TransactionConfirmationStore.swift b/ios/brave-ios/Sources/BraveWallet/Crypto/Stores/TransactionConfirmationStore.swift index 0b0b15e8af0c..9879f4f3907d 100644 --- a/ios/brave-ios/Sources/BraveWallet/Crypto/Stores/TransactionConfirmationStore.swift +++ b/ios/brave-ios/Sources/BraveWallet/Crypto/Stores/TransactionConfirmationStore.swift @@ -394,22 +394,7 @@ public class TransactionConfirmationStore: ObservableObject, WalletObserverStore } @MainActor func openActiveTxStatusStore() async -> TransactionStatusStore { - var followUpAction = TransactionStatusStore.FollowUpAction.none - if case .ethSend(let detail) = activeParsedTransaction.details, - let fromValue = BDouble(detail.fromAmount), - fromValue == 0, activeTransaction.ethTxData.isEmpty - { - // loop through allTx to find if there is a tx that has the same chain id, coin type, nonce and from address - // maxFeePerGas/gasPrice is smaller than this active tx - if let txInfo = allTxs.first(where: { - $0.id != activeTransaction.id && $0.coin == activeTransaction.coin - && $0.chainId == activeTransaction.chainId - && $0.ethTxNonce == activeTransaction.ethTxNonce - && $0.fromAddress == activeTransaction.fromAddress - }), let parsedTx = await parseTransaction(txInfo) { - followUpAction = .cancel(toCancelParsedTx: parsedTx) - } - } + let followUpAction = await determineFollowUpAction() let txStatusStore = TransactionStatusStore( activeTxStatus: activeTxStatus, activeTxParsed: activeParsedTransaction, @@ -436,6 +421,53 @@ public class TransactionConfirmationStore: ObservableObject, WalletObserverStore return true } + @MainActor private func determineFollowUpAction() async -> TransactionStatusStore.FollowUpAction { + if case .ethSend(let detail) = activeParsedTransaction.details, + let fromValue = BDouble(detail.fromAmount), + fromValue == 0, activeTransaction.ethTxData.isEmpty + { + // loop through allTx to find if there is a tx that has the same chain id, coin type, nonce and from address + // gasFee of this active tx should be bigger than the original one + if let txInfo = allTxs.first(where: { + $0.id != activeTransaction.id + && $0.coin == activeTransaction.coin + && $0.chainId == activeTransaction.chainId + && $0.ethTxNonce == activeTransaction.ethTxNonce + && $0.fromAddress == activeTransaction.fromAddress + && activeTransaction.ethTxData.isEmpty + }), let parsedTx = await parseTransaction(txInfo) { + if let activeTxGasFee = BDouble(activeParsedTransaction.gasFee?.fee ?? "0"), + let originalTxGasFee = BDouble(parsedTx.gasFee?.fee ?? "0"), + activeTxGasFee > originalTxGasFee + { + return .cancel(toCancelParsedTx: parsedTx) + } + } + } else if activeTransaction.coin == .eth { + // loop through allTx to find if there is a tx that has the same chain id, coin type, nonce, from address, + // to address, data and value + // gasFee of this active tx should be bigger than the original one + if let txInfo = allTxs.first(where: { + $0.id != activeTransaction.id + && $0.coin == activeTransaction.coin + && $0.chainId == activeTransaction.chainId + && $0.ethTxNonce == activeTransaction.ethTxNonce + && $0.fromAddress == activeTransaction.fromAddress + && $0.ethTxToAddress == activeTransaction.ethTxToAddress + && $0.ethTxData == activeTransaction.ethTxData + && $0.ethTxValue == activeTransaction.ethTxValue + }), let parsedTx = await parseTransaction(txInfo) { + if let activeTxGasFee = BDouble(activeParsedTransaction.gasFee?.fee ?? "0"), + let originalTxGasFee = BDouble(parsedTx.gasFee?.fee ?? "0"), + activeTxGasFee > originalTxGasFee + { + return .speedUp + } + } + } + return .none + } + private func clearTrasactionInfoBeforeUpdate() { // clear fields that could have dynamic async changes fiat = "" diff --git a/ios/brave-ios/Sources/BraveWallet/Crypto/Stores/TransactionStatusStore.swift b/ios/brave-ios/Sources/BraveWallet/Crypto/Stores/TransactionStatusStore.swift index f7fea6a5e2ac..c78a4688995a 100644 --- a/ios/brave-ios/Sources/BraveWallet/Crypto/Stores/TransactionStatusStore.swift +++ b/ios/brave-ios/Sources/BraveWallet/Crypto/Stores/TransactionStatusStore.swift @@ -20,7 +20,7 @@ public class TransactionStatusStore: ObservableObject, WalletObserverStore { enum FollowUpAction: Equatable { case cancel(toCancelParsedTx: ParsedTransaction) - case speedup + case speedUp case none }