Skip to content

Commit

Permalink
fix: estimateGas for blob transactions (reth) (#11524)
Browse files Browse the repository at this point in the history
- provide high `gas` input for functions estimating gas
- remove `prepareTransactionRequest` for blob TXs as `estimateGas` is
now supported
  • Loading branch information
spypsy authored Jan 27, 2025
1 parent e45271f commit 8724606
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions yarn-project/ethereum/src/l1_tx_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import { formatViemError } from './utils.js';

const WEI_CONST = 1_000_000_000n;

// @note using this large gas limit to avoid the issue of `gas limit too low` when estimating gas in reth
const LARGE_GAS_LIMIT = 10_000_000n;

// setting a minimum bump percentage to 10% due to geth's implementation
// https://github.com/ethereum/go-ethereum/blob/e3d61e6db028c412f74bc4d4c7e117a9e29d0de0/core/txpool/legacypool/list.go#L298
const MIN_REPLACEMENT_BUMP_PERCENTAGE = 10;
Expand Down Expand Up @@ -549,23 +552,21 @@ export class L1TxUtils {
): Promise<bigint> {
const gasConfig = { ...this.config, ..._gasConfig };
let initialEstimate = 0n;
// Viem does not allow blobs to be sent via public client's estimate gas, so any estimation will fail.
// Strangely, the only way to get gas and send blobs is prepareTransactionRequest().
// See: https://github.com/wevm/viem/issues/2075
if (_blobInputs) {
// @note requests with blobs also require maxFeePerBlobGas to be set
const gasPrice = await this.getGasPrice(gasConfig, true, 0);
initialEstimate = (
await this.walletClient.prepareTransactionRequest({
account,
...request,
..._blobInputs,
maxFeePerBlobGas: gasPrice.maxFeePerBlobGas!,
})
)?.gas;
this.logger?.debug('L1 gas used in estimateGas by blob tx', { gas: initialEstimate });
initialEstimate = await this.publicClient.estimateGas({
account,
...request,
..._blobInputs,
maxFeePerBlobGas: gasPrice.maxFeePerBlobGas!,
gas: LARGE_GAS_LIMIT,
});

this.logger?.debug(`L1 gas used in estimateGas by blob tx: ${initialEstimate}`);
} else {
initialEstimate = await this.publicClient.estimateGas({ account, ...request });
this.logger?.debug('L1 gas used in estimateGas by non-blob tx', { gas: initialEstimate });
initialEstimate = await this.publicClient.estimateGas({ account, ...request, gas: LARGE_GAS_LIMIT });
this.logger?.debug(`L1 gas used in estimateGas by non-blob tx: ${initialEstimate}`);
}

// Add buffer based on either fixed amount or percentage
Expand Down Expand Up @@ -599,7 +600,7 @@ export class L1TxUtils {
data: request.data,
maxFeePerGas: gasPrice.maxFeePerGas,
maxPriorityFeePerGas: gasPrice.maxPriorityFeePerGas,
gas: request.gas ?? 10_000_000n,
gas: request.gas ?? LARGE_GAS_LIMIT,
nonce,
},
],
Expand Down

0 comments on commit 8724606

Please sign in to comment.