-
Notifications
You must be signed in to change notification settings - Fork 3
/
ongBet.py
315 lines (272 loc) · 8.18 KB
/
ongBet.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
from boa.interop.Ontology.Contract import Migrate
from boa.interop.System.Storage import GetContext, Get, Put
from boa.interop.System.Runtime import CheckWitness, GetTime, Notify
from boa.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHash, GetEntryScriptHash
from boa.interop.Ontology.Native import Invoke
from boa.interop.Ontology.Runtime import GetCurrentBlockHash
from boa.builtins import ToScriptHash, state
# the script hash of this contract
ContractAddress = GetExecutingScriptHash()
ONGAddress = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02')
# E.T account
Admin = ToScriptHash("ARiYs54Kq9T5h3QY1xTyXGb9gsJ1TUbMB7")
# skyinglyh account
# Admin = ToScriptHash("AQf4Mzu1YJrhz9f3aRkkwSm9n3qhXGSh4p")
INITIALIZED = "INIT"
TOTAL_ONG_KEY = "totalONG"
def Main(operation, args):
############### for Admin to invoke Begin ##################
if operation == "init":
return init()
if operation == "invest":
if len(args) == 1:
ongAmount = args[0]
return invest(ongAmount)
else:
return False
if operation == "withdraw":
if len(args) == 1:
ongAmount = args[0]
return withdraw(ongAmount)
else:
return False
if operation == "migrateContract":
if len(args) != 9:
return False
account = args[0]
code = args[1]
needStorage = args[2]
name = args[3]
version = args[4]
author = args[5]
email = args[6]
description = args[7]
newReversedContractHash = args[8]
return migrateContract(account, code, needStorage, name, version, author, email, description, newReversedContractHash)
############### for Admin to invoke Begin ##################
############### for players to invoke Begin ##################
if operation == "bet":
if len(args) == 3:
account = args[0]
ongAmount = args[1]
number = args[2]
return bet(account, ongAmount, number)
else:
return False
############### for players to invoke End ##################
if operation == "getTotalONG":
return getTotalONG()
return False
def init():
RequireWitness(Admin)
inited = Get(GetContext(), INITIALIZED)
if inited:
Notify(["idiot admin, you have initialized the contract"])
return False
else:
Put(GetContext(), INITIALIZED, 1)
Put(GetContext(), TOTAL_ONG_KEY, 0)
Notify(["Initialized contract successfully"])
return True
def bet(account, ongAmount, number):
"""
:param account:
:param ongAmount:
:param number:
:return:
"""
RequireWitness(account)
# to prevent hack from other contract
callerHash = GetCallingScriptHash()
entryHash = GetEntryScriptHash()
Require(callerHash == entryHash)
# make sure the contract has enough ong to pay to accont if account wins
tryPayOutToWin = _calculatePayOutToWin(ongAmount, number)
totalOngAmount = getTotalONG()
Require(totalOngAmount >= tryPayOutToWin)
Require(_transferONG(account, ContractAddress, ongAmount))
Require(number < 97)
Require(number > 1)
theNumber = _rollANumber()
payOutToWin = 0
if theNumber < number:
payOutToWin = tryPayOutToWin
Require(_transferONGFromContact(account, payOutToWin))
ongAmountToBeSub = Sub(payOutToWin, ongAmount)
Put(GetContext(), TOTAL_ONG_KEY, Sub(totalOngAmount, ongAmountToBeSub))
else:
Put(GetContext(), TOTAL_ONG_KEY, Add(totalOngAmount, ongAmount))
Notify(["bet", account, number, theNumber,ongAmount, payOutToWin])
return True
def invest(ongAmount):
RequireWitness(Admin)
Require(_transferONG(Admin, ContractAddress, ongAmount))
Put(GetContext(), TOTAL_ONG_KEY, Add(getTotalONG(), ongAmount))
Notify(["invest", ongAmount])
return True
def withdraw(ongAmount):
RequireWitness(Admin)
Require(_transferONGFromContact(Admin, ongAmount))
Put(GetContext(), TOTAL_ONG_KEY, Sub(getTotalONG(), ongAmount))
Notify(["withdraw", ongAmount])
return True
def migrateContract(code, needStorage, name, version, author, email, description, newReversedContractHash):
RequireWitness(Admin)
res = _transferONGFromContact(newReversedContractHash, getTotalONG())
Require(res)
if res == True:
res = Migrate(code, needStorage, name, version, author, email, description)
Require(res)
Notify(["Migrate Contract successfully", Admin, GetTime()])
return True
else:
Notify(["MigrateContractError", "transfer ONG to new contract error"])
return False
def getTotalONG():
return Get(GetContext(), TOTAL_ONG_KEY)
def _calculatePayOutToWin(ongAmount, number):
payOutToWin = Div(Mul(98, ongAmount), Sub(number, 1))
return payOutToWin
def _rollANumber():
blockHash = GetCurrentBlockHash()
theNumber = abs(blockHash) % 100
theNumber = Add(abs(theNumber), 1)
return theNumber
def _transferONG(fromAcct, toAcct, amount):
"""
transfer ONG
:param fromacct:
:param toacct:
:param amount:
:return:
"""
RequireWitness(fromAcct)
param = state(fromAcct, toAcct, amount)
res = Invoke(0, ONGAddress, 'transfer', [param])
if res and res == b'\x01':
return True
else:
return False
def _transferONGFromContact(toAcct, amount):
param = state(ContractAddress, toAcct, amount)
res = Invoke(0, ONGAddress, 'transfer', [param])
if res and res == b'\x01':
return True
else:
return False
"""
https://github.com/tonyclarking/python-template/blob/master/libs/Utils.py
"""
def Revert():
"""
Revert the transaction. The opcodes of this function is `09f7f6f5f4f3f2f1f000f0`,
but it will be changed to `ffffffffffffffffffffff` since opcode THROW doesn't
work, so, revert by calling unused opcode.
"""
raise Exception(0xF1F1F2F2F3F3F4F4)
"""
https://github.com/tonyclarking/python-template/blob/master/libs/SafeCheck.py
"""
def Require(condition):
"""
If condition is not satisfied, return false
:param condition: required condition
:return: True or false
"""
if not condition:
Revert()
return True
def RequireScriptHash(key):
"""
Checks the bytearray parameter is script hash or not. Script Hash
length should be equal to 20.
:param key: bytearray parameter to check script hash format.
:return: True if script hash or revert the transaction.
"""
Require(len(key) == 20)
return True
def RequireWitness(witness):
"""
Checks the transaction sender is equal to the witness. If not
satisfying, revert the transaction.
:param witness: required transaction sender
:return: True if transaction sender or revert the transaction.
"""
Require(CheckWitness(witness))
return True
"""
SafeMath
"""
def Add(a, b):
"""
Adds two numbers, throws on overflow.
"""
c = a + b
Require(c >= a)
return c
def Sub(a, b):
"""
Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
:param a: operand a
:param b: operand b
:return: a - b if a - b > 0 or revert the transaction.
"""
Require(a>=b)
return a-b
def ASub(a, b):
if a > b:
return a - b
if a < b:
return b - a
else:
return 0
def Mul(a, b):
"""
Multiplies two numbers, throws on overflow.
:param a: operand a
:param b: operand b
:return: a - b if a - b > 0 or revert the transaction.
"""
if a == 0:
return 0
c = a * b
Require(c / a == b)
return c
def Div(a, b):
"""
Integer division of two numbers, truncating the quotient.
"""
Require(b > 0)
c = a / b
return c
def Pwr(a, b):
"""
a to the power of b
:param a the base
:param b the power value
:return a^b
"""
c = 0
if a == 0:
c = 0
elif b == 0:
c = 1
else:
i = 0
c = 1
while i < b:
c = Mul(c, a)
i = i + 1
return c
def Sqrt(a):
"""
Return sqrt of a
:param a:
:return: sqrt(a)
"""
c = Div(Add(a, 1), 2)
b = a
while(c < b):
b = c
c = Div(Add(Div(a, c), c), 2)
return c