-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvotingContract2.js
189 lines (164 loc) · 6.2 KB
/
votingContract2.js
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
// NebVote - Voting system on Nebulas blockchain
var Poll = function(data) {
this.votedUsersCount = new BigNumber(0);
this.votersPayments = new BigNumber(0);
this.wasWithdrawalProceeded = false;
this.votedAdressesList = [];
this.id = data.id;
this.pollVariants = data.pollVariants;
this.pollResults = {};
var that = this;
data.pollVariants.forEach(function(variant) {
that.pollResults[variant] = 0;
});
this.pollName = data.name;
this.pollCreator = data.creatorAddress;
this.votingPrice = data.price;
this.maxVotersNumber = data.maxVotersNumber;
this.endDate = data.endDate;
this.endDateTimestamp = data.endDateTimestamp;
}
Poll.prototype = {
toString: function() {
return JSON.stringify(this);
}
};
var VotingContract = function() {
LocalContractStorage.defineMapProperty(this, "polls");
LocalContractStorage.defineProperty(this, "polls_number");
}
VotingContract.prototype = {
init: function() {
this.polls_number = new BigNumber(0);
},
createPoll: function(name, variants, price, maxVoters, endDate) {
if (!Blockchain.transaction.value.eq(0)) {
throw new Error("Creating poll is free! Set transaction value to 0!");
}
var poll_data = {};
if (variants && variants.length && Array.isArray(variants)) {
if (variants.length < 31) {
poll_data.pollVariants = variants;
} else {
throw new Error("Maximum 30 poll variants is allowed.");
}
} else {
throw new Error("Poll variants array not provided.");
}
if (name) {
var defined_name = String(name);
defined_name = defined_name.length > 50 ? defined_name.substr(0,49) : defined_name;
poll_data.name = defined_name;
} else {
throw new Error("Poll name not provided.");
}
if (price !== undefined) {
var defined_voting_price = parseFloat(String(price));
defined_voting_price = defined_voting_price >= 0 ? defined_voting_price : 0;
poll_data.price = new BigNumber(defined_voting_price);
} else {
throw new Error("Poll voting price not provided.");
}
if (maxVoters) {
var defined_max_voters = parseInt(String(maxVoters));
poll_data.maxVotersNumber = new BigNumber(defined_max_voters);
} else {
throw new Error("Max numbers of voters not provided.");
}
if (endDate) {
var date_string = new Date(endDate);
if (!isNaN(date_string)) {
var now = Date.now();
var d_utc = Date.UTC(date_string.getUTCFullYear(), date_string.getUTCMonth(), date_string.getUTCDate(), 23, 59, 59);
if (now > d_utc) {
throw new Error("End date must be in future.");
} else {
poll_data.endDate = date_string;
poll_data.endDateTimestamp = d_utc;
}
} else {
throw new Error("End date is not valid.");
}
} else {
throw new Error("End date not provided.");
}
poll_data.creatorAddress = Blockchain.transaction.from;
poll_data.id = Blockchain.transaction.from + 'poll' + this.polls_number;
var newPoll = new Poll(poll_data);
this.polls.put(poll_data.id, newPoll);
this.polls_number = new BigNumber(this.polls_number).plus(1);
return poll_data.id;
},
requestPoll: function(pollId) {
var poll_response = {};
var poll = this.polls.get(pollId);
if (poll) {
poll_response.poll = poll;
poll_response.isPollEndDateActive = new BigNumber(poll.endDateTimestamp).gt(new BigNumber(Date.now()));
poll_response.maxVotersLimit = new BigNumber(poll.maxVotersNumber).gt(new BigNumber(poll.votedUsersCount));
return poll_response;
} else {
throw new Error("Poll ID is not correct.");
}
},
vote: function(pollId, voteVariant) {
var poll = this.polls.get(pollId);
if (poll) {
if (poll.votedAdressesList.indexOf(Blockchain.transaction.from) !== -1) {
throw new Error("The address already voted in this poll.");
}
if (!(new BigNumber(poll.maxVotersNumber).gt(new BigNumber(poll.votedUsersCount)))) {
throw new Error("Sorry, no more votes are accepted in this poll.");
}
var nas_transaction_value = new BigNumber(Blockchain.transaction.value).div(new BigNumber(10).pow(18));
if (nas_transaction_value.lt(new BigNumber(poll.votingPrice))) {
throw new Error("You must pay small fee to submit your vote. Voting price for this poll is " + poll.votingPrice + " NAS");
}
if(new BigNumber(poll.endDateTimestamp).lt(new BigNumber(Date.now()))) {
throw new Error("Sorry, no more votes are accepted in this poll.");
}
if (poll.pollResults[voteVariant] !== undefined) {
poll.pollResults[voteVariant] = new BigNumber(poll.pollResults[voteVariant]).plus(1);
poll.votedAdressesList.push(Blockchain.transaction.from);
poll.votedUsersCount = new BigNumber(poll.votedUsersCount).plus(1);
poll.votersPayments = new BigNumber(poll.votersPayments).plus(Blockchain.transaction.value);
this.polls.put(poll.id, poll);
} else {
throw new Error("Voting variant is not valid.");
}
return poll;
} else {
throw new Error("Poll ID is not correct.");
}
},
withdraw: function(pollId) {
if (!Blockchain.transaction.value.eq(0)) {
throw new Error("Withdrawal is free! Set transaction value to 0!");
}
var poll = this.polls.get(pollId);
if (!poll) {
throw new Error("Poll ID is not correct.");
}
if (Blockchain.transaction.from !== poll.pollCreator) {
throw new Error("Only poll creator could withdraw funds.");
}
if (poll.wasWithdrawalProceeded) {
throw new Error("Withdrawal from this poll has been proceeded already.");
}
if (new BigNumber(poll.votersPayments).eq(0)) {
throw new Error("No payments were made for this poll");
}
if (new BigNumber(poll.endDateTimestamp).lt(new BigNumber(Date.now())) || new BigNumber(poll.maxVotersNumber).eq(new BigNumber(poll.votedUsersCount))){
var transferResult = Blockchain.transfer(poll.pollCreator, new BigNumber(poll.votersPayments));
if (!transferResult) {
throw new Error("Unable to process withdrawal, please try again later.");
} else {
poll.wasWithdrawalProceeded = true;
this.polls.put(poll.id, poll);
}
} else {
throw new Error("Poll is not completed, withdrawal is not available.");
}
}
}
module.exports = VotingContract;