{"id":15010186,"url":"https://github.com/primitivefinance/hardhat-marmite","last_synced_at":"2025-04-09T19:17:37.581Z","repository":{"id":40615900,"uuid":"443602226","full_name":"primitivefinance/hardhat-marmite","owner":"primitivefinance","description":"🥘 Hassle-free Hardhat plugin to compare gas cost among different Solidity code snippets.","archived":false,"fork":false,"pushed_at":"2023-01-03T19:42:09.000Z","size":10202,"stargazers_count":196,"open_issues_count":7,"forks_count":5,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-04-09T19:17:33.086Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/primitivefinance.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-01T18:46:00.000Z","updated_at":"2024-09-29T15:05:32.000Z","dependencies_parsed_at":"2023-02-01T09:16:07.409Z","dependency_job_id":null,"html_url":"https://github.com/primitivefinance/hardhat-marmite","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primitivefinance%2Fhardhat-marmite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primitivefinance%2Fhardhat-marmite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primitivefinance%2Fhardhat-marmite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primitivefinance%2Fhardhat-marmite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/primitivefinance","download_url":"https://codeload.github.com/primitivefinance/hardhat-marmite/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248094988,"owners_count":21046770,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T19:31:47.044Z","updated_at":"2025-04-09T19:17:37.547Z","avatar_url":"https://github.com/primitivefinance.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🥘 Marmite\n\n![version](https://img.shields.io/npm/v/@primitivefi/hardhat-marmite) ![npm](https://img.shields.io/npm/dt/@primitivefi/hardhat-marmite) ![license](https://img.shields.io/npm/l/@primitivefi/hardhat-marmite) ![stars](https://img.shields.io/github/stars/primitivefinance/hardhat-marmite?style=social\u0026color=%23FFB31A)\n\n\u003e Hassle-free Hardhat plugin to compare gas cost among different Solidity code snippets.\n\n![Demo](./gifs/demo.gif)\n\n## 🧩 Features\n\n- 📊 Compare code snippets directly in your contracts\n- ✅ Compatible with any Solidity versions\n- 🔍 Checks function calls and contract deployments\n- 💯 Accurate gas cost metrics using code preprocessing\n- 🧰 Supports single contracts and complex scripts\n\n## 📦 Installation\n\nFirst thing to do is to install the plugin in your Hardhat project:\n\n```bash\n# Using yarn\nyarn add @primitivefi/hardhat-marmite\n\n# Or using npm\nnpm i @primitivefi/hardhat-marmite\n```\n\nNext step is simply to include the plugin into your `hardhat.config.js` or `hardhat.config.ts` file:\n\n```typescript\n// Using JavaScript\nrequire('@primitivefi/hardhat-marmite');\n\n// Using ES6 or TypeScript\nimport '@primitivefi/hardhat-marmite';\n```\n\n## ⛽️ Usage\n\nMarmite offers 2 different tasks that you can use to compare gas costs:\n- `golf:contract` for standalone contracts and simple function calls\n- `golf:script` for all the more complex scenarios\n\nIn both cases, the first thing to do is always to write the different snippets that you want to compare, these are called the *implementations*.\n\n### ✍️ Solidity implementations\n\nDeclaring an implementation is a piece of cake, just put your Solidity code within the tags `@start\u003cName-of-your-implementation\u003e` and `@end`.\n\nLet's say that you want to know if it's cheaper to check if a variable is \"different from 0\" or \"higher than 0\", simply write inside of your contract:\n\n```solidity\n// SPDX-License-Identifier: WTFPL\npragma solidity 0.8.9;\n\ncontract Foo {\n    uint256 public bar;\n\n    function set(uint256 newBar) external {\n        // Declaring our first implementation\n        @start\u003cDifferent-from\u003e\n        if (newBar != 0) {\n            bar = newBar;\n        }\n        @end\n\n        // Declaring our second implementation\n        @start:\u003cGreater-than\u003e\n        if (newBar \u003e 0) {\n            bar = newBar;\n        }\n        @end\n    }\n}\n```\n\nMarmite will know that it has to compare the gas cost of `Different-from` and `Greater-than`. Under the hood, this contract will be compiled 2 times, using each time the code of a different implementation.\n\n### 📃 `golf:contract`\n\nStandalone contracts and unique function calls can be quickly *gas golf-ed* using this task:\n\n```bash\nnpx hardhat golf:contract --contract Foo --func set --params 42\n```\n\n![golf:contract](./gifs/contract.gif)\n\nThis will tell Marmite to deploy the contract `Foo` and call the function `set` with the parameter `42`. Note that since no implementation names were specified, Marmite will naively measure all of them.\n\nHere are all the options of the `golf:contract` task:\n- `--contract` **(mandatory)**: Name of the contract to deploy, e.g. `Foo`\n- `--ctorParams` (optional): Parameters to pass to the constructor during the contract deployment, separated by a comma, e.g. `42,true`\n- `--func` **(mandatory)**: Name of the function to call, e.g. `set`\n- `--params` (optional): Parameters to pass with the function call, separated by a comma, e.g. `42,true`\n- `--impls` (optional): Name of the implementations to compare, separated by a comma, note that if this parameter is missing, all the found implementations will be compared, e.g. `Different-from,Greater-than`\n\n### 📽 `golf:script`\n\nSome more complex scenarios require a script to deploy the contracts or to set up a particular environment, this task is especially made to tackle these cases.\n\nScripts are very similar to the usual Hardhat tasks, scripts or fixtures, but the only difference is that all the logic must happen inside the Marmite context function:\n\n```typescript\nimport hre from 'hardhat';\nimport marmite from '@primitivefi/hardhat-marmite';\n\nasync function main() {\n  await marmite(hre, async (flag) =\u003e {\n    const Foo = await hre.ethers.getContractFactory('Foo');\n    const foo = await Foo.deploy();\n\n    const tx = await foo.set(42);\n    await flag('set function', tx);\n  });\n}\n\nmain();\n```\n\nThis example is showcasing the important steps to write a script:\n1. Import the `marmite` context function\n2. Pass the `hre` (Hardhat Runtime Environment) variable to the context function, along with a callback function deploying your contracts and executing your transactions\n3. A `flag` function is provided to your callback function as a parameter, use it to pin a transaction whenever you are interested in having its gas cost compared in the final results\n\nHere is a quick API describing these two functions:\n\n#### 🥘 `marmite`\n\n```typescript\n/**\n * Marmite context function\n * @param hre Hardhat Runtime Environment variable\n * @param callback Function deploying contracts and flagging transactions\n * @param implementations (optional) Array of implementation names to compare, omitting\n *                        this parameter will compare all the found implementations\n */\nexport default async function marmite(\n  hre: HardhatRuntimeEnvironment,\n  callback: CallbackFunction,\n  implementations: string[] = [],\n): Promise\u003cvoid\u003e\n```\n\n#### 🚩 `flag`\n\n```typescript\n/**\n * Flags a transaction to display its gas cost in the final results\n * @param name Name of the flag\n * @param tx Transaction to flag\n */\nexport type FlagFunction = (name: string, tx: ContractTransaction | TransactionResponse) =\u003e void;\n```\n\nOnce your script is ready, you can run:\n\n```bash\nnpx hardhat golf:script ./path/to/yourScript.ts\n```\n\n![golf:script](./gifs/script.gif)\n\nThis task only takes one unnamed parameter, which is the path to your script, e. g. `./examples/scripts/foo.ts`.\n\n## 🔧 Config\n\nComing soon 👀\n\n## ⛑ Help\n\nFeel free to open an issue if you need help or if you encounter a problem! Here are some already known problems though:\n- Naming a flag `constructor` will create a JS / TS issue\n- Compiling your contracts using `npx hardhat compile` might not work if Marmite tags are still present in your code\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprimitivefinance%2Fhardhat-marmite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprimitivefinance%2Fhardhat-marmite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprimitivefinance%2Fhardhat-marmite/lists"}