-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.py
221 lines (184 loc) · 6.59 KB
/
contract.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from beaker import GlobalStateValue, LocalStateValue, Application, Authorize
from pyteal import (
TealType,
abi,
Expr,
Seq,
Txn,
Assert,
Int,
If,
Reject,
Approve,
Subroutine,
InnerTxnBuilder,
TxnField,
TxnType,
)
class FitnessStates:
### global ###
challenger_addr = GlobalStateValue(
stack_type=TealType.bytes, static=True, descr="Address of the challenger"
)
challenger_stake = GlobalStateValue(
stack_type=TealType.uint64, static=True, descr="Stake amount of the challenger"
)
supporter_stake = GlobalStateValue(
stack_type=TealType.uint64, static=True, descr="Stake amount of the supporter"
)
total_stake = GlobalStateValue(
stack_type=TealType.uint64, descr="Stake amount of the supporter"
)
num_supporter = GlobalStateValue(
stack_type=TealType.uint64, default=Int(0), descr="Number of supporter"
)
challenge_started = GlobalStateValue(
stack_type=TealType.uint64,
default=Int(0),
descr="Check if challenge is in progress. Int(0): not started, Int(1): in progress, Int(2): finished",
)
challenge_result = GlobalStateValue(
stack_type=TealType.uint64,
descr="Int(1) if challenger successful, Int(0) if challenger fails.",
)
supporter_reward = GlobalStateValue(
stack_type=TealType.uint64,
static=True,
descr="How much each supporter should get if challenger fails.",
)
### local ###
staked = LocalStateValue(
stack_type=TealType.uint64,
default=Int(0),
descr="Check if the account staked. false: Int(0), true: Int(1)",
)
role = LocalStateValue(
stack_type=TealType.uint64,
descr="Supporter: Int(0), Challenger: Int(1)",
)
claimed = LocalStateValue(
stack_type=TealType.uint64,
default=Int(0),
descr="Check if the account claimed reward. false: Int(0), true: Int(1)",
)
app = Application("fitness challenge app", state=FitnessStates())
@app.create
def app_create(stake_amt: abi.Uint64) -> Expr:
return Seq(
app.initialize_global_state(),
app.state.challenger_addr.set(Txn.sender()),
app.state.challenger_stake.set(stake_amt.get()),
app.state.supporter_stake.set(calculate_support_stake(stake_amt)),
)
@app.external
def read_challenger_addr(*, output: abi.Address) -> Expr:
return output.set(app.state.challenger_addr)
@app.opt_in
def optin_role(addr_role: abi.Uint8, *, output: abi.String) -> Expr:
return Seq(
Assert(app.state.challenge_started == Int(0)),
app.initialize_local_state(),
If(addr_role.get() == Int(1))
.Then(
Seq(
Assert(Txn.sender() == app.state.challenger_addr),
app.state.role[Txn.sender()].set(Int(1)),
)
)
.ElseIf(addr_role.get() == Int(0))
.Then(
Seq(
Assert(Txn.sender() != app.state.challenger_addr),
app.state.role[Txn.sender()].set(Int(0)),
app.state.num_supporter.set(app.state.num_supporter + Int(1)),
)
)
.Else(output.set("Test!")),
)
@app.external(authorize=Authorize.opted_in())
def deposit_stake(pay: abi.PaymentTransaction) -> Expr:
return Seq(
Assert(app.state.staked[Txn.sender()] == Int(0)),
If(app.state.role[Txn.sender()] == Int(1))
.Then(
Assert(pay.get().amount() == app.state.challenger_stake),
app.state.total_stake.set(app.state.total_stake + pay.get().amount()),
app.state.staked[Txn.sender()].set(Int(1)),
)
.ElseIf(app.state.role[Txn.sender()] == Int(0))
.Then(
Assert(pay.get().amount() == app.state.supporter_stake),
app.state.total_stake.set(app.state.total_stake + pay.get().amount()),
app.state.staked[Txn.sender()].set(Int(1)),
)
.Else(Reject()),
)
@app.external(authorize=Authorize.only_creator())
def start_challenge() -> Expr:
return Seq(
Assert(app.state.staked[Txn.sender()] == Int(1)),
Assert(app.state.num_supporter > Int(0)),
app.state.supporter_reward.set(app.state.total_stake / app.state.num_supporter),
app.state.challenge_started.set(Int(1)),
)
@app.external(authorize=Authorize.only_creator())
def end_challenge(result: abi.Uint8) -> Expr:
return Seq(
app.state.challenge_result.set(result.get()),
app.state.challenge_started.set(Int(2)),
Approve(),
)
@app.external(authorize=Authorize.opted_in())
def claim_funds() -> Expr:
return Seq(
Assert(app.state.claimed[Txn.sender()] == Int(0)),
Assert(app.state.challenge_started == Int(2)),
Assert(app.state.staked[Txn.sender()] == Int(1)),
Assert(app.state.total_stake > Int(0)),
If(app.state.challenge_result == Int(1))
.Then(
Seq(
Assert(Txn.sender() == app.state.challenger_addr),
Assert(app.state.role[Txn.sender()] == Int(1)),
send_payment(Txn.sender(), app.state.total_stake),
app.state.claimed[Txn.sender()].set(Int(1)),
app.state.staked[Txn.sender()].set(Int(0)),
app.state.total_stake.set(Int(0)),
)
)
.ElseIf(app.state.challenge_result == Int(0))
.Then(
Seq(
Assert(Txn.sender() != app.state.challenger_addr),
Assert(app.state.role[Txn.sender()] == Int(0)),
send_payment(Txn.sender(), app.state.supporter_reward),
app.state.claimed[Txn.sender()].set(Int(1)),
app.state.staked[Txn.sender()].set(Int(0)),
app.state.total_stake.set(
app.state.total_stake - app.state.supporter_reward
),
)
)
.Else(Reject()),
)
@app.delete(authorize=Authorize.only_creator())
def delete() -> Expr:
return Seq(
Assert(app.state.challenge_started != Int(1)),
Assert(app.state.total_stake == Int(0)),
Approve(),
)
### Internal Subroutines ###
@Subroutine(return_type=TealType.uint64)
def calculate_support_stake(stake: abi.Uint64) -> Expr:
return stake.get() / Int(2)
@Subroutine(return_type=TealType.none)
def send_payment(receiver: Expr, amount: Expr) -> Expr:
return InnerTxnBuilder.Execute(
{
TxnField.type_enum: TxnType.Payment,
TxnField.receiver: receiver,
TxnField.amount: amount,
TxnField.fee: Int(0), # cover fee with outer txn
}
)