Skip to content

Commit

Permalink
update docs, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burnto committed Jan 23, 2024
1 parent c3a0761 commit 26e0846
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 35 deletions.
6 changes: 5 additions & 1 deletion script/SamplePizza.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ pragma solidity ^0.8.13;
import {Script} from "forge-std/Script.sol";
import {PizzaFactory} from "../src/PizzaFactory.sol";

/**
* @title DeploySamplePizza
* @dev A contract for deploying a sample pizza contract.
*/
contract DeploySamplePizza is Script {
function setUp() public {}

Expand All @@ -17,6 +21,6 @@ contract DeploySamplePizza is Script {
uint256[] memory shares = new uint256[](2);
shares[0] = 100;
shares[1] = 100;
return address(f.create(payees, shares));
return address(f.create(payees, shares, 0));
}
}
14 changes: 14 additions & 0 deletions src/IPizzaInitializer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@ pragma solidity 0.8.23;
* @dev Interface for initializing a pizza contract.
*/
interface IPizzaInitializer {
/**
* @dev Initializes the contract.
* @param _payees The addresses of the payees.
* @param _shares The shares of each payee.
* @param _bounty The bounty amount.
*/
function initialize(address[] memory _payees, uint256[] memory _shares, uint256 _bounty) external;

/**
* @dev Initializes the contract.
* @param _payees The addresses of the payees.
* @param _shares The shares of each payee.
* @param _bounty The bounty amount.
* @param _bountyTokens The tokens to be used for the bounty.
* @param _bountyReceiver The address of the bounty receiver.
*/
function initializeWithBountyRelease(
address[] calldata _payees,
uint256[] calldata _shares,
Expand Down
1 change: 0 additions & 1 deletion src/Pizza.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity 0.8.23;
import {Initializable} from "openzeppelin-contracts/proxy/utils/Initializable.sol";
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";
import {ReentrancyGuard} from "openzeppelin-contracts/utils/ReentrancyGuard.sol";
Expand Down
60 changes: 41 additions & 19 deletions src/PizzaFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.23;
import {Clones} from "openzeppelin-contracts/proxy/Clones.sol";
import {IPizzaInitializer} from "./IPizzaInitializer.sol";

event PizzaCreated(address indexed pizza);
event PizzaCreated(address indexed pizza, address indexed creator);

/**
* @title PizzaFactory
Expand All @@ -20,12 +20,6 @@ contract PizzaFactory {
*/
address public implementation;

/**
* @dev A mapping that stores pizzas created.
* The key is the address of the pizza owner, and the value is a boolean indicating creator.
*/
mapping(address => address) public pizzas;

/* ////////////////////////////////////////////////////////////////////////
Constructor
//////////////////////////////////////////////////////////////////////// */
Expand All @@ -38,20 +32,46 @@ contract PizzaFactory {
Factory
//////////////////////////////////////////////////////////////////////// */

/**
* @dev Creates a new Pizza contract with the given payees and shares.
* @param _payees The addresses of the payees.
* @param _shares The corresponding shares of each payee.
* @return pizza address of the newly created Pizza contract.
*/

function create(address[] memory _payees, uint256[] memory _shares) external returns (address pizza) {
pizza = address(Clones.clone(implementation));
_initFreePizza(pizza, _payees, _shares);
IPizzaInitializer(pizza).initialize(_payees, _shares, 0);
emit PizzaCreated(pizza, msg.sender);
}

function createDeterministic(address[] memory _payees, uint256[] memory _shares, uint256 _salt)
/**
* @dev Creates a new Pizza contract with the given payees, shares, and salt.
* @param _payees The addresses of the payees.
* @param _shares The corresponding shares of each payee.
* @param _salt The salt value used for deterministic cloning.
* @return pizza address of the newly created Pizza contract.
*/
function create(address[] memory _payees, uint256[] memory _shares, uint256 _salt)
external
returns (address pizza)
{
pizza = address(Clones.cloneDeterministic(implementation, salt(_payees, _shares, 0, _salt)));
_initFreePizza(pizza, _payees, _shares);
IPizzaInitializer(pizza).initialize(_payees, _shares, 0);
emit PizzaCreated(pizza, msg.sender);
}

function createDeterministicAndRelease(
/**
* @dev Creates a new Pizza contract with the given payees, shares, salt, bounty, bounty tokens, and bounty receiver.
* @param _payees The addresses of the payees.
* @param _shares The corresponding shares of each payee.
* @param _salt The salt value used for deterministic cloning.
* @param _bounty The bounty amount to be released.
* @param _bountyTokens The addresses of the bounty tokens.
* @param _bountyReceiver The address of the bounty receiver.
* @return pizza address of the newly created Pizza contract.
*/
function createAndRelease(
address[] memory _payees,
uint256[] memory _shares,
uint256 _salt,
Expand All @@ -61,10 +81,18 @@ contract PizzaFactory {
) external returns (address pizza) {
pizza = address(Clones.cloneDeterministic(implementation, salt(_payees, _shares, _bounty, _salt)));
IPizzaInitializer(pizza).initializeWithBountyRelease(_payees, _shares, _bounty, _bountyTokens, _bountyReceiver);
pizzas[pizza] = msg.sender;
emit PizzaCreated(pizza);
emit PizzaCreated(pizza, msg.sender);
}

/**
* @dev Predicts the address of the pizza contract with the given params
*
* @param _payees The addresses of the payees who will receive a share of the pizza order.
* @param _shares The corresponding shares of each payee.
* @param _bounty The bounty amount to be awarded to the successful predictor.
* @param _salt A random value used in case multiple contracts are created with the same params.
* @return The predicted address of the pizza contract.
*/
function predict(address[] memory _payees, uint256[] memory _shares, uint256 _bounty, uint256 _salt)
external
view
Expand All @@ -84,10 +112,4 @@ contract PizzaFactory {
{
return keccak256(abi.encode(_payees, _shares, _bounty, _salt));
}

function _initFreePizza(address _pizza, address[] memory _payees, uint256[] memory _shares) private {
IPizzaInitializer(_pizza).initialize(_payees, _shares, 0);
pizzas[_pizza] = msg.sender;
emit PizzaCreated(_pizza);
}
}
8 changes: 4 additions & 4 deletions test/Pizza.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity 0.8.23;

import {Test, console2} from "forge-std/Test.sol";
import {PizzaFactory, PizzaCreated} from "../src/PizzaFactory.sol";
import {PizzaFactory} 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";
Expand All @@ -26,7 +26,7 @@ contract PizzaTest is Test {
payees.push(address(0x2));
shares.push(2);
shares.push(3);
pizza = Pizza(payable(address(f.create(payees, shares))));
pizza = Pizza(payable(address(f.create(payees, shares, 0))));
}

function test_emitPaymentReceived() public {
Expand Down Expand Up @@ -103,7 +103,7 @@ contract PizzaTest is Test {
vm.deal(payable(predicted), 2e18);

vm.expectRevert(abi.encodeWithSelector(Pizza.InvalidBounty.selector));
f.createDeterministicAndRelease(payees, shares, salt, bounty, bountyTokens, bountyReceiver);
f.createAndRelease(payees, shares, salt, bounty, bountyTokens, bountyReceiver);
}

function test_bountyCreate(uint256 salt) public {
Expand All @@ -125,7 +125,7 @@ contract PizzaTest is Test {
address bountyReceiver = address(0x4);

vm.prank(bountyDeployer);
f.createDeterministicAndRelease(payees, shares, salt, bounty, bountyTokens, bountyReceiver);
f.createAndRelease(payees, shares, salt, bounty, bountyTokens, bountyReceiver);

assertEq(token.balanceOf(bountyDeployer), 0);
assertEq(token.balanceOf(bountyReceiver), 1e16);
Expand Down
6 changes: 3 additions & 3 deletions test/PizzaFactory.fork.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity 0.8.23;

import {Test, console2} from "forge-std/Test.sol";
import {PizzaFactory, PizzaCreated} from "../src/PizzaFactory.sol";
import {PizzaFactory} 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";
Expand All @@ -28,7 +28,7 @@ contract PizzaFactoryForkTest is Test {
}

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

address sender = address(0x3);
vm.deal(sender, 3 ether);
Expand All @@ -51,7 +51,7 @@ contract PizzaFactoryForkTest is Test {
}

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

address sender = address(0x3);
vm.deal(sender, 1 ether);
Expand Down
14 changes: 7 additions & 7 deletions test/PizzaFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ contract PizzaFactoryTest is Test {
shares.push(3);
}

function test_createDeterministic(uint256 nonce) public {
function test_create(uint256 nonce) public {
address predicted = f.predict(payees, shares, 0, nonce);
vm.expectEmit(true, true, true, true);
emit PizzaCreated(predicted);
Pizza p = Pizza(payable(address(f.createDeterministic(payees, shares, nonce))));
emit PizzaCreated(predicted, address(this));
Pizza p = Pizza(payable(address(f.create(payees, shares, nonce))));
assertEq(address(p), predicted);

assertNotEq(address(p), address(0));
Expand All @@ -48,14 +48,14 @@ contract PizzaFactoryTest is Test {
assertEq(p.erc20TotalReleased(IERC20(address(0))), 0);
}

function test_createDeterministicOnce(uint256 nonce) public {
Pizza(payable(address(f.createDeterministic(payees, shares, nonce))));
function test_createOnce(uint256 nonce) public {
Pizza(payable(address(f.create(payees, shares, nonce))));
vm.expectRevert(abi.encodeWithSelector(Clones.ERC1167FailedCreateClone.selector));
f.createDeterministic(payees, shares, nonce);
f.create(payees, shares, nonce);
}

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

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

0 comments on commit 26e0846

Please sign in to comment.