anyone has an example on how to perform gas estimation for a function call in hardhat tests? i can't get good documentation on this, all i can find is ethers methods but this isn't what i want to use since hardhat makes you use their own version of ethers

Nov 11, 2023, 11:20 AM
You have to write unit test for your functions, also have to activate reportGas configurations in your hardhat config file
Nov 11, 2023, 7:25 PM
var methodSignature = web3.eth.abi.encodeFunctionSignature(func);
var encodedParameter = web3.eth.abi.encodeParameter("string", "ABCDEFGH");

var data = methodSignature //method signature
+ encodedParameter.substring(2); //hex of input string without '0x' prefix

let gas = await provider.estimateGas({
from: owner.address,
to: addr2.address,
data: data,
value: 1000000000000000,
function(estimatedGas, err) {
console.log("estimatedGas: " + estimatedGas);
console.log("Err:" + err);
}
});
console.log("Gas: " + gas);
i ended up using this with with web3 on hardhat provider object
Nov 11, 2023, 8:22 PM
:o
you want to get a specific tx's gas or in general?

for general
https://github.com/cgewecke/hardhat-gas-reporter
for specific

const myGas = await yourContract.estimateGas.yourFunction(parameters)
not sure this is up to date with the new ethers versions
this will generate an exception if the transaction will revert.
That's also used to extract data from payable functions without paying gas
Nov 11, 2023, 8:31 PM
i needed to estimate the gas for some weird contract i'm working on that can go above block limit
Nov 11, 2023, 8:33 PM
why would you ever do that?
on most EVM chains there's a block limit and you cannot go over it
same goes for the max contract size
Nov 11, 2023, 8:33 PM
not my contract
i'll try to fix this too
Nov 11, 2023, 8:34 PM
yup, gas is bad
make the logic more simpler and do less read/write
Nov 11, 2023, 8:34 PM

© 2024 Draquery.com All rights reserved.