Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
burnto committed Jan 10, 2024
1 parent 55bd391 commit dc5a17e
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 31 deletions.
114 changes: 85 additions & 29 deletions src/Pizza.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ contract Pizza is Initializable, Context, Multicall, ReentrancyGuard {
mapping(IERC20 => uint256) public erc20TotalReleased;

/**
* @notice The amount of funds released to a payee.
* @notice The bounty paid to releaser.
*/
uint32 public releaseBountyBIPS;
uint32 public releaseBountyBIPs;

/* ////////////////////////////////////////////////////////////////////////
Construction + Initialization
Expand All @@ -128,18 +128,42 @@ contract Pizza is Initializable, Context, Multicall, ReentrancyGuard {
* @notice Initializes the contract with the specified payees and shares.
* @param _payees The addresses of the payees.
* @param _shares The corresponding shares of each payee.
* @param _releaseBountyBIPs The bounty paid to releaser.
*/
function initialize(address[] memory _payees, uint256[] memory _shares) external initializer {
if (_payees.length != _shares.length) {
revert PayeeShareLengthMismatch();
}
if (_payees.length == 0) {
revert NoPayees();
}
function initialize(address[] memory _payees, uint256[] memory _shares, uint32 _releaseBountyBIPs)
external
initializer
{
_initializePayeeShares(_payees, _shares);
releaseBountyBIPs = _releaseBountyBIPs;
}

for (uint256 i = 0; i < _payees.length; i++) {
_addPayee(_payees[i], _shares[i]);
/**
* @notice Initializes the contract with the specified payees and shares.
* @param _payees The addresses of the payees.
* @param _shares The corresponding shares of each payee.
* @param _releaseBountyBIPs The bounty paid to releaser.
* @param _deployBountyBIPs The bounty paid to deployer.
* @param _deployBountyTokens The tokens to pay to deployer.
*/
function initializeWithBounty(
address[] memory _payees,
uint256[] memory _shares,
uint32 _releaseBountyBIPs,
uint32 _deployBountyBIPs,
IERC20[] memory _deployBountyTokens
) external initializer {
_initializePayeeShares(_payees, _shares);
releaseBountyBIPs = _releaseBountyBIPs;
address bountyReceiver = _msgSender();
for (uint256 i = 0; i < _deployBountyTokens.length; i++) {
SafeERC20.safeTransfer(
_deployBountyTokens[i],
bountyReceiver,
_deployBountyTokens[i].balanceOf(address(this)) * _deployBountyBIPs / BIPS_PRECISION
);
}
Address.sendValue(payable(bountyReceiver), address(this).balance * _deployBountyBIPs / BIPS_PRECISION);
}

/* ////////////////////////////////////////////////////////////////////////
Expand All @@ -163,7 +187,7 @@ contract Pizza is Initializable, Context, Multicall, ReentrancyGuard {
/**
* @notice Releases available ETH balance.
*/
function release() external {
function release() public {
uint256 totalReleasable = address(this).balance;
address account;
uint256 amountToPay;
Expand All @@ -181,26 +205,24 @@ contract Pizza is Initializable, Context, Multicall, ReentrancyGuard {
emit Release(released);
}

function bountyRelease() external {
uint256 amountToPay = address(this).balance * releaseBountyBIPs / BIPS_PRECISION;
Address.sendValue(payable(_msgSender()), amountToPay);
release();
}

/**
* @notice Releases available ERC20 token balance.
* @param token The ERC20 token to be released.
*/
function erc20Release(IERC20 token) external nonReentrant {
uint256 erc20TotalReleasable = token.balanceOf(address(this));
address account;
uint256 amountToPay;
uint256 released;
for (uint256 i = 0; i < payee.length; ++i) {
account = payee[i];
amountToPay = (erc20TotalReleasable * shares[account]) / totalShares;
released += amountToPay;
SafeERC20.safeTransfer(token, payable(account), amountToPay);
}
if (released == 0) {
revert NoPaymentDue();
}
erc20TotalReleased[token] += released;
emit ERC20Release(token, released);
function erc20Release(IERC20 token) public nonReentrant {
_erc20Release(token);
}

function bountyERC20Release(IERC20 token) external nonReentrant {
uint256 amountToPay = token.balanceOf(address(this)) * releaseBountyBIPs / BIPS_PRECISION;
SafeERC20.safeTransfer(token, payable(_msgSender()), amountToPay);
_erc20Release(token);
}

/* ////////////////////////////////////////////////////////////////////////
Expand All @@ -227,6 +249,19 @@ contract Pizza is Initializable, Context, Multicall, ReentrancyGuard {
Helpers
//////////////////////////////////////////////////////////////////////// */

function _initializePayeeShares(address[] memory _payees, uint256[] memory _shares) private {
if (_payees.length != _shares.length) {
revert PayeeShareLengthMismatch();
}
if (_payees.length == 0) {
revert NoPayees();
}

for (uint256 i = 0; i < _payees.length; i++) {
_addPayee(_payees[i], _shares[i]);
}
}

/**
* @notice Add a new payee to the contract.
* @param account The address of the payee to add.
Expand All @@ -242,9 +277,30 @@ contract Pizza is Initializable, Context, Multicall, ReentrancyGuard {
if (shares[account] != 0) {
revert DuplicatePayee(account);
}

payee.push(account);
shares[account] = _shares;
totalShares = totalShares + _shares;
}

/**
* @notice Releases available ERC20 token balance.
* @param token The ERC20 token to be released.
*/
function _erc20Release(IERC20 token) private {
uint256 erc20TotalReleasable = token.balanceOf(address(this));
address account;
uint256 amountToPay;
uint256 released;
for (uint256 i = 0; i < payee.length; ++i) {
account = payee[i];
amountToPay = (erc20TotalReleasable * shares[account]) / totalShares;
released += amountToPay;
SafeERC20.safeTransfer(token, payable(account), amountToPay);
}
if (released == 0) {
revert NoPaymentDue();
}
erc20TotalReleased[token] += released;
emit ERC20Release(token, released);
}
}
33 changes: 31 additions & 2 deletions src/PizzaFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.23;
import {Clones} from "openzeppelin-contracts/proxy/Clones.sol";
import {IPizzaInitializer} from "./IPizzaInitializer.sol";
import {Context} from "openzeppelin-contracts/utils/Context.sol";
import {IERC20} from "openzeppelin-contracts/token/ERC20/IERC20.sol";

event PizzaCreated(address indexed pizza);

Expand Down Expand Up @@ -45,14 +46,42 @@ contract PizzaFactory is Context {
}

function createDeterministic(address[] memory _payees, uint256[] memory _shares, uint256 _salt)
external
public
returns (address pizza)
{
pizza = address(Clones.cloneDeterministic(implementation, keccak256(abi.encode(_payees, _shares, _salt))));
_initPizza(pizza, _payees, _shares);
}

function predict(address[] memory _payees, uint256[] memory _shares, uint256 _salt)
function predictPizza(address[] memory _payees, uint256[] memory _shares, uint256 _salt)
external
view
returns (address)
{
return Clones.predictDeterministicAddress(
implementation, keccak256(abi.encode(_payees, _shares, _salt)), address(this)
);
}

function createDeterministic(
address[] memory _payees,
uint256[] memory _shares,
uint32 _releaseBountyBIPs,
uint256 _salt,
uint32 _deployBountyBIPs,
IERC20[] memory _deployBountyTokens
) public returns (address pizza) {
pizza = address(
Clones.cloneDeterministic(
implementation, keccak256(abi.encode(_payees, _shares, _releaseBountyBIPs, _salt))
)
);
IPizzaInitializer(pizza).initializeWithBounty(_payees, _shares, _deployBounty);
pizzas[pizza] = _msgSender();
emit PizzaCreated(pizza);
}

function predictBountyPizza(address[] memory _payees, uint256[] memory _shares, uint256 _salt)
external
view
returns (address)
Expand Down

0 comments on commit dc5a17e

Please sign in to comment.