Skip to content

Commit

Permalink
add deploy scripts, makefile, updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burnto committed Dec 21, 2023
1 parent 81614cf commit c70ef83
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ docs/

# Dotenv file
.env

artifacts
53 changes: 53 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Adapted from https://github.com/smartcontractkit/foundry-starter-kit/blob/main/Makefile

# -include .env

# .SILENT:

.PHONY: all test clean

all: clean update build

# Clean the repo
clean :; forge clean

# Remove modules
remove :; rm -rf .gitmodules && rm -rf .git/modules/* && rm -rf lib && touch .gitmodules && git add . && git commit -m "modules"

# install :; forge install smartcontractkit/chainlink-brownie-contracts && forge install rari-capital/solmate && forge install foundry-rs/forge-std

# Update Dependencies
update:; forge update

build:; forge build

test :; forge test

snapshot :; forge snapshot

slither :; slither ./src

format :; forge fmt

abi:
mkdir -p artifacts
forge inspect PizzaFactory abi > artifacts/PizzaFactory.abi.json && forge inspect Pizza abi > artifacts/Pizza.abi.json

# solhint should be installed globally
lint :; solhint src/**/*.sol && solhint src/*.sol

anvil :; anvil -m 'test test test test test test test test test test test junk'

# use the "@" to hide the command from your shell
deploy-sepolia :; @forge script script/${contract}.s.sol:Deploy${contract} --rpc-url ${SEPOLIA_RPC_URL} --private-key ${PRIVATE_KEY} --broadcast --verify --etherscan-api-key ${ETHERSCAN_API_KEY} -vvvv

# anvil deploy with the default user
deploy-anvil :; @forge script script/PizzaFactory.s.sol:DeployPizzaFactory --rpc-url http://localhost:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --broadcast

# anvil deploy with the default user
deploy-anvil-sample-pizza :; @forge script script/PizzaFactory.s.sol:DeploySamplePizza --rpc-url http://localhost:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --broadcast -vvvv


# deploy-all :; make deploy-${network} contract=APIConsumer && make deploy-${network} contract=KeepersCounter && make deploy-${network} contract=PriceFeedConsumer && make deploy-${network} contract=VRFConsumerV2

-include ${FCT_PLUGIN_PATH}/makefile-external
11 changes: 11 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ out = "out"
libs = ["lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

[profile.forked]
fork_block_number = 18814000
fork_url = "mainnet"

[rpc_endpoints]
mainnet = "${MAINNET_RPC_URL}"

[etherscan]
mainnet = { key = "${ETHERSCAN_API_KEY}" }
goerli = { key = "${ETHERSCAN_API_KEY}", chain = "goerli"}
2 changes: 1 addition & 1 deletion lib/forge-std
Submodule forge-std updated 3 files
+1 −1 package.json
+23 −2 src/Vm.sol
+1 −1 test/Vm.t.sol
2 changes: 1 addition & 1 deletion lib/openzeppelin-contracts
12 changes: 0 additions & 12 deletions script/Counter.s.sol

This file was deleted.

34 changes: 34 additions & 0 deletions script/PizzaFactory.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Script, console2} from "forge-std/Script.sol";
import {Pizza} from "../src/Pizza.sol";
import {PizzaFactory} from "../src/PizzaFactory.sol";

contract DeployPizzaFactory is Script {
function setUp() public {}

function run() public returns (Pizza implementation, PizzaFactory factory) {
vm.startBroadcast();
bytes32 salt = bytes32(vm.envUint("DEPLOY_SALT"));
implementation = new Pizza{salt: salt}();
factory = new PizzaFactory{salt: salt}(address(implementation));
}
}

contract DeploySamplePizza is Script {
function setUp() public {}

function run(address factory) public returns (address) {
PizzaFactory f = PizzaFactory(factory);
vm.startBroadcast();

address[] memory payees = new address[](2);
payees[0] = vm.createWallet("wallet a").addr;
payees[1] = vm.createWallet("wallet b").addr;
uint256[] memory shares = new uint256[](2);
shares[0] = 100;
shares[1] = 100;
return address(f.create(payees, shares));
}
}
15 changes: 2 additions & 13 deletions src/Pizza.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import {SafeERC20} from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol"
import {IERC20} from "openzeppelin-contracts/token/ERC20/IERC20.sol";
import {Context} from "openzeppelin-contracts/utils/Context.sol";
import {Address} from "openzeppelin-contracts/utils/Address.sol";
import {Multicall} from "openzeppelin-contracts/utils/Multicall.sol";

/**
* @title Pizza
* @dev This contract is a simplifications of OpenZeppelin's PaymentSplitter.
* It allows for the release of ERC20 tokens as well as Ether.
* Releases are modified to be only callable in a batch, rather than individually.
*/
contract Pizza is Initializable, Context {
contract Pizza is Initializable, Context, Multicall {
using SafeERC20 for IERC20;

/**
Expand Down Expand Up @@ -122,18 +123,6 @@ contract Pizza is Initializable, Context {
emit ERC20Release(token, erc20TotalReleasable);
}

function releaseWithBounty(address bountyReceiver) external {
uint256 bounty = address(this).balance * releaseBountyBIPS / BIPS_PRECISION;
uint256 totalReleasable = address(this).balance - bounty;
require(totalReleasable > 0, "PaymentSplitter: no payment is due");
totalReleased += totalReleasable;
for (uint256 i = 0; i < payee.length; i++) {
_releaseTo(totalReleasable, payee[i]);
}
_releaseTo(bounty, bountyReceiver);
emit Release(totalReleasable);
}

/* ////////////////////////////////////////////////////////////////////////
Getters
//////////////////////////////////////////////////////////////////////// */
Expand Down
69 changes: 69 additions & 0 deletions test/PizzaFactory.fork.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.23;

import {Test, console2} from "forge-std/Test.sol";
import {PizzaFactory, PizzaCreated} from "../src/PizzaFactory.sol";
import {Pizza} from "../src/Pizza.sol";
import {Address} from "openzeppelin-contracts/utils/Address.sol";
import {IERC20} from "openzeppelin-contracts/token/ERC20/IERC20.sol";
import {Clones} from "openzeppelin-contracts/proxy/Clones.sol";

event PaymentReceived(address from, uint256 amount);

contract PizzaFactoryForkTest is Test {
PizzaFactory public f;
address[] payees;
uint256[] shares;

function setUp() public {
vm.createSelectFork("mainnet", 18814000);

f = new PizzaFactory(address(new Pizza()));

payees.push(vm.createWallet("wallet a").addr);
payees.push(vm.createWallet("wallet b").addr);
shares.push(2);
shares.push(3);
}

function test_cloneReceive() public {
Pizza p = Pizza(payable(address(f.create(payees, shares))));

address sender = address(0x3);
vm.deal(sender, 3 ether);

vm.startPrank(sender);

vm.expectEmit(true, true, false, true);
emit PaymentReceived(sender, 1 ether);
payable(p).transfer(1 ether);

vm.expectEmit(true, true, false, true);
emit PaymentReceived(sender, 1 ether);
bool ok = payable(p).send(1 ether);
assertTrue(ok);

vm.expectEmit(true, true, false, true);
emit PaymentReceived(sender, 1 ether);
(ok,) = payable(p).call{value: 1 ether}("");
assertTrue(ok);
}

function test_release() public {
Pizza p = Pizza(payable(address(f.create(payees, shares))));

address sender = address(0x3);
vm.deal(sender, 1 ether);
vm.expectEmit(true, true, false, true);
emit PaymentReceived(sender, 1 ether);
vm.prank(sender);
Address.sendValue(payable(p), 1 ether);

assertEq(payable(payees[0]).balance, 0);
assertEq(payable(payees[1]).balance, 0);

p.release();
assertEq(payable(payees[0]).balance, 0.4 ether);
assertEq(payable(payees[1]).balance, 0.6 ether);
}
}
23 changes: 23 additions & 0 deletions test/PizzaFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ contract PizzaFactoryTest is Test {
f.createDeterministic(payees, shares, nonce);
}

function test_cloneReceive() public {
Pizza p = Pizza(payable(address(f.create(payees, shares))));

address sender = address(0x3);
vm.deal(sender, 3 ether);

vm.startPrank(sender);

vm.expectEmit(true, true, false, true);
emit PaymentReceived(sender, 1 ether);
payable(p).transfer(1 ether);

vm.expectEmit(true, true, false, true);
emit PaymentReceived(sender, 1 ether);
bool ok = payable(p).send(1 ether);
assertTrue(ok);

vm.expectEmit(true, true, false, true);
emit PaymentReceived(sender, 1 ether);
(ok,) = payable(p).call{value: 1 ether}("");
assertTrue(ok);
}

function test_release() public {
Pizza p = Pizza(payable(address(f.create(payees, shares))));

Expand Down

0 comments on commit c70ef83

Please sign in to comment.