forked from tzConnectBerlin/developers-presentation
-
Notifications
You must be signed in to change notification settings - Fork 5
/
poll.py
99 lines (85 loc) · 4.64 KB
/
poll.py
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
import smartpy as sp
class Poll(sp.Contract):
def __init__(self, admin: sp.TAddress):
self.init(
administrator = admin,
votes = sp.big_map(),
polls = sp.big_map().
)
# @sp.entry_point
def create_poll_internal(self, id, params):
poll_meta = sp.record(end_date = params.e, num_option = params.n, ipfs_meta = params.i)
tot = sp.map(tkey = sp.TNat, tvalue = sp.TNat)
poll = sp.record(metadata = poll_meta, totals = tot)
self.data.polls = sp.update_map(self.data.polls, id, sp.some(poll))
def decrement_old_vote(self,old_vote, totals_map):
total_vote = totals_map[old_vote]
totals_map = sp.update_map(totals_map, old_vote, sp.some(abs(total_vote - sp.nat(1))))
sp.result(totals_map)
def increment_new_voteself(self, new_vote, totals_map):
sp.if totals_map.contains(new_vote):
total_vote = totals_map[new_vote]
new_totals_map = sp.update_map(totals_map, new_vote, sp.some(total_vote + sp.nat(1)))
sp.else:
new_totals_map = sp.update_map(totals_map, new_vote, sp.some(sp.nat(1)))
sp.result(new_totals_map)
@sp.entry_point
def create_poll(self, create_poll_arg):
# sp.set_type(create_poll_arg, sp.TRecord(sp.TString, sp.TRecord(sp.TTimestamp, sp.TNat)))
sp.verify(self.data.administrator == sp.sender)
self.create_poll_internal(create_poll_arg.id, create_poll_arg.meta_data)
@sp.entry_point
def vote(self, vote_arg):
poll = self.data.polls[vote_arg.id]
metadata = poll.metadata
totals = poll.totals
sp.verify(sp.now <= metadata.end_date)
sp.verify(vote_arg.my_vote <= metadata.num_option)
mvote = vote_arg.my_vote
sp.trace(mvote)
sp.if self.data.votes.contains((sp.sender, vote_arg.id)):
old_vote, self.data.votes = sp.get_and_update(self.data.votes, (sp.sender, vote_arg.id), sp.some(mvote))
old_val = totals[old_vote]
new_old_val = abs(old_val - 1 )
totals = sp.update_map(totals, old_vote, sp.some(new_old_val))
sp.if totals.contains(mvote):
old_new_val = totals[mvote]
new_val = old_new_val + sp.nat(1)
totals = sp.update_map(totals, old_vote, sp.some(new_val))
poll = sp.record(metadata = metadata, totals = totals)
self.data.polls = sp.update_map(self.data.polls, vote_arg.id, sp.some(poll))
sp.else:
totals = sp.update_map(totals, mvote, sp.some(sp.nat(1)))
poll = sp.record(metadata = metadata, totals = totals)
self.data.polls = sp.update_map(self.data.polls, vote_arg.id, sp.some(poll))
sp.else:
self.data.votes = sp.update_map(self.data.votes, (sp.sender, vote_arg.id), sp.some(2))
sp.if totals.contains(mvote):
self.data.votes = sp.update_map(self.data.votes, (sp.sender, vote_arg.id), sp.some(1))
old_val = totals.get(mvote)
new_val = sp.nat(1) + old_val #sp.as_nat(old_val) + sp.nat(1)
totals = sp.update_map(totals, mvote, sp.some((new_val)))
poll = sp.record(metadata = metadata, totals = totals)
self.data.polls = sp.update_map(self.data.polls, vote_arg.id, sp.some(poll))
sp.else:
self.data.votes = sp.update_map(self.data.votes, (sp.sender, vote_arg.id), sp.some(3))
totals = sp.update_map(totals, mvote, sp.some(sp.nat(1)))
poll = sp.record(metadata = metadata, totals = totals)
self.data.polls = sp.update_map(self.data.polls, vote_arg.id, sp.some(poll))
@sp.add_test(name = "Poll")
def test():
contract = Poll(sp.address("tz1VWU45MQ7nxu5PGgWxgDePemev6bUDNGZ2"))
scenario = sp.test_scenario()
#scenario = contract.test_update_map()
# scenario = sp.test_scenario()
scenario.h1("Poll")
scenario += contract
# scenario += contract.check("bajs")
# scenario += contract.test_update_map(sp.some(sp.unit))
scenario += contract.create_poll(sp.record(id = "first_poll", meta_data = sp.record(e = sp.timestamp(100), n = sp.nat(4), i = "ipfshash123412341234"))).run(sender=sp.address("tz1VWU45MQ7nxu5PGgWxgDePemev6bUDNGZ2"))
scenario += contract.vote(sp.record(id = "first_poll", my_vote = sp.nat(4))).run(sender=sp.address("tz1azKk3gBJRjW11JAh8J1CBP1tF2NUu5yJ3"), now = sp.timestamp(90), valid=True)
# sp.record(id = "first_poll", my_vote = sp.nat(2))
# c1.test_map_get_opt({})
# c1.test_map_get_opt({12: "A"})
# # scenario += contract.replace(2)
# scenario += contract.double()