Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: safe tx hash replacement #892

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 5 additions & 11 deletions src/hooks/transactions/transactionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
const errors = validateTransaction(transaction)

if (errors.length > 0) {
throw new Error(['Unable to add transaction', ...errors].join('\n'))

Check failure on line 329 in src/hooks/transactions/transactionStore.ts

View workflow job for this annotation

GitHub Actions / coverage

Unhandled error

Error: Unable to add transaction Invalid transaction hash ❯ addTransaction src/hooks/transactions/transactionStore.ts:329:13 ❯ onReplaced src/hooks/transactions/transactionStore.ts:405:17 ❯ Timeout._onTimeout src/hooks/transactions/transactionStore.test.ts:57:9 ❯ listOnTimeout node:internal/timers:581:17 ❯ processTimers node:internal/timers:519:7 This error originated in "src/hooks/transactions/transactionStore.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "should allow a repriced transaction". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
}

updateTransactions(account, chainId, (transactions) => {
Expand Down Expand Up @@ -399,21 +399,15 @@
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
}
Loading