DGRed is a source-level optimizer that reduces the deployment gas cost of Solidity smart contracts by reordering their state-variable declarations. It applies semantics-preserving mutations (state variables are only reordered, never renamed, removed, or retyped), re-measures deployment gas with Foundry, and keeps a mutation only when it lowers deployment gas without increasing the number of storage slots.
DGRed parses Solidity with a tree-sitter grammar, so it operates directly on the concrete syntax tree and does not require the contract to be part of a larger build system.
This repository accompanies the paper "Tetris for Gas: State Variable Mutation for Reducing Smart Contract Deployment Costs." It contains the DGRed tool together with 20 real-world contracts (
src/) and their matching Foundry deployment tests (test/) so the tool can be run end-to-end out of the box.
.
├── dgred # The DGRed tool (single entry point)
├── src/ # 20 Solidity contracts to optimize
├── test/ # 20 matching Foundry deployment tests
├── foundry.toml # Foundry config (optimizer on, 200 runs, London EVM)
├── libtree-sitter-solidity.so # Prebuilt Solidity grammar (Linux x86-64)
├── scripts/build_parser.sh # Rebuild the grammar for your platform
├── requirements.txt # Python dependency (tree_sitter)
├── Dockerfile # Fully reproducible container build
└── lib/forge-std # Foundry standard library (git submodule, pinned)
Each test in test/*.t.sol imports exactly one contract from src/ and
deploys it inside a testDeploy* function. DGRed reads the reported deployment
gas from forge test to drive its search.
For every contract that has a matching test, DGRed:
- Establishes a baseline — runs the deployment test and records the deployment gas and the number of storage slots.
- Mutates — for a fixed number of iterations (default: 10), it randomly swaps pairs of state-variable declarations within each contract.
- Re-measures — recompiles and re-runs the test after each mutation.
- Accepts or rejects — a mutation is kept only if it lowers deployment gas and does not increase the storage-slot count; otherwise it is reverted.
- Reports — prints per-contract and overall gas savings.
Because the search is stochastic, some contracts improve on a given run and others do not — this matches the behavior reported in the paper across the full dataset.
Docker builds the exact tree-sitter grammar, installs Foundry, and pins
forge-std, so no local toolchain is required.
# 1. Build the image (takes a few minutes the first time)
docker build -t dgred .
# 2. Start a container
docker run -it dgred
# 3. Inside the container, run DGRed on all bundled contracts
python3 dgred src/
# ...or run more mutation iterations per contract (default is 10)
python3 dgred src/ -iter 15Handy aliases are preconfigured inside the container:
run-dgred # == python3 dgred src/
test-forge # == forge test --gas-report| Requirement | Notes |
|---|---|
| Python 3.8+ | with the tree_sitter binding (see requirements.txt) |
Foundry (forge) |
https://book.getfoundry.sh/getting-started/installation |
| git | to fetch the forge-std submodule |
| Linux x86-64 | the prebuilt libtree-sitter-solidity.so targets this platform; other platforms should rebuild it (see below) |
# 1. Clone the repository together with the forge-std submodule
git clone --recursive https://github.com/<your-org>/dgred.git
cd dgred
# (if you already cloned without --recursive:)
git submodule update --init --recursive
# 2. Install Foundry (if not already installed)
curl -L https://foundry.paradigm.xyz | bash && foundryup
# 3. Install the Python dependency
pip install -r requirements.txt
# 4. Sanity check: all 20 deployment tests should pass
forge test
# 5. Run DGRed on all bundled contracts
python3 dgred src/You can also point DGRed at any single-contract subset by placing the .sol
files in src/ and a matching *.t.sol deployment test in test/.
python3 dgred <src_directory> [-iter N]
<src_directory> directory holding the .sol contracts to optimize (e.g. src/)
-iter N, number of mutation iterations per contract
--iterations N (default: 10; higher values search harder but take longer)
Examples:
python3 dgred src/ # default: 10 iterations per contract
python3 dgred src/ -iter 15 # search harder with 15 iterations
python3 dgred src/ -iter 3 # quick pass with 3 iterationsSince the search is stochastic, more iterations increase the chance of finding a
gas-reducing ordering, at the cost of more forge compilations.
The tool loads ./libtree-sitter-solidity.so. A prebuilt Linux x86-64 binary is
included. On other platforms, or if you hit a tree-sitter ABI error, rebuild it:
./scripts/build_parser.sh # requires git + a C compiler (and Node/tree-sitter CLI)DGRed is not limited to the bundled contracts — you can run it on any contract of your own. You only need two files: the contract, and a small deployment test that lets DGRed measure its deployment gas.
-
Add your contract. Put your
.solfile insrc/(e.g.src/MyToken.sol). -
Write a deployment test. Create a matching
*.t.solintest/(e.g.test/MyToken.t.sol) that imports your contract and deploys it inside atestDeploy*function. It only needs to construct the contract — that is what DGRed reads the deployment gas from:// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "forge-std/Test.sol"; import "../src/MyToken.sol"; contract MyTokenTest is Test { function testDeployMyToken() public { MyToken c = new MyToken(/* constructor args, if any */); assertTrue(address(c) != address(0)); } }
-
Run DGRed. From either Docker or a local clone:
python3 dgred src/ # optimize every contract in src/ python3 dgred src/ -iter 15 # search harder with more iterations
DGRed processes each contract in
src/that has a matching test intest/, so your contract is picked up automatically alongside (or instead of) the bundled ones.
Tip: Before running DGRed, make sure
forge testpasses for your new test — DGRed relies on a passing deployment test to establish the baseline gas.
DGRed: Deployment Gas Reduction Tool
==================================================
Baseline gas costs established for 20 contracts:
...
======================================================================
FINAL OPTIMIZATION REPORT
======================================================================
<Contract>.sol 2,974,348 → 2,948,120 (↓26,228, 0.88%)
...
----------------------------------------------------------------------
Total gas before: ...
Total gas after: ...
Total gas saved: ...
Overall improvement: X.XX%
Note: DGRed optimizes deployment gas only. Runtime execution gas is unaffected, since state-variable order that respects storage packing does not change the runtime storage layout cost.
The 20 contracts in src/ are real, verified contracts drawn from the paper's
evaluation dataset (some are named, some retain their on-chain address as the
filename). Each has a self-contained, standalone Foundry deployment test in
test/. All 20 tests pass with the pinned forge-std (v1.9.2) and the
optimizer configuration in foundry.toml (optimizer enabled, 200 runs, London
EVM), matching the paper's measurement setup.