Skip to content

Commit

Permalink
refactor: minimize redundant requests in prepareTransactionRequest (#…
Browse files Browse the repository at this point in the history
…2021)

* refactor: prepareTransactionRequest

* Create popular-fireants-shop.md
  • Loading branch information
jxom authored Mar 26, 2024
1 parent b3b989f commit 8a173cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
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

0 comments on commit 8a173cc

Please sign in to comment.