Code Monkey home page Code Monkey logo

forge-gas-snapshot's Introduction

Forge Gas Snapshot

Flexible, checked-in gas snapshotting for Foundry.

Forge has native gas reporting with forge snapshot and forge test --gas-report, but neither perfectly fit my needs. Specifically, forge-gas-snapshot aims to allow for:

  • Gas reports over specific, known flows
    • not entire tests and not an average of all calls
  • Check gas diffs into version control
  • See gas changes over time through commit history

Installation

forge install marktoda/forge-gas-snapshot
  • NOTE: foundry.toml must be updated to allow forge to write the snapshots
[profile.default]
...
ffi = true
fs_permissions = [{ access = "read-write", path = ".forge-snapshots/"}]

Usage

By default, gas snapshots are automatically written to ./forge-snapshots/<test-name>.snap on run.

Snapshot modes

Wrap

Wrap arbitrary code in snapStart(testName) and snapEnd to snapshot gas usage.

import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";

contract MyTest is GasSnapshot {
    function test() public {
        snapStart("test name");
        // do stuff
        snapEnd();
    }
}

Closure

Snapshot a zero-parameter function pointer with snap

import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";

contract MyTest is GasSnapshot {
    function doStuff() internal {
        // do stuff
    }

    function test() public {
        snap("test name", doStuff);
    }
}

Arbitrary values

Snapshot arbitrary values with snap

import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";

contract MyTest is GasSnapshot {
    function test() public {
        uint256 value = getValue()
        snap("test name", value);
    }
}

Contract Size

Snapshot contract size with snapSize

import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";

contract MyTest is GasSnapshot {
    function test() public {
        address addr = new Contract();
        snapSize("test name", addr);
    }
}

Check Mode

Snapshots can be run in check-mode where they revert on mismatch by setting an environment variable FORGE_SNAPSHOT_CHECK=true

TODO Improvements

  • Introspection for file / function name
  • Group related snapshots in a single file
  • Check overhead and accuracy
  • Env config for snap dir

forge-gas-snapshot's People

Contributors

marktoda avatar ylv-io avatar 0xscratch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.