Introducing Marmite: Hassle-free Solidity gas cost comparisons

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

Overview

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!

What’s the soup?

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:

Inline solidity comparisons
Inline solidity comparisons

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:

Gas comparison using Marmite
Gas comparison using Marmite

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:

Customizing the gas comparison
Customizing the gas comparison

After that, you can use the golf:script task to execute the script:

Conclusion

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!

Subscribe to Primitive
Receive the latest updates directly to your inbox.
Verification
This entry has been permanently stored onchain and signed by its creator.