Skip to content

Commit dffaf51

Browse files
committed
example foundry contract
1 parent a05d532 commit dffaf51

File tree

10 files changed

+238
-0
lines changed

10 files changed

+238
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: test
2+
3+
on: workflow_dispatch
4+
5+
env:
6+
FOUNDRY_PROFILE: ci
7+
8+
jobs:
9+
check:
10+
strategy:
11+
fail-fast: true
12+
13+
name: Foundry project
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
with:
18+
submodules: recursive
19+
20+
- name: Install Foundry
21+
uses: foundry-rs/foundry-toolchain@v1
22+
with:
23+
version: nightly
24+
25+
- name: Run Forge build
26+
run: |
27+
forge --version
28+
forge build --sizes
29+
id: build
30+
31+
- name: Run Forge tests
32+
run: |
33+
forge test -vvv
34+
id: test
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Compiler files
2+
cache/
3+
out/
4+
5+
# Ignores development broadcast logs
6+
!/broadcast
7+
/broadcast/*/31337/
8+
/broadcast/**/dry-run/
9+
10+
# Docs
11+
docs/
12+
13+
# Dotenv file
14+
.env
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## Foundry
2+
3+
**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**
4+
5+
Foundry consists of:
6+
7+
- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
8+
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
9+
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
10+
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.
11+
12+
## Documentation
13+
14+
https://book.getfoundry.sh/
15+
16+
## Usage
17+
18+
### Build
19+
20+
```shell
21+
$ forge build
22+
```
23+
24+
### Test
25+
26+
```shell
27+
$ forge test
28+
```
29+
30+
### Format
31+
32+
```shell
33+
$ forge fmt
34+
```
35+
36+
### Gas Snapshots
37+
38+
```shell
39+
$ forge snapshot
40+
```
41+
42+
### Anvil
43+
44+
```shell
45+
$ anvil
46+
```
47+
48+
### Deploy
49+
50+
```shell
51+
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
52+
```
53+
54+
### Cast
55+
56+
```shell
57+
$ cast <subcommand>
58+
```
59+
60+
### Help
61+
62+
```shell
63+
$ forge --help
64+
$ anvil --help
65+
$ cast --help
66+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[profile.default]
2+
src = "src"
3+
out = "out"
4+
libs = ["lib"]
5+
6+
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.13;
3+
4+
import {Script, console} from "forge-std/Script.sol";
5+
6+
contract CounterScript is Script {
7+
function setUp() public {}
8+
9+
function run() public {
10+
vm.broadcast();
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.13;
3+
4+
contract Counter {
5+
uint256 public number;
6+
7+
function setNumber(uint256 newNumber) public {
8+
number = newNumber;
9+
}
10+
11+
function increment() public {
12+
number++;
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.13;
3+
4+
import {Test, console} from "forge-std/Test.sol";
5+
import {Counter} from "../src/Counter.sol";
6+
7+
contract CounterTest is Test {
8+
Counter public counter;
9+
10+
function setUp() public {
11+
counter = new Counter();
12+
counter.setNumber(0);
13+
}
14+
15+
function test_Increment() public {
16+
counter.increment();
17+
assertEq(counter.number(), 1);
18+
}
19+
20+
function testFuzz_SetNumber(uint256 x) public {
21+
counter.setNumber(x);
22+
assertEq(counter.number(), x);
23+
}
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[contracts_src]
2+
[blockchain_a]
3+
type = "anvil"
4+
docker_cmd_params = ["-b", "3"]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package examples
2+
3+
import (
4+
"github.com/smartcontractkit/chainlink-testing-framework/framework"
5+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain"
6+
"github.com/smartcontractkit/chainlink-testing-framework/framework/examples/example_components/onchain"
7+
"github.com/smartcontractkit/chainlink-testing-framework/seth"
8+
"github.com/stretchr/testify/require"
9+
"testing"
10+
)
11+
12+
type VerifyCfg struct {
13+
ContractsSrc *onchain.Input `toml:"contracts_src" validate:"required"`
14+
BlockchainA *blockchain.Input `toml:"blockchain_a" validate:"required"`
15+
}
16+
17+
func TestVerify(t *testing.T) {
18+
in, err := framework.Load[VerifyCfg](t)
19+
require.NoError(t, err)
20+
21+
bc, err := blockchain.NewBlockchainNetwork(in.BlockchainA)
22+
require.NoError(t, err)
23+
24+
// connect 2 clients
25+
scSrc, err := seth.NewClientBuilder().
26+
WithRpcUrl(bc.Nodes[0].HostWSUrl).
27+
WithGasPriceEstimations(true, 0, seth.Priority_Fast).
28+
WithTracing(seth.TracingLevel_All, []string{seth.TraceOutput_Console}).
29+
WithPrivateKeys([]string{blockchain.DefaultAnvilPrivateKey}).
30+
Build()
31+
require.NoError(t, err)
32+
in.ContractsSrc.URL = bc.Nodes[0].HostWSUrl
33+
c, err := onchain.NewProductOnChainDeployment(scSrc, in.ContractsSrc)
34+
require.NoError(t, err)
35+
36+
t.Run("test something", func(t *testing.T) {
37+
err := framework.VerifyContract(
38+
bc.Nodes[0].HostHTTPUrl,
39+
c.Addresses[0].String(),
40+
"/Users/fahrenheit/GolandProjects/chainlink-testing-framework/framework/examples/myproject/example_components/onchain/src/Counter.sol",
41+
"Counter",
42+
"http://localhost",
43+
)
44+
require.NoError(t, err)
45+
})
46+
}

framework/verify.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package framework
2+
3+
import "fmt"
4+
5+
// VerifyContract wraps the forge verify-contract command.
6+
func VerifyContract(rpcURL, address, contractFile, contractName, blockscoutURL string) error {
7+
verifierURL := fmt.Sprintf("%s/api/", blockscoutURL)
8+
args := []string{
9+
"verify-contract",
10+
"--rpc-url", rpcURL,
11+
"--compiler-version=0.8.17",
12+
address,
13+
fmt.Sprintf("%s:%s", contractFile, contractName),
14+
"--verifier", "blockscout",
15+
"--verifier-url", verifierURL,
16+
}
17+
return runCommand("forge", args...)
18+
}

0 commit comments

Comments
 (0)