-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesportivos.sol
378 lines (230 loc) · 10.2 KB
/
Desportivos.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
pragma solidity >=0.4.22 <0.6.0;
/// @title Desportivos ~0% fees
contract Desportivos {
// It will represent a single voter of each MCQ qid.
struct Voter {
address add; //address of voter
uint power; // how many votes to the selected option/answer. More the power, more winnings..
}
// It will represent a single voter of each Integer qid.
struct IntegerVoter{
address add; //address of voter
uint submittedans; //ans submitted by each voter
}
// This is a type for a single MCQ Option.
struct Option {
bytes32 name; // short name of mcq text
uint voteCount; // number of accumulated votes
Voter[] voters; // all voters who voted for particular option
}
struct Question {
//uint qid; // question id
uint _type; // 1=mcq, 2=integer type
string contenthash; // content hash of questions
uint vote_price; // price/rate for 1 vote in DAI
uint correct_ans_id;
bytes32 correct_answer; // after event has occured
Option[6] options; // all max 6 options
uint totalCount;
address[] winners;
uint8 active;
uint8 declared_result;
uint8 stopped;
}
address public chairperson;
// 10^8 DAI
mapping(address => uint256) public balanceOf;
// for mcq questions
mapping(uint => Question) alloptions;
// for integer questions
mapping(uint => IntegerVoter[]) allsubmissions;
// contribution/ditribution
event FundTransfer(address backer, uint amount, bool isContribution);
event QuestionCreation(address backer, uint qid, uint _type);
event FantasyCreation(address backer, uint qid, uint _type);
event VotingDone(address backer, uint qid, uint power);
event FantasyParticipation(address backer, uint qid);
/// Create a new ballot to choose one of `proposalNames`.
constructor() public {
chairperson = msg.sender;
}
//_type; // 1=mcq, 2=integer type
function createquestionMCQ(uint _qid, string memory _contenthash,
uint _vote_price, bytes32[] memory _options ) public{
require(alloptions[_qid].active!=1,"Question id already exists");
//bytes32[] memory a = new bytes32[](6);
//bytes memory b = new bytes(len);
//Option memory opt;
for (uint i = 0; i < _options.length; i++) {
alloptions[_qid].options[i].name=_options[i];
alloptions[_qid].options[i].voteCount = 0;
}
alloptions[_qid]._type=1;
alloptions[_qid].contenthash=_contenthash;
alloptions[_qid].vote_price=_vote_price;
alloptions[_qid].totalCount=0;
alloptions[_qid].active=1;
alloptions[_qid].declared_result=0;
alloptions[_qid].stopped=0;
emit QuestionCreation(msg.sender, _qid, 1);
}
function createquestionInteger(uint _qid, string memory _contenthash,
uint _vote_price) public{
require(alloptions[_qid].active!=1,"Question id already exists");
alloptions[_qid]._type=2;
alloptions[_qid].contenthash=_contenthash;
alloptions[_qid].vote_price=_vote_price;
alloptions[_qid].totalCount=0;
alloptions[_qid].active=1;
alloptions[_qid].declared_result=0;
alloptions[_qid].stopped=0;
emit QuestionCreation(msg.sender, _qid, 2);
}
function stopVoting(uint _qid) public{
// based on live feed this should be updated
require(
msg.sender == chairperson,
"Only chairperson can submit the solution"
);
require(alloptions[_qid].stopped!=1,"Voting process has been already stopped");
// when result become deterministic
alloptions[_qid].stopped=1;
}
// ans id in case of mcq and integer in case of integer type question
function submitSolution(uint _qid, uint ansid) public{
// based on live feed this should be updated
require(
msg.sender == chairperson,
"Only chairperson can submit the solution"
);
require(alloptions[_qid].declared_result!=1,"Voting process has been already declared already");
alloptions[_qid].correct_ans_id=ansid;
alloptions[_qid].declared_result=1;
alloptions[_qid].stopped=1;
uint typeofQ = alloptions[_qid]._type;
// in case of MCQ
if(typeofQ==1)
alloptions[_qid].correct_answer = alloptions[_qid].options[ansid].name;
}
function addBal(address _payer, uint _quantity) public{
require(
msg.sender == chairperson,
"Only chairperson can update the DAI balance"
);
balanceOf[_payer]+=(_quantity);
emit FundTransfer(_payer, _quantity, true);
}
// ans id in case of mcq and integer in case of integer type question
function vote(uint _qid, uint ansid, uint _amount) public payable{
// allowed 1 time only for particular acct
// not allowed after solution given
require(alloptions[_qid].declared_result!=1,"Voting process has been declared");
require(alloptions[_qid].stopped!=1,"Voting process has been stopped");
uint typeofQ = alloptions[_qid]._type;
// commented for testing
// balanceOf[msg.sender]-=_amount;
uint _power= (_amount)/ (alloptions[_qid].vote_price);
//if 1 => mcq
if(typeofQ==1)
{
alloptions[_qid].options[ansid].voters.push(Voter({
add:msg.sender,
power:_power
}));
alloptions[_qid].options[ansid].voteCount +=_power;
alloptions[_qid].totalCount +=_power;
}
//2=> integer type
else if(typeofQ==2){
require(_amount == alloptions[_qid].vote_price,"Participation fees couldnot be submitted");
allsubmissions[_qid].push(IntegerVoter({
add:msg.sender,
submittedans:ansid
}));
alloptions[_qid].totalCount +=1;
}
emit VotingDone(msg.sender, _qid, _power);
}
// call this after submit solution
function distributewin(uint _qid) public{
uint ansid=alloptions[_qid].correct_ans_id;
uint totalCount= alloptions[_qid].totalCount;
address[] memory rewardees= new address[](10);
uint typeofQ = alloptions[_qid]._type;
//if 1 => mcq
if(typeofQ==1)
{
uint winningCount = alloptions[_qid].options[ansid].voteCount;
uint part = (totalCount)/winningCount;
for(uint i=0;i<alloptions[_qid].options[ansid].voters.length;i++){
rewardees[i]= alloptions[_qid].options[ansid].voters[i].add;
uint totalreward= part * (alloptions[_qid].options[ansid].voters[i].power) * (alloptions[_qid].vote_price);
balanceOf[rewardees[i]]+=totalreward;
}
}
// if 2=>integer
else if(typeofQ==2){
uint totalreward= totalCount*(alloptions[_qid].vote_price);
uint nearest=allsubmissions[_qid][0].submittedans;
uint nearest_diff;
if(ansid<nearest)
nearest_diff=nearest - ansid;
else
nearest_diff=ansid - nearest;
// rewardees are there as more than 1 can commit same integer, dividing amongst them
// finding most accurate commit
for(uint i=1;i<allsubmissions[_qid].length;i++){
uint current=allsubmissions[_qid][i].submittedans;
uint current_diff;
if(allsubmissions[_qid][i].submittedans > ansid)
current_diff=allsubmissions[_qid][i].submittedans - ansid;
else
current_diff=ansid - allsubmissions[_qid][i].submittedans ;
if( ( current_diff) < (nearest_diff)){
nearest_diff =current_diff;
nearest=current;
}
}
// finding all addresses with most accurate commit
uint cnt=0;
for(uint i=0;i<allsubmissions[_qid].length;i++){
uint current=allsubmissions[_qid][i].submittedans;
if(current == nearest){
//yaha
rewardees[cnt] = (allsubmissions[_qid][i].add);
cnt++;
}
}
uint distributereward = totalreward/(cnt+1);
// distributing the reward to addresses with most accurate commit
for(uint i=0;i<=cnt;i++)
balanceOf[rewardees[i]]+=distributereward;
}
alloptions[_qid].winners = rewardees;
}
function getWinnings(address rewardee) public{
require(
msg.sender == address(this),
"Contract is only eligible to update winnings to 0"
);
uint amt = balanceOf[rewardee];
//msg.sender.transfer(amt);
balanceOf[msg.sender]=0;
emit FundTransfer(msg.sender,amt,false);
}
function getBalance(address from) public view returns(uint256){
return balanceOf[from];
}
function getWinners(uint _qid) public view returns(address[] memory){
return alloptions[_qid].winners;
}
function getRate(uint _qid) public view returns(uint){
return alloptions[_qid].vote_price;
}
}
// rate user & his questions as per response, shouldnot be clear and predictable easily
// createquestion
// 1
// 1
// 0x77696c6c20766b206869742063656e7475727900000000000000000000000000
// 1