hardhat
Version:
Hardhat is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.
88 lines (68 loc) • 2.79 kB
text/typescript
import type { EthereumProvider } from "../../../../../../types/providers.js";
import { assertHardhatInvariant } from "@nomicfoundation/hardhat-errors";
import { ensureError } from "@nomicfoundation/hardhat-utils/error";
import {
hexStringToNumber,
numberToHexString,
} from "@nomicfoundation/hardhat-utils/hex";
/**
* This class handles gas estimation for transactions by applying a multiplier to the estimated gas value.
* It requests a gas estimation from the provider and multiplies it by a predefined gas multiplier, ensuring the gas does not exceed the block's gas limit.
* If an execution error occurs, the method returns the block's gas limit instead.
* The block gas limit is cached after the first retrieval to optimize performance.
*/
export abstract class MultipliedGasEstimation {
readonly
readonly
constructor(provider: EthereumProvider, gasMultiplier: number) {
this.
this.
}
protected async getMultipliedGasEstimation(params: any[]): Promise<string> {
try {
const realEstimation = await this.
method: "eth_estimateGas",
params,
});
assertHardhatInvariant(
typeof realEstimation === "string",
"realEstimation should be a string",
);
if (this.
return realEstimation;
}
const normalGas = hexStringToNumber(realEstimation);
const gasLimit = await this.
const multiplied = Math.floor(normalGas * this.
const gas = multiplied > gasLimit ? gasLimit - 1 : multiplied;
return numberToHexString(gas);
} catch (error) {
ensureError(error);
if (error.message.toLowerCase().includes("execution error")) {
const blockGasLimitTmp = await this.
return numberToHexString(blockGasLimitTmp);
}
throw error;
}
}
async
if (this.
const latestBlock = await this.
method: "eth_getBlockByNumber",
params: ["latest", false],
});
assertHardhatInvariant(
typeof latestBlock === "object" &&
latestBlock !== null &&
"gasLimit" in latestBlock &&
typeof latestBlock.gasLimit === "string",
"latestBlock should have a gasLimit",
);
const fetchedGasLimit = hexStringToNumber(latestBlock.gasLimit);
// We store a lower value in case the gas limit varies slightly
this.
}
return this.
}
}