Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup foundry for local development #175

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}

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

export default defineConfig({
out: '../web/wagmi.generated.ts',
contracts: [],
plugins: [
foundry({
exclude: [
// We have a MockERC20.sol contract that is conflicting with the MockERC20 contract from forge
// Until we refactor, we need to ignore it for the wagmi compilation to succeed
'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
Loading