https://github.com/0xpolarzero/gas-metering-comparison
Comparing gas measurements from Forge, Hardhat, Tevm and forge-gas-metering against testnet transactions.
https://github.com/0xpolarzero/gas-metering-comparison
Last synced: about 1 year ago
JSON representation
Comparing gas measurements from Forge, Hardhat, Tevm and forge-gas-metering against testnet transactions.
- Host: GitHub
- URL: https://github.com/0xpolarzero/gas-metering-comparison
- Owner: 0xpolarzero
- Created: 2024-02-17T12:03:26.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-28T10:40:20.000Z (over 2 years ago)
- Last Synced: 2024-04-15T05:02:26.492Z (about 2 years ago)
- Language: Solidity
- Homepage:
- Size: 207 KB
- Stars: 15
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Gas measurements comparison
Comparing the way Forge, Tevm, Hardhat and forge-gas-metering will report gas usage against Sepolia testnet transactions.
There are two subsequent mint transactions on a newly deployed mock ERC20 contract, for each of the two scenarios. There are 50 `0` bytes from the transaction's data in the first scenario that are turned into `1` bytes in the second one, which explains the expected 600 gas overhead (`600 = 50 * (16 - 4)`).
The idea is to figure out which of these tools will report different gas usage based on the amount of zero and non-zero bytes that need to be read/written.
## Results
> The process is the following:
>
> 1. deploy the contract;
> 2. mint the same amount of tokens to the same address twice.
>
> `recipient = 0x0000000000000000000000000000000000000001 | 0x1111111111111111111111111111111111111111`
>
> `amount = 0x0000000000000000000000000000000000000000000000000000000000000001 | 0x1111111111111111111111111111111111111111111111111111111111111111`
| Medium | Zero bytes (1st) | Non-zero bytes (1st) | Zero bytes (2nd) | Non-zero bytes (2nd) | Bytes cost diff included | Exact match |
| ---------------------- | ---------------- | -------------------- | ---------------- | -------------------- | ------------------------ | ----------- |
| Reference (Sepolia tx) | 67,839 | 68,439 | 33,639 | 34,239 | ✅ | ✅ |
| Forge (isolated) | 67,839 | 68,439 | 33,639 | 34,239 | ✅ | ✅ |
| Hardhat | 67,839 | 68,439 | 33,639 | 34,239 | ✅ | ✅ |
| forge-gas-metering | 63,879 | 64,479 | 21,579 | 22,179 | ✅ | ❌ |
| Forge | 46,495 | 46,495 | 2,695 | 2,695 | ❌ | ❌ |
| Tevm | 46,495 | 46,495 | 2,695 | 2,695 | ❌ | ❌ |
## How to reproduce
### Clone and install
```bash
git clone git@github.com:0xpolarzero/gas-metering-comparison.git
cd gas-metering-comparison
# From the root:
# Foundry
cd foundry
forge install
# Hardhat
cd hardhat
pnpm install
# Tevm
cd tevm
pnpm install
```
### Sepolia
This will deploy the contract and mint the tokens twice. Which will provide both the reference Sepolia txs and the measurements from the script.
**In `/foundry`**:
1. Create `.env` and fill it with the content in `.env.example`;
2. run `source .env`;
3. deploy the contract and mint the tokens:
```bash
forge script script/DeployAndCall.s.sol:DeployAndCall --rpc-url $RPC_URL_SEPOLIA --broadcast -vvvv --sig "run(address, uint256)" 0x0000000000000000000000000000000000000001 0x0000000000000000000000000000000000000000000000000000000000000001
# or
forge script script/DeployAndCall.s.sol:DeployAndCall --rpc-url $RPC_URL_SEPOLIA --broadcast -vvvv --sig "run(address, uint256)" 0x1111111111111111111111111111111111111111 0x1111111111111111111111111111111111111111111111111111111111111111
```
### Foundry (Forge)
**In `/foundry`**: run `forge test --mc MockERC20Foundry -vvvv --isolate`.
### Hardhat
**In `/hardhat`**: run `pnpm hardhat test`.
### forge-gas-metering
**In `/foundry`**: run `forge test --mc MockERC20ForgeGasMetering -vv`.
### Tevm
**In `/tevm`**: run `pnpm ts-node index.ts`.