Optimizing Solidity code allows users to pay less transaction fees when they are interacting with dApps, however mastering gas golfing requires some tools and practice. Here comes Marmite: a Hardhat plugin helping developers perform gas cost comparisons among different Solidity code snippets.
"You can cook good Solidity recipes with marmite."
- Clément Lakhal, Chief of Protocol at Primitive
Around Christmas last year, transmissions11 offered us one of the best gifts a team of Solidity developers could ever dream of: pull requests full of gas optimizations!
This kind of spawned in us a desire to gas golf our contracts even deeper, but we quickly realized that we didn’t have a proper tool to help us test different Solidity implementations and compare the gas costs.
So after sending our smart-contracts to a final audit (performed by Trail Of Bits team), we decided that it was time to build this new tool!
Marmite allows you to write different implementations of some Solidity code directly in your contracts, and then obtain a gas cost comparison among them.
These declarations are done using the tags @start<Implementation-name>
and @end
:
Note: Implementations are meant to be used during the process of gas optimization and should not be kept in final contracts.
Once you’ve done that, the next step is simply to run the golf:contract
command:
If you have a more complex setup (with multiple contracts deployed) or a specific environment, you can write a script using the Marmite context function:
After that, you can use the golf:script
task to execute the script:
Marmite offers a simple and flexible way to compare gas costs directly in your contracts, and in case you were wondering: it supports any Solidity versions!
If you want to try it out, check out the README on GitHub for the installation and usage instructions.
Happy gas golfing!