Contract con_ttt_lottery


Contract Code


  
1 import currency
2
3 owner = Variable()
4 current_round = Variable()
5 min_amount = Variable()
6 interval_seconds = Variable()
7 genesis_round_run = Variable()
8 total = Variable()
9
10 rounds = Hash()
11 user_rounds = Hash()
12
13 @construct
14 def seed():
15 owner.set(ctx.caller)
16 min_amount.set(1)
17 interval_seconds.set(3600) # one hour
18 current_round.set(1)
19 genesis_round_run.set(False)
20 total.set(0)
21
22 @export
23 def transfer_ownership(new: str):
24 assert owner.get() == ctx.caller, 'Only owner can execute transfer_ownership method.'
25 owner.set(new)
26
27
28 @export
29 def buy(bet_number: int, amount: float, round_num: int):
30 assert bet_number <= 6 and bet_number >= 1, 'The betting number must be between 1~6'
31 assert amount >= min_amount.get(), f'At least {min_amount.get()} Taus are required.'
32
33 assert rounds[round_num, "startTime"] is not None and rounds[round_num, "startTime"] <= now, \
34 f'Round #{round_num} not started'
35 assert rounds[round_num, "endTime"] is not None and rounds[round_num, "endTime"] >= now, \
36 f'Round #{round_num} has ended'
37
38 caller = ctx.caller
39
40 currency.transfer_from(amount=amount, to=ctx.this, main_account=caller)
41
42 # Store bet info
43 rounds[round_num, "betInfo"].append({
44 'buyer': caller,
45 'number': bet_number,
46 'amount': amount
47 })
48
49 # Store user bet info
50 if user_rounds[caller] is None:
51 user_rounds[caller] = []
52
53 if bet_number not in user_rounds[caller]:
54 user_rounds[caller].append(round_num)
55
56
57
58 @export
59 def run():
60 assert owner.get() == ctx.caller, 'Only owner can execute start method.'
61
62 current = current_round.get()
63 if not genesis_round_run.get():
64 start_round(current)
65 genesis_round_run.set(True)
66 return
67
68 end_round(current)
69 # Calculate rewards
70 calculate_rewards()
71
72 # Increment current round to next round
73 next_round = current + 1
74 current_round.set(next_round)
75
76 start_round(next_round)
77
78
79 @export
80 def claim(round_num: int):
81 caller = ctx.caller
82 assert rounds[round_num, "status"] == "Ended", f"Claim failed, round #{round_num} not ended"
83 assert not rounds[round_num, caller, "claimed"], "You have claimed for this round"
84
85 winning_number = rounds[round_num, "winNumber"]
86 bet_amount = 0
87 for value in rounds[round_num, "betInfo"]:
88 if value.get('buyer') == caller and value.get('number') == winning_number:
89 bet_amount = bet_amount + value.get('amount')
90
91 assert bet_amount > 0, "Not eligible for claim"
92
93 winner_bet_amount = rounds[round_num, "winnerBetAmount"]
94 total_amount = rounds[round_num, "totalAmount"]
95 rewards = (bet_amount / winner_bet_amount) * total_amount
96 rounds[round_num, caller, "claimed"] = True
97
98 currency.transfer(amount=rewards, to=caller)
99
100 def start_round(round_num: int):
101 now_time = now
102 rounds[round_num, "startTime"] = now_time
103 rounds[round_num, "endTime"] = now_time + datetime.timedelta(seconds=interval_seconds.get())
104 rounds[round_num, "status"] = "Starting"
105 rounds[round_num, "betInfo"] = []
106
107 def end_round(round_num: int):
108 assert rounds[round_num, "endTime"] is not None and rounds[round_num, "endTime"] < now, \
109 f"Round #{round_num} not ended"
110 rounds[round_num, "status"] = "Ended"
111
112
113 def calculate_rewards():
114
115 current = current_round.get()
116 betinfo = rounds[current, "betInfo"]
117
118 # Called once per transaction
119 random.seed()
120 # Calculate the draw number
121 winning_number = random.randint(1,7)
122
123 winner_bet_amount = 0
124 total_amount = 0
125 for value in betinfo:
126 if winning_number == value.get('number'):
127 winner_bet_amount = winner_bet_amount + value.get('amount')
128 total_amount = total_amount + value.get('amount')
129
130 rounds[current, "winnerBetAmount"] = winner_bet_amount
131 rounds[current, "totalAmount"] = total_amount
132 rounds[current, "totalAwards"] = total_amount + total
133 rounds[current, "winNumber"] = winning_number
134
135 if winner_bet_amount > 0:
136 # Clear the prize pool
137 total.set(0)
138 else:
139 total.set(total.get() + total_amount)
140
141
142
143

Byte Code

e3000000000000000000000000050000004000000073ee000000640064016c005a0065016402640364048d025a0265016402640564048d025a0365016402640664048d025a0465016402640764048d025a0565016402640864048d025a0665016402640964048d025a0765086402640a64048d025a0965086402640b64048d025a0a640c640d84005a0b650c64028301650d640e9c01640f6410840483015a0e650c64028301650f6510650f64119c0364126413840483015a11650c6402830164146415840083015a12650c64028301650f64169c0164176418840483015a13650f64169c016419641a84045a14650f64169c01641b641c84045a15641d641e84005a1664015300291fe9000000004eda0f636f6e5f7474745f6c6f7474657279da056f776e65722902da08636f6e7472616374da046e616d65da0d63757272656e745f726f756e64da0a6d696e5f616d6f756e74da10696e74657276616c5f7365636f6e6473da1167656e657369735f726f756e645f72756eda05746f74616cda06726f756e6473da0b757365725f726f756e6473630000000000000000000000000200000043000000734200000074006a0174026a038301010074046a0164018301010074056a0164028301010074066a0164018301010074076a0164038301010074086a016404830101006400530029054ee90100000069100e00004672010000002909da075f5f6f776e6572da03736574da03637478da0663616c6c6572da0c5f5f6d696e5f616d6f756e74da125f5f696e74657276616c5f7365636f6e6473da0f5f5f63757272656e745f726f756e64da135f5f67656e657369735f726f756e645f72756eda075f5f746f74616ca90072170000007217000000da00da045f5f5f5f0e000000730c00000000010c010a010a010a010a0172190000002901da036e6577630100000000000000010000000200000043000000732400000074006a01830074026a036b027316740464018301820174006a057c00830101006400530029024e7a314f6e6c79206f776e65722063616e2065786563757465207472616e736665725f6f776e657273686970206d6574686f642e2906720e000000da0367657472100000007211000000da0e417373657274696f6e4572726f72720f0000002901721a000000721700000072170000007218000000da127472616e736665725f6f776e657273686970170000007306000000000206011001721d0000002903da0a6265745f6e756d626572da06616d6f756e74da09726f756e645f6e756d63030000000000000004000000050000004300000073fc0000007c0064016b0172107c0064026b05731874006403830182017c0174016a0283006b0573387400640474016a0283009b0064059d038301820174037c0264066602190064006b09725874037c0264066602190074046b017368740064077c029b0064089d038301820174037c0264096602190064006b09728874037c0264096602190074046b057398740064077c029b00640a9d038301820174056a067d0374076a087c0174056a097c03640b8d03010074037c02640c660219006a0a7c037c007c01640d9c0383010100740b7c03190064006b0872de6700740b7c033c007c00740b7c0319006b0772f8740b7c0319006a0a7c028301010064005300290e4ee906000000720d0000007a265468652062657474696e67206e756d626572206d757374206265206265747765656e20317e367a094174206c65617374207a132054617573206172652072657175697265642eda09737461727454696d657a07526f756e6420237a0c206e6f742073746172746564da07656e6454696d657a0a2068617320656e6465642903721f000000da02746fda0c6d61696e5f6163636f756e74da07626574496e666f2903da056275796572da066e756d626572721f000000290c721c0000007212000000721b000000da085f5f726f756e6473da036e6f7772100000007211000000da0863757272656e6379da0d7472616e736665725f66726f6dda0474686973da06617070656e64da0d5f5f757365725f726f756e64732904721e000000721f00000072200000007211000000721700000072170000007218000000da036275791e000000731e000000000218010e01120112011e0112011e01060112010e010c010c0108010c017230000000630000000000000000020000000200000043000000736800000074006a01830074026a036b027316740464018301820174056a0183007d0074066a018300733c74077c008301010074066a086402830101006400530074097c0083010100740a830001007c00640317007d0174056a087c018301010074077c01830101006400530029044e7a244f6e6c79206f776e65722063616e2065786563757465207374617274206d6574686f642e54720d000000290b720e000000721b00000072100000007211000000721c00000072140000007215000000da0d5f5f73746172745f726f756e64720f000000da0b5f5f656e645f726f756e64da135f5f63616c63756c6174655f726577617264732902da0763757272656e74da0a6e6578745f726f756e64721700000072170000007218000000da0372756e310000007316000000000216010801080108010a0104010801060108010a0172360000002901722000000063010000000000000008000000050000004300000073e200000074006a017d0174027c0064016602190064026b027326740364037c009b0064049d038301820174027c007c016405660319000c00733e740364068301820174027c006407660219007d0264087d03783e74027c0064096602190044005d2e7d047c046a04640a83017c016b02725c7c046a04640b83017c026b02725c7c037c046a04640c830117007d03715c57007c0364086b04739e7403640d8301820174027c00640e660219007d0574027c00640f660219007d067c037c051b007c0614007d07641074027c007c01640566033c0074056a067c077c0164118d0201006400530029124eda06737461747573da05456e6465647a15436c61696d206661696c65642c20726f756e6420237a0a206e6f7420656e646564da07636c61696d65647a1f596f75206861766520636c61696d656420666f72207468697320726f756e64da0977696e4e756d6265727201000000722600000072270000007228000000721f0000007a164e6f7420656c696769626c6520666f7220636c61696dda0f77696e6e6572426574416d6f756e74da0b746f74616c416d6f756e74542902721f00000072240000002907721000000072110000007229000000721c000000721b000000722b000000da087472616e73666572290872200000007211000000da0e77696e6e696e675f6e756d626572da0a6265745f616d6f756e74da0576616c7565da1177696e6e65725f6265745f616d6f756e74da0c746f74616c5f616d6f756e74da0772657761726473721700000072170000007218000000da05636c61696d400000007322000000000206010a011601120106010c010401120116010601120110010c010c010c010e017244000000630100000000000000020000000400000043000000734800000074007d017c0174017c00640166023c007c0174026a0374046a05830064028d01170074017c00640366023c00640474017c00640566023c00670074017c00640666023c006400530029074e72220000002901da077365636f6e64737223000000da085374617274696e67723700000072260000002906722a0000007229000000da086461746574696d65da0974696d6564656c74617213000000721b00000029027220000000da086e6f775f74696d65721700000072170000007218000000723100000055000000730c000000000104010c01060116010c017231000000630100000000000000010000000400000043000000734000000074007c0064016602190064006b09722074007c0064016602190074016b007330740264027c009b0064039d0383018201640474007c00640566023c006400530029064e72230000007a07526f756e6420237a0a206e6f7420656e6465647238000000723700000029037229000000722a000000721c0000002901722000000072170000007217000000721800000072320000005e0000007306000000000112011e01723200000063000000000000000006000000040000004300000073c600000074006a0183007d0074027c006401660219007d0174036a048300010074036a056402640383027d0264047d0364047d0478367c0144005d2e7d057c027c056a01640583016b0272567c037c056a016406830117007d037c047c056a016406830117007d04713657007c0374027c00640766023c007c0474027c00640866023c007c047406170074027c00640966023c007c0274027c00640a66023c007c0364046b0472b074066a076404830101006e1274066a0774066a0183007c0417008301010064005300290b4e7226000000720d000000e90700000072010000007228000000721f000000723b000000723c000000da0b746f74616c417761726473723a00000029087214000000721b0000007229000000da0672616e646f6dda0473656564da0772616e64696e747216000000720f00000029067234000000da07626574696e666f723e0000007241000000724200000072400000007217000000721700000072180000007233000000640000007322000000000108010c0108010c01040104010a010e010e0112010c010c0110010c0108010c0272330000002917722b000000da085661726961626c65720e00000072140000007212000000721300000072150000007216000000da04486173687229000000722f0000007219000000da085f5f6578706f7274da03737472721d000000da03696e74da05666c6f61747230000000723600000072440000007231000000723200000072330000007217000000721700000072170000007218000000da083c6d6f64756c653e01000000732a00000008010c010c010c0104010801040108010c010c010c0308090601100606011412100f060110140e090e06