Contract con_neb_key_tst02


Contract Code

1 import con_nebula as neb
2
3 staking = Hash(default_value=None)
4 balances = Hash(default_value=0)
5 metadata = Hash()
6
7 active = Variable()
8 total_supply = Variable()
9
10 stake_tax = Variable()
11 stake_amount = Variable()
12 stake_start_date = Variable()
13 stake_start_period = Variable()
14 stake_period = Variable()
15
16 OPERATORS = [
17 'ae7d14d6d9b8443f881ba6244727b69b681010e782d4fe482dbfb0b6aca02d5d',
18 'e787ed5907742fa8d50b3ca2701ab8e03ec749ced806a15cdab800a127d7f863'
19 ]
20
21 @construct
22 def seed():
23 metadata['token_name'] = "Nebula TKEY"
24 metadata['token_symbol'] = "TKEY"
25 metadata['operator'] = ctx.caller
26
27 stake_tax.set(1)
28 stake_amount.set(100)
29 stake_start_date.set(now)
30 stake_start_period.set(4)
31 stake_period.set(21)
32
33 active.set(False)
34 total_supply.set(0)
35
36 @export
37 def change_metadata(key: str, value: Any):
38 assert_owner()
39
40 metadata[key] = value
41
42 @export
43 def transfer(amount: float, to: str):
44 assert amount > 0, 'Cannot send negative balances!'
45 assert balances[ctx.caller] >= amount, 'Not enough coins to send!'
46 assert isinstance(amount, int), 'Amount must be an Integer!'
47
48 balances[ctx.caller] -= amount
49 balances[to] += amount
50
51 @export
52 def approve(amount: float, to: str):
53 assert amount > 0, 'Cannot send negative balances!'
54 balances[ctx.caller, to] += amount
55
56 @export
57 def transfer_from(amount: float, to: str, main_account: str):
58 assert amount > 0, 'Cannot send negative balances!'
59 assert balances[main_account, ctx.caller] >= amount, 'Not enough coins approved to send! You have {} and are trying to spend {}'\
60 .format(balances[main_account, ctx.caller], amount)
61 assert balances[main_account] >= amount, 'Not enough coins to send!'
62 assert isinstance(amount, int), 'Amount must be an Integer!'
63
64 balances[main_account, ctx.caller] -= amount
65 balances[main_account] -= amount
66 balances[to] += amount
67
68 @export
69 def stake():
70 assert active.get() == True, 'Contract inactive!'
71 assert staking[ctx.caller] == None, 'Address is already staking!'
72 assert now > stake_start_date.get(), 'Staking not started yet!'
73 assert now < stake_start_date.get() + datetime.timedelta(minutes=stake_period.get()), 'Staking period ended!'
74
75 neb.transfer_from(amount=stake_amount.get(), to=ctx.this, main_account=ctx.caller)
76
77 staking[ctx.caller] = now
78
79 @export
80 def unstake():
81 assert active.get() == True, 'Contract inactive!'
82 assert staking[ctx.caller] != None, 'Address is not staking!'
83
84 total_stake_time = stake_start_period.get() + stake_period.get()
85
86 if now < (stake_start_date.get() + datetime.timedelta(minutes=total_stake_time)):
87 # Calculate early unstake tax and payout amount
88 tax = int(stake_amount.get() / 100 * stake_tax.get())
89 payout = stake_amount.get() - tax
90
91 # Pay NEB back to user (minus tax for early unstake)
92 neb.transfer(amount=payout, to=ctx.caller)
93
94 # Retrieve vault contract
95 vault = ForeignVariable(foreign_contract='con_nebula', foreign_name='vault_contract')
96
97 # If no vault contract set, use internal vault
98 if not vault.get():
99 vault = Variable()
100 vault.set('INTERNAL_NEB_VAULT')
101
102 # Pay tax to vault
103 neb.transfer(amount=tax, to=vault.get())
104
105 # Reset staking date
106 staking[ctx.caller] = None
107
108 return 'Unstaked early. No KEY token minted. Payed back {} NEB'.format(int(payout))
109
110 else:
111 # Pay NEB back to user
112 neb.transfer(amount=stake_amount.get(), to=ctx.caller)
113
114 # Mint KEY token for user
115 balances[ctx.caller] += 1
116
117 # Add newly minted KEY to total supply
118 total_supply.set(total_supply.get() + 1)
119
120 # Reset staking date
121 staking[ctx.caller] = None
122
123 return 'Unstaked and minted 1 KEY token. Payed back {} NEB'.format(int(stake_amount.get()))
124
125 @export
126 def start():
127 assert_owner()
128 stake_start_date.set(now)
129 staking = Hash(default_value=None)
130
131 @export
132 def set_stake_start_period(minutes: int):
133 assert_owner()
134 stake_start_period.set(minutes)
135
136 @export
137 def set_stake_period(minutes: int):
138 assert_owner()
139 stake_period.set(minutes)
140
141 @export
142 def set_stake_amount(amount: float):
143 assert_owner()
144 assert amount > 0, 'Cannot set negative amount!'
145 stake_amount.set(amount)
146
147 @export
148 def set_stake_tax(percent: float):
149 assert_owner()
150 assert percent > 0 and percent < 100, 'Wrong tax value!'
151 stake_tax.set(percent)
152
153 # FIXME: TypeError("<class 'NoneType'> is not a Datetime!",)
154 @export
155 def time_until_unstake():
156 assert staking[ctx.caller] != None, 'Address is not staking!'
157 total_stake_time = stake_start_period.get() + stake_period.get()
158 return (stake_start_date.get() + datetime.timedelta(minutes=total_stake_time)) - now
159
160 @export
161 def enable():
162 assert_owner()
163 active.set(True)
164
165 @export
166 def disable():
167 assert_owner()
168 active.set(False)
169
170 @export
171 def emergency_withdraw(amount: float):
172 assert_owner()
173 neb.transfer(amount, ctx.caller)
174
175 @export
176 def total_supply():
177 return int(total_supply.get())
178
179 def assert_owner():
180 assert ctx.caller in OPERATORS, 'Only executable by operators!'
181

Byte Code

e3000000000000000000000000050000004000000073e0010000640064016c005a01650264016402640364048d035a03650264006402640564048d035a0465026402640664078d025a0565066402640864078d025a0765066402640964078d025a0865066402640a64078d025a0965066402640b64078d025a0a65066402640c64078d025a0b65066402640d64078d025a0c65066402640e64078d025a0d640f641067025a0e6411641284005a0f6510640283016511651264139c0264146415840483015a136510640283016514651164169c0264176418840483015a156510640283016514651164169c026419641a840483015a16651064028301651465116511641b9c03641c641d840483015a17651064028301641e641f840083015a1865106402830164206421840083015a1965106402830164226423840083015a1a651064028301651b64249c0164256426840483015a1c651064028301651b64249c0164276428840483015a1d651064028301651464299c01642a642b840483015a1e6510640283016514642c9c01642d642e840483015a1f651064028301642f6430840083015a2065106402830164316432840083015a2165106402830164336434840083015a22651064028301651464299c0164356436840483015a2365106402830164376409840083015a246438643984005a2564015300293ae9000000004eda11636f6e5f6e65625f6b65795f7473743032da077374616b696e672903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d65da0862616c616e636573da086d65746164617461290272050000007206000000da06616374697665da0c746f74616c5f737570706c79da097374616b655f746178da0c7374616b655f616d6f756e74da107374616b655f73746172745f64617465da127374616b655f73746172745f706572696f64da0c7374616b655f706572696f64da4061653764313464366439623834343366383831626136323434373237623639623638313031306537383264346665343832646266623062366163613032643564da406537383765643539303737343266613864353062336361323730316162386530336563373439636564383036613135636461623830306131323764376638363363000000000000000000000000030000004300000073640000006401740064023c006403740064043c0074016a02740064053c0074036a0464068301010074056a0464078301010074066a0474078301010074086a0464088301010074096a04640983010100740a6a04640a83010100740b6a04640b8301010064005300290c4e7a0b4e6562756c6120544b4559da0a746f6b656e5f6e616d65da04544b4559da0c746f6b656e5f73796d626f6cda086f70657261746f72e901000000e964000000e904000000e915000000467201000000290cda0a5f5f6d65746164617461da03637478da0663616c6c6572da0b5f5f7374616b655f746178da03736574da0e5f5f7374616b655f616d6f756e74da125f5f7374616b655f73746172745f64617465da036e6f77da145f5f7374616b655f73746172745f706572696f64da0e5f5f7374616b655f706572696f64da085f5f616374697665da0e5f5f746f74616c5f737570706c79a90072260000007226000000da00da045f5f5f5f1400000073140000000001080108010a010a010a010a010a010a010a0172280000002902da036b6579da0576616c756563020000000000000002000000030000004300000073120000007400830001007c0174017c003c006400530029014e2902da0e5f5f6173736572745f6f776e6572721a00000029027229000000722a000000722600000072260000007227000000da0f6368616e67655f6d6574616461746121000000730400000000020601722c0000002902da06616d6f756e74da02746f630200000000000000020000000400000043000000735e0000007c0064016b0473107400640283018201740174026a0319007c006b057326740064038301820174047c007405830273387400640483018201740174026a03050019007c00380003003c0074017c01050019007c00370003003c006400530029054e72010000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a194e6f7420656e6f75676820636f696e7320746f2073656e64217a1a416d6f756e74206d75737420626520616e20496e7465676572212906da0e417373657274696f6e4572726f72da0a5f5f62616c616e636573721b000000721c000000da0a6973696e7374616e6365da03696e742902722d000000722e000000722600000072260000007227000000da087472616e7366657227000000730a000000000210011601120112017233000000630200000000000000020000000400000043000000732a0000007c0064016b0473107400640283018201740174026a037c016602050019007c00370003003c006400530029034e72010000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573212904722f0000007230000000721b000000721c0000002902722d000000722e000000722600000072260000007227000000da07617070726f76653000000073040000000002100172340000002903722d000000722e000000da0c6d61696e5f6163636f756e74630300000000000000030000000500000043000000739c0000007c0064016b047310740064028301820174017c0274026a03660219007c006b05733c740064036a0474017c0274026a03660219007c0083028301820174017c0219007c006b057350740064048301820174057c00740683027362740064058301820174017c0274026a036602050019007c00380003003c0074017c02050019007c00380003003c0074017c01050019007c00370003003c006400530029064e72010000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a494e6f7420656e6f75676820636f696e7320617070726f76656420746f2073656e642120596f752068617665207b7d20616e642061726520747279696e6720746f207370656e64207b7d7a194e6f7420656e6f75676820636f696e7320746f2073656e64217a1a416d6f756e74206d75737420626520616e20496e7465676572212907722f0000007230000000721b000000721c000000da06666f726d6174723100000072320000002903722d000000722e0000007235000000722600000072260000007227000000da0d7472616e736665725f66726f6d360000007312000000000210010c010c01140114011201160110017237000000630000000000000000000000000500000043000000738800000074006a01830064016b0273147402640283018201740374046a05190064006b02732a7402640383018201740674076a0183006b04733e7402640483018201740674076a01830074086a09740a6a01830064058d0117006b0073627402640683018201740b6a0c740d6a01830074046a0e74046a0564078d0301007406740374046a053c006400530029084e547a12436f6e747261637420696e616374697665217a1b4164647265737320697320616c7265616479207374616b696e67217a185374616b696e67206e6f74207374617274656420796574212901da076d696e757465737a155374616b696e6720706572696f6420656e646564212903722d000000722e0000007235000000290f7224000000da03676574722f000000da095f5f7374616b696e67721b000000721c00000072210000007220000000da086461746574696d65da0974696d6564656c74617223000000da036e65627237000000721f000000da04746869737226000000722600000072260000007227000000da057374616b6543000000731000000000021401160114010c0118010e010a01723f000000630000000000000000040000000600000043000000733601000074006a01830064016b0273147402640283018201740374046a05190064006b03732a740264038301820174066a01830074076a01830017007d00740874096a018300740a6a0b7c0064048d0117006b0072de740c740d6a01830064051b00740e6a018300140083017d01740d6a0183007c0118007d02740f6a107c0274046a0564068d0201007411640764086409640a640b8d047d037c036a01830073b474126409640a640c8d027d037c036a13640d83010100740f6a107c017c036a01830064068d0201006400740374046a053c00640e6a14740c7c02830183015300740f6a10740d6a01830074046a0564068d020100741574046a0505001900640f370003003c0074166a1374166a018300640f1700830101006400740374046a053c0064106a14740c740d6a0183008301830153006400530029114e547a12436f6e747261637420696e616374697665217a1741646472657373206973206e6f74207374616b696e67212901723800000072170000002902722d000000722e000000da0a636f6e5f6e6562756c61da0e7661756c745f636f6e74726163747202000000da057661756c742904da10666f726569676e5f636f6e7472616374da0c666f726569676e5f6e616d6572050000007206000000290272050000007206000000da12494e5445524e414c5f4e45425f5641554c547a36556e7374616b6564206561726c792e204e6f204b455920746f6b656e206d696e7465642e205061796564206261636b207b7d204e454272160000007a32556e7374616b656420616e64206d696e7465642031204b455920746f6b656e2e205061796564206261636b207b7d204e4542291772240000007239000000722f000000723a000000721b000000721c0000007222000000722300000072210000007220000000723b000000723c0000007232000000721f000000721d000000723d0000007233000000da0f466f726569676e5661726961626c65da085661726961626c65721e0000007236000000723000000072250000002904da10746f74616c5f7374616b655f74696d65da03746178da067061796f7574da075f5f7661756c74722600000072260000007227000000da07756e7374616b654f000000733000000000021401160110010c010c0118010c01100104010401080108010c010a0112010a0104010a021401120112010a010601724c000000630000000000000000010000000500000043000000732200000074008300010074016a02740383010100740464006401640264038d037d006400530029044e7202000000720300000029037204000000720500000072060000002905722b0000007220000000721e0000007221000000da04486173682901723a000000722600000072260000007227000000da0573746172746c0000007308000000000206010a010601724e00000029017238000000630100000000000000010000000200000043000000731400000074008300010074016a027c00830101006400530029014e2903722b0000007222000000721e00000029017238000000722600000072260000007227000000da167365745f7374616b655f73746172745f706572696f6474000000730400000000020601724f000000630100000000000000010000000200000043000000731400000074008300010074016a027c00830101006400530029014e2903722b0000007223000000721e00000029017238000000722600000072260000007227000000da107365745f7374616b655f706572696f647a00000073040000000002060172500000002901722d00000063010000000000000001000000020000004300000073240000007400830001007c0064016b047316740164028301820174026a037c00830101006400530029034e72010000007a1b43616e6e6f7420736574206e6567617469766520616d6f756e74212904722b000000722f000000721f000000721e0000002901722d000000722600000072260000007227000000da107365745f7374616b655f616d6f756e7480000000730600000000020601100172510000002901da0770657263656e74630100000000000000010000000200000043000000732c0000007400830001007c0064016b0472167c0064026b00731e740164038301820174026a037c00830101006400530029044e720100000072170000007a1057726f6e67207461782076616c7565212904722b000000722f000000721d000000721e00000029017252000000722600000072260000007227000000da0d7365745f7374616b655f7461788700000073060000000002060118017253000000630000000000000000010000000400000043000000733e000000740074016a02190064006b037316740364018301820174046a05830074066a05830017007d0074076a05830074086a097c0064028d011700740a1800530029034e7a1741646472657373206973206e6f74207374616b696e672129017238000000290b723a000000721b000000721c000000722f0000007222000000723900000072230000007220000000723b000000723c000000722100000029017248000000722600000072260000007227000000da1274696d655f756e74696c5f756e7374616b658e00000073060000000002160110027254000000630000000000000000000000000200000043000000731400000074008300010074016a026401830101006400530029024e542903722b0000007224000000721e0000007226000000722600000072260000007227000000da06656e61626c65960000007304000000000206017255000000630000000000000000000000000200000043000000731400000074008300010074016a026401830101006400530029024e462903722b0000007224000000721e0000007226000000722600000072260000007227000000da0764697361626c659c0000007304000000000206017256000000630100000000000000010000000300000043000000731800000074008300010074016a027c0074036a04830201006400530029014e2905722b000000723d0000007233000000721b000000721c0000002901722d000000722600000072260000007227000000da12656d657267656e63795f7769746864726177a20000007304000000000206017257000000630000000000000000000000000200000043000000730c000000740074016a0283008301530029014e29037232000000722500000072390000007226000000722600000072260000007227000000720a000000a800000073020000000002630000000000000000000000000200000043000000731600000074006a0174026b06731274036401830182016400530029024e7a1d4f6e6c792065786563757461626c65206279206f70657261746f7273212904721b000000721c000000da094f50455241544f5253722f0000007226000000722600000072260000007227000000722b000000ad00000073020000000001722b00000029267240000000723d000000724d000000723a0000007230000000721a000000724700000072240000007225000000721d000000721f00000072200000007222000000722300000072580000007228000000da085f5f6578706f7274da03737472da03416e79722c000000da05666c6f6174723300000072340000007237000000723f000000724c000000724e0000007232000000724f0000007250000000725100000072530000007254000000725500000072560000007257000000720a000000722b0000007226000000722600000072260000007227000000da083c6d6f64756c653e010000007356000000080106010801060108010c010c010c010c010c0104010801040108010c0102010603080d0601120506011208060112050601140c100c101d100806011005060110050601100606011006100810061006060110051005