Contract con_dec_fix_7_stake


Contract Code


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

Byte Code

e30000000000000000000000000500000040000000738801000065006a01640083015a0265006a01640183015a0365046402640364048d025a05650464056402640664078d035a0665046402640864048d025a0765086402640964048d025a0965086402640a64048d025a0a65086402640b64048d025a0b640c640d84005a0c650d64028301640e640f840083015a0e650d64028301650f64109c0164116412840483015a10650d64028301650f64109c0164136414840483015a11650d64028301650f64159c0164166417840483015a12650d64028301650f651364189c026419641a840483015a14650d640283016513641b9c01641c641d840483015a15650d6402830165136513641e9c02641f6420840483015a16650d64028301650f651364189c0264216422840483015a17650d64028301650f6513651364239c0364246425840483015a18650d6402830164266427840083015a19650d640283016513651a64289c026429642a840483015a1b650d640283016513642b9c01642c642d840483015a1c642e642f84005a1d650f64309c016431643284045a1e643353002934da11636f6e5f6465635f6669785f375f746164da13636f6e5f6465635f6669785f375f7661756c74da13636f6e5f6465635f6669785f375f7374616b65da04726174652902da08636f6e7472616374da046e616d65e900000000da0862616c616e6365732903da0d64656661756c745f76616c756572050000007206000000da086d65746164617461da0c746f74616c5f6d696e746564da086f70657261746f72da0d74656d706f726172795f766172630000000000000000000000000300000043000000735a00000074006a0174026a038301010074048300740564013c00740664028301740564033c006404740564053c006406740764073c006408740764093c00640a7407640b3c0074026a037407640c3c0074086a01640d8301010064005300290e4eda0a73746172745f74696d657a12312e303030303030303031353436393239377204000000e901000000da0b73746172745f70726963657a0a5374616b656420746164da0a746f6b656e5f6e616d65da0473746164da0c746f6b656e5f73796d626f6c7a0a696d6167652e73697465da0e746f6b656e5f6c6f676f5f75726c720c00000072070000002909da0a5f5f6f70657261746f72da03736574da03637478da0663616c6c6572da0d6765745f74696d657374616d70da065f5f72617465da07646563696d616cda0a5f5f6d65746164617461da0e5f5f746f74616c5f6d696e746564a900721e000000721e000000da00da045f5f5f5f0d000000731200000000010c010a010c0108010801080108010a017220000000630000000000000000010000000800000043000000731e000000740074016a01640164026402640364036403830618007d007c006a02530029044e69b2070000720f00000072070000002903da036e6f77da086461746574696d65da077365636f6e64732901da027464721e000000721e000000721f00000072190000001900000073040000000002180172190000002901da06616d6f756e74630100000000000000030000000500000043000000735a0000007c0064016b047310740064028301820174016a0274036a047c0074036a0564038d0301007c00740683001b007d0174076a0883007c0117007d0274076a097c0283010100740a74036a05050019007c01370003003c007c01530029044e72070000007a1e5374616b6520616d6f756e74206d75737420626520706f736974697665212903da02746f7225000000da0c6d61696e5f6163636f756e74290bda0e417373657274696f6e4572726f72da0c7461645f636f6e7472616374da0d7472616e736665725f66726f6d7217000000da04746869737218000000da096765745f7072696365721d000000da036765747216000000da0a5f5f62616c616e63657329037225000000da0d616d6f756e745f6d696e746564da05746f74616c721e000000721e000000721f000000da057374616b651f000000730e0000000002100114020a010c010a011201723100000063010000000000000006000000040000004300000073a80000007c0064016b0473107400640283018201740174026a0319007c006b0573267400640383018201740174026a03050019007c00380003003c00740483007d017c017c0014007d0274056a0683007d0374077c0374086a0974026a0a83011b0083017d047c007c0414007d0574056a0b7c037c001800830101007c027c05180064016b047294740c6a0d7c027c05180064048d01010074086a0e7c0274026a0364058d0201007c02530029064e72070000007a1e5374616b6520616d6f756e74206d75737420626520706f736974697665217a1d4e6f7420656e6f75676820636f696e7320746f2077697468647261772129017225000000290272250000007226000000290f7228000000722e00000072170000007218000000722c000000721d000000722d000000da0d5f5f6669785f646563696d616c7229000000da0a62616c616e63655f6f66722b0000007216000000da0e7661756c745f636f6e7472616374da0c6d696e745f72657761726473da087472616e7366657229067225000000da0d63757272656e745f7072696365da0d72657475726e5f616d6f756e74da06737570706c79da0f63757272656e745f61766572616765da147472616e736665725f617761795f616d6f756e74721e000000721e000000721f000000da0e77697468647261775f7374616b652b000000731a0000000002100116011201060108010801140108010e010c0110021001723c0000002901da086e65775f72617465630100000000000000020000000300000043000000733a0000007400830001007c0064016b0573167401640283018201740283007d0174038300740464033c007c00740464043c007c01740464053c006400530029064e72070000007a1d43616e6e6f742068617665206e65676174697665207374616b696e6721720e000000720400000072100000002905da0e5f5f6173736572745f6f776e65727228000000722c0000007219000000721a0000002902723d0000007237000000721e000000721e000000721f000000da0b6368616e67655f726174653d000000730c00000000020601100106010a010801723f000000290272250000007226000000630200000000000000030000000400000043000000734e0000007c0064016b047310740064028301820174016a027d0274037c0219007c006b05732a740064038301820174037c02050019007c00380003003c0074037c01050019007c00370003003c006400530029044e72070000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a194e6f7420656e6f75676820636f696e7320746f2073656e64212904722800000072170000007218000000722e000000290372250000007226000000da0673656e646572721e000000721e000000721f000000723600000047000000730a0000000002100106011401100172360000002901da076163636f756e74630100000000000000010000000200000043000000730800000074007c001900530029014e2901722e00000029017241000000721e000000721e000000721f0000007233000000500000007302000000000272330000002902da056f776e6572da077370656e646572630200000000000000020000000300000043000000730c00000074007c007c0166021900530029014e2901722e000000290272420000007243000000721e000000721e000000721f000000da09616c6c6f77616e636555000000730200000000027244000000630200000000000000030000000400000043000000734a0000007c0064016b047310740064028301820174016a027d0274037c0219007c006b05732a740064038301820174037c027c016602050019007c00370003003c0074037c027c0166021900530029044e72070000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a3243616e6e6f7420617070726f76652062616c616e63652074686174206578636565647320746f74616c2062616c616e6365212904722800000072170000007218000000722e0000002903722500000072260000007240000000721e000000721e000000721f000000da07617070726f76655a000000730c00000000021001060106010e01140172450000002903722500000072260000007227000000630300000000000000040000000500000043000000738a0000007c0064016b047310740064028301820174016a027d0374037c0219007c006b05732a740064038301820174037c027c03660219007c006b057352740064046a0474037c027c03660219007c0083028301820174037c027c036602050019007c00380003003c0074037c02050019007c00380003003c0074037c01050019007c00370003003c006400530029054e72070000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a194e6f7420656e6f75676820636f696e7320746f2073656e64217a494e6f7420656e6f75676820636f696e7320617070726f76656420746f2073656e642120596f752068617665207b7d20616e642061726520747279696e6720746f207370656e64207b7d2905722800000072170000007218000000722e000000da06666f726d617429047225000000722600000072270000007240000000721e000000721e000000721f000000722a00000064000000731200000000021001060114010a010c01120114011001722a000000630000000000000000000000000500000043000000731e00000074006401190074006402190074018300740064031900180013001400530029044e72100000007204000000720e0000002902721a0000007219000000721e000000721e000000721e000000721f000000722c00000071000000730400000000021001722c0000002902da036b6579da0576616c7565630200000000000000020000000300000043000000732200000074006a017402640119006b02731674036402830182017c0174027c003c006400530029034e720c0000007a1f4f6e6c79206f70657261746f722063616e20736574206d6574616461746121290472170000007218000000721c0000007228000000290272470000007248000000721e000000721e000000721f000000da0f6368616e67655f6d6574616461746177000000730600000000021001060172490000002901da096e65775f6f776e6572630100000000000000010000000200000043000000731400000074008300010074016a027c00830101006400530029014e2903723e000000721500000072160000002901724a000000721e000000721e000000721f000000da0c6368616e67655f6f776e65727e000000730400000000020601724b000000630000000000000000000000000200000043000000731a00000074006a0174026a0383006b02731674046401830182016400530029024e7a174f6e6c79206f70657261746f722063616e2063616c6c212905721700000072180000007215000000722d0000007228000000721e000000721e000000721e000000721f000000723e0000008400000073020000000001723e0000002901da0b6f6c645f646563696d616c630100000000000000020000000200000043000000731600000074006a017c008301010074006a0283007d017c01530029014e2903da0f5f5f74656d706f726172795f7661727216000000722d0000002902724c000000da0b6e65775f646563696d616c721e000000721e000000721f000000723200000088000000730600000000010a01080172320000004e291fda09696d706f72746c6962da0d696d706f72745f6d6f64756c6572290000007234000000da0448617368721a000000722e000000721c000000da085661726961626c65721d0000007215000000724d0000007220000000da085f5f6578706f72747219000000da05666c6f61747231000000723c000000723f000000da037374727236000000723300000072440000007245000000722a000000722c000000da03416e797249000000724b000000723e0000007232000000721e000000721e000000721e000000721f000000da083c6d6f64756c653e0100000073420000000a010a010c01060108010c010c010c010c04080c10060601100b0601101106011009060112080601100406011204060112090601140c100606011206060110050804