From f18bbe1507aa239caf08e8fa86e923b5b023225a Mon Sep 17 00:00:00 2001 From: "moxey.eth" Date: Wed, 27 Mar 2024 07:19:07 +1100 Subject: [PATCH 1/2] refactor: prepareTransactionRequest --- .../wallet/prepareTransactionRequest.ts | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/actions/wallet/prepareTransactionRequest.ts b/src/actions/wallet/prepareTransactionRequest.ts index 375030db4a..d850b82150 100644 --- a/src/actions/wallet/prepareTransactionRequest.ts +++ b/src/actions/wallet/prepareTransactionRequest.ts @@ -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')) { @@ -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' @@ -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' } } @@ -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 ( From 98f69368b45ef19c5eca85c101e834d85c2cfebc Mon Sep 17 00:00:00 2001 From: jxom Date: Wed, 27 Mar 2024 07:29:00 +1100 Subject: [PATCH 2/2] Create popular-fireants-shop.md --- .changeset/popular-fireants-shop.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/popular-fireants-shop.md diff --git a/.changeset/popular-fireants-shop.md b/.changeset/popular-fireants-shop.md new file mode 100644 index 0000000000..e425e46193 --- /dev/null +++ b/.changeset/popular-fireants-shop.md @@ -0,0 +1,5 @@ +--- +"viem": patch +--- + +Minimized redundant requests in `prepareTransactionRequest` (addressed #2017).