In Ethereum, the surge in gas costs often becomes an issue. While this may be acceptable on localnets or testnets, it becomes a significant financial burden on the mainnet due to the real costs involved. Against this background, understanding and, if necessary, improving gas usage is an important task.
Gas Reporter
On day 20, we introduced libraries used in hardhat development, including the hardhat-gas-reporter library. This library allows you to generate reports on the gas usage of the contracts you are implementing.
The specific settings can be found in hardhat.config.ts, as explained on day 21, and are as follows. The explanations for each item are as noted in the comments:
// Excerpt from hardhat.config.ts
...
gasReporter: { // Gas reporter (a tool to visualize gas costs)
enabled: true, // Flag to toggle the gas report on and off
currency: "USD", // Specify the currency for displaying the amount of gas used
outputFile: "gas-report.txt", // Specify the output destination for the generated gas report
noColors: true, // Specify whether to disable color display in the terminal
},
...
Gas Report
When you execute yarn hardhat test, a file named gas-reporter.txt is generated. The results are as follows.
·-------------------------|----------------------------|-------------|-----------------------------·
| Solc version: 0.8.8 · Optimizer enabled: false · Runs: 200 · Block limit: 30000000 gas │
··························|····························|·············|······························
| Methods · 41 gwei/gas · 1656.05 usd/eth │
·············|············|··············|·············|·············|···············|··············
| Contract · Method · Min · Max · Avg · # calls · usd (avg) │
·············|············|··············|·············|·············|···············|··············
| Funding · fund · 72474 · 89574 · 86154 · 5 · 5.85 │
·············|············|··············|·············|·············|···············|··············
| Funding · withdraw · - · - · 44146 · 2 · 3.00 │
·············|············|··············|·············|·············|···············|··············
| Deployments · · % of limit · │
··························|··············|·············|·············|···············|··············
| Funding · - · - · 538126 · 1.8 % · 36.54 │
·-------------------------|--------------|-------------|-------------|---------------|-------------·
This Gas Report shows the gas usage for each method and deployment of Solidity contracts. Here is a detailed explanation of the report:
- Solc version: This is the version of the Solidity compiler. In this example, version 0.8.8 is used.
- Optimizer enabled: Indicates whether the optimizer is enabled or not. In this example, the optimizer is disabled.
- Runs: The number of times the optimizer optimizes the code when compiling Solidity code.
- Block limit: Indicates the gas block limit. It’s the maximum amount of gas that can be used in a single block.
Next, an explanation of the columns for each method in the report:
- Contract: The name of the contract. In this case, it’s the “Funding” contract.
- Method: The name of the method within the contract. There are “fund” and “withdraw” methods.
- Min: The minimum amount of gas used for executing this method.
- Max: The maximum amount of gas used for executing this method.
- Avg: The average amount of gas used for executing this method.
- calls: The number of times this method was called.
- usd (avg): The cost in USD based on the average gas used. (To display in USD, the API of a service called CoinMarketCap is required. This will be explained in the next section.)
The deployment columns show the gas usage for each deployed contract.
Through this report, you can understand how much gas each method and deployment uses, and whether optimization is effective or not. This allows you to analyze the gas usage of contract executions and design efficient contracts.
Registering a CoinMarketCap API Key
Register for an account on the CoinMarketCap website. After email verification and signing in, you will be presented with the following screen. Copy the API key listed here and add it to your .env file. Call the variable defined in the .env file in hardhat.config.ts and define the variable (please refer to the examples of .env settings on day 20 and hardhat.config.ts settings on day 21).
コメント