-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoting_contract.py
More file actions
132 lines (117 loc) · 4.93 KB
/
voting_contract.py
File metadata and controls
132 lines (117 loc) · 4.93 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
129
130
131
132
from pyteal import *
def approval_program():
on_creation = Seq(
[
# name of this application
App.globalPut(Bytes("AppName"), Bytes("Community 1 Governance Application")),
# choice A
App.globalPut(Bytes("Option A"), Bytes("Description for option one.")),
# choice B
App.globalPut(Bytes("Option B"), Bytes("Description for option two.")),
# creator is set to the contract creator
App.globalPut(Bytes("Creator"), Txn.sender()),
# expecting four arguments for the registration and voting time frames
Assert(Txn.application_args.length() == Int(4)),
# registration begins blockround
App.globalPut(Bytes("RegBegin"), Btoi(Txn.application_args[0])),
# registration ending blockround
App.globalPut(Bytes("RegEnd"), Btoi(Txn.application_args[1])),
# vote begining blockround
App.globalPut(Bytes("VoteBegin"), Btoi(Txn.application_args[2])),
# vote ending blockround
App.globalPut(Bytes("VoteEnd"), Btoi(Txn.application_args[3])),
Return(Int(1)),
]
)
# checks to see if txn sender is the contract creator
is_creator = Txn.sender() == App.globalGet(Bytes("Creator"))
# this gets the sender vote from an external application's local state
get_vote_of_sender = App.localGetEx(Int(0), App.id(), Bytes("voted"))
# when delete app is called get vote of sender is called and the if statement is called
on_closeout = Seq(
[
get_vote_of_sender,
# if vote hasnt ended and the user has voted, we delete their vote
If(
And(
Global.round() <= App.globalGet(Bytes("VoteEnd")),
get_vote_of_sender.hasValue(),
),
App.globalPut(
get_vote_of_sender.value(),
App.globalGet(get_vote_of_sender.value()) - Int(1),
),
),
# otherwise we just approve the app deletion
Return(Int(1)),
]
)
# checks that the registration period is active before approving opt in
on_register = Return(
And(
Global.round() >= App.globalGet(Bytes("RegBegin")),
Global.round() <= App.globalGet(Bytes("RegEnd")),
)
)
# first app arg is assigned to choice variable
choice = Txn.application_args[1]
# gets the current choice count value
choice_tally = App.globalGet(choice)
# this is the only noop call in this application
on_vote = Seq(
[
# first we check that the voting period is active
Assert(
And(
Global.round() >= App.globalGet(Bytes("VoteBegin")),
Global.round() <= App.globalGet(Bytes("VoteEnd")),
)
),
# next the vote of the txn sender is retrieved
get_vote_of_sender,
# if the vote exists then we continue executing the sequence
If(get_vote_of_sender.hasValue(), Return(Int(0))),
# the choice key is accessed and the tally is updated by adding one
App.globalPut(choice, choice_tally + Int(1)),
# records the voter's choice in the voted key of the voter's local state
App.localPut(Int(0), Bytes("voted"), choice),
Return(Int(1)),
]
)
program = Cond(
[Txn.application_id() == Int(0), on_creation],
[Txn.on_completion() == OnComplete.DeleteApplication, Return(is_creator)],
[Txn.on_completion() == OnComplete.UpdateApplication, Return(is_creator)],
[Txn.on_completion() == OnComplete.CloseOut, on_closeout],
[Txn.on_completion() == OnComplete.OptIn, on_register],
[Txn.application_args[0] == Bytes("vote"), on_vote],
)
return program
def clear_state_program():
# gets the vote of the voted value from the external app
get_vote_of_sender = App.localGetEx(Int(0), App.id(), Bytes("voted"))
program = Seq(
[
get_vote_of_sender,
# if the vote has not ended, then remove the account's vote
If(
And(
Global.round() <= App.globalGet(Bytes("VoteEnd")),
get_vote_of_sender.hasValue(),
),
App.globalPut(
get_vote_of_sender.value(),
App.globalGet(get_vote_of_sender.value()) - Int(1),
),
),
Return(Int(1)),
]
)
return program
if __name__ == "__main__":
with open("vote_approval.teal", "w") as f:
compiled = compileTeal(approval_program(), mode=Mode.Application, version=2)
f.write(compiled)
with open("vote_clear_state.teal", "w") as f:
compiled = compileTeal(clear_state_program(), mode=Mode.Application, version=2)
f.write(compiled)