Contract con_puzzles_v1


Contract Code

1 import currency
2 import con_luck_lst001
3
4 puzzleInfo = Hash()
5 puzzleOwner = Hash()
6 ownersToPuzzles = Hash()
7 puzzleHash = Hash(default_value='')
8 puzzleSalts = Hash(default_value='')
9 puzzlePrizes = Hash(default_value=0)
10 puzzleImages = Hash(default_value='')
11 puzzleGuesses = Hash(default_value=0)
12 owner = Variable()
13 activePuzzles = Variable()
14 solvedPuzzles = Variable()
15
16 @construct
17 def seed():
18 owner.set(ctx.caller)
19 ap = []
20 activePuzzles.set(ap)
21 sp = []
22 solvedPuzzles.set(sp)
23
24 @export
25 def changeOwner(newOwner: str):
26 sender = ctx.caller
27 assert sender == owner.get(), 'error: only the current owner can transfer ownership'
28 owner.set(newOwner)
29
30 # @export
31 # def withdrawLuck(amount: float):
32 # sender = ctx.caller
33 # assert sender == owner.get(), 'error: only the current owner can withdraw'
34 # con_luck_lst001.transfer(amount, sender)
35
36 @export
37 def create(puzzleName: str, hint: str, solution: str, prize: int, imageData: str):
38 sender = ctx.caller
39 assert not puzzleInfo[puzzleName], 'error: puzzle name \'{}\' already taken'.format(puzzleName)
40 if(prize > 0):
41 assert currency.balance_of(sender) >= prize, "error: TAU/dTAU balance of less than prize of {}".format(prize)
42 con_luck_lst001.transfer_from(amount=10, to=ctx.this, main_account=sender)
43 currency.transfer_from(amount=prize, to=ctx.this, main_account=sender)
44 pInfo = {
45 'name': puzzleName,
46 'prize': prize,
47 'hint': hint,
48 'date': now,
49 'active': True
50 }
51 puzzleInfo[puzzleName] = pInfo
52 puzzleOwner[puzzleName] = sender
53 puzzleImages[puzzleName] = imageData
54 puzzleGuesses[puzzleName] = 0
55 random.seed()
56 salt = str(random.randint(0, 999999999))
57 puzzleHash[puzzleName] = hashlib.sha256(salt + solution)
58 puzzleSalts[puzzleName] = salt
59 puzzlePrizes[puzzleName] = prize
60 ap = activePuzzles.get()
61 ap.append(puzzleName)
62 activePuzzles.set(ap)
63 return 'puzzle created'
64
65 @export
66 def updateHint(puzzleName: str, hint: str):
67 sender = ctx.caller
68 assert sender == puzzleOwner[puzzleName], "error: only the owner can update a puzzle"
69 pInfo = puzzleInfo[puzzleName]
70 pInfo['hint'] = hint
71 puzzleInfo[puzzleName] = pInfo
72
73 @export
74 def increasePrize(puzzleName: str, increaseToPrize: int):
75 sender = ctx.caller
76 assert sender == puzzleOwner[puzzleName], "error: only the owner can update a puzzle"
77 assert increaseToPrize > 0, "error: increase to prize must be greater than 0"
78 currency.transfer_from(amount=increaseToPrize, to=ctx.this, main_account=sender)
79 prize = puzzlePrizes[puzzleName] + increaseToPrize
80 puzzlePrizes[puzzleName] = prize
81 pInfo = puzzleInfo[puzzleName]
82 pInfo['prize'] = prize
83 puzzleInfo[puzzleName] = pInfo
84
85 @export
86 def guess(puzzleName: str, guess: str):
87 ap = activePuzzles.get()
88 assert puzzleName in ap, 'error: puzzle is already solved'
89 sender = ctx.caller
90 prize = puzzlePrizes[puzzleName]
91 if(prize > 0):
92 con_luck_lst001.transfer_from(amount=1, to=puzzleOwner[puzzleName], main_account=sender)
93 g = puzzleGuesses[puzzleName]
94 g += 1
95 puzzleGuesses[puzzleName] = g
96 if(puzzleHash[puzzleName] == hashlib.sha256(puzzleSalts[puzzleName] + guess)):
97 ap.remove(puzzleName)
98 activePuzzles.set(ap)
99 prize = puzzlePrizes[puzzleName]
100 if(prize > 0):
101 currency.transfer(prize, sender)
102 puzzlePrizes[puzzleName] = 0
103 sp = solvedPuzzles.get()
104 sp.append(puzzleName)
105 solvedPuzzles.set(sp)
106 pInfo = puzzleInfo[puzzleName]
107 pInfo['active'] = False
108 pInfo['winner'] = sender
109 pInfo['solvedDate'] = now
110 pInfo['solution'] = guess
111 puzzleInfo[puzzleName] = pInfo
112 return 'You guessed correctly!'
113 else:
114 return 'You guessed incorrectly...'
115
116 @export
117 def violation(puzzleName: str):
118 sender = ctx.caller
119 assert sender == owner.get(), 'error: only the current owner can withdraw'
120 ap = activePuzzles.get()
121 ap.remove(puzzleName)
122 activePuzzles.set(ap)
123 puzzleImages[puzzleName] = ''
124 pInfo = puzzleInfo[puzzleName]
125 pInfo['active'] = False
126 pInfo['hint'] = ''
127 pInfo['name'] = 'cancelled due to volation'
128 puzzleInfo[puzzleName] = pInfo
129 prize = puzzlePrizes[puzzleName]
130 if(prize > 0):
131 currency.transfer(prize, sender)
132 puzzlePrizes[puzzleName] = 0
133

Byte Code

e30000000000000000000000000700000040000000733c010000640064016c005a00640064016c015a0165026402640364048d025a0365026402640564048d025a0465026402640664048d025a05650264076402640864098d035a06650264076402640a64098d035a07650264006402640b64098d035a08650264076402640c64098d035a09650264006402640d64098d035a0a650b6402640e64048d025a0c650b6402640f64048d025a0d650b6402641064048d025a0e6411641284005a0f651064028301651164139c0164146415840483015a126510640283016511651165116513651164169c0564176418840483015a146510640283016511651164199c02641a641b840483015a1565106402830165116513641c9c02641d641e840483015a1665106402830165116511641f9c0264206421840483015a17651064028301651164229c0164236424840483015a18640153002925e9000000004eda0e636f6e5f70757a7a6c65735f7631da0a70757a7a6c65496e666f2902da08636f6e7472616374da046e616d65da0b70757a7a6c654f776e6572da0f6f776e657273546f50757a7a6c6573da00da0a70757a7a6c65486173682903da0d64656661756c745f76616c756572040000007205000000da0b70757a7a6c6553616c7473da0c70757a7a6c655072697a6573da0c70757a7a6c65496d61676573da0d70757a7a6c6547756573736573da056f776e6572da0d61637469766550757a7a6c6573da0d736f6c76656450757a7a6c6573630000000000000000020000000200000043000000732c00000074006a0174026a038301010067007d0074046a017c008301010067007d0174056a017c01830101006400530029014e2906da075f5f6f776e6572da03736574da03637478da0663616c6c6572da0f5f5f61637469766550757a7a6c6573da0f5f5f736f6c76656450757a7a6c65732902da026170da027370a900721a0000007208000000da045f5f5f5f15000000730a00000000010c0104010a010401721b0000002901da086e65774f776e6572630100000000000000020000000200000043000000732800000074006a017d017c0174026a0383006b02731a740464018301820174026a057c00830101006400530029024e7a346572726f723a206f6e6c79207468652063757272656e74206f776e65722063616e207472616e73666572206f776e6572736869702906721400000072150000007212000000da03676574da0e417373657274696f6e4572726f7272130000002902721c000000da0673656e646572721a000000721a0000007208000000da0b6368616e67654f776e65721d0000007308000000000206010e01060172200000002905da0a70757a7a6c654e616d65da0468696e74da08736f6c7574696f6eda057072697a65da09696d6167654461746163050000000000000009000000060000004300000073f000000074006a017d0574027c0019000c00731e740364016a047c008301830182017c0364026b04726674056a067c0583017c036b057342740364036a047c0383018301820174076a08640474006a097c0564058d03010074056a087c0374006a097c0564058d0301007c007c037c01740a640664079c057d067c0674027c003c007c05740b7c003c007c04740c7c003c006402740d7c003c00740e6a0f830001007410740e6a1164026408830283017d0774126a137c077c021700830174147c003c007c0774157c003c007c0374167c003c0074176a1883007d087c086a197c008301010074176a1a7c088301010064095300290a4e7a256572726f723a2070757a7a6c65206e616d6520277b7d2720616c72656164792074616b656e72010000007a306572726f723a205441552f645441552062616c616e6365206f66206c657373207468616e207072697a65206f66207b7de90a0000002903da06616d6f756e74da02746fda0c6d61696e5f6163636f756e74542905720500000072240000007222000000da0464617465da0661637469766569ffc99a3b7a0e70757a7a6c652063726561746564291b72140000007215000000da0c5f5f70757a7a6c65496e666f721e000000da06666f726d6174da0863757272656e6379da0a62616c616e63655f6f66da0f636f6e5f6c75636b5f6c7374303031da0d7472616e736665725f66726f6dda0474686973da036e6f77da0d5f5f70757a7a6c654f776e6572da0e5f5f70757a7a6c65496d61676573da0f5f5f70757a7a6c6547756573736573da0672616e646f6dda0473656564da03737472da0772616e64696e74da07686173686c6962da06736861323536da0c5f5f70757a7a6c6548617368da0d5f5f70757a7a6c6553616c7473da0e5f5f70757a7a6c655072697a65737216000000721d000000da06617070656e647213000000290972210000007222000000722300000072240000007225000000721f000000da0570496e666fda0473616c747218000000721a000000721a0000007208000000da06637265617465250000007332000000000306010c010c01080108010c0108010a01080112010801080108010801080108010801100112010801080108010a010a017243000000290272210000007222000000630200000000000000040000000300000043000000733600000074006a017d027c0274027c0019006b02731a740364018301820174047c0019007d037c017c0364023c007c0374047c003c006400530029034e7a296572726f723a206f6e6c7920746865206f776e65722063616e2075706461746520612070757a7a6c6572220000002905721400000072150000007234000000721e000000722c000000290472210000007222000000721f0000007241000000721a000000721a0000007208000000da0a75706461746548696e7443000000730c000000000206010e01060108010801724400000029027221000000da0f696e637265617365546f5072697a65630200000000000000050000000500000043000000736c00000074006a017d027c0274027c0019006b02731a74036401830182017c0164026b04732a740364038301820174046a057c0174006a067c0264048d03010074077c0019007c0117007d037c0374077c003c0074087c0019007d047c037c0464053c007c0474087c003c006400530029064e7a296572726f723a206f6e6c7920746865206f776e65722063616e2075706461746520612070757a7a6c6572010000007a2f6572726f723a20696e63726561736520746f207072697a65206d7573742062652067726561746572207468616e2030290372270000007228000000722900000072240000002909721400000072150000007234000000721e000000722e00000072310000007232000000723f000000722c000000290572210000007245000000721f00000072240000007241000000721a000000721a0000007208000000da0d696e6372656173655072697a654d0000007316000000000206010e01060110010a0108010c01080108010801724600000029027221000000da056775657373630200000000000000080000000500000043000000730401000074006a0183007d027c007c026b067318740264018301820174036a047d0374057c0019007d047c0464026b04724274066a07640374087c0019007c0364048d03010074097c0019007d057c05640337007d057c0574097c003c00740a7c001900740b6a0c740d7c0019007c01170083016b0272fc7c026a0e7c008301010074006a0f7c028301010074057c0019007d047c0464026b0472ac74106a117c047c0383020100640274057c003c0074126a0183007d067c066a137c008301010074126a0f7c068301010074147c0019007d0764057c0764063c007c037c0764073c0074157c0764083c007c017c0764093c007c0774147c003c00640a5300640b530064005300290c4e7a1f6572726f723a2070757a7a6c6520697320616c726561647920736f6c7665647201000000e901000000290372270000007228000000722900000046722b000000da0677696e6e6572da0a736f6c7665644461746572230000007a16596f75206775657373656420636f72726563746c79217a1a596f75206775657373656420696e636f72726563746c792e2e2e29167216000000721d000000721e00000072140000007215000000723f0000007230000000723100000072340000007236000000723d000000723b000000723c000000723e000000da0672656d6f76657213000000722e000000da087472616e7366657272170000007240000000722c00000072330000002908722100000072470000007218000000721f0000007224000000da016772190000007241000000721a000000721a000000720800000072470000005c000000733a0000000002080110010601080108010c01080108010801080110010a010a010a01080108010c01080108010a010a010801080108010801080108010402724700000029017221000000630100000000000000050000000300000043000000738e00000074006a017d017c0174026a0383006b02731a740464018301820174056a0383007d027c026a067c008301010074056a077c0283010100640274087c003c0074097c0019007d0364037c0364043c0064027c0364053c0064067c0364073c007c0374097c003c00740a7c0019007d047c0464086b04728a740b6a0c7c047c01830201006408740a7c003c006400530029094e7a2a6572726f723a206f6e6c79207468652063757272656e74206f776e65722063616e207769746864726177720800000046722b00000072220000007a1963616e63656c6c65642064756520746f20766f6c6174696f6e72050000007201000000290d721400000072150000007212000000721d000000721e0000007216000000724b00000072130000007235000000722c000000723f000000722e000000724c00000029057221000000721f000000721800000072410000007224000000721a000000721a0000007208000000da0976696f6c6174696f6e7e0000007320000000000206010e01060108010a010a01080108010801080108010801080108010c01724e0000002919722e0000007230000000da0448617368722c0000007234000000da115f5f6f776e657273546f50757a7a6c6573723d000000723e000000723f00000072350000007236000000da085661726961626c65721200000072160000007217000000721b000000da085f5f6578706f727472390000007220000000da03696e747243000000724400000072460000007247000000724e000000721a000000721a000000721a0000007208000000da083c6d6f64756c653e01000000733e000000080108010c010c010c0106010801060108010601080106010801060108010c010c010c0308080601100706010801101c060112090601120e060112210601