-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCouncil.py
87 lines (66 loc) · 2.84 KB
/
Council.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
from pyteal import *
def approval_program():
@Subroutine(TealType.bytes)
def itoa(i):
"""itoa converts an integer to the ascii byte string it represents"""
return If(
i == Int(0),
Bytes("0"),
Concat(
If(i / Int(10) > Int(0), itoa(i / Int(10)), Bytes("")),
int_to_ascii(i % Int(10)),
),
)
@Subroutine(TealType.bytes)
def int_to_ascii(arg):
"""int_to_ascii converts an integer to the ascii byte that represents it"""
return Extract(Bytes("0123456789"), arg, Int(1))
# don't need any real fancy initialization
handle_creation = Return(
Seq(
App.globalPut(Bytes("proposalNum"), Int(0)),
Int(1)
)
)
propose = Seq(
Assert(Txn.sender() == Addr("YRVK422KP65SU4TBAHY34R7YT3OYFOL4DUSFR4UADQEQHS2HMXKORIC6TE")),
App.box_put(Concat(Bytes("Proposal"), itoa(App.globalGet(Bytes("proposalNum"))), Bytes(" "), Txn.application_args[1]), itoa(Txn.applications[1])),
App.globalPut(Bytes("proposalNum"), Add(App.globalGet(Bytes("proposalNum")), Int(1))),
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields({
TxnField.type_enum: TxnType.Payment,
TxnField.receiver: Txn.accounts[1],
TxnField.amount: Int(20000000),
}),
InnerTxnBuilder.Submit(),
Int(1)
)
# doesn't need anyone to opt in
handle_optin = Return(Int(1))
# only the creator can closeout the contract
handle_closeout = Return(Int(1))
# nobody can update the contract
handle_updateapp = Return(Txn.sender() == Global.creator_address())
# only creator can delete the contract
handle_deleteapp = Return(Txn.sender() == Global.creator_address())
# handle the types of application calls
program = Cond(
[Txn.application_id() == Int(0), handle_creation],
[Txn.on_completion() == OnComplete.OptIn, handle_optin],
[Txn.on_completion() == OnComplete.CloseOut, handle_closeout],
[Txn.on_completion() == OnComplete.UpdateApplication, handle_updateapp],
[Txn.on_completion() == OnComplete.DeleteApplication, handle_deleteapp],
[Txn.application_args[0] == Bytes("propose"), Return(propose)]
)
return program
# let clear state happen
def clear_state_program():
program = Return(Int(1))
return program
if __name__ == "__main__":
with open("vote_approval.teal", "w") as f:
compiled = compileTeal(approval_program(), mode=Mode.Application, version=8)
f.write(compiled)
with open("vote_clear_state.teal", "w") as f:
compiled = compileTeal(clear_state_program(), mode=Mode.Application, version=8)
f.write(compiled)