-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
290 lines (257 loc) · 10.2 KB
/
helpers.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from pyteal import *
import json
import urllib
# Reserve helpers
#
# Takes uint64 and returns it as uvarint array
@Subroutine(TealType.bytes)
def __Itovi(num):
output = ScratchVar(TealType.bytes)
temp = ScratchVar(TealType.uint64)
main = Seq([
output.store(Substring(Itob(num), Int(7), Int(8))),
If(num >= Int(128)).Then(
Seq([
temp.store(num / Int(128)),
While(temp.load() >= Int(128)).Do(
Seq([
output.store(Concat(output.load(), Substring(Itob(temp.load()), Int(7), Int(8)))),
temp.store(temp.load() >> Int(7))
])),
output.store(Concat(output.load(), Substring(Itob(temp.load()), Int(7), Int(8))))
]))
])
return Seq([main, Return(output.load())])
def __get(lookup):
# V1 values
dict = {}
dict["validator_id"] = 684650147
dict["gard_id"] = 684649988
dict["close_fee_id"] = 684649986
dict["open_fee_id"] = 684649985
dict["devfee_address"] = "MTMJ5ADRK3CFG3HGUI7FS4Y55WGCUO5CRUMVNBZ7FW5IIG6T3IU2VJHUMM"
with urllib.request.urlopen(
"https://storage.googleapis.com/algo-pricing-data-2022/latest_pricing.json"
) as url:
data = json.loads(url.read().decode())
dict["current_price"] = data["float_price"]
return dict[lookup]
def __get_cdp_debt(client, cdp_address):
validator_id = __get("validator_id")
result = client.account_application_info(cdp_address, validator_id)
if 'message' in result.keys(): return 0
try:
for localval in result['app-local-state']["key-value"]:
if localval['key'] == 'R0FSRF9ERUJU':
return localval['value']['uint']
except:
return 0
def __cdp(user, cdp_id):
# V1 values
valid_id = __get("validator_id")
stab_id = __get("gard_id")
devfee_add = __get("devfee_address")
user_address = Addr(user)
stable_id = Int(stab_id)
validator_id = Int(valid_id)
devfee_address = Addr(devfee_add)
# arg_id = 0
# txn 0 -> self vote account
# txn 1 -> payment txn of 0
Vote = And(
Gtxn[0].amount() == Int(cdp_id),
Gtxn[0].sender() == user_address,
Gtxn[1].rekey_to() == Global.zero_address(),
Gtxn[1].fee() == Int(0),
If(Global.group_size() == Int(2)).Then(
Or(
And(
Gtxn[1].type_enum() == TxnType.Payment,
Gtxn[1].amount() == Int(0),
Gtxn[1].close_remainder_to() == Global.zero_address()
),
Gtxn[1].type_enum() == TxnType.KeyRegistration,
)
).Else(
And(
Global.group_size() == Int(3),
Gtxn[1].type_enum() == TxnType.ApplicationCall,
Gtxn[1].application_id() != validator_id,
Gtxn[2].application_id() == validator_id,
Gtxn[2].on_completion() == OnComplete.NoOp,
Gtxn[2].application_args[0] == Bytes("AppCheck"),
)
)
)
# To liquidate accounts with insufficient collateral
# arg_id = 1
# txn 0 -> Application call to price validator (application args["liquidate"])
# txn 1 -> payment to buyer
# txn 2 -> payment to reserve (in GARD)
# txn 3 -> payment to devfee address (in GARD)
# txn 4 -> payment to user address (in GARD)
Liquidate = And(
Global.group_size() == Int(5),
Gtxn[0].on_completion() == OnComplete.CloseOut,
Gtxn[0].application_id() == validator_id,
Gtxn[0].assets[0] == stable_id,
Gtxn[3].asset_receiver() == devfee_address,
Gtxn[4].asset_receiver() == user_address
)
# For user to redeem outstanding stable tokens for collateral w/ fee
# arg_id = 2
# txn 0 -> Application call (application args[Bytes("CloseFee")])
# (asset array args[stable_id])
# txn 1 -> stable to reserve (from holder)
# txn 2 -> Close out validator local state
# txn 3 -> payment to fee account and the rest to user
RedeemStableFee = And(
Global.group_size() == Int(4),
Gtxn[0].on_completion() == OnComplete.NoOp,
Gtxn[0].application_id() == validator_id,
Gtxn[0].application_args[0] == Bytes("CloseFee"),
Gtxn[0].assets[0] == stable_id,
Gtxn[1].sender() == user_address,
Gtxn[2].on_completion() == OnComplete.ClearState,
Gtxn[3].receiver() == devfee_address,
Gtxn[3].close_remainder_to() == user_address,
)
# For user to redeem outstanding stable tokens for collateral w/out fee
# arg_id = 3
# txn 0 -> Application call (to obtain reserve address) (application args[Bytes("CloseNoFee")])
# (asset array args[stable_id])
# txn 1 -> stable to reserve (from holder)
# txn 2 -> Close out validator local state
# Txn 3 -> payment to holder
RedeemStableNoFee = And(
Global.group_size() == Int(4),
Gtxn[0].on_completion() == OnComplete.NoOp,
Gtxn[0].application_id() == validator_id,
Gtxn[0].application_args[0] == Bytes("CloseNoFee"),
Gtxn[0].assets[0] == stable_id,
Gtxn[1].sender() == user_address,
Gtxn[2].on_completion() == OnComplete.ClearState,
)
# arg_id = 4
Validator_OptIn = And(
Txn.on_completion() == OnComplete.OptIn,
Txn.application_id() == validator_id,
Txn.rekey_to() == Global.zero_address(),
Txn.fee() == Int(0)
)
# For user to mint more GARD leveraging the algo balance of the position
# arg_id = 5
# txn 0 -> Application call (application args[Bytes("MoreGARD)])
# (asset array args[stable_id])
# txn 1 -> devfee payment
# txn 2 -> GARD transfer to user
More_gard = And(
Global.group_size() == Int(3),
Gtxn[0].on_completion() == OnComplete.NoOp,
Gtxn[0].application_id() == validator_id,
Gtxn[0].application_args[0] == Bytes("MoreGARD"),
Gtxn[1].sender() == user_address,
Gtxn[1].receiver() == devfee_address
)
# arg_id = 6
StartAuction = Or(
And(
Txn.application_id() == validator_id,
Txn.on_completion() == OnComplete.NoOp,
Txn.application_args[0] == Bytes("Auction"),
),
And(
Global.group_size() == Int(3),
Gtxn[0].application_id() == validator_id,
Gtxn[0].on_completion() == OnComplete.NoOp,
Gtxn[0].application_args[0] == Bytes("ClearApp"),
Gtxn[1].on_completion() == OnComplete.ClearState
)
)
# Only txns of one of the 7 types will be approved
program = Cond(
[Btoi(Arg(0)) == Int(0), Vote],
[Btoi(Arg(0)) == Int(1), Liquidate],
[Btoi(Arg(0)) == Int(2), RedeemStableFee],
[Btoi(Arg(0)) == Int(3), RedeemStableNoFee],
[Btoi(Arg(0)) == Int(4), Validator_OptIn],
[Btoi(Arg(0)) == Int(5), More_gard],
[Btoi(Arg(0)) == Int(6), StartAuction]
)
return program
def __reserve():
# V1 values
valid_id = __get("validator_id")
stable_id = __get("gard_id")
devfee_add = __get("devfee_address")
template = __cdp("RHN53AKL3IJGOIF5BJTIUFDOH4KMPR45XS4JM63W46PWMFFR3PPZXF5DOQ", 12)
# public key of DAO Devfee address
devfee_address = Addr(devfee_add)
validator_id = Int(valid_id)
# template base64 encoding from compiling cdp("RHN53AKL3IJGOIF5BJTIUFDOH4KMPR45XS4JM63W46PWMFFR3PPZXF5DOQ", 12)
# from cdp_escrow.py
# address will be replaced with user address
contract_logic = template
y = Concat(Bytes("Program"), Bytes("base64", contract_logic))
x1 = Substring(y, Int(0), Int(30))
x2 = Substring(y, Int(62), Int(455))
x3 = Substring(y, Int(456), Int(561))
contract_addr = Sha512_256(Concat(x1, Gtxn[0].sender(), x2, __Itovi(Gtxn[0].assets[1]), x3))
# For Opt-in to GARD
# arg_id = 0
optInStable = And(
Txn.type_enum() == TxnType.AssetTransfer,
Txn.xfer_asset() == Int(stable_id),
Txn.asset_amount() == Int(0),
Txn.rekey_to() == Global.zero_address(),
Txn.asset_close_to() == Global.zero_address(),
Txn.fee() == Int(0)
)
# For opening new position
# arg_id = 1
# txn 0 -> Call to price validator (application args["NewPosition", Int(unix_start)]) all as bytes
# account array [sender, contract_address]
# txn 1 -> proper algos to contract address (pays fee)
# txn 2 -> Algo transfer to Tapera Fee account
# txn 3 -> GARD transfer to User
Core = And(
Global.group_size() == Int(4),
Gtxn[0].on_completion() == OnComplete.NoOp,
Gtxn[0].application_id() == validator_id,
Gtxn[0].application_args[0] == Bytes("NewPosition"),
Gtxn[0].accounts[1] == Gtxn[1].receiver(),
Gtxn[0].assets[0] == Int(stable_id),
Gtxn[1].type_enum() == TxnType.Payment,
Gtxn[1].sender() == Gtxn[0].sender(),
# contract address computed by filling in template
Gtxn[1].receiver() == contract_addr,
Gtxn[2].type_enum() == TxnType.Payment,
# Gtxn[2].amount() is Checked by ApplicationCall
Gtxn[2].sender() == Gtxn[1].sender(),
Gtxn[2].receiver() == devfee_address,
Gtxn[3].type_enum() == TxnType.AssetTransfer,
Gtxn[3].xfer_asset() == Int(stable_id),
Gtxn[3].fee() == Int(0),
# Amount of GARD to be minted
Gtxn[3].asset_close_to() == Global.zero_address(),
Gtxn[3].rekey_to() == Global.zero_address(),
)
# For minting more from an open position
more_gard = And(
Global.group_size() == Int(3),
Gtxn[0].on_completion() == OnComplete.NoOp,
Gtxn[0].application_id() == validator_id,
Gtxn[0].application_args[0] == Bytes("MoreGARD"),
Gtxn[0].assets[0] == Int(stable_id),
Gtxn[2].type_enum() == TxnType.AssetTransfer,
Gtxn[2].fee() == Int(0),
Gtxn[2].asset_close_to() == Global.zero_address(),
Gtxn[2].rekey_to() == Global.zero_address(),
)
# Approved Txns must be one of the 3 types
program = Cond(
[Btoi(Arg(0)) == Int(0), optInStable],
[Btoi(Arg(0)) == Int(1), Core],
[Btoi(Arg(0)) == Int(2), more_gard]
)
return program