Skip to content

Commit a5d4dc7

Browse files
authored
Competition Factory (#813)
* Competition Factory * bump v
1 parent c9e4f3e commit a5d4dc7

File tree

4 files changed

+219
-2
lines changed

4 files changed

+219
-2
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
pragma solidity 0.5.17;
2+
3+
import "@daostack/infra/contracts/votingMachines/GenesisProtocolInterface.sol";
4+
import "../schemes/Competition.sol";
5+
import "../schemes/ContributionRewardExt.sol";
6+
7+
/**
8+
* @title CompetitionFactory
9+
*/
10+
contract CompetitionFactory {
11+
uint8 public constant CUSTOM = 0;
12+
uint8 public constant FAST = 1;
13+
uint8 public constant NORMAL = 2;
14+
uint8 public constant SLOW = 3;
15+
16+
event NewCompetition(address competition, address contributionRewardExt);
17+
18+
function createCompetition(
19+
Avatar _avatar,
20+
IntVoteInterface _votingMachine,
21+
uint8 _voteParamsType,
22+
uint256[11] memory _votingParams,
23+
address _voteOnBehalf
24+
) public returns(address, address) {
25+
require(_voteParamsType < 4, "Vote params type specified does not exist");
26+
ContributionRewardExt contributionRewardExt = new ContributionRewardExt();
27+
Competition competition = new Competition();
28+
competition.initialize(address(contributionRewardExt));
29+
30+
uint256[11] memory voteParams;
31+
if (_voteParamsType == CUSTOM) {
32+
// Custom params hash
33+
voteParams = _votingParams;
34+
} else {
35+
voteParams = getDefaultVoteParams(_voteParamsType);
36+
}
37+
38+
bytes32 voteParamsHash = GenesisProtocolInterface(address(_votingMachine))
39+
.setParameters(voteParams, _voteOnBehalf);
40+
41+
contributionRewardExt.initialize(
42+
_avatar, _votingMachine, voteParamsHash, address(competition)
43+
);
44+
45+
emit NewCompetition(address(competition), address(contributionRewardExt));
46+
return (address(competition), address(contributionRewardExt));
47+
}
48+
49+
function getDefaultVoteParams(uint8 _voteParamsType) private pure returns(uint256[11] memory voteParams) {
50+
if (_voteParamsType == FAST) {
51+
// Fast params hash
52+
voteParams = [
53+
uint256(50),
54+
uint256(604800),
55+
uint256(129600),
56+
uint256(43200),
57+
uint256(1200),
58+
uint256(86400),
59+
uint256(10000000000000000000),
60+
uint256(1),
61+
uint256(50000000000000000000),
62+
uint256(10),
63+
uint256(0)
64+
];
65+
} else if (_voteParamsType == NORMAL) {
66+
// Normal params hash
67+
voteParams = [
68+
uint256(50),
69+
uint256(2592000),
70+
uint256(345600),
71+
uint256(86400),
72+
uint256(1200),
73+
uint256(172800),
74+
uint256(50000000000000000000),
75+
uint256(4),
76+
uint256(150000000000000000000),
77+
uint256(10),
78+
uint256(0)
79+
];
80+
} else if (_voteParamsType == SLOW) {
81+
// Slow params hash
82+
voteParams = [
83+
uint256(50),
84+
uint256(5184000),
85+
uint256(691200),
86+
uint256(172800),
87+
uint256(1500),
88+
uint256(345600),
89+
uint256(200000000000000000000),
90+
uint256(4),
91+
uint256(500000000000000000000),
92+
uint256(10),
93+
uint256(0)
94+
];
95+
}
96+
}
97+
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@daostack/arc",
3-
"version": "0.0.1-rc.53",
3+
"version": "0.0.1-rc.54",
44
"description": "A platform for building DAOs",
55
"files": [
66
"contracts/",

test/competitionfactory.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
const helpers = require('./helpers');
2+
3+
const Competition = artifacts.require('./Competition.sol');
4+
const ContributionRewardExt = artifacts.require('./ContributionRewardExt.sol');
5+
const CompetitionFactory = artifacts.require('./CompetitionFactory.sol');
6+
7+
const params = [
8+
[
9+
50,
10+
1800,
11+
600,
12+
600,
13+
2000,
14+
300,
15+
web3.utils.toWei('5', "ether"),
16+
1,
17+
web3.utils.toWei('10', "ether"),
18+
10,
19+
0
20+
],
21+
[
22+
50,
23+
604800,
24+
129600,
25+
43200,
26+
1200,
27+
86400,
28+
web3.utils.toWei('10', "ether"),
29+
1,
30+
web3.utils.toWei('50', "ether"),
31+
10,
32+
0
33+
],
34+
[
35+
50,
36+
2592000,
37+
345600,
38+
86400,
39+
1200,
40+
172800,
41+
web3.utils.toWei('50', "ether"),
42+
4,
43+
web3.utils.toWei('150', "ether"),
44+
10,
45+
0
46+
],
47+
[
48+
50,
49+
5184000,
50+
691200,
51+
172800,
52+
1500,
53+
345600,
54+
web3.utils.toWei('200', "ether"),
55+
4,
56+
web3.utils.toWei('500', "ether"),
57+
10,
58+
0
59+
]
60+
];
61+
62+
const setup = async function () {
63+
var testSetup = new helpers.TestSetup();
64+
testSetup.competitionFactory = await CompetitionFactory.new();
65+
return testSetup;
66+
};
67+
68+
contract('competitionFactory', function(accounts) {
69+
it('initialize', async () => {
70+
let testSetup = await setup();
71+
let votingMachine = await helpers.setupGenesisProtocol(accounts,helpers.SOME_ADDRESS,0,helpers.NULL_ADDRESS);
72+
73+
for (let i=0; i < 4; i++) {
74+
let addresses = await testSetup.competitionFactory.createCompetition.call(
75+
helpers.SOME_ADDRESS,
76+
votingMachine.genesisProtocol.address,
77+
i,
78+
(i === 0 ? params[0] : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
79+
helpers.NULL_ADDRESS
80+
);
81+
82+
let competitionAddress = addresses['0'];
83+
let creAddress = addresses['1'];
84+
85+
await testSetup.competitionFactory.createCompetition(
86+
helpers.SOME_ADDRESS,
87+
votingMachine.genesisProtocol.address,
88+
i,
89+
(i === 0 ? params[0] : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
90+
helpers.NULL_ADDRESS
91+
);
92+
93+
let contributionRewardExt = await ContributionRewardExt.at(creAddress);
94+
let competition = await Competition.at(competitionAddress);
95+
assert.equal(await contributionRewardExt.avatar(), helpers.SOME_ADDRESS);
96+
assert.equal(await contributionRewardExt.votingMachine(), votingMachine.genesisProtocol.address);
97+
assert.equal(
98+
await contributionRewardExt.voteParams(),
99+
await votingMachine.genesisProtocol.getParametersHash(params[i], helpers.NULL_ADDRESS)
100+
);
101+
assert.equal(await contributionRewardExt.rewarder(), competitionAddress);
102+
assert.equal(await competition.contributionRewardExt(), creAddress);
103+
}
104+
105+
try {
106+
await testSetup.competitionFactory.createCompetition(
107+
helpers.SOME_ADDRESS,
108+
votingMachine.genesisProtocol.address,
109+
4,
110+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
111+
helpers.NULL_ADDRESS,
112+
);
113+
assert(false, "Vote params type specified does not exist");
114+
} catch(error) {
115+
helpers.assertVMException(error);
116+
}
117+
118+
});
119+
120+
});

0 commit comments

Comments
 (0)