-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelection.clar
135 lines (108 loc) · 4.56 KB
/
election.clar
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
(define-constant admin 'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7 ) ;; admin only allowed to add candidates and open or close elections
(define-constant open 1 ) ;; constant to set registration / election open
(define-constant close 0 );; constant to set registration / election closed
(define-map Candidates ;; mapping to store all candidates and their voting count
((candidateId int) )
((name (buff 40)) (votescount uint)))
(define-map voters ((voter principal)) ((voted uint ))) ;; mapping to store all voted address
(define-data-var numberOfCandidates int 0) ;; variable to track number of candidates
(define-data-var RegistrationIsOpen int open) ;; Registration is open =1 otherwise is closed
(define-data-var votingIsOpen int open) ;;voting is open=1 otherwise is closed
(define-private (incrementCandidates) ;; incress number of candidates counter
(begin
(var-set numberOfCandidates (+ (var-get numberOfCandidates) 1))
(ok (var-get numberOfCandidates))))
(define-private (getName (candidateId int) ) ;; cntract only to get candidate name
(begin
(default-to " "
(get name
(map-get? Candidates ((candidateId candidateId) ))))
))
(define-private (getVotesCount (candidateId int) ) ;; cntract only to get candidate votes count
(begin
(default-to u0
(get votescount
(map-get? Candidates ((candidateId candidateId) ))))
))
(define-private (getVoted (voter principal) ) ;; cntract only to get voter vote status u0 means did not vote before
(begin
(default-to u0
(get voted
(map-get? voters ((voter voter) ))))
))
(define-public (getCandidateName (candidateId int) ) ;; to get a candidate name by id
(let ((name (getName candidateId)))
(begin
(ok name)
)))
(define-public (getCandidateVotesCount (candidateId int) ) ;; to get a candidate votes count by id
(let ((count (getVotesCount candidateId)))
(begin
(ok count)
)))
(define-public (getNumberOfCandidates) ;; to get number of candidates registered
(ok (var-get numberOfCandidates)))
(define-public (isRegistrationOpen) ;; to get Registration status
(ok (var-get RegistrationIsOpen)))
(define-public (isVotingOpen) ;; to get if voting open status
(ok (var-get votingIsOpen)))
(define-public (endRegistration) ;; closes registering new candidates
(if (is-eq admin tx-sender)
(begin
(var-set RegistrationIsOpen close)
(ok 1))
(err 0)
)
)
;; Admin controls the smart contract functionalities
(define-public (endElection) ;; stop accepting new votes
(if (is-eq admin tx-sender) ;; only admin
(begin
(var-set votingIsOpen close)
(ok 1))
(err 0)
)
)
(define-public (startRegistration) ;; allowing to register new candidates
(if (is-eq admin tx-sender) ;;only admin
(begin
(var-set RegistrationIsOpen open)
(ok 1))
(err 0)
)
)
(define-public (startElection) ;; start election voting and accepting new votes
(if (is-eq admin tx-sender) ;; only admin
(begin
(var-set votingIsOpen open)
(ok 1))
(err 0)
)
)
;; Admin registering new candidate
(define-public (addCandidate ( name (buff 40))) ;; to register new Candidates
(if (and (is-eq admin tx-sender) ;; only admin allowed to add new candidate
(is-eq (var-get RegistrationIsOpen) 1) ;;check if Registration is open to register new candidate
) (begin
(map-set Candidates ((candidateId (+ (var-get numberOfCandidates) 1))) ((name name)
(votescount (to-uint 0))))
(incrementCandidates )
(ok 1)
) (err 0))
)
;; Voters casting their vote for a candidate only one vote allowed
(define-public (vote (candidateId int)) ;; to vote for a Candidate
(let ((voted (getVoted tx-sender)) (electionStatus (var-get votingIsOpen)) (count (getVotesCount candidateId)) (name (getName candidateId)))
(if (and (is-eq voted u0) ;; is voter did not vote before
(is-eq electionStatus open)
(> candidateId 0) (<= candidateId (var-get numberOfCandidates) ) ;; validating candidateID
)
(begin ;;
(map-set Candidates ((candidateId candidateId)) ((name name)
(votescount (+ count u1) ))) ;; add new vote for candidateID
(map-set voters ((voter tx-sender)) ((voted u1) ;; change voter state to voted by setting to 1 ,not 0
))
(ok 1) ;; return ok
)
(err 0)) ;; return error when one of validation condations failed
))