Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
fix race condition when replacing/cancelling onchain txs (#2474)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramirotw authored Mar 2, 2022
1 parent e88eb5e commit 9059360
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/custom/state/enhancedTransactions/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export interface EnhancedTransactionDetails {

// Wallet specific
safeTransaction?: SafeMultisigTransactionResponse // Gnosis Safe transaction info

// Cancelling/Replacing
replacementType?: ReplacementType // if the user cancelled or speedup the tx it will be reflected here
linkedTransactionHash?: string
}

export interface EnhancedTransactionState {
Expand Down Expand Up @@ -113,22 +116,33 @@ export default createReducer(initialState, (builder) =>
}
tx.receipt = receipt
tx.confirmedTime = now()

if (tx.linkedTransactionHash) {
delete transactions[chainId]?.[tx.linkedTransactionHash]
}
})

.addCase(replaceTransaction, (transactions, { payload: { chainId, oldHash, newHash, type } }) => {
if (!transactions[chainId]?.[oldHash]) {
const allTxs = transactions[chainId] ?? {}
if (!allTxs[oldHash]) {
console.error('Attempted to replace an unknown transaction.')
return
}
const allTxs = transactions[chainId] ?? {}

if (allTxs[newHash]) {
return
}

allTxs[newHash] = {
...allTxs[oldHash],
hash: newHash,
transactionHash: newHash,
addedTime: new Date().getTime(),
replacementType: type,
linkedTransactionHash: oldHash,
}
delete allTxs[oldHash]

allTxs[oldHash].linkedTransactionHash = newHash
})

.addCase(updateSafeTransaction, (transactions, { payload: { chainId, safeTransaction, blockNumber } }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function CancelReplaceTxUpdater(): null {
useEffect(() => {
if (!chainId || !library) return

// Watch the mempool for cancelation/replacement of tx
// Watch the mempool for cancellation/replacement of tx
watchTxChanges(pendingHashes, chainId, dispatch)

return () => {
Expand Down

0 comments on commit 9059360

Please sign in to comment.