/
ballot-vote-with-alex-a-6T9bJ.clar
186 lines (155 loc) · 7.33 KB
/
ballot-vote-with-alex-a-6T9bJ.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
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
;; ballot
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-constant CONTRACT-OWNER tx-sender)
;; Errors
(define-constant ERR-NOT-STARTED (err u1001))
(define-constant ERR-ENDED (err u1002))
(define-constant ERR-ALREADY-VOTED (err u1003))
(define-constant ERR-FAILED-STRATEGY (err u1004))
(define-constant ERR-NOT-VOTED (err u1005))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; data maps and vars
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-data-var title (string-utf8 512) u"")
(define-data-var description (string-utf8 512) u"")
(define-data-var voting-system (string-ascii 512) "")
(define-data-var start uint u0)
(define-data-var end uint u0)
(define-map token-ids-map {token-id: uint} {user: principal, vote-id: uint})
(define-map btc-holder-map {domain: (buff 20), namespace: (buff 48)} {user: principal, vote-id: uint})
(define-map results {id: (string-ascii 36)} {count: uint, name: (string-utf8 256)} )
(define-map users {id: principal} {id: uint, vote: (list 2 (string-ascii 36)), volume: (list 2 uint), voting-power: uint})
(define-map register {id: uint} {user: principal, vote: (list 2 (string-ascii 36)), volume: (list 2 uint), voting-power: uint})
(define-data-var total uint u0)
(define-data-var total-votes uint u0)
(define-data-var options (list 2 (string-ascii 36)) (list))
(define-data-var temp-voting-power uint u0)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; private functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-private (get-voting-power-by-ft-holdings)
(let
(
(ft-balance (unwrap-panic (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.age000-governance-token get-balance tx-sender)))
(ft-decimals (unwrap-panic (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.age000-governance-token get-decimals)))
)
(if (> ft-balance u0)
(if (> ft-decimals u0)
(/ ft-balance (pow u10 ft-decimals))
ft-balance
)
ft-balance
)
)
)
(define-private (have-i-voted)
(match (map-get? users {id: tx-sender})
success true
false
)
)
(define-private (fold-boolean (left bool) (right bool))
(and (is-eq left true) (is-eq right true))
)
(define-private (check-volume (each-volume uint))
(> each-volume u0)
)
(define-private (validate-vote-volume (volume (list 2 uint)))
(begin
(fold fold-boolean (map check-volume volume) true)
)
)
(define-private (get-volume-by-voting-power (volume uint))
(var-get temp-voting-power)
)
(define-private (get-pow-value (volume uint))
(pow volume u2)
)
(define-private (process-my-vote (option-id (string-ascii 36)) (volume uint))
(match (map-get? results {id: option-id})
result (let
(
(new-count-tuple {count: (+ volume (get count result))})
)
;; Capture the vote
(map-set results {id: option-id} (merge result new-count-tuple))
;; Return
true
)
false
)
)
(define-private (get-single-result (option-id (string-ascii 36)))
(let
(
(volume (default-to u0 (get count (map-get? results {id: option-id}))))
)
;; Return volume
volume
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; public functions for all
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-public (cast-my-vote (vote (list 2 (string-ascii 36))) (volume (list 2 uint))
(bns (string-ascii 256)) (domain (buff 20)) (namespace (buff 48)) (token-ids (list 60000 uint))
)
(let
(
(vote-id (+ u1 (var-get total)))
(voting-power (get-voting-power-by-ft-holdings))
;; FPTP and Block voting
(temp (var-set temp-voting-power voting-power))
(volume-by-voting-power (map get-volume-by-voting-power volume))
;; FPTP and Block voting - Number of votes
(my-votes voting-power)
)
;; Validation
(asserts! (and (> (len vote) u0) (is-eq (len vote) (len volume-by-voting-power)) (validate-vote-volume volume-by-voting-power)) ERR-NOT-VOTED)
(asserts! (>= block-height (var-get start)) ERR-NOT-STARTED)
(asserts! (<= block-height (var-get end)) ERR-ENDED)
(asserts! (not (have-i-voted)) ERR-ALREADY-VOTED)
;; FPTP and Block voting
(asserts! (> voting-power u0) ERR-FAILED-STRATEGY)
;; Business logic
;; Process my vote
(map process-my-vote vote volume-by-voting-power)
;; Register for reference
(map-set users {id: tx-sender} {id: vote-id, vote: vote, volume: volume-by-voting-power, voting-power: voting-power})
(map-set register {id: vote-id} {user: tx-sender, vote: vote, volume: volume-by-voting-power, voting-power: voting-power})
;; Increase the total votes
(var-set total-votes (+ my-votes (var-get total-votes)))
;; Increase the total
(var-set total vote-id)
;; Return
(ok true)
)
)
(define-read-only (get-results)
(begin
(ok {
total: (var-get total),
total-votes: (var-get total-votes),
options: (var-get options),
results: (map get-single-result (var-get options))
})
)
)
(define-read-only (get-result-at-position (position uint))
(ok (map-get? register {id: position}))
)
(define-read-only (get-result-by-user (user principal))
(ok (map-get? users {id: user}))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Default assignments
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(var-set title u"(Vote with ALEX) Approve Launch of OrdzGames on the ALEX Launchpad")
(var-set description u"This proposal is to approve OrdzGames for the ALEX Decentralized Launchpad. Information on the project is available on the application form OrdzGames has submitted.OrdzGames Launchpad Application Form: https://docs.google.com/spreadsheets/d/1TowsBT4zvpXxbwZQPy5xJqKOxkj8P8dG9HtRCEN_SMw/edit?usp=sharingX/Twitter: https://twitter.com/OrdzGamesWebsite: https://www.ordz.games/Linktree: https://linktr.ee/ordz_games*Note that OrdzGames is changing their BRC20 token ticker from $OG$ to ORDG, the launch will distrib")
(var-set voting-system "fptp")
(var-set options (list "e89956ff-aeaf-4f45-ab9e-6db501720859" "8ed4b197-6048-4b76-890f-2a22bf9ad935"))
(var-set start u132582)
(var-set end u133014)
(map-set results {id: "e89956ff-aeaf-4f45-ab9e-6db501720859"} {count: u0, name: u"Approve Launch of OrdzGames on the ALEX Launchpad"}) (map-set results {id: "8ed4b197-6048-4b76-890f-2a22bf9ad935"} {count: u0, name: u"Disapprove Launch of OrdzGames on the ALEX Launchpad"})