Contract con_october_stake


Contract Code


  
1 tad_contract = importlib.import_module('con_october_tad')
2 vault_contract = importlib.import_module('con_october_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 temporary_var = Variable()
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 fix_decimal(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 = fix_decimal(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 vault_contract.mint_rewards(
69 amount=return_amount - transfer_away_amount)
70
71 tad_contract.transfer(amount=return_amount, to=ctx.caller)
72
73 return return_amount
74
75
76 @export
77 def change_rate(new_rate: float): # takes yearly interest as decimal
78 assert_owner()
79 assert new_rate >= 0, 'Cannot have negative staking!'
80
81 current_price = get_price()
82
83 rate['start_time'] = get_timestamp()
84 rate['rate'] = new_rate # interest per second
85 rate['start_price'] = current_price
86
87
88 @export
89 def transfer(amount: float, to: str):
90 assert amount > 0, 'Cannot send negative balances!'
91
92 sender = ctx.caller
93
94 assert balances[sender] >= amount, 'Not enough coins to send!'
95
96 balances[sender] -= amount
97 balances[to] += amount
98
99
100 @export
101 def balance_of(account: str):
102 return balances[account]
103
104
105 @export
106 def allowance(owner: str, spender: str):
107 return balances[owner, spender]
108
109
110 @export
111 def approve(amount: float, to: str):
112 assert amount > 0, 'Cannot send negative balances!'
113 sender = ctx.caller
114 assert balances[sender] >= amount, 'Cannot approve balance that exceeds total balance!'
115 balances[sender, to] += amount
116 return balances[sender, to]
117
118
119 @export
120 def transfer_from(amount: float, to: str, main_account: str):
121 assert amount > 0, 'Cannot send negative balances!'
122
123 sender = ctx.caller
124
125 assert balances[main_account] >= amount, 'Not enough coins to send!'
126 assert balances[main_account, sender] >= amount, 'Not enough coins approved to send! You have {} and are trying to spend {}'\
127 .format(balances[main_account, sender], amount)
128
129 balances[main_account, sender] -= amount
130 balances[main_account] -= amount
131
132 balances[to] += amount
133
134
135 @export
136 def get_price():
137 return rate['start_price'] * rate['rate'] ** (
138 get_timestamp() - rate['start_time'])
139
140
141 @export
142 def change_metadata(key: str, value: Any):
143 assert ctx.caller == metadata['operator'], 'Only operator can set metadata!'
144 metadata[key] = value
145
146
147 @export
148 def change_owner(new_owner: str):
149 assert_owner()
150 operator.set(new_owner)
151
152
153 def assert_owner():
154 assert ctx.caller == operator.get(), 'Only operator can call!'
155
156
157 def fix_decimal(old_decimal: float):
158 temporary_var.set(old_decimal)
159 new_decimal = temporary_var.get()
160
161 return new_decimal
162

Byte Code

e30000000000000000000000000500000040000000738801000065006a01640083015a0265006a01640183015a0365046402640364048d025a05650464056402640664078d035a0665046402640864048d025a0765086402640964048d025a0965086402640a64048d025a0a65086402640b64048d025a0b640c640d84005a0c650d64028301640e640f840083015a0e650d64028301650f64109c0164116412840483015a10650d64028301650f64109c0164136414840483015a11650d64028301650f64159c0164166417840483015a12650d64028301650f651364189c026419641a840483015a14650d640283016513641b9c01641c641d840483015a15650d6402830165136513641e9c02641f6420840483015a16650d64028301650f651364189c0264216422840483015a17650d64028301650f6513651364239c0364246425840483015a18650d6402830164266427840083015a19650d640283016513651a64289c026429642a840483015a1b650d640283016513642b9c01642c642d840483015a1c642e642f84005a1d650f64309c016431643284045a1e643353002934da0f636f6e5f6f63746f6265725f746164da11636f6e5f6f63746f6265725f7661756c74da11636f6e5f6f63746f6265725f7374616b65da04726174652902da08636f6e7472616374da046e616d65e900000000da0862616c616e6365732903da0d64656661756c745f76616c756572050000007206000000da086d65746164617461da0c746f74616c5f6d696e746564da086f70657261746f72da0d74656d706f726172795f766172630000000000000000000000000300000043000000735a00000074006a0174026a038301010074048300740564013c00740664028301740564033c006404740564053c006406740764073c006408740764093c00640a7407640b3c0074026a037407640c3c0074086a01640d8301010064005300290e4eda0a73746172745f74696d657a12312e303030303030303031353436393239377204000000e901000000da0b73746172745f70726963657a0a5374616b656420746164da0a746f6b656e5f6e616d65da0473746164da0c746f6b656e5f73796d626f6c7a0a696d6167652e73697465da0e746f6b656e5f6c6f676f5f75726c720c00000072070000002909da0a5f5f6f70657261746f72da03736574da03637478da0663616c6c6572da0d6765745f74696d657374616d70da065f5f72617465da07646563696d616cda0a5f5f6d65746164617461da0e5f5f746f74616c5f6d696e746564a900721e000000721e000000da00da045f5f5f5f0c000000731200000000010c010a010c0108010801080108010a0172200000006300000000000000000100000008000000430000007322000000740074016a01640164026402640364036403830618007d0074027c006a038301530029044e69b2070000720f00000072070000002904da036e6f77da086461746574696d65da0d5f5f6669785f646563696d616cda077365636f6e64732901da027464721e000000721e000000721f00000072190000001800000073040000000002180172190000002901da06616d6f756e74630100000000000000030000000500000043000000735a0000007c0064016b047310740064028301820174016a0274036a047c0074036a0564038d0301007c00740683001b007d0174076a0883007c0117007d0274076a097c0283010100740a74036a05050019007c01370003003c007c01530029044e72070000007a1e5374616b6520616d6f756e74206d75737420626520706f736974697665212903da02746f7226000000da0c6d61696e5f6163636f756e74290bda0e417373657274696f6e4572726f72da0c7461645f636f6e7472616374da0d7472616e736665725f66726f6d7217000000da04746869737218000000da096765745f7072696365721d000000da036765747216000000da0a5f5f62616c616e63657329037226000000da0d616d6f756e745f6d696e746564da05746f74616c721e000000721e000000721f000000da057374616b651e000000730e0000000002100114020a010c010a011201723200000063010000000000000006000000040000004300000073a80000007c0064016b0473107400640283018201740174026a0319007c006b0573267400640383018201740174026a03050019007c00380003003c00740483007d017c017c0014007d0274056a0683007d0374077c0374086a0974026a0a83011b0083017d047c007c0414007d0574056a0b7c037c001800830101007c027c05180064016b047294740c6a0d7c027c05180064048d01010074086a0e7c0274026a0364058d0201007c02530029064e72070000007a1e5374616b6520616d6f756e74206d75737420626520706f736974697665217a1d4e6f7420656e6f75676820636f696e7320746f2077697468647261772129017226000000290272260000007227000000290f7229000000722f00000072170000007218000000722d000000721d000000722e0000007223000000722a000000da0a62616c616e63655f6f66722c0000007216000000da0e7661756c745f636f6e7472616374da0c6d696e745f72657761726473da087472616e7366657229067226000000da0d63757272656e745f7072696365da0d72657475726e5f616d6f756e74da06737570706c79da0f63757272656e745f61766572616765da147472616e736665725f617761795f616d6f756e74721e000000721e000000721f000000da0e77697468647261775f7374616b652a000000731a0000000002100116011201060108010801140108010e010c0110021001723c0000002901da086e65775f72617465630100000000000000020000000300000043000000733a0000007400830001007c0064016b0573167401640283018201740283007d0174038300740464033c007c00740464043c007c01740464053c006400530029064e72070000007a1d43616e6e6f742068617665206e65676174697665207374616b696e6721720e000000720400000072100000002905da0e5f5f6173736572745f6f776e65727229000000722d0000007219000000721a0000002902723d0000007237000000721e000000721e000000721f000000da0b6368616e67655f726174653c000000730c00000000020601100106010a010801723f000000290272260000007227000000630200000000000000030000000400000043000000734e0000007c0064016b047310740064028301820174016a027d0274037c0219007c006b05732a740064038301820174037c02050019007c00380003003c0074037c01050019007c00370003003c006400530029044e72070000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a194e6f7420656e6f75676820636f696e7320746f2073656e64212904722900000072170000007218000000722f000000290372260000007227000000da0673656e646572721e000000721e000000721f000000723600000046000000730a0000000002100106011401100172360000002901da076163636f756e74630100000000000000010000000200000043000000730800000074007c001900530029014e2901722f00000029017241000000721e000000721e000000721f00000072330000004f0000007302000000000272330000002902da056f776e6572da077370656e646572630200000000000000020000000300000043000000730c00000074007c007c0166021900530029014e2901722f000000290272420000007243000000721e000000721e000000721f000000da09616c6c6f77616e636554000000730200000000027244000000630200000000000000030000000400000043000000734a0000007c0064016b047310740064028301820174016a027d0274037c0219007c006b05732a740064038301820174037c027c016602050019007c00370003003c0074037c027c0166021900530029044e72070000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a3243616e6e6f7420617070726f76652062616c616e63652074686174206578636565647320746f74616c2062616c616e6365212904722900000072170000007218000000722f0000002903722600000072270000007240000000721e000000721e000000721f000000da07617070726f766559000000730c00000000021001060106010e01140172450000002903722600000072270000007228000000630300000000000000040000000500000043000000738a0000007c0064016b047310740064028301820174016a027d0374037c0219007c006b05732a740064038301820174037c027c03660219007c006b057352740064046a0474037c027c03660219007c0083028301820174037c027c036602050019007c00380003003c0074037c02050019007c00380003003c0074037c01050019007c00370003003c006400530029054e72070000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a194e6f7420656e6f75676820636f696e7320746f2073656e64217a494e6f7420656e6f75676820636f696e7320617070726f76656420746f2073656e642120596f752068617665207b7d20616e642061726520747279696e6720746f207370656e64207b7d2905722900000072170000007218000000722f000000da06666f726d617429047226000000722700000072280000007240000000721e000000721e000000721f000000722b00000063000000731200000000021001060114010a010c01120114011001722b000000630000000000000000000000000500000043000000731e00000074006401190074006402190074018300740064031900180013001400530029044e72100000007204000000720e0000002902721a0000007219000000721e000000721e000000721e000000721f000000722d00000070000000730400000000021001722d0000002902da036b6579da0576616c7565630200000000000000020000000300000043000000732200000074006a017402640119006b02731674036402830182017c0174027c003c006400530029034e720c0000007a1f4f6e6c79206f70657261746f722063616e20736574206d6574616461746121290472170000007218000000721c0000007229000000290272470000007248000000721e000000721e000000721f000000da0f6368616e67655f6d6574616461746176000000730600000000021001060172490000002901da096e65775f6f776e6572630100000000000000010000000200000043000000731400000074008300010074016a027c00830101006400530029014e2903723e000000721500000072160000002901724a000000721e000000721e000000721f000000da0c6368616e67655f6f776e65727d000000730400000000020601724b000000630000000000000000000000000200000043000000731a00000074006a0174026a0383006b02731674046401830182016400530029024e7a174f6e6c79206f70657261746f722063616e2063616c6c212905721700000072180000007215000000722e0000007229000000721e000000721e000000721e000000721f000000723e0000008300000073020000000001723e0000002901da0b6f6c645f646563696d616c630100000000000000020000000200000043000000731600000074006a017c008301010074006a0283007d017c01530029014e2903da0f5f5f74656d706f726172795f7661727216000000722e0000002902724c000000da0b6e65775f646563696d616c721e000000721e000000721f000000722300000087000000730600000000010a01080172230000004e291fda09696d706f72746c6962da0d696d706f72745f6d6f64756c65722a0000007234000000da0448617368721a000000722f000000721c000000da085661726961626c65721d0000007215000000724d0000007220000000da085f5f6578706f72747219000000da05666c6f61747232000000723c000000723f000000da037374727236000000723300000072440000007245000000722b000000722d000000da03416e797249000000724b000000723e0000007223000000721e000000721e000000721e000000721f000000da083c6d6f64756c653e0100000073420000000a010a010c01060108010c010c010c010c03080c10060601100b0601101106011009060112080601100406011204060112090601140c100606011206060110050804