![[TWIL] Week of May 19, 2024](https://cdn.hashnode.com/res/hashnode/image/upload/v1717055547035/44fc37ed-bdc2-4279-9c0e-d28c1c68f298.png)
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:
Run
npm install -D hardhat-gas-reporterAdd the following to
hardhat.config.ts:import "hardhat-gas-reporter"; // other imports... const config: HardhatUserConfig = { // other configs... gasReporter: { enabled: true, }, };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.
About the author
Engineer @ ___ | Ex-Ourkive | Ex-Meta
Comments
Join the discussionNo comments yet. Be the first to comment.
This Week I Learned
This series is about my weekly learning experiences, covering a broad range of topics with a focus on sharing engineering tips and techniques whenever possible.






