-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
]; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
}); | ||
|
||
}); |