Skip to content

Commit

Permalink
determine the nature of a transaction: none/original, cancel, speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
nuo-xu committed Dec 11, 2024
1 parent e5ae714 commit 505ff53
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TransactionStatusStore: ObservableObject, WalletObserverStore {

enum FollowUpAction: Equatable {
case cancel(toCancelParsedTx: ParsedTransaction)
case speedup
case speedUp
case none
}

Expand Down

0 comments on commit 505ff53

Please sign in to comment.