4

All the contract deploying and setup cost some gas fees.

Is there a way to disable or set gas fees to 0, so we can calculate and test only the meaningful business logic change on the balance, without minding the little changes of payed gas fees ? I'm using hardhat.

3
  • It depends on your test and which network are you running them on. But in general, yes. You can set gas price to 0, and thus you will not spend any of it. If you are for example running your tests over truffle, by using ganache-cli, you can set gas price in truffle-config.js
    – Sky
    Jun 3, 2022 at 20:50
  • @Sky I forgot to say I'm using hardhat.
    – gneric
    Jun 4, 2022 at 16:50
  • Alright, but hardHat is also JavaScript based tool. So it too should have some hardHat-config.js file (or something like that). It's the file where tou write port number and network ID etc. And there you should be able to set the gas price.
    – Sky
    Jun 4, 2022 at 20:56

2 Answers 2

1
+50

In your hardhat.config.js, you can explicitly state the network that you are using and subsequently the gasPrice that you would like to use. In the docs, there is a gasPrice configuration which can be set.

gas: Its value should be "auto" or a number. If a number is used, it will be the gas limit used by default in every transaction. If "auto" is used, the gas limit will be automatically estimated. Default value: "auto".

Alternatively, if you're hung up on gas, you can also the plugin hardhat-gas-reporter which can be read more on here.

1
  • I've missed that documentation, thanks. More precisely I need to set gasPrice to 0 .
    – gneric
    Jun 7, 2022 at 14:21
1

If you want to optimize your function's gas cost, with hardhat it's very easy and costs nothing.

Write a unit test for each business logic function (as you probably already have), now you can measure gas costs in several possible ways -

  • Install and run Ganache, then run your tests npx hardhat test --network ganache, in Ganache you'll be able to see how much gas each method consumed.
  • Integrate hardhat-gas-trackdooor package, it'll print gas cost for each test: enter image description here
  • Use hardhat-gas-reporter, it'll print a report at the end of gas cost per function. enter image description here
1
  • Thanks for this info, but I was asking on ho these functions to cost 0 gas, so they don't mess up with my user's balance calculation (for example if I do a refund and want to test it)
    – gneric
    Jun 7, 2022 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.