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

refactor: minimize redundant requests in prepareTransactionRequest #2021

Merged
merged 2 commits into from
Mar 26, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/popular-fireants-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Minimized redundant requests in `prepareTransactionRequest` (addressed #2017).
50 changes: 27 additions & 23 deletions src/actions/wallet/prepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,6 @@ export async function prepareTransactionRequest<
} = args
const account = account_ ? parseAccount(account_) : undefined

const block = await getAction(
client,
getBlock,
'getBlock',
)({ blockTag: 'latest' })

const request = { ...args, ...(account ? { from: account?.address } : {}) }

if (parameters.includes('chainId')) {
Expand All @@ -276,6 +270,11 @@ export async function prepareTransactionRequest<
blockTag: 'pending',
})

const block = await (() => {
if (typeof request.type !== 'undefined') return
return getAction(client, getBlock, 'getBlock')({ blockTag: 'latest' })
})()

if (
(parameters.includes('fees') || parameters.includes('type')) &&
typeof type === 'undefined'
Expand All @@ -287,7 +286,7 @@ export async function prepareTransactionRequest<
} catch {
// infer type from block
request.type =
typeof block.baseFeePerGas === 'bigint' ? 'eip1559' : 'legacy'
typeof block?.baseFeePerGas === 'bigint' ? 'eip1559' : 'legacy'
}
}

Expand All @@ -296,24 +295,29 @@ export async function prepareTransactionRequest<

if (request.type === 'eip1559' || request.type === 'eip4844') {
// EIP-1559 fees
const { maxFeePerGas, maxPriorityFeePerGas } =
await internal_estimateFeesPerGas(client, {
block: block as Block,
chain,
request: request as PrepareTransactionRequestParameters,
})

if (
typeof args.maxPriorityFeePerGas === 'undefined' &&
args.maxFeePerGas &&
args.maxFeePerGas < maxPriorityFeePerGas
)
throw new MaxFeePerGasTooLowError({
maxPriorityFeePerGas,
})
typeof request.maxFeePerGas === 'undefined' ||
typeof request.maxPriorityFeePerGas === 'undefined'
) {
const { maxFeePerGas, maxPriorityFeePerGas } =
await internal_estimateFeesPerGas(client, {
block: block as Block,
chain,
request: request as PrepareTransactionRequestParameters,
})

if (
typeof args.maxPriorityFeePerGas === 'undefined' &&
args.maxFeePerGas &&
args.maxFeePerGas < maxPriorityFeePerGas
)
throw new MaxFeePerGasTooLowError({
maxPriorityFeePerGas,
})

request.maxPriorityFeePerGas = maxPriorityFeePerGas
request.maxFeePerGas = maxFeePerGas
request.maxPriorityFeePerGas = maxPriorityFeePerGas
request.maxFeePerGas = maxFeePerGas
}
} else {
// Legacy fees
if (
Expand Down
Loading