Skip to content

Commit

Permalink
fix contract size limit
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Sep 14, 2023
1 parent 3cbfb25 commit 1f90797
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 89 deletions.
142 changes: 104 additions & 38 deletions contracts/contracts/ClrFund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@ import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';

// Import ownable from OpenZeppelin contracts

import {MACI} from 'maci-contracts/contracts/MACI.sol';
import {SnarkCommon} from 'maci-contracts/contracts/crypto/SnarkCommon.sol';
import {SignUpGatekeeper} from "maci-contracts/contracts/gatekeepers/SignUpGatekeeper.sol";
import {InitialVoiceCreditProxy} from "maci-contracts/contracts/initialVoiceCreditProxy/InitialVoiceCreditProxy.sol";
import {Params} from 'maci-contracts/contracts/Params.sol';
import {PollFactory} from 'maci-contracts/contracts/Poll.sol';
import {DomainObjs} from 'maci-contracts/contracts/DomainObjs.sol';

import './userRegistry/IUserRegistry.sol';
import './recipientRegistry/IRecipientRegistry.sol';
import './MACIFactory.sol';
import './FundingRound.sol';
import './OwnableUpgradeable.sol';

contract ClrFund is OwnableUpgradeable, SnarkCommon, Params, DomainObjs {
contract ClrFund is OwnableUpgradeable, IPubKey, SnarkCommon {
using EnumerableSet for EnumerableSet.AddressSet;
using SafeERC20 for ERC20;

Expand All @@ -46,6 +40,19 @@ contract ClrFund is OwnableUpgradeable, SnarkCommon, Params, DomainObjs {
event TokenChanged(address _token);
event CoordinatorChanged(address _coordinator);

// errors
error FundingSourceAlreadyAdded();
error FundingSourceNotFound();
error AlreadyFinalized();
error NotFinalized();
error NotAuthorized();
error NoCurrentRound();
error NoCoordinator();
error NoToken();
error NoRecipientRegistry();
error NoUserRegistry();
error NotOwnerOfMaciFactory();

function init(
MACIFactory _maciFactory
)
Expand Down Expand Up @@ -88,7 +95,9 @@ contract ClrFund is OwnableUpgradeable, SnarkCommon, Params, DomainObjs {
onlyOwner
{
bool result = fundingSources.add(_source);
require(result, 'Factory: Funding source already added');
if (!result) {
revert FundingSourceAlreadyAdded();
}
emit FundingSourceAdded(_source);
}

Expand All @@ -101,7 +110,9 @@ contract ClrFund is OwnableUpgradeable, SnarkCommon, Params, DomainObjs {
onlyOwner
{
bool result = fundingSources.remove(_source);
require(result, 'Factory: Funding source not found');
if (!result) {
revert FundingSourceNotFound();
}
emit FundingSourceRemoved(_source);
}

Expand All @@ -117,39 +128,55 @@ contract ClrFund is OwnableUpgradeable, SnarkCommon, Params, DomainObjs {
}

function setMaciParameters(
uint8 _messageTreeDepth,
uint8 _voteOptionTreeDepth
)
uint8 stateTreeDepth,
uint8 intStateTreeDepth,
uint8 messageTreeSubDepth,
uint8 messageTreeDepth,
uint8 voteOptionTreeDepth,
uint256 maxMessages,
uint256 maxVoteOptions,
uint256 messageBatchSize,
VerifyingKey calldata processVk,
VerifyingKey calldata tallyVk
)
external
onlyOwner
onlyCoordinator
{
maciFactory.setMaciParameters(
_messageTreeDepth,
_voteOptionTreeDepth
stateTreeDepth,
intStateTreeDepth,
messageTreeSubDepth,
messageTreeDepth,
voteOptionTreeDepth,
maxMessages,
maxVoteOptions,
messageBatchSize,
processVk,
tallyVk
);
}

/**
/**
* @dev Deploy new funding round.
* @param duration The poll duration in seconds
* @param vkRegistry The VkRegistry contract address
*/
function deployNewRound(uint256 duration, address vkRegistry, address pollFactory, TreeDepths calldata treeDepths)
function deployNewRound(uint256 duration)
external
onlyOwner
requireToken
requireCoordinator
requireRecipientRegistry
requireUserRegistry
{
require(maciFactory.owner() == address(this), 'Factory: MACI factory is not owned by FR factory');
require(address(userRegistry) != address(0), 'Factory: User registry is not set');
require(address(recipientRegistry) != address(0), 'Factory: Recipient registry is not set');
require(address(nativeToken) != address(0), 'Factory: Native token is not set');
require(coordinator != address(0), 'Factory: No coordinator');
if (maciFactory.owner() != address(this)) {
revert NotOwnerOfMaciFactory();
}
FundingRound currentRound = getCurrentRound();
require(
address(currentRound) == address(0) || currentRound.isFinalized(),
'Factory: Current round is not finalized'
);
if (address(currentRound) != address(0) && !currentRound.isFinalized()) {
revert NotFinalized();
}
// Make sure that the max number of recipients is set correctly
(uint256 maxMessages, uint256 maxVoteOptions) = maciFactory.maxValues();
(, uint256 maxVoteOptions) = maciFactory.maxValues();
recipientRegistry.setMaxRecipients(maxVoteOptions);
// Deploy funding round and MACI contracts
FundingRound newRound = new FundingRound(
Expand All @@ -159,18 +186,16 @@ contract ClrFund is OwnableUpgradeable, SnarkCommon, Params, DomainObjs {
coordinator
);
rounds.push(newRound);

MACI maci = maciFactory.deployMaci(
SignUpGatekeeper(newRound),
InitialVoiceCreditProxy(newRound),
vkRegistry,
pollFactory,
address(nativeToken)
address(nativeToken),
duration,
coordinatorPubKey
);

MaxValues memory maxValues = MaxValues(maxMessages, maxVoteOptions);
maci.deployPoll(duration, maxValues, treeDepths, coordinatorPubKey);
newRound.setMaci(maci);

emit RoundStarted(address(newRound));
}

Expand Down Expand Up @@ -206,7 +231,8 @@ contract ClrFund is OwnableUpgradeable, SnarkCommon, Params, DomainObjs {
onlyOwner
{
FundingRound currentRound = getCurrentRound();
require(address(currentRound) != address(0), 'Factory: Funding round has not been deployed');
requireCurrentRound(currentRound);

ERC20 roundToken = currentRound.nativeToken();
// Factory contract is the default funding source
uint256 matchingPoolSize = roundToken.balanceOf(address(this));
Expand Down Expand Up @@ -235,8 +261,12 @@ contract ClrFund is OwnableUpgradeable, SnarkCommon, Params, DomainObjs {
onlyOwner
{
FundingRound currentRound = getCurrentRound();
require(address(currentRound) != address(0), 'Factory: Funding round has not been deployed');
require(!currentRound.isFinalized(), 'Factory: Current round is finalized');
requireCurrentRound(currentRound);

if (currentRound.isFinalized()) {
revert AlreadyFinalized();
}

currentRound.cancel();
emit RoundFinalized(address(currentRound));
}
Expand Down Expand Up @@ -287,7 +317,43 @@ contract ClrFund is OwnableUpgradeable, SnarkCommon, Params, DomainObjs {
}

modifier onlyCoordinator() {
require(msg.sender == coordinator, 'Factory: Sender is not the coordinator');
if (msg.sender != coordinator) {
revert NotAuthorized();
}
_;
}

function requireCurrentRound(FundingRound currentRound) private {
if (address(currentRound) == address(0)) {
revert NoCurrentRound();
}
}

modifier requireToken() {
if (address(nativeToken) == address(0)) {
revert NoToken();
}
_;
}

modifier requireCoordinator() {
if (coordinator == address(0)) {
revert NoCoordinator();
}
_;
}

modifier requireUserRegistry() {
if (address(userRegistry) == address(0)) {
revert NoUserRegistry();
}
_;
}

modifier requireRecipientRegistry() {
if (address(recipientRegistry) == address(0)) {
revert NoRecipientRegistry();
}
_;
}
}
Loading

0 comments on commit 1f90797

Please sign in to comment.