-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractions_state_machine.py
780 lines (638 loc) · 26.6 KB
/
interactions_state_machine.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
# ----------------- Description -----------------
# This script provides a state machine for interaction with Compound smart Contract through the Algorand SDK.
# ----------------- Imports -----------------
import base64
import glob
from algosdk.v2client import algod
from algosdk import account, mnemonic, error, transaction
from algosdk.abi import Contract
from algosdk import encoding
from algosdk.atomic_transaction_composer import AtomicTransactionComposer, AccountTransactionSigner, \
TransactionWithSigner
from algosdk.logic import get_application_address
from demo.interact_w_CompoundContract import *
from util import *
from contract import deploy
# ----------------- Global variables -----------------
# Nodes
algod_client = None
# User secret key - FOR TEST PURPOSES ONLY!
user_sk = None
# User address
user_address = None
# Short form for user address
user_address_short = None
# ID of created compound contract
cc_id = 0
# Staking contract ID (i.e. of the staking pool which you would like to be compounding)
sc_id = 0
# ID of associated contract to the staking contract
ac_id = 0
# ID of AMM contract for swapping reward asset back to stake asset
amm_id = 0
# Address of the pool of AMM for swapping
p_addr = ""
# ID of staking asset
s_asa_id = 0
# ID of reward asset
r_asa_id = 0
# Contract type of the compounding - i.e. normal for staking pool (CC_TYPE) or for farming pool (FC_TYPE)
CC_TYPE = 0
FC_TYPE = 1
contract_type = CC_TYPE
# State (unique) encoding
S_INIT = 0
S_CHOOSE_USER = 1
S_TOP_MENU = 2
S_DEPLOY = 3
S_CONNECT = 4
S_CREATOR = 5
S_SETUP = 6
S_DELETE = 7
S_BOXES = 8
S_USER = 9
S_OPTIN = 10
S_OPTOUT = 11
S_FORCE_CLOSE = 12
S_STAKE = 13
S_WITHDRAW = 14
S_COMPOUND = 15
S_ACCUMULATE = 16
S_COMPOUND_NOW = 17
S_SCHEDULE_COMPOUND = 18
S_READ_BOXES = 19
# Current state
cs = -1
# Next state
ns = -1
# Previous state
ps = -1
# ---------------------------------------------------------------
# ----------------- Functions -----------------
def init():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
print("Welcome to interface for interacting with autocompounding contracts!")
# # ---- FOR TEST PURPOSES ONLY ----
# Algod connection parameters. Node must have EnableDeveloperAPI set to true in its config
algod_address = "https://node.testnet.algoexplorerapi.io" # "http://localhost:4001" #
algod_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
# Initialize an algodClient
algod_client = algod.AlgodClient(algod_token, algod_address)
# # ---- ---- ---- ---- ---- ---- ----
# while True:
# algod_address = input("First please input address to algod node to connect to: ")
# algod_token = input("Enter algod token: ")
#
# # Initialize an algodClient
# algod_client = algod.AlgodClient(algod_token, algod_address)
#
# try:
# algod_client.health()
# break
# except error.AlgodHTTPError as e:
# print("\tError: " + str(e))
# print("\tPlease try connecting to a different node")
ns = S_CHOOSE_USER
def choose_user():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
while True:
path_to_m = input("Please enter path to .txt file with your wallet's mnemonic. " + \
"(THIS IS FOR TEST PURPOSES ONLY!): ")
try:
with open(path_to_m, 'r') as f:
user_mnemonic = f.read()
user_sk = mnemonic.to_private_key(user_mnemonic)
user_address = account.address_from_private_key(user_sk)
user_address_short = str(user_address[0:4]) + "..." + str(user_address[-4:])
print("Welcome user: " + user_address_short)
break
except Exception:
print("You did not enter a path to a .txt file with mnemonic!")
continue
ns = S_TOP_MENU
def top_menu():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
print("Your options are:")
print("\t1) Switch user")
print("\t2) Deploy new compounding contract")
print("\t3) Connect to existing compounding contract")
print("\t4) Exit")
while True:
c = input("Please enter number of the option you would like to choose: ")
try:
c = int(c)
except ValueError:
print("You did not enter a valid number!")
continue
if c == 1:
ns = S_CHOOSE_USER
return
elif c == 2:
ns = S_DEPLOY
return
elif c == 3:
ns = S_CONNECT
return
elif c == 4:
exit(1)
else:
print("You did not enter a valid number!")
continue
def deploy_new_CC():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
while True:
print("Please enter the following parameters of the compounding contract you want to create:")
sc_id = input("App ID of Cometa staking contract to compound: ")
try:
sc_id = int(sc_id)
except ValueError:
print("You did not enter a valid number!")
continue
try:
algod_client.application_info(sc_id)
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
continue
ac_id = input("App ID of Cometa staking contract's associated contract: ")
try:
ac_id = int(ac_id)
except ValueError:
print("You did not enter a valid number!")
continue
try:
algod_client.application_info(ac_id)
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
continue
cp = input("Claim period - number of rounds after pool ends that claiming can be done: ")
try:
cp = int(cp)
if cp <= 0:
raise ValueError
except ValueError:
print("You did not enter a valid number!")
continue
try:
print("")
[cc_id, s_asa_id] = createCompoundContract(algod_client, user_sk, sc_id, ac_id, cp)
print("\nCreated compound contract with app ID: " + str(cc_id))
print("For asset with ID: " + str(s_asa_id))
break
except Exception as e:
print("\tError: " + str(e))
continue
ns = S_CREATOR
def connect_to_CC():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
while True:
cc_id = input("Please enter the app ID of the compound contract you are trying to connect to: ")
try:
cc_id = int(cc_id)
cc_state = read_global_state(algod_client, cc_id)
sc_id = cc_state["SC_ID"]
ac_id = cc_state["AC_ID"]
s_asa_id = cc_state["S_ASA_ID"]
if "AMM_ID" not in cc_state:
contract_type = CC_TYPE
else:
contract_type = FC_TYPE
r_asa_id = cc_state["R_ASA_ID"]
amm_id = cc_state["AMM_ID"]
p_addr = encoding.encode_address(cc_state["P_ADDR"])
except ValueError:
print("You did not enter a valid number!")
continue
except KeyError:
print("Are you really conneting to a compounding contract?")
continue
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
continue
try:
cc_creator_address = algod_client.application_info(cc_id)["params"]["creator"]
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
ns = ps
return
if cc_creator_address == user_address:
ns = S_CREATOR
print("Welcome " + user_address_short + ", compound contract creator!")
return
else:
print("Welcome " + user_address_short + "!")
ns = S_USER
return
def creator_interact():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
print("You are managing contract with ID " + str(cc_id) + " and following parameters:")
try:
cc_state = read_global_state(algod_client, cc_id)
print("\tConnected to staking contract ID: {}".format(cc_state["SC_ID"]))
print("\tConnected to associated contract ID: {}".format(cc_state["AC_ID"]))
print("\tStaking ASA ID: {}".format(cc_state["S_ASA_ID"]))
if contract_type == FC_TYPE:
print("\tReward ASA ID: {}".format(cc_state["R_ASA_ID"]))
print("\tConnected AMM contract ID: {}".format(cc_state["AMM_ID"]))
print("\tConnected AMM pool address: {}".format(encoding.encode_address(cc_state["P_ADDR"])))
print("\tMinimum amount of reward ASA ID before they are added to the farming pool: {} [base unit]".format(
cc_state["MRAAL"]))
print("\tTotal stake: {} [base unit]".format(cc_state["TS"]))
print("\tPool start round: {} [round]".format(cc_state["PSR"]))
print("\tPool end round: {} [round]".format(cc_state["PER"]))
print("\tClaiming period: {} [rounds]".format(cc_state["CP"]))
print("\tLast compound done: {}".format(cc_state["LCD"]))
print("\tLast compound round: {} [round]".format(cc_state["LCR"]))
print("\tNumber of stakers: {}".format(cc_state["NS"]))
print("\tNumber of boxes: {}".format(cc_state["NB"]))
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
ns = S_TOP_MENU
return
except KeyError:
print("Wrong contract!")
ns = S_TOP_MENU
return
print("\nYour options are:")
print("\t1) Setup contract")
print("\t2) Delete contract")
print("\t3) Delete contract boxes")
print("\t4) Interact as user")
print("\t5) Read all compounding increments")
print("\t6) Go to the top menu")
while True:
c = input("Please enter number of the option you would like to choose: ")
try:
c = int(c)
except ValueError:
print("You did not enter a valid number!")
continue
if c == 1:
ns = S_SETUP
return
elif c == 2:
ns = S_DELETE
return
elif c == 3:
ns = S_BOXES
return
elif c == 4:
ns = S_USER
return
elif c == 5:
ns = S_READ_BOXES
return
elif c == 6:
ns = S_TOP_MENU
return
else:
print("You did not enter a valid number!")
continue
def setup_CC():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
setupCompoundContract(algod_client, user_sk, cc_id, sc_id, s_asa_id)
print("\nSuccessfully setup contract with app ID: " + str(cc_id))
except Exception as e:
print("\tError: " + str(e))
ns = S_CREATOR
def delete_CC():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
deleteCompoundContract(algod_client, user_sk, cc_id, sc_id, ac_id, s_asa_id)
print("\nSuccessfully deleted contract with app ID: " + str(cc_id))
except Exception as e:
print("\tError: " + str(e))
ns = S_CREATOR
def delete_boxes():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
deleteAllBoxes(algod_client, user_sk, cc_id)
print("\nSuccessfully deleted all boxes of app ID: " + str(cc_id))
except Exception as e:
print("\tError: " + str(e))
ns = S_CREATOR
def user_interact():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
print("You are interacting with contract with ID " + str(cc_id) + " and following parameters:")
try:
cc_state = read_global_state(algod_client, cc_id)
print("\tConnected to staking contract ID: {}".format(cc_state["SC_ID"]))
print("\tConnected to associated contract ID: {}".format(cc_state["AC_ID"]))
print("\tStaking ASA ID: {}".format(cc_state["S_ASA_ID"]))
if contract_type == FC_TYPE:
print("\tReward ASA ID: {}".format(cc_state["R_ASA_ID"]))
print("\tConnected AMM contract ID: {}".format(cc_state["AMM_ID"]))
print("\tConnected AMM pool address: {}".format(encoding.encode_address(cc_state["P_ADDR"])))
print("\tMinimum amount of reward ASA ID before they are added to the farming pool: {} [base unit]".format(
cc_state["MRAAL"]))
print("\tTotal stake: {} [base unit]".format(cc_state["TS"]))
print("\tPool start round: {} [round]".format(cc_state["PSR"]))
print("\tPool end round: {} [round]".format(cc_state["PER"]))
print("\tClaiming period: {} [rounds]".format(cc_state["CP"]))
print("\tLast compound done: {}".format(cc_state["LCD"]))
print("\tLast compound round: {} [round]".format(cc_state["LCR"]))
print("\tNumber of stakers: {}".format(cc_state["NS"]))
print("\tNumber of boxes: {}".format(cc_state["NB"]))
next_trig_round = getTriggerRound(algod_client, cc_id)
if next_trig_round == 0:
print("\n\tCompounding can be triggered!")
elif next_trig_round > 0:
print("\n\tNext scheduled compounding can be trigger at round: " + str(next_trig_round))
elif next_trig_round == -1:
print("\n\tPool has already ended. Please withdraw your stake.")
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
ns = S_TOP_MENU
return
except KeyError:
print("Wrong contract!")
ns = S_TOP_MENU
return
print("")
opted_in_already = True
try:
cc_local_state = read_local_state(algod_client, user_address, cc_id)
except KeyError:
opted_in_already = False
print("\tIf you are a new user, please opt in!")
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
opted_in_already = False
print("\tIf you are a new user, please opt in!")
if opted_in_already:
your_stake = getUsersCompoundStake(algod_client, user_address, cc_id)
print("\nYou have {} [base unit] of ASA ID '{}' in the contract".format(
your_stake, s_asa_id))
NB_diff = cc_state["NB"] - cc_local_state["LNB"]
if NB_diff > 0:
print("\t**For advance users:** You have {} results to claim".format(NB_diff))
try:
ai = algod_client.account_asset_info(user_address, s_asa_id)
print("\nYou are holding {} [base unit] of staking asset".format(ai['asset-holding']['amount']))
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
print("\tAre you opted-in the staking asset?")
print("\nYour options are:")
print("For basic users:")
if not(opted_in_already):
print("\t0) Opt-in to contract")
print("\t1) Stake")
print("\t2) Withdraw")
print("\t3) Opt-out of contract")
print("For advance users:")
print("\t4) Trigger compounding")
print("\t5) Compound now - even if not scheduled")
print("\t6) Schedule additional optimal compounding")
print("\t7) Locally accumulate")
print("\t8) Clear your contract state")
print("\t9) Read all compounding increments")
print("\t10) Go to the top menu")
while True:
c = input("Please enter number of the option you would like to choose: ")
try:
c = int(c)
except ValueError:
print("You did not enter a valid number!")
continue
if c == 0:
ns = S_OPTIN
return
elif c == 1:
ns = S_STAKE
return
elif c == 2:
ns = S_WITHDRAW
return
elif c == 3:
ns = S_OPTOUT
return
elif c == 4:
ns = S_COMPOUND
return
elif c == 5:
ns = S_COMPOUND_NOW
return
elif c == 6:
ns = S_SCHEDULE_COMPOUND
return
elif c == 7:
ns = S_ACCUMULATE
return
elif c == 8:
ns = S_FORCE_CLOSE
return
elif c == 9:
ns = S_READ_BOXES
return
elif c == 10:
ns = S_TOP_MENU
return
else:
print("You did not enter a valid number!")
continue
def optin_to_CC():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
optinCompoundContract(algod_client, user_sk, cc_id)
print("\nSuccessfully opted into app ID: " + str(cc_id))
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
except KeyError:
print("\tAre you opted into the contract?")
except Exception as e:
print("\tError: " + str(e))
ns = S_USER
def optout_of_CC():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
optoutCompoundContract(algod_client, user_sk, cc_id)
print("\nSuccessfully opted out of app ID: " + str(cc_id))
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
except KeyError:
print("\tAre you opted into the contract?")
except Exception as e:
print("\tError: " + str(e))
print("\tHave you withdrawn you full amount?")
ns = S_USER
def force_opt_out_of_CC():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
clearStateCompoundContract(algod_client, user_sk, cc_id)
print("\nSuccessfully forcefully opted out of app ID: " + str(cc_id))
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
except KeyError:
print("\tAre you opted into the contract?")
except Exception as e:
print("\tError: " + str(e))
ns = S_USER
def stake_to_CC():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
while True:
amt = input("Please enter the amount [base unit] you would like to deposit to the compound contract: ")
try:
amt = int(amt)
break
except ValueError:
print("You did not enter a valid number!")
continue
try:
stakeCompoundContract(algod_client, user_sk, cc_id, sc_id, ac_id, s_asa_id, amt)
print("\nSuccessfully staked {} to app ID: {}".format(amt, str(cc_id)))
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
except KeyError:
print("\tAre you opted into the contract?")
except Exception as e:
print("\tError: " + str(e))
ns = S_USER
def withdraw_from_CC():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
while True:
amt = input("Please enter the amount [base unit] you would like to withdraw from the compound contract: ")
try:
amt = int(amt)
break
except ValueError:
print("You did not enter a valid number!")
continue
try:
amt = withdrawCompoundContract(algod_client, user_sk, cc_id, sc_id, ac_id, s_asa_id, amt)
print("\nSuccessfully withdrawn " + str(amt))
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
except KeyError:
print("\tAre you opted into the contract?")
except Exception as e:
print("\tError: " + str(e))
ns = S_USER
def trigger_compounding():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
tmp = triggerCompoundingCompoundContract(algod_client, user_sk, cc_id, sc_id, ac_id, s_asa_id)
if tmp == 1:
print("\nSuccessfully compounded stake!")
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
except KeyError:
print("\tAre you opted into the contract?")
except Exception as e:
print("\tError: " + str(e))
ns = S_USER
def locally_accumulate():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
localClaimCompoundContract(algod_client, user_sk, cc_id)
print("\nSuccessfully locally claimed the stake. You can now withdraw it!")
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
except KeyError:
print("\tAre you opted into the contract?")
except Exception as e:
print("\tError: " + str(e))
ns = S_USER
def compound_now():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
compoundNowCompoundContract(algod_client, user_sk, cc_id, sc_id, ac_id, s_asa_id)
print("\nSuccessfully compounded the stake!")
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
except KeyError:
print("\tAre you opted into the contract?")
except Exception as e:
print("\tError: " + str(e))
ns = S_USER
def schedule_optimal_compound():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
sheduleAdditionalCompounding(algod_client, user_sk, cc_id)
print("\nSuccessfully scheduled an optimal additional compounding!")
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
except KeyError:
print("\tAre you opted into the contract?")
except Exception as e:
print("\tError: " + str(e))
ns = S_USER
def read_all_boxes():
global cs, ns, ps, cc_id, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
print("\n----------------------------------------------------------------------------------------")
try:
readAllCompoundingContributions(algod_client, cc_id)
except error.AlgodHTTPError as e:
print("\tError: " + str(e))
except Exception as e:
print("\tError: " + str(e))
ns = ps
# ---------------------------------------------------------------
def main():
global cs, ns, ps, sc_id, ac_id, contract_type, amm_id, p_addr, s_asa_id, r_asa_id, user_sk, user_address, user_address_short, algod_client
cs = S_INIT
while True:
if cs == S_INIT:
init()
elif cs == S_CHOOSE_USER:
choose_user()
elif cs == S_TOP_MENU:
top_menu()
elif cs == S_DEPLOY:
deploy_new_CC()
elif cs == S_CONNECT:
connect_to_CC()
elif cs == S_CREATOR:
creator_interact()
elif cs == S_SETUP:
setup_CC()
elif cs == S_DELETE:
delete_CC()
elif cs == S_BOXES:
delete_boxes()
elif cs == S_USER:
user_interact()
elif cs == S_OPTIN:
optin_to_CC()
elif cs == S_OPTOUT:
optout_of_CC()
elif cs == S_FORCE_CLOSE:
force_opt_out_of_CC()
elif cs == S_STAKE:
stake_to_CC()
elif cs == S_WITHDRAW:
withdraw_from_CC()
elif cs == S_COMPOUND:
trigger_compounding()
elif cs == S_ACCUMULATE:
locally_accumulate()
elif cs == S_COMPOUND_NOW:
compound_now()
elif cs == S_SCHEDULE_COMPOUND:
schedule_optimal_compound()
elif cs == S_READ_BOXES:
read_all_boxes()
else:
raise ValueError('Invalid state')
ps = cs
cs = ns
if __name__ == "__main__":
main()