Contract con_rubix_test_decimal_stake


Contract Code

1 tad_contract = importlib.import_module('con_rubix_test_decimal_tad')
2 vault_contract = importlib.import_module('con_rubix_test_decimal_vault')
3
4 rate = Hash()
5
6 balances = Hash(default_value=0)
7 metadata = Hash()
8
9 total_minted = Variable()
10 operator = Variable()
11
12 # 31536000 seconds per year
13
14
15 @construct
16 def seed():
17 operator.set(ctx.caller)
18
19 rate['start_time'] = get_timestamp()
20 rate['rate'] = 1.0000000015469297 # interest per second
21 rate['start_price'] = 1
22
23 metadata['token_name'] = 'Staked tad'
24 metadata['token_symbol'] = 'stad'
25 metadata['token_logo_url'] = 'image.site'
26 metadata['operator'] = ctx.caller
27 total_minted.set(0)
28
29
30 @export
31 def get_timestamp():
32 # https://developers.lamden.io/docs/smart-contracts/datetime-module/
33 td = now - datetime.datetime(1970, 1, 1, 0, 0, 0)
34 return td.seconds
35
36
37 @export
38 def stake(amount: float):
39 assert amount > 0, 'Stake amount must be positive!'
40 tad_contract.transfer_from(
41 to=ctx.this, amount=amount, main_account=ctx.caller)
42
43 amount_minted = amount / get_price()
44 total = total_minted.get() + amount_minted
45 total_minted.set(total)
46
47 balances[ctx.caller] += amount_minted
48
49 return amount_minted
50
51
52 @export
53 def withdraw_stake(amount: float):
54 assert amount > 0, 'Stake amount must be positive!'
55 assert balances[ctx.caller] >= amount, 'Not enough coins to withdraw!'
56
57 balances[ctx.caller] -= amount
58
59 current_price = get_price()
60 return_amount = current_price * amount
61
62 supply = total_minted.get()
63 current_average = supply / tad_contract.balance_of(ctx.this)
64 transfer_away_amount = amount * current_average
65
66 total_minted.set(supply - amount)
67 if return_amount - transfer_away_amount > 0:
68 # todo: double check in future
69 vault_contract.mint_rewards(
70 amount=return_amount - transfer_away_amount)
71
72 tad_contract.transfer(amount=return_amount, to=ctx.caller)
73
74 return return_amount
75
76
77 @export
78 def change_rate(new_rate: float): # takes yearly interest as decimal
79 assert_owner()
80 assert new_rate >= 0, 'Cannot have negative staking!'
81
82 current_price = get_price()
83
84 rate['start_time'] = get_timestamp()
85 rate['rate'] = new_rate # interest per second
86 rate['start_price'] = current_price
87
88
89 @export
90 def transfer(amount: float, to: str):
91 assert amount > 0, 'Cannot send negative balances!'
92
93 sender = ctx.caller
94
95 assert balances[sender] >= amount, 'Not enough coins to send!'
96
97 balances[sender] -= amount
98 balances[to] += amount
99
100
101 @export
102 def balance_of(account: str):
103 return balances[account]
104
105
106 @export
107 def allowance(owner: str, spender: str):
108 return balances[owner, spender]
109
110
111 @export
112 def approve(amount: float, to: str):
113 assert amount > 0, 'Cannot send negative balances!'
114 sender = ctx.caller
115 assert balances[sender] >= amount, 'Cannot approve balance that exceeds total balance!'
116 balances[sender, to] += amount
117 return balances[sender, to]
118
119
120 @export
121 def transfer_from(amount: float, to: str, main_account: str):
122 assert amount > 0, 'Cannot send negative balances!'
123
124 sender = ctx.caller
125
126 assert balances[main_account] >= amount, 'Not enough coins to send!'
127 assert balances[main_account, sender] >= amount, 'Not enough coins approved to send! You have {} and are trying to spend {}'\
128 .format(balances[main_account, sender], amount)
129
130 balances[main_account, sender] -= amount
131 balances[main_account] -= amount
132
133 balances[to] += amount
134
135
136 @export
137 def get_price():
138 return rate['start_price'] * rate['rate'] ** (
139 get_timestamp() - rate['start_time'])
140
141
142 @export
143 def change_metadata(key: str, value: Any):
144 assert ctx.caller == metadata['operator'], 'Only operator can set metadata!'
145 metadata[key] = value
146
147
148 @export
149 def change_owner(new_owner: str):
150 assert_owner()
151 operator.set(new_owner)
152
153
154 def assert_owner():
155 assert ctx.caller == operator.get(), 'Only operator can call!'
156

Byte Code

e30000000000000000000000000500000040000000736e01000065006a01640083015a0265006a01640183015a0365046402640364048d025a05650464056402640664078d035a0665046402640864048d025a0765086402640964048d025a0965086402640a64048d025a0a640b640c84005a0b650c64028301640d640e840083015a0d650c64028301650e640f9c0164106411840483015a0f650c64028301650e640f9c0164126413840483015a10650c64028301650e64149c0164156416840483015a11650c64028301650e651264179c0264186419840483015a13650c640283016512641a9c01641b641c840483015a14650c6402830165126512641d9c02641e641f840483015a15650c64028301650e651264179c0264206421840483015a16650c64028301650e6512651264229c0364236424840483015a17650c6402830164256426840083015a18650c640283016512651964279c0264286429840483015a1a650c640283016512642a9c01642b642c840483015a1b642d642e84005a1c642f53002930da1a636f6e5f72756269785f746573745f646563696d616c5f746164da1c636f6e5f72756269785f746573745f646563696d616c5f7661756c74da1c636f6e5f72756269785f746573745f646563696d616c5f7374616b65da04726174652902da08636f6e7472616374da046e616d65e900000000da0862616c616e6365732903da0d64656661756c745f76616c756572050000007206000000da086d65746164617461da0c746f74616c5f6d696e746564da086f70657261746f72630000000000000000000000000300000043000000735a00000074006a0174026a038301010074048300740564013c00740664028301740564033c006404740564053c006406740764073c006408740764093c00640a7407640b3c0074026a037407640c3c0074086a01640d8301010064005300290e4eda0a73746172745f74696d657a12312e303030303030303031353436393239377204000000e901000000da0b73746172745f70726963657a0a5374616b656420746164da0a746f6b656e5f6e616d65da0473746164da0c746f6b656e5f73796d626f6c7a0a696d6167652e73697465da0e746f6b656e5f6c6f676f5f75726c720c00000072070000002909da0a5f5f6f70657261746f72da03736574da03637478da0663616c6c6572da0d6765745f74696d657374616d70da065f5f72617465da07646563696d616cda0a5f5f6d65746164617461da0e5f5f746f74616c5f6d696e746564a900721d000000721d000000da00da045f5f5f5f0c000000731200000000010c010a010c0108010801080108010a01721f000000630000000000000000010000000800000043000000731e000000740074016a01640164026402640364036403830618007d007c006a02530029044e69b2070000720e00000072070000002903da036e6f77da086461746574696d65da077365636f6e64732901da027464721d000000721d000000721e00000072180000001800000073040000000002180172180000002901da06616d6f756e74630100000000000000030000000500000043000000735a0000007c0064016b047310740064028301820174016a0274036a047c0074036a0564038d0301007c00740683001b007d0174076a0883007c0117007d0274076a097c0283010100740a74036a05050019007c01370003003c007c01530029044e72070000007a1e5374616b6520616d6f756e74206d75737420626520706f736974697665212903da02746f7224000000da0c6d61696e5f6163636f756e74290bda0e417373657274696f6e4572726f72da0c7461645f636f6e7472616374da0d7472616e736665725f66726f6d7216000000da04746869737217000000da096765745f7072696365721c000000da036765747215000000da0a5f5f62616c616e63657329037224000000da0d616d6f756e745f6d696e746564da05746f74616c721d000000721d000000721e000000da057374616b651e000000730e0000000002100114020a010c010a011201723000000063010000000000000006000000040000004300000073a40000007c0064016b0473107400640283018201740174026a0319007c006b0573267400640383018201740174026a03050019007c00380003003c00740483007d017c017c0014007d0274056a0683007d037c0374076a0874026a0983011b007d047c007c0414007d0574056a0a7c037c001800830101007c027c05180064016b047290740b6a0c7c027c05180064048d01010074076a0d7c0274026a0364058d0201007c02530029064e72070000007a1e5374616b6520616d6f756e74206d75737420626520706f736974697665217a1d4e6f7420656e6f75676820636f696e7320746f2077697468647261772129017224000000290272240000007225000000290e7227000000722d00000072160000007217000000722b000000721c000000722c0000007228000000da0a62616c616e63655f6f66722a0000007215000000da0e7661756c745f636f6e7472616374da0c6d696e745f72657761726473da087472616e7366657229067224000000da0d63757272656e745f7072696365da0d72657475726e5f616d6f756e74da06737570706c79da0f63757272656e745f61766572616765da147472616e736665725f617761795f616d6f756e74721d000000721d000000721e000000da0e77697468647261775f7374616b652a000000731a0000000002100116011201060108010801100108010e010c0110021001723a0000002901da086e65775f72617465630100000000000000020000000300000043000000733a0000007400830001007c0064016b0573167401640283018201740283007d0174038300740464033c007c00740464043c007c01740464053c006400530029064e72070000007a1d43616e6e6f742068617665206e65676174697665207374616b696e6721720d0000007204000000720f0000002905da0e5f5f6173736572745f6f776e65727227000000722b000000721800000072190000002902723b0000007235000000721d000000721d000000721e000000da0b6368616e67655f726174653c000000730c00000000020601100106010a010801723d000000290272240000007225000000630200000000000000030000000400000043000000734e0000007c0064016b047310740064028301820174016a027d0274037c0219007c006b05732a740064038301820174037c02050019007c00380003003c0074037c01050019007c00370003003c006400530029044e72070000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a194e6f7420656e6f75676820636f696e7320746f2073656e64212904722700000072160000007217000000722d000000290372240000007225000000da0673656e646572721d000000721d000000721e000000723400000046000000730a0000000002100106011401100172340000002901da076163636f756e74630100000000000000010000000200000043000000730800000074007c001900530029014e2901722d0000002901723f000000721d000000721d000000721e00000072310000004f0000007302000000000272310000002902da056f776e6572da077370656e646572630200000000000000020000000300000043000000730c00000074007c007c0166021900530029014e2901722d000000290272400000007241000000721d000000721d000000721e000000da09616c6c6f77616e636554000000730200000000027242000000630200000000000000030000000400000043000000734a0000007c0064016b047310740064028301820174016a027d0274037c0219007c006b05732a740064038301820174037c027c016602050019007c00370003003c0074037c027c0166021900530029044e72070000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a3243616e6e6f7420617070726f76652062616c616e63652074686174206578636565647320746f74616c2062616c616e6365212904722700000072160000007217000000722d000000290372240000007225000000723e000000721d000000721d000000721e000000da07617070726f766559000000730c00000000021001060106010e01140172430000002903722400000072250000007226000000630300000000000000040000000500000043000000738a0000007c0064016b047310740064028301820174016a027d0374037c0219007c006b05732a740064038301820174037c027c03660219007c006b057352740064046a0474037c027c03660219007c0083028301820174037c027c036602050019007c00380003003c0074037c02050019007c00380003003c0074037c01050019007c00370003003c006400530029054e72070000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a194e6f7420656e6f75676820636f696e7320746f2073656e64217a494e6f7420656e6f75676820636f696e7320617070726f76656420746f2073656e642120596f752068617665207b7d20616e642061726520747279696e6720746f207370656e64207b7d2905722700000072160000007217000000722d000000da06666f726d61742904722400000072250000007226000000723e000000721d000000721d000000721e000000722900000063000000731200000000021001060114010a010c011201140110017229000000630000000000000000000000000500000043000000731e00000074006401190074006402190074018300740064031900180013001400530029044e720f0000007204000000720d000000290272190000007218000000721d000000721d000000721d000000721e000000722b00000070000000730400000000021001722b0000002902da036b6579da0576616c7565630200000000000000020000000300000043000000732200000074006a017402640119006b02731674036402830182017c0174027c003c006400530029034e720c0000007a1f4f6e6c79206f70657261746f722063616e20736574206d6574616461746121290472160000007217000000721b0000007227000000290272450000007246000000721d000000721d000000721e000000da0f6368616e67655f6d6574616461746176000000730600000000021001060172470000002901da096e65775f6f776e6572630100000000000000010000000200000043000000731400000074008300010074016a027c00830101006400530029014e2903723c0000007214000000721500000029017248000000721d000000721d000000721e000000da0c6368616e67655f6f776e65727d0000007304000000000206017249000000630000000000000000000000000200000043000000731a00000074006a0174026a0383006b02731674046401830182016400530029024e7a174f6e6c79206f70657261746f722063616e2063616c6c212905721600000072170000007214000000722c0000007227000000721d000000721d000000721d000000721e000000723c0000008300000073020000000001723c0000004e291dda09696d706f72746c6962da0d696d706f72745f6d6f64756c6572280000007232000000da04486173687219000000722d000000721b000000da085661726961626c65721c0000007214000000721f000000da085f5f6578706f72747218000000da05666c6f61747230000000723a000000723d000000da0373747272340000007231000000724200000072430000007229000000722b000000da03416e7972470000007249000000723c000000721d000000721d000000721d000000721e000000da083c6d6f64756c653e0100000073400000000a010a010c01060108010c01040108010c03080c10060601100b0601101106011009060112080601100406011204060112090601140c10060601120606011005