-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenVoting.scilla
More file actions
128 lines (110 loc) · 3.25 KB
/
Copy pathOpenVoting.scilla
File metadata and controls
128 lines (110 loc) · 3.25 KB
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
scilla_version 0
(***************************************************)
(* Associated library *)
(***************************************************)
import BoolUtils
library OpenVoting
(*Functions*)
let blk_leq =
fun(blk1 : BNum)=>
fun(blk2 : BNum)=>
let bc1 = builtin blt blk1 blk2 in
let bc2 = builtin eq blk1 blk2 in
orb bc1 bc2
let valid_vote =
fun(within_time_limit : Bool)=>
fun(has_not_voted : Bool)=>
andb within_time_limit has_not_voted
(*Error Codes*)
let vote_yes_accepted_code = Int256 1
let vote_no_accepted_code = Int256 2
let voting_ended_code = Int256 3
let already_voted_code =Int256 4
(***************************************************)
(* The contract definition *)
(***************************************************)
contract OpenVoting
(* Parameters *)
(organizer : ByStr20,
description : String,
voting_start : BNum,
vote_max_block : BNum)
(* Mutable fields *)
field voters : Map ByStr20 Bool = Emp ByStr20 Bool
field one_vote : Int256 = Int256 1
field yes_count : Int256 = Int256 0
field no_count : Int256 = Int256 0
(*VoteYes*)
procedure votingYesEvent(failure: Bool, error_code: Int256)
match failure with
| False =>
e = {_eventname: "VotingYesSuccess"; shoutout : "Thank you for Voting!"; voter: _sender; amount: _amount; code: vote_yes_accepted_code};
event e
| True =>
e = {_eventname: "VotingYesFailure"; shoutout : "Please try again!"; voter: _sender; amount: _amount; code: error_code};
event e
end
end
procedure performVoteYes()
c <- exists voters[_sender];
match c with
| False =>
(*tt = True;
voters[_sender]:= tt;*)
one256 = Int256 1;
yes_count_total <- yes_count;
yes_count_plus_one = builtin add yes_count_total one256;
yes_count:= yes_count_plus_one;
votingYesEvent c vote_yes_accepted_code
| True =>
votingYesEvent c already_voted_code
end
end
transition voteYes()
current_blk <- & BLOCKNUMBER;
in_time = blk_leq current_blk vote_max_block;
match in_time with
| True =>
performVoteYes
| False =>
tt = True;
votingYesEvent tt voting_ended_code
end
end
(*VoteNo*)
procedure votingNoEvent(failure: Bool, error_code: Int256)
match failure with
| False =>
e = {_eventname: "VotingNoSuccess"; shoutout : "Thank you for Voting!"; voter: _sender; amount: _amount; code: vote_no_accepted_code};
event e
| True =>
e = {_eventname: "VotingYesFailure"; shoutout : "Please try again!"; voter: _sender; amount: _amount; code: error_code};
event e
end
end
procedure performVoteNo()
c <- exists voters[_sender];
match c with
| False =>
tt = True;
voters[_sender]:= tt;
one256 = Int256 1;
no_count_total <- no_count;
no_count_plus_one = builtin add no_count_total one256;
no_count:= no_count_plus_one;
votingNoEvent c vote_no_accepted_code
| True =>
votingNoEvent c already_voted_code
end
end
transition voteNo()
current_blk <- & BLOCKNUMBER;
in_time = blk_leq current_blk vote_max_block;
match in_time with
| True =>
performVoteNo
| False =>
tt = True;
votingNoEvent tt voting_ended_code
end
end