Skip to content

Commit

Permalink
fix: resolves #3281
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Jan 27, 2025
1 parent 1b5e775 commit 8534499
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-goats-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Removed redundant `eth_getBlockByNumber` calls.
11 changes: 8 additions & 3 deletions src/actions/wallet/prepareTransactionRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -65,6 +68,8 @@ test('default', async () => {
test('legacy fees', async () => {
await setup()

eip1559NetworkCache.clear()

vi.spyOn(getBlock, 'getBlock').mockResolvedValueOnce({
baseFeePerGas: undefined,
} as any)
Expand Down Expand Up @@ -100,6 +105,8 @@ test('legacy fees', async () => {
"value": 1000000000000000000n,
}
`)

eip1559NetworkCache.clear()
})

test('args: account', async () => {
Expand Down Expand Up @@ -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,
{
Expand Down
38 changes: 23 additions & 15 deletions src/actions/wallet/prepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export const defaultParameters = [
'type',
] as const

/** @internal */
export const eip1559NetworkCache = /*#__PURE__*/ new Map<string, boolean>()

export type PrepareTransactionRequestParameterType =
| 'blobVersionedHashes'
| 'chainId'
Expand Down Expand Up @@ -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'
}
}

Expand Down Expand Up @@ -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_
}
}
}

Expand Down

0 comments on commit 8534499

Please sign in to comment.