Skip to content

Commit

Permalink
Merge pull request #551 from GridPlus/nb/forge
Browse files Browse the repository at this point in the history
adds forge test suite
  • Loading branch information
netbonus authored May 8, 2024
2 parents 32b5389 + 1ecbbef commit a8453fd
Show file tree
Hide file tree
Showing 16 changed files with 1,467 additions and 82 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ node_modules
coverage
.env
*.temp
~/node_modules
~/node_modules
cache
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "forge/lib/forge-std"]
path = forge/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "forge/lib/openzeppelin-contracts"]
path = forge/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
18 changes: 9 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"editor.codeActionsOnSave": {
"source.addMissingImports": true,
"source.organizeImports": true,
"source.formatDocument": true,
"source.fixAll": true
},
"editor.showUnused": true,
"editor.formatOnPaste": true,
"editor.formatOnSave": true
"editor.codeActionsOnSave": {
"source.addMissingImports": true,
"source.fixAll": true,
"source.formatDocument": true,
"source.organizeImports": true
},
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.showUnused": true
}
34 changes: 34 additions & 0 deletions forge/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: test

on: workflow_dispatch

env:
FOUNDRY_PROFILE: ci

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

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

- name: Run Forge build
run: |
forge --version
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
14 changes: 14 additions & 0 deletions forge/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
66 changes: 66 additions & 0 deletions forge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
6 changes: 6 additions & 0 deletions forge/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions forge/lib/forge-std
Submodule forge-std added at bb4cee
1 change: 1 addition & 0 deletions forge/lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
34 changes: 34 additions & 0 deletions forge/src/NegativeAmountHandler.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "openzeppelin-contracts/contracts/utils/cryptography/EIP712.sol";
import "openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol";

contract NegativeAmountHandler is EIP712 {
string private constant SIGNING_DOMAIN = "NegativeAmountHandler";
string private constant SIGNATURE_VERSION = "1";

struct Data {
int256 amount;
string message;
}

constructor() EIP712(SIGNING_DOMAIN, SIGNATURE_VERSION) {}

function verify(Data calldata data, bytes calldata signature) public view returns (bool) {
address signer = _verify(_hash(data), signature);
return signer == msg.sender; // Ensure that the signer is the sender of the message
}

function _hash(Data calldata data) internal view returns (bytes32) {
return _hashTypedDataV4(keccak256(abi.encode(
keccak256("Data(int256 amount,string message)"),
data.amount,
keccak256(bytes(data.message))
)));
}

function _verify(bytes32 digest, bytes memory signature) internal view returns (address) {
return ECDSA.recover(digest, signature);
}
}
Loading

0 comments on commit a8453fd

Please sign in to comment.