Skip to content

Commit 8534499

Browse files
committed
fix: resolves #3281
1 parent 1b5e775 commit 8534499

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

.changeset/rare-goats-yell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"viem": patch
3+
---
4+
5+
Removed redundant `eth_getBlockByNumber` calls.

src/actions/wallet/prepareTransactionRequest.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import { parseGwei } from '../../utils/unit/parseGwei.js'
1313
import { anvilMainnet } from '../../../test/src/anvil.js'
1414
import { http, createClient, toBlobs } from '../../index.js'
1515
import { nonceManager } from '../../utils/index.js'
16-
import { prepareTransactionRequest } from './prepareTransactionRequest.js'
16+
import {
17+
eip1559NetworkCache,
18+
prepareTransactionRequest,
19+
} from './prepareTransactionRequest.js'
1720

1821
const client = anvilMainnet.getClient()
1922

@@ -65,6 +68,8 @@ test('default', async () => {
6568
test('legacy fees', async () => {
6669
await setup()
6770

71+
eip1559NetworkCache.clear()
72+
6873
vi.spyOn(getBlock, 'getBlock').mockResolvedValueOnce({
6974
baseFeePerGas: undefined,
7075
} as any)
@@ -100,6 +105,8 @@ test('legacy fees', async () => {
100105
"value": 1000000000000000000n,
101106
}
102107
`)
108+
109+
eip1559NetworkCache.clear()
103110
})
104111

105112
test('args: account', async () => {
@@ -253,8 +260,6 @@ test('args: nonce', async () => {
253260
test('args: gasPrice', async () => {
254261
await setup()
255262

256-
vi.spyOn(getBlock, 'getBlock').mockResolvedValueOnce({} as any)
257-
258263
const { nonce: _nonce, ...request } = await prepareTransactionRequest(
259264
client,
260265
{

src/actions/wallet/prepareTransactionRequest.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ export const defaultParameters = [
7676
'type',
7777
] as const
7878

79+
/** @internal */
80+
export const eip1559NetworkCache = /*#__PURE__*/ new Map<string, boolean>()
81+
7982
export type PrepareTransactionRequestParameterType =
8083
| 'blobVersionedHashes'
8184
| 'chainId'
@@ -325,10 +328,13 @@ export async function prepareTransactionRequest<
325328
request as TransactionSerializable,
326329
) as any
327330
} catch {
328-
// infer type from block
329-
const block = await getBlock()
330-
request.type =
331-
typeof block?.baseFeePerGas === 'bigint' ? 'eip1559' : 'legacy'
331+
let isEip1559Network = eip1559NetworkCache.get(client.uid)
332+
if (typeof isEip1559Network === 'undefined') {
333+
const block = await getBlock()
334+
isEip1559Network = typeof block?.baseFeePerGas === 'bigint'
335+
eip1559NetworkCache.set(client.uid, isEip1559Network)
336+
}
337+
request.type = isEip1559Network ? 'eip1559' : 'legacy'
332338
}
333339
}
334340

@@ -369,17 +375,19 @@ export async function prepareTransactionRequest<
369375
)
370376
throw new Eip1559FeesNotSupportedError()
371377

372-
const block = await getBlock()
373-
const { gasPrice: gasPrice_ } = await internal_estimateFeesPerGas(
374-
client,
375-
{
376-
block: block as Block,
377-
chain,
378-
request: request as PrepareTransactionRequestParameters,
379-
type: 'legacy',
380-
},
381-
)
382-
request.gasPrice = gasPrice_
378+
if (typeof args.gasPrice === 'undefined') {
379+
const block = await getBlock()
380+
const { gasPrice: gasPrice_ } = await internal_estimateFeesPerGas(
381+
client,
382+
{
383+
block: block as Block,
384+
chain,
385+
request: request as PrepareTransactionRequestParameters,
386+
type: 'legacy',
387+
},
388+
)
389+
request.gasPrice = gasPrice_
390+
}
383391
}
384392
}
385393

0 commit comments

Comments
 (0)