|
| 1 | +pragma solidity ^0.5.16; |
| 2 | + |
| 3 | +import "@daostack/infra-experimental/contracts/votingMachines/IntVoteInterface.sol"; |
| 4 | +import "@daostack/infra-experimental/contracts/votingMachines/VotingMachineCallbacksInterface.sol"; |
| 5 | +import "../votingMachines/VotingMachineCallbacks.sol"; |
| 6 | +import "../utils/DAOFactory.sol"; |
| 7 | +import "@openzeppelin/upgrades/contracts/Initializable.sol"; |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * @title A factory and registrar for Schemes for organizations |
| 12 | + * @dev The SchemeFactory is used for deploying and registering schemes to organisations |
| 13 | + */ |
| 14 | +contract SchemeFactory is Initializable, VotingMachineCallbacks, ProposalExecuteInterface { |
| 15 | + event NewSchemeProposal( |
| 16 | + address indexed _avatar, |
| 17 | + bytes32 indexed _proposalId, |
| 18 | + address indexed _intVoteInterface, |
| 19 | + string _schemeName, |
| 20 | + bytes _schemeData, |
| 21 | + uint64[3] _packageVersion, |
| 22 | + bytes4 _permissions, |
| 23 | + address _schemeToReplace, |
| 24 | + string _descriptionHash |
| 25 | + ); |
| 26 | + |
| 27 | + event ProposalExecuted(address indexed _avatar, bytes32 indexed _proposalId, int256 _param); |
| 28 | + event ProposalDeleted(address indexed _avatar, bytes32 indexed _proposalId); |
| 29 | + |
| 30 | + // a proposal to add or remove a scheme to/from the an organization |
| 31 | + struct Proposal { |
| 32 | + string schemeName; |
| 33 | + bytes schemeData; |
| 34 | + uint64[3] packageVersion; |
| 35 | + address schemeToReplace; |
| 36 | + bytes4 permissions; |
| 37 | + } |
| 38 | + |
| 39 | + mapping(bytes32=>Proposal) public proposals; |
| 40 | + |
| 41 | + IntVoteInterface public votingMachine; |
| 42 | + bytes32 public voteParams; |
| 43 | + Avatar public avatar; |
| 44 | + DAOFactory public daoFactory; |
| 45 | + |
| 46 | + /** |
| 47 | + * @dev initialize |
| 48 | + * @param _avatar the avatar this scheme referring to. |
| 49 | + * @param _votingMachine the voting machines address to |
| 50 | + * @param _voteParams voting machine parameters to register scheme. |
| 51 | + */ |
| 52 | + function initialize( |
| 53 | + Avatar _avatar, |
| 54 | + IntVoteInterface _votingMachine, |
| 55 | + bytes32 _voteParams, |
| 56 | + DAOFactory _daoFactory |
| 57 | + ) |
| 58 | + external |
| 59 | + initializer |
| 60 | + { |
| 61 | + require(_avatar != Avatar(0), "avatar cannot be zero"); |
| 62 | + avatar = _avatar; |
| 63 | + votingMachine = _votingMachine; |
| 64 | + voteParams = _voteParams; |
| 65 | + daoFactory = _daoFactory; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @dev execution of proposals, can only be called by the voting machine in which the vote is held. |
| 70 | + * @param _proposalId the ID of the voting in the voting machine |
| 71 | + * @param _decision the voting result, 1 yes and 2 is no. |
| 72 | + * @return true (if function did not revert) |
| 73 | + */ |
| 74 | + function executeProposal(bytes32 _proposalId, int256 _decision) |
| 75 | + external |
| 76 | + onlyVotingMachine(_proposalId) |
| 77 | + returns(bool) { |
| 78 | + Proposal memory proposal = proposals[_proposalId]; |
| 79 | + delete proposals[_proposalId]; |
| 80 | + emit ProposalDeleted(address(avatar), _proposalId); |
| 81 | + Controller controller = Controller(avatar.owner()); |
| 82 | + if (_decision == 1) { |
| 83 | + if (bytes(proposal.schemeName).length > 0) { |
| 84 | + address scheme = address(daoFactory.createInstance( |
| 85 | + proposal.packageVersion, |
| 86 | + proposal.schemeName, |
| 87 | + address(avatar), |
| 88 | + proposal.schemeData)); |
| 89 | + |
| 90 | + require(controller.registerScheme(scheme, proposal.permissions), "faild to register new scheme"); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + if (proposal.schemeToReplace != address(0) && controller.isSchemeRegistered(proposal.schemeToReplace)) { |
| 95 | + require(controller.unregisterScheme(proposal.schemeToReplace), "faild to unregister old scheme"); |
| 96 | + } |
| 97 | + } |
| 98 | + emit ProposalExecuted(address(avatar), _proposalId, _decision); |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @dev create a proposal to register a scheme |
| 104 | + * @param _packageVersion the Arc version to use for deploying the scheme |
| 105 | + * @param _schemeName the name of the scheme to be added |
| 106 | + * @param _schemeData initialize data for the scheme to be added |
| 107 | + * @param _permissions the permission of the scheme to be registered |
| 108 | + * @param _schemeToReplace address of scheme to replace with the new scheme (zero for none) |
| 109 | + * @param _descriptionHash proposal's description hash |
| 110 | + * @return a proposal Id |
| 111 | + */ |
| 112 | + function proposeScheme( |
| 113 | + uint64[3] memory _packageVersion, |
| 114 | + string memory _schemeName, |
| 115 | + bytes memory _schemeData, |
| 116 | + bytes4 _permissions, |
| 117 | + address _schemeToReplace, |
| 118 | + string memory _descriptionHash |
| 119 | + ) |
| 120 | + public |
| 121 | + returns(bytes32) |
| 122 | + { |
| 123 | + if (bytes(_schemeName).length > 0) { |
| 124 | + require( |
| 125 | + daoFactory.getImplementation(_packageVersion, _schemeName) != address(0), |
| 126 | + "scheme name does not exist in ArcHive" |
| 127 | + ); |
| 128 | + } else if (_schemeToReplace != address(0)) { |
| 129 | + Controller controller = Controller(avatar.owner()); |
| 130 | + require( |
| 131 | + controller.isSchemeRegistered(_schemeToReplace), |
| 132 | + "scheme to replace is not registered in the organization" |
| 133 | + ); |
| 134 | + } else { |
| 135 | + revert("proposal must have a scheme name to reister or address to unregister"); |
| 136 | + } |
| 137 | + |
| 138 | + bytes32 proposalId = votingMachine.propose( |
| 139 | + 2, |
| 140 | + voteParams, |
| 141 | + msg.sender, |
| 142 | + address(avatar) |
| 143 | + ); |
| 144 | + |
| 145 | + Proposal memory proposal = Proposal({ |
| 146 | + schemeName: _schemeName, |
| 147 | + schemeData: _schemeData, |
| 148 | + packageVersion: _packageVersion, |
| 149 | + schemeToReplace: _schemeToReplace, |
| 150 | + permissions: _permissions |
| 151 | + }); |
| 152 | + |
| 153 | + emit NewSchemeProposal( |
| 154 | + address(avatar), |
| 155 | + proposalId, |
| 156 | + address(votingMachine), |
| 157 | + _schemeName, |
| 158 | + _schemeData, |
| 159 | + _packageVersion, |
| 160 | + _permissions, |
| 161 | + _schemeToReplace, |
| 162 | + _descriptionHash |
| 163 | + ); |
| 164 | + |
| 165 | + proposals[proposalId] = proposal; |
| 166 | + proposalsInfo[address(votingMachine)][proposalId] = ProposalInfo({ |
| 167 | + blockNumber:block.number, |
| 168 | + avatar:avatar |
| 169 | + }); |
| 170 | + return proposalId; |
| 171 | + } |
| 172 | +} |
0 commit comments