From 604a88676561379927e59fab1de5de09eea29f28 Mon Sep 17 00:00:00 2001 From: tate Date: Fri, 4 Oct 2024 08:55:26 +1000 Subject: [PATCH] fix: safe race condition --- src/hooks/transactions/transactionStore.ts | 16 +++++----------- src/hooks/transactions/waitForTransaction.ts | 12 +++++++++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/hooks/transactions/transactionStore.ts b/src/hooks/transactions/transactionStore.ts index 090dec304..62863b701 100644 --- a/src/hooks/transactions/transactionStore.ts +++ b/src/hooks/transactions/transactionStore.ts @@ -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) } }, diff --git a/src/hooks/transactions/waitForTransaction.ts b/src/hooks/transactions/waitForTransaction.ts index b50d1f61a..f5226aff4 100644 --- a/src/hooks/transactions/waitForTransaction.ts +++ b/src/hooks/transactions/waitForTransaction.ts @@ -4,7 +4,7 @@ import type { EIP1193RequestFn, Hash, PublicRpcSchema, - WaitForTransactionReceiptParameters, + ReplacementReason, WaitForTransactionReceiptReturnType, } from 'viem' import { hexToString } from 'viem' @@ -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 @@ -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') { @@ -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 }