Skip to content

Commit

Permalink
better foundry opt, tidy some scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
erhant committed Dec 19, 2024
1 parent b585866 commit df00337
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 629 deletions.
6 changes: 6 additions & 0 deletions .solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "solhint:default",
"rules": {
"max-line-length": ["off", 120]
}
}
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["JuanBlanco.solidity"]
}
10 changes: 8 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"editor.formatOnSave": true
}
"solidity.packageDefaultDependenciesContractsDirectory": "src",
"solidity.packageDefaultDependenciesDirectory": "lib",
"editor.formatOnSave": true,
"[solidity]": {
"editor.defaultFormatter": "JuanBlanco.solidity"
},
"solidity.formatter": "forge"
}
59 changes: 38 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,14 @@ forge install
Compile the contracts with:

```sh
forge clean && forge build
forge build
```

### Upgradability

We are using [openzeppelin-foundry-upgrades](https://github.com/OpenZeppelin/openzeppelin-foundry-upgrades) library. To make sure upgrades are **safe**, you must do one of the following (as per their [docs](https://github.com/OpenZeppelin/openzeppelin-foundry-upgrades?tab=readme-ov-file#before-running)) before you run `forge script` or `forge test`:

- `forge clean` beforehand, e.g. `forge clean && forge test`
- include `--force` option when running, e.g. `forge test --force`

> [!NOTE]
>
> Note that for some users this may fail (see [issue](https://github.com/firstbatchxyz/dria-oracle-contracts/issues/16)) due to a missing NPM package called `@openzeppelin/upgrades-core`. To fix it, do:
> We are using [openzeppelin-foundry-upgrades](https://github.com/OpenZeppelin/openzeppelin-foundry-upgrades) library, which [requires](https://github.com/OpenZeppelin/openzeppelin-foundry-upgrades?tab=readme-ov-file#before-running) clean-up per compilation to ensure upgrades are done safely. We use `force = true` option in `foundry.toml` for this, which may increase build times.
>
> Note that for some users this may fail (see [issue](https://github.com/firstbatchxyz/dria-oracle-contracts/issues/16)) due to a missing NPM package called `@openzeppelin/upgrades-core`. To fix it, you can install the package manually:
>
> ```sh
> npm install @openzeppelin/upgrades-core@latest -g
Expand Down Expand Up @@ -115,7 +110,7 @@ You will use this endpoint for the commands that interact with the blockchain, s
Deploy the contract with:

```sh
forge clean && forge script ./script/Deploy.s.sol:Deploy<CONTRACT_NAME> \
forge script ./script/Deploy.s.sol:Deploy<CONTRACT_NAME> \
--rpc-url <RPC_URL> \
--account <WALLET_NAME> \
--broadcast
Expand All @@ -126,7 +121,7 @@ You can see deployed contract addresses under the `deployment/<chainid>.json`
You can verify the contract during deployment by adding the verification arguments as well:

```sh
forge clean && forge script ./script/Deploy.s.sol:Deploy<CONTRACT_NAME> \
forge script ./script/Deploy.s.sol:Deploy<CONTRACT_NAME> \
--rpc-url <RPC_URL> \
--account <WALLET_NAME> \
--broadcast \
Expand Down Expand Up @@ -155,57 +150,79 @@ Note that the `--verifier-url` value should be the target explorer's homepage UR
>
> The `--verifier` can accept any of the following: `etherscan`, `blockscout`, `sourcify`, `oklink`. We are using Blockscout most of the time.
### Generate ABIs

To interact with the contracts, you need the contract ABIs. We store the ABIs under the [`abis`](./abis/) folder, and these can be generated using the following script:

```sh
./export-abis.sh
```

## Testing & Diagnostics

Run tests on local network:

```sh
forge clean && forge test
forge test

# or -vvv to show reverts in detail
forge clean && forge test -vvv
forge test -vvv
```

or fork an existing chain and run the tests on it:

```sh
forge clean && forge test --rpc-url <RPC_URL>
forge test --rpc-url <RPC_URL>
```

### Code Coverage

We have a script that generates the coverage information as an HTML page. This script requires [`lcov`](https://linux.die.net/man/1/lcov) and [`genhtml`](https://linux.die.net/man/1/genhtml) command line tools. To run, do:

```sh
forge clean && ./coverage.sh
./coverage.sh
```

Alternatively, you can see a summarized text-only output as well:

```sh
forge clean && forge coverage --no-match-coverage "(test|mock|script)"
forge coverage --no-match-coverage "(test|mock|script)"
```

### Storage Layout

Get storage layout with:
You can print storage layouts for each contract using:

```sh
./storage.sh
```

You can see storage layouts under the [`storage`](./storage/) directory.
The resulting Markdown files will be created under the [`storage`](./storage/) directory.

### Gas Snapshot

Take the gas snapshot with:
You can examine the gas usage metrics using the command:

```sh
forge clean && forge snapshot
forge snapshot --snap ./test/.gas-snapshot
```

You can see the snapshot `.gas-snapshot` file in the current directory.

### Styling

You can format the contracts with:

```sh
forge fmt ./src/**/*.sol ./script/**/*.sol
```

If you have solhint installed, you can lint all contracts with:

```sh
solhint 'contracts/**/*.sol'
```

## Documentation

We have auto-generated MDBook documentations under the [`docs`](./docs) folder, generated with the following command:
Expand All @@ -219,4 +236,4 @@ forge doc --serve

## License

We are using Apache-2.0 license.
We are using [Apache-2.0](./LICENSE) license.
10 changes: 7 additions & 3 deletions coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ then
exit
fi

# create coverage directory for outputs
mkdir -p coverage

# generate coverage info
forge coverage \
--report lcov \
--report summary \
--no-match-coverage "(test|mock|script)"
--no-match-coverage "(test|mock|script)" \
--report-file ./coverage/lcov.info

# generate HTML report from lcov.info
genhtml lcov.info -o coverage --branch-coverage --ignore-errors inconsistent,category,corrupt
genhtml ./coverage/lcov.info -o coverage --branch-coverage --ignore-errors inconsistent,category,corrupt

# open report
open coverage/index.html
open ./coverage/index.html
8 changes: 7 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ test = 'test'
script = 'script'
cache_path = 'cache'

optimizer = true

# required by upgradability
# see: https://github.com/OpenZeppelin/openzeppelin-foundry-upgrades
ffi = true
ast = true
build_info = true
optimizer = true
extra_output = ["storageLayout"]

# forces recompilation, required by Upgradable contracts
force = true

# fs permissions for deployment (false by default)
fs_permissions = [
{ access = "read", path = "out" },
Expand Down
Loading

0 comments on commit df00337

Please sign in to comment.