Skip to content

Commit

Permalink
fix: safe race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Oct 3, 2024
1 parent 597999a commit 604a886
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
16 changes: 5 additions & 11 deletions src/hooks/transactions/transactionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,21 +399,15 @@ export function createTransactionStore(config_: ConfigWithEns) {
const requestPromise = waitForTransaction(config, {
confirmations: 1,
hash: hash as `0x${string}`,
onReplaced: (replacedTransaction) => {
if (replacedTransaction.reason === 'repriced') {
setTransactionStatus(
account,
chainId,
hash,
'repriced',
replacedTransaction.transaction.hash,
)
onReplaced: (response) => {
if (response.reason === 'repriced') {
setTransactionStatus(account, chainId, hash, 'repriced', response.transactionHash)
addTransaction(account, chainId, {
...transaction,
isSafeTx: false,
hash: replacedTransaction.transaction.hash,
hash: response.transactionHash,
})
transactionRequestCache.set(replacedTransaction.transaction.hash, requestPromise)
transactionRequestCache.set(response.transactionHash, requestPromise)
transactionRequestCache.delete(hash)
}
},
Expand Down
12 changes: 9 additions & 3 deletions src/hooks/transactions/waitForTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
EIP1193RequestFn,
Hash,
PublicRpcSchema,
WaitForTransactionReceiptParameters,
ReplacementReason,
WaitForTransactionReceiptReturnType,
} from 'viem'
import { hexToString } from 'viem'
Expand All @@ -24,7 +24,7 @@ export type WaitForTransactionArgs = {
/** Transaction hash to monitor */
hash: Hash
/** Callback to invoke when the transaction has been replaced (sped up). */
onReplaced?: WaitForTransactionReceiptParameters['onReplaced']
onReplaced?: (response: { reason: ReplacementReason; transactionHash: Hash }) => void
/*
* Maximum amount of time to wait before timing out in milliseconds
* @default 0
Expand Down Expand Up @@ -80,7 +80,8 @@ export async function waitForTransaction(
const receipt = await waitForTransactionReceipt(client, {
hash,
confirmations,
onReplaced,
onReplaced: ({ reason, transaction }) =>
onReplaced?.({ reason, transactionHash: transaction.hash }),
timeout,
})
if (receipt.status === 'reverted') {
Expand All @@ -95,6 +96,11 @@ export async function waitForTransaction(
} as CallParameters)) as unknown as string
const reason = hexToString(`0x${code.substring(138)}`)
throw new Error(reason)
} else if (isSafeTx) {
onReplaced?.({
reason: 'repriced',
transactionHash: receipt.transactionHash,
})
}
return receipt
}

0 comments on commit 604a886

Please sign in to comment.