Skip to content

Commit

Permalink
fix: block not found issue on waitForTransactionReceipt
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 30, 2023
1 parent 1bbd32f commit 1218e97
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-carpets-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed `BlockNotFoundError` issue on `waitForTransactionReceipt`.
31 changes: 23 additions & 8 deletions src/actions/public/waitForTransactionReceipt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Client } from '../../clients/createClient.js'
import type { Transport } from '../../clients/transports/createTransport.js'
import { BlockNotFoundError } from '../../errors/block.js'
import {
TransactionNotFoundError,
TransactionReceiptNotFoundError,
Expand Down Expand Up @@ -228,14 +229,28 @@ export async function waitForTransactionReceipt<
replacedTransaction = transaction

// Let's retrieve the transactions from the current block.
const block = await getAction(
client,
getBlock,
'getBlock',
)({
blockNumber,
includeTransactions: true,
})
// We need to retry as some RPC Providers may be slow to sync
// up mined blocks.
retrying = true
const block = await withRetry(
() =>
getAction(
client,
getBlock,
'getBlock',
)({
blockNumber,
includeTransactions: true,
}),
{
// exponential backoff
delay: ({ count }) => ~~(1 << count) * 200,
retryCount: 6,
shouldRetry: ({ error }) =>
error instanceof BlockNotFoundError,
},
)
retrying = false

const replacementTransaction = (
block.transactions as {} as Transaction[]
Expand Down

1 comment on commit 1218e97

@vercel
Copy link

@vercel vercel bot commented on 1218e97 Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.