diff --git a/.changeset/rare-goats-yell.md b/.changeset/rare-goats-yell.md new file mode 100644 index 0000000000..4c760529d9 --- /dev/null +++ b/.changeset/rare-goats-yell.md @@ -0,0 +1,5 @@ +--- +"viem": patch +--- + +Removed redundant `eth_getBlockByNumber` calls. diff --git a/src/actions/wallet/prepareTransactionRequest.test.ts b/src/actions/wallet/prepareTransactionRequest.test.ts index 46da196191..571649cd6e 100644 --- a/src/actions/wallet/prepareTransactionRequest.test.ts +++ b/src/actions/wallet/prepareTransactionRequest.test.ts @@ -13,7 +13,10 @@ import { parseGwei } from '../../utils/unit/parseGwei.js' import { anvilMainnet } from '../../../test/src/anvil.js' import { http, createClient, toBlobs } from '../../index.js' import { nonceManager } from '../../utils/index.js' -import { prepareTransactionRequest } from './prepareTransactionRequest.js' +import { + eip1559NetworkCache, + prepareTransactionRequest, +} from './prepareTransactionRequest.js' const client = anvilMainnet.getClient() @@ -65,6 +68,8 @@ test('default', async () => { test('legacy fees', async () => { await setup() + eip1559NetworkCache.clear() + vi.spyOn(getBlock, 'getBlock').mockResolvedValueOnce({ baseFeePerGas: undefined, } as any) @@ -100,6 +105,8 @@ test('legacy fees', async () => { "value": 1000000000000000000n, } `) + + eip1559NetworkCache.clear() }) test('args: account', async () => { @@ -253,8 +260,6 @@ test('args: nonce', async () => { test('args: gasPrice', async () => { await setup() - vi.spyOn(getBlock, 'getBlock').mockResolvedValueOnce({} as any) - const { nonce: _nonce, ...request } = await prepareTransactionRequest( client, { diff --git a/src/actions/wallet/prepareTransactionRequest.ts b/src/actions/wallet/prepareTransactionRequest.ts index d99523d93c..0c41b5ccd4 100644 --- a/src/actions/wallet/prepareTransactionRequest.ts +++ b/src/actions/wallet/prepareTransactionRequest.ts @@ -76,6 +76,9 @@ export const defaultParameters = [ 'type', ] as const +/** @internal */ +export const eip1559NetworkCache = /*#__PURE__*/ new Map() + export type PrepareTransactionRequestParameterType = | 'blobVersionedHashes' | 'chainId' @@ -325,10 +328,13 @@ export async function prepareTransactionRequest< request as TransactionSerializable, ) as any } catch { - // infer type from block - const block = await getBlock() - request.type = - typeof block?.baseFeePerGas === 'bigint' ? 'eip1559' : 'legacy' + let isEip1559Network = eip1559NetworkCache.get(client.uid) + if (typeof isEip1559Network === 'undefined') { + const block = await getBlock() + isEip1559Network = typeof block?.baseFeePerGas === 'bigint' + eip1559NetworkCache.set(client.uid, isEip1559Network) + } + request.type = isEip1559Network ? 'eip1559' : 'legacy' } } @@ -369,17 +375,19 @@ export async function prepareTransactionRequest< ) throw new Eip1559FeesNotSupportedError() - const block = await getBlock() - const { gasPrice: gasPrice_ } = await internal_estimateFeesPerGas( - client, - { - block: block as Block, - chain, - request: request as PrepareTransactionRequestParameters, - type: 'legacy', - }, - ) - request.gasPrice = gasPrice_ + if (typeof args.gasPrice === 'undefined') { + const block = await getBlock() + const { gasPrice: gasPrice_ } = await internal_estimateFeesPerGas( + client, + { + block: block as Block, + chain, + request: request as PrepareTransactionRequestParameters, + type: 'legacy', + }, + ) + request.gasPrice = gasPrice_ + } } }