Skip to main content

Command Palette

Search for a command to run...

[TWIL] Week of May 19, 2024

Updated
2 min read
[TWIL] Week of May 19, 2024

This week, while working on creating smart contracts, I discovered a couple of great tools from a tutorial in Base's official documentation. I'll share what I learned:

Hardhat Gas Reporter Plugin

This plugin is essential if you're developing with Hardhat, Truffle, or any other smart contract development tool that supports it. When you run tests, it automatically creates a table of stats, showing the average gas used for each method in your smart contracts. The installation is quick and easy:

  1. Run npm install -D hardhat-gas-reporter

  2. Add the following to hardhat.config.ts:

     import "hardhat-gas-reporter";
     // other imports...
    
     const config: HardhatUserConfig = {
       // other configs...
       gasReporter: {
         enabled: true,
       },
     };
    
  3. Once you write your smart contract and test code, run npx hardhat test, and you'll see a table of stats.

With the resulting report, you can see how much gas is used for each transaction. This helps you estimate the cost of each transaction and decide if you need to optimize the smart contract code for better gas efficiency.

Solidity Optimizer

This helps optimize your Solidity code for gas efficiency. To use it, add the following to hardhat.config.ts:

const config: HardhatUserConfig = {
  solidity: {
    // ...
    settings: {
      optimizer: {
        enabled: true,
        runs: <number>,
      },
    },
  },
};

Here, <number> depends on your preference. If <number> is high, the optimizer focuses on making the Solidity code more gas-efficient for each method execution, but it costs more to deploy the smart contract. If <number> is low, the deployment cost of the smart contract will be lower, but each method will cost more to execute.

More from this blog

// Sean's SWE Journey

19 posts

Love to learn, develop and share new ideas 👨‍💻