Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dphilipson committed Sep 6, 2023
0 parents commit 56c0a52
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DEPLOYER_PRIVATE_KEY=

RPC_URL_MAINNET=
RPC_URL_GOERLI=

ETHERSCAN_API_KEY=
34 changes: 34 additions & 0 deletions .gasestimates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gas Estimates
Generated via `bash utils/inspect.sh`.

---

`forge test --gas-report --no-match-path "test/script/**/*"`
| src/Counter.sol:Counter contract | | | | | |
|----------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 77935 | 417 | | | | |
| Function Name | min | avg | median | max | # calls |
| increment | 22334 | 22334 | 22334 | 22334 | 1 |
| number | 269 | 269 | 269 | 269 | 2 |
| setNumber | 2338 | 8971 | 2338 | 22238 | 3 |



`forge inspect src/Counter.sol:Counter gasestimates`
```json
{
"creation": {
"codeDepositCost": "77800",
"executionCost": "135",
"totalCost": "77935"
},
"external": {
"increment()": "24434",
"number()": "2269",
"setNumber(uint256)": "22238"
},
"internal": {}
}
```

25 changes: 25 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
Borrowed from foundry.
Thank you for your Pull Request. Please provide a description above and review
the requirements below.
Bug fixes and new features should include tests.
-->

## Motivation

<!--
Explain the context and why you're making that change. What is the problem
you're trying to solve? In some cases there is not a problem and this can be
thought of as being the motivation for your change.
If your PR solves a particular issue, tag that issue.
-->

## Solution

<!--
Summarize the solution and provide any necessary context needed to understand
the code change.
-->
63 changes: 63 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: sc-template Test CI

on: [pull_request, workflow_dispatch]

concurrency:
group: ${{github.workflow}}-${{github.ref}}
cancel-in-progress: true

# Runs linter, tests, and inspection checker in parallel
jobs:
lint:
name: Run Linter
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- run: forge install

- run: forge fmt --check

check-inspect:
name: Verify Inspections
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- run: forge install
- run: bash ./utils/inspect.sh

- run: git status --untracked-files=no --porcelain
- run: git --no-pager diff

- name: Check Inspections
run: if [[ -n "$(git status --untracked-files=no --porcelain)" ]]; then echo "Inspection difference detected, verify tests are passing and run \`bash ./utils/inspect.sh\` to fix." && exit 1; fi

test:
name: Run Forge Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Install forge dependencies
run: forge install

- name: Build project
run: forge build

- name: Run tests
run: forge test -vvv
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Foundry build and cache directories
out/
cache/
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
branch = v1.3.0
10 changes: 10 additions & 0 deletions .storagelayout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Storage Layouts
Generated via `bash utils/inspect.sh`.

---

`forge inspect --pretty src/Counter.sol:Counter storage-layout`
| Name | Type | Slot | Offset | Bytes | Contract |
|--------|---------|------|--------|-------|-------------------------|
| number | uint256 | 0 | 0 | 32 | src/Counter.sol:Counter |

11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"solidity.packageDefaultDependenciesContractsDirectory": "src",
"solidity.packageDefaultDependenciesDirectory": "lib",
"solidity.compileUsingRemoteVersion": "v0.8.14",
"editor.formatOnSave": true,
"[solidity]": {
"editor.defaultFormatter": "JuanBlanco.solidity"
},
"solidity.formatter": "forge",
"search.exclude": { "lib": true }
}
22 changes: 22 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[profile.default]
solc = '0.8.14'
via_ir = true
src = 'src'
out = 'out'
test = 'test'
libs = ['lib']
optimizer = true
optimizer_runs = 10_000

[fuzz]
runs = 5000

[rpc_endpoints]
mainnet = "${RPC_URL_MAINNET}"
goerli = "${RPC_URL_GOERLI}"

[etherscan]
mainnet = { key = "${ETHERSCAN_API_KEY}" }
goerli = { key = "${ETHERSCAN_API_KEY}" }

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 066ff1
2 changes: 2 additions & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ds-test/=lib/forge-std/lib/ds-test/src/
forge-std/=lib/forge-std/src/
12 changes: 12 additions & 0 deletions script/Counter.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.14;

import "forge-std/Script.sol";

contract CounterScript is Script {
function setUp() public {}

function run() public {
vm.broadcast();
}
}
3 changes: 3 additions & 0 deletions slither.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"filter_paths": "lib"
}
19 changes: 19 additions & 0 deletions src/Counter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.14;

/// @title A public counter for anyone to use.
contract Counter {
uint256 public number;

/// @notice Set the counter's number to a new value.
/// @param newNumber The new number for the counter.
function setNumber(uint256 newNumber) public {
number = newNumber;
}

/// @notice Increase the counter's value by one.
/// @dev The number is not in an unchecked block, so overflows will revert.
function increment() public {
number++;
}
}
24 changes: 24 additions & 0 deletions test/Counter.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.14;

import "forge-std/Test.sol";
import "../src/Counter.sol";

contract CounterTest is Test {
Counter public counter;

function setUp() public {
counter = new Counter();
counter.setNumber(0);
}

function testIncrement() public {
counter.increment();
assertEq(counter.number(), 1);
}

function testSetNumber(uint256 x) public {
counter.setNumber(x);
assertEq(counter.number(), x);
}
}
46 changes: 46 additions & 0 deletions utils/inspect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# Generate inspection files in MD format for all primary contracts in src.

CONTRACT_FILES=($(find src -iname '*.sol' | sort))

rm .storagelayout.md
rm .gasestimates.md

echo "# Storage Layouts" >> .storagelayout.md
echo "Generated via \`bash utils/inspect.sh\`." >> .storagelayout.md
echo "" >> .storagelayout.md
echo "---" >> .storagelayout.md
echo "" >> .storagelayout.md

echo "# Gas Estimates" >> .gasestimates.md
echo "Generated via \`bash utils/inspect.sh\`." >> .gasestimates.md
echo "" >> .gasestimates.md
echo "---" >> .gasestimates.md
echo "" >> .gasestimates.md
echo "\`forge test --gas-report --no-match-path \"test/script/**/*\"\`" >> .gasestimates.md
# Sed strings to strip color data and only start printing after the first '|' character, to exclude previous contents (compilation, test results, etc)
forge test --gas-report --no-match-path "test/script/**/*" | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g" | sed -nr '/\|/,$p' >> .gasestimates.md

for index in ${!CONTRACT_FILES[*]}; do
# echo "${CONTRACT_NAMES[$index]} is in ${CONTRACT_FILES[$index]}"
CONTRACT_NAME=$(basename -s ".sol" ${CONTRACT_FILES[${index}]})
# echo ${CONTRACT_NAME}
# If file does not contain a contract named the same as the filename, discard from inspection (e.g. libraries).
if ! grep -q "contract ${CONTRACT_NAME}" ${CONTRACT_FILES[$index]}; then
# echo "Skipping ${CONTRACT_NAME}"
continue
fi

# Show command names in files
echo "\`forge inspect --pretty ${CONTRACT_FILES[$index]}:${CONTRACT_NAME} storage-layout\`" >> .storagelayout.md
forge inspect --pretty ${CONTRACT_FILES[$index]}:${CONTRACT_NAME} storage-layout >> .storagelayout.md
echo "" >> .storagelayout.md

echo "\`forge inspect ${CONTRACT_FILES[$index]}:${CONTRACT_NAME} gasestimates\`" >> .gasestimates.md
echo "\`\`\`json" >> .gasestimates.md
forge inspect ${CONTRACT_FILES[$index]}:${CONTRACT_NAME} gasestimates >> .gasestimates.md
echo "\`\`\`" >> .gasestimates.md
echo "" >> .gasestimates.md

done

0 comments on commit 56c0a52

Please sign in to comment.