Ideas box
Ideas box voting process.
An ideas box enables to vote for ideas in order to select the ideas that appeal to the maximum numbers of voters.
In the process presented here, voters are each granted with 5 voting tokens that they can attribute to the ideas of their choice: a voter can either vote for 5 ideas each with 1 token, or vote for 1 idea with 5 tokens.
When the voting process is over, the box administrator may declare the selected ideas: the top 3 ideas are selected with the constraint that a selected idea must reach at least 6 votes.
1
archetype ideasbox
2
3
constant chairman : address = @tz1ZAQXACaEqryobpBoLbJUc2DjG5ZzrhARu
4
constant expiration : date = 2020-07-22
5
6
asset idea {
7
id : int;
8
desc : string;
9
nbvotes : int = 0;
10
}
11
12
asset voter {
13
addr : address;
14
remaining : int = 5;
15
}
16
17
asset selected {
18
wid : int;
19
}
20
21
entry register (a_voter : address) {
22
called by chairman
23
effect { voter.add({addr = a_voter}) }
24
}
25
26
entry add_idea(description : string) {
27
require {
28
r0 : now <= expiration - 2d;
29
}
30
effect {
31
idea.add({ id = idea.count(); desc = description})
32
}
33
}
34
35
entry vote(n : int, weight : int) {
36
specification {
37
p1 : let some v = voter[caller] in
38
let some bv = before.voter[caller] in
39
v.remaining = bv.remaining - weight
40
otherwise true otherwise true
41
}
42
require {
43
r1 : voter.contains(caller);
44
r2 : expiration - 2d < now <= expiration;
45
r3 : voter[caller].remaining >= weight;
46
}
47
effect {
48
voter[caller].remaining -= weight;
49
idea[n].nbvotes += weight;
50
}
51
}
52
53
entry finalize() {
54
called by chairman
55
require {
56
r4 : now > expiration;
57
r5 : selected.count() = 0; /* cannot finalize twice */
58
}
59
effect {
60
for i in idea.select(the.nbvotes >= 5).sort(desc(nbvotes)).head(3) do
61
selected.add({i})
62
done
63
}
64
}
65
66
specification {
67
i1 : 5 * voter.count() = idea.sum(nbvotes) + voter.sum(remaining)
68
}
69
Copied!
Last modified 1yr ago
Export as PDF
Copy link