From a5d4dc7299a03dcbd0b6891fc70e4d1077521c51 Mon Sep 17 00:00:00 2001 From: benk10 Date: Wed, 23 Dec 2020 13:31:51 +0200 Subject: [PATCH] Competition Factory (#813) * Competition Factory * bump v --- contracts/utils/CompetitionFactory.sol | 97 ++++++++++++++++++++ package-lock.json | 2 +- package.json | 2 +- test/competitionfactory.js | 120 +++++++++++++++++++++++++ 4 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 contracts/utils/CompetitionFactory.sol create mode 100644 test/competitionfactory.js diff --git a/contracts/utils/CompetitionFactory.sol b/contracts/utils/CompetitionFactory.sol new file mode 100644 index 00000000..ee4d4825 --- /dev/null +++ b/contracts/utils/CompetitionFactory.sol @@ -0,0 +1,97 @@ +pragma solidity 0.5.17; + +import "@daostack/infra/contracts/votingMachines/GenesisProtocolInterface.sol"; +import "../schemes/Competition.sol"; +import "../schemes/ContributionRewardExt.sol"; + +/** + * @title CompetitionFactory + */ +contract CompetitionFactory { + uint8 public constant CUSTOM = 0; + uint8 public constant FAST = 1; + uint8 public constant NORMAL = 2; + uint8 public constant SLOW = 3; + + event NewCompetition(address competition, address contributionRewardExt); + + function createCompetition( + Avatar _avatar, + IntVoteInterface _votingMachine, + uint8 _voteParamsType, + uint256[11] memory _votingParams, + address _voteOnBehalf + ) public returns(address, address) { + require(_voteParamsType < 4, "Vote params type specified does not exist"); + ContributionRewardExt contributionRewardExt = new ContributionRewardExt(); + Competition competition = new Competition(); + competition.initialize(address(contributionRewardExt)); + + uint256[11] memory voteParams; + if (_voteParamsType == CUSTOM) { + // Custom params hash + voteParams = _votingParams; + } else { + voteParams = getDefaultVoteParams(_voteParamsType); + } + + bytes32 voteParamsHash = GenesisProtocolInterface(address(_votingMachine)) + .setParameters(voteParams, _voteOnBehalf); + + contributionRewardExt.initialize( + _avatar, _votingMachine, voteParamsHash, address(competition) + ); + + emit NewCompetition(address(competition), address(contributionRewardExt)); + return (address(competition), address(contributionRewardExt)); + } + + function getDefaultVoteParams(uint8 _voteParamsType) private pure returns(uint256[11] memory voteParams) { + if (_voteParamsType == FAST) { + // Fast params hash + voteParams = [ + uint256(50), + uint256(604800), + uint256(129600), + uint256(43200), + uint256(1200), + uint256(86400), + uint256(10000000000000000000), + uint256(1), + uint256(50000000000000000000), + uint256(10), + uint256(0) + ]; + } else if (_voteParamsType == NORMAL) { + // Normal params hash + voteParams = [ + uint256(50), + uint256(2592000), + uint256(345600), + uint256(86400), + uint256(1200), + uint256(172800), + uint256(50000000000000000000), + uint256(4), + uint256(150000000000000000000), + uint256(10), + uint256(0) + ]; + } else if (_voteParamsType == SLOW) { + // Slow params hash + voteParams = [ + uint256(50), + uint256(5184000), + uint256(691200), + uint256(172800), + uint256(1500), + uint256(345600), + uint256(200000000000000000000), + uint256(4), + uint256(500000000000000000000), + uint256(10), + uint256(0) + ]; + } + } +} diff --git a/package-lock.json b/package-lock.json index a7ea46e1..b9768144 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@daostack/arc", - "version": "0.0.1-rc.53", + "version": "0.0.1-rc.54", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index e35d289b..ac34acb5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@daostack/arc", - "version": "0.0.1-rc.53", + "version": "0.0.1-rc.54", "description": "A platform for building DAOs", "files": [ "contracts/", diff --git a/test/competitionfactory.js b/test/competitionfactory.js new file mode 100644 index 00000000..141fc3be --- /dev/null +++ b/test/competitionfactory.js @@ -0,0 +1,120 @@ +const helpers = require('./helpers'); + +const Competition = artifacts.require('./Competition.sol'); +const ContributionRewardExt = artifacts.require('./ContributionRewardExt.sol'); +const CompetitionFactory = artifacts.require('./CompetitionFactory.sol'); + +const params = [ + [ + 50, + 1800, + 600, + 600, + 2000, + 300, + web3.utils.toWei('5', "ether"), + 1, + web3.utils.toWei('10', "ether"), + 10, + 0 + ], + [ + 50, + 604800, + 129600, + 43200, + 1200, + 86400, + web3.utils.toWei('10', "ether"), + 1, + web3.utils.toWei('50', "ether"), + 10, + 0 + ], + [ + 50, + 2592000, + 345600, + 86400, + 1200, + 172800, + web3.utils.toWei('50', "ether"), + 4, + web3.utils.toWei('150', "ether"), + 10, + 0 + ], + [ + 50, + 5184000, + 691200, + 172800, + 1500, + 345600, + web3.utils.toWei('200', "ether"), + 4, + web3.utils.toWei('500', "ether"), + 10, + 0 + ] +]; + +const setup = async function () { + var testSetup = new helpers.TestSetup(); + testSetup.competitionFactory = await CompetitionFactory.new(); + return testSetup; +}; + +contract('competitionFactory', function(accounts) { + it('initialize', async () => { + let testSetup = await setup(); + let votingMachine = await helpers.setupGenesisProtocol(accounts,helpers.SOME_ADDRESS,0,helpers.NULL_ADDRESS); + + for (let i=0; i < 4; i++) { + let addresses = await testSetup.competitionFactory.createCompetition.call( + helpers.SOME_ADDRESS, + votingMachine.genesisProtocol.address, + i, + (i === 0 ? params[0] : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), + helpers.NULL_ADDRESS + ); + + let competitionAddress = addresses['0']; + let creAddress = addresses['1']; + + await testSetup.competitionFactory.createCompetition( + helpers.SOME_ADDRESS, + votingMachine.genesisProtocol.address, + i, + (i === 0 ? params[0] : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), + helpers.NULL_ADDRESS + ); + + let contributionRewardExt = await ContributionRewardExt.at(creAddress); + let competition = await Competition.at(competitionAddress); + assert.equal(await contributionRewardExt.avatar(), helpers.SOME_ADDRESS); + assert.equal(await contributionRewardExt.votingMachine(), votingMachine.genesisProtocol.address); + assert.equal( + await contributionRewardExt.voteParams(), + await votingMachine.genesisProtocol.getParametersHash(params[i], helpers.NULL_ADDRESS) + ); + assert.equal(await contributionRewardExt.rewarder(), competitionAddress); + assert.equal(await competition.contributionRewardExt(), creAddress); + } + + try { + await testSetup.competitionFactory.createCompetition( + helpers.SOME_ADDRESS, + votingMachine.genesisProtocol.address, + 4, + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + helpers.NULL_ADDRESS, + ); + assert(false, "Vote params type specified does not exist"); + } catch(error) { + helpers.assertVMException(error); + } + + }); + +});