How to Estimate Gas and Cost (in USD) to deploy Solidity Contracts and Functions

Abhijeet Sinha
5 min readMar 22, 2023

TL;DR

In this tutorial, we’ll explore how to leverage the hardhat-gas-reporter package to accurately calculate the gas cost of deploying a smart contract or calling specific functions, such as Mint or Transfer, with ease. Furthermore, we’ll learn how to convert these gas costs into fiat currencies such as USD or INR, allowing you to calculate the exact financial implications of your operations.

This knowledge is particularly valuable if you’re preparing to launch your project on the mainnet and require detailed budget projections that include optimization strategies for efficient execution. By following the steps outlined in this tutorial, you can confidently assess the performance and cost of your smart contract, ensuring the successful launch of your project.

Let’s Start.

As a Solidity developer, it is crucial to not only focus on auditing your smart contract but also optimizing it for gas usage. A poorly optimized contract can result in unexpected costs for users when calling functions, which can lead to frustration and disappointment.

To avoid this, it’s important to have a good understanding of the gas costs involved in deploying a contract and calling its functions. Additionally, when preparing to launch your contract for production, you must consider the gas budget available.

The efficiency of a developer’s skills is often measured by their ability to write contracts that use the least amount of gas possible. By minimizing gas usage, you can increase the contract’s speed and reduce the overall cost of usage, which will lead to a better experience for users.

Pre-requisites:

i. Familiar to Solidity Smart Contracts

ii. Hands on Hardhat Framework

iii. Basic understanding in writing Chai Mocha Test Cases

So in this thread I’ll share about a npm package called “hardhat-gas-reporter”. According to source,

A Mocha reporter for Ethereum test suites:

  • Gas usage per unit test.
  • Metrics for method calls and deployments.
  • National currency costs of deploying and using your contract system.

So you need to setup Hardhat framework for this thread. We will consider the Lock.sol contract and its testcase is already written by default.

Lock.sol smart contract will look like this:

Lock.sol

Now let’s understand step by step process to calculate the gas required to deploy your contract and for calling other functions.

  1. You need to install the package:
npm install hardhat-gas-reporter --save-dev

2. Now you need to create API keys from CoinMarketCap website. You can visit this link to create API. Once logged-in, copy the API.

3. To enhance the security of your code, it’s recommended to create a separate .env file and store your CoinMarketApi key there. This way, you can reference the key in your hardhat.config.js file using the process.env.COINMARKETCAP_API_KEY variable. Alternatively, you can directly paste your API key into the coinmarketcap field.

2. In the hardhat.config.js file, go to module.exports section and add the gasReporter section as shown in below image:

NOTE:

If you do not specify a token field, the report will be generated in ETH by default. However, you have the option to generate reports for other tokens by specifying the token field name, such as MATIC, BNB, and so on. This allows you to easily switch between different tokens and view their respective reports.

3. In order to generate a report, you will need to write test cases that will execute the functions and deployment of your smart contract. For this purpose, I recommend using Chai Mocha in JavaScript, which is a widely used testing framework. By executing the test cases, the report will be generated automatically in a txt file format for easy viewing and analysis.

Chai Mocha test case of Lock.sol

To accurately calculate the appropriate price and gas for your smart contract, it’s crucial to include a diverse range of test cases that cover all the important functions.

text file generated
ETH gas report for Lock.sol
Matic gas report for Lock.sol

As you can see above, to deploy Lock.sol on Ethereum network you need to pay 15.98 USD, whereas on Matic network it is just 0.01 USD. Similarly, the ‘withdraw’ function will cost 1.67 USD on Ethereum and almost 0 cost on Matic network.

So, take the time to carefully plan and optimize your smart contract to ensure that it is both gas-efficient and cost-effective. Not only will this benefit your users, but it will also enhance your reputation as a skilled and knowledgeable Solidity developer.

Follow me and I’ll follow you back ;)

Twitter: 0x_Abhijeet

LinkedIn

Discord: #Abhijeet9181

Thank You :)

--

--

Abhijeet Sinha

Blockchain Developer, Certified Google Data Analyst (Beginner)