Skip to content

Commit

Permalink
Setup foundry for local development
Browse files Browse the repository at this point in the history
Why:
* We prefer Foundry and its tooling for developing locally

How:
* Adding a `script/DevDeploy.s.sol` script to deploy the contracts
  locally
* Setting up `wagmi` CLI and adding a config file to output the
  generated ABIs to the `web` package
* Adding a `Justfile` for easier connection of tools
* Updating `.gitignore`
  • Loading branch information
DavideSilva committed Mar 5, 2024
1 parent d7d94b2 commit 73522a0
Show file tree
Hide file tree
Showing 7 changed files with 825 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ src/types
deployments/localhost
*.csv
cache_forge
broadcast/
86 changes: 86 additions & 0 deletions packages/contracts/Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
set positional-arguments
set dotenv-load

mnemonic := env_var_or_default("DEV_MNEMONIC", "test test test test test test test test test test test junk")
sender := env_var_or_default("DEV_SENDER", "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")

#
# aliases
#

alias w := watch-test
alias d := dev

#
# public commands
#


# full dev stack
# 1. eth-watch (live-reloading anvil w/ deploy)
# 2. next.js
dev:
#!/bin/bash -ue
trap 'kill %1; kill %2' SIGINT
just eth-watch | sed -e 's/^/\x1b[0;31m[node]\x1b[0m /' &
just web 2>&1 | sed -e 's/^/\x1b[0;32m[next]\x1b[0m /' &
wait

watch-test:
forge test --watch -vvv

test:
forge test

#
# private commands
#

# 1. build contracts
# 2. run test suite
# 3. re-gen wagmi asynchronously
# 4. wait a bit for anvil to boot, then run deploy script asynchronously
eth:
#!/bin/bash
killall -9 anvil
just build-contracts
# just test
sleep 0.2 && just eth-deploy &
just wagmi &
anvil --host 0.0.0.0

build-contracts:
forge build

# start next.js dev server
web:
yarn dev

# generate wagmi
wagmi:
yarn run wagmi generate

# restart `just eth` when contracts change
eth-watch:
watchexec \
--watch contracts \
--watch test \
--watch foundry.toml \
--restart \
--exts sol,toml \
just eth

# deploy contracts for development
eth-deploy:
just forge-script script/DevDeploy.s.sol

# run a forge script from the appropriate sender
forge-script script:
forge script \
$1 \
--fork-url http://localhost:8545 \
--broadcast \
--mnemonics "{{ mnemonic }}" \
--sender "{{ sender }}" \
--json

3 changes: 2 additions & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"devDependencies": {
"@nomicfoundation/hardhat-foundry": "^1.1.1",
"@openzeppelin/test-helpers": "^0.5.15"
"@openzeppelin/test-helpers": "^0.5.15",
"@wagmi/cli": "^2.1.2"
}
}
38 changes: 38 additions & 0 deletions packages/contracts/script/DevDeploy.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.12;

import {Script} from "lib/forge-std/src/Script.sol";

import {Citizend} from "contracts/token/Citizend.sol";
import {Staking} from "contracts/discovery/Staking.sol";

contract DevDeployScript is Script {
address alice = address(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266);
address bob = address(0x70997970C51812dc3A010C7d01b50e0d17dc79C8);
address carol = address(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266);

address[] testAccounts;

function setUp() public {
testAccounts = new address[](3);
testAccounts[0] = alice;
testAccounts[1] = bob;
testAccounts[2] = carol;
}

function run() public {
vm.startBroadcast();

Citizend citizend = new Citizend(alice);
Staking staking = new Staking(address(citizend));

for (uint256 i; i < testAccounts.length; i++) {
address addr = testAccounts[i];
(bool success,) = addr.call{value: 10 ether}("");
require(success, "transfer failed");
}

vm.stopBroadcast();
}

}
24 changes: 24 additions & 0 deletions packages/contracts/wagmi.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineConfig } from '@wagmi/cli'
import { foundry, react } from "@wagmi/cli/plugins";

export default defineConfig({
out: '../web/wagmi.generated.ts',
contracts: [],
plugins: [
foundry({
exclude: [
'MockERC20.sol',
],
project: "./",
deployments: {
Citizend: {
31337: "0x5fbdb2315678afecb367f032d93f642f64180aa3",
},
Staking: {
31337: "0xe7f1725e7734ce288f8367e1bb143e90bb3f0512",
},
},
})
],

})
2 changes: 2 additions & 0 deletions packages/web/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Contracts
contracts/localhost

wagmi.generated.ts
Loading

0 comments on commit 73522a0

Please sign in to comment.