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

Fix race condition when replacing/cancelling on-chain txs #2474

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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