This question has been asked a hundred times before. But with the change from v5 to v6 of the ethers.js library it has become unclear again how to do it. How can I estimate the cost of a transaction like a function call or contract deployment with ethers.js v6?
2 Answers
I had to figure it out too, but it's quite simple:
Ethers v5: myContract.estimateGas.myFunction(param1, param2);
Ethers v6: myContract.myFunction.estimateGas(param1, param2);
Turns out that the function name and .estimateGas
have switched places; .estimateGas
is now part of the function.
That's actually called gasLimit
.
gasPrice
is a different thing and means the amount of Wei they charge for 1 gas.
So if the transaction costs 2000 gas, and the gasPrice
is 1, you have to pay at least 2000 gas.
In V6, the way you can get the gasPrice
is:
(await provider.getFeeData()).gasPrice
-
You mean how I call it in the title? So to be more accurate the question should be: How to estimate the gas paid for a transaction?– rajohsCommented Jul 29 at 17:00