Contract con_rebase_example


Contract Code


  
1 balances = Hash(default_value=0)
2 holders_forward_index = Hash(default_value=False)
3 holders_reverse_index = Hash(default_value=False)
4 holders_amount = Variable()
5 metadata = Hash()
6
7 @construct
8 def seed():
9 balances[ctx.caller] = 100_000_000
10
11 holders_amount.set(1)
12 holders_forward_index[1] = ctx.caller
13 holders_reverse_index[ctx.caller] = 1
14
15 metadata['token_name'] = "Rebase Example"
16 metadata['token_symbol'] = "REBE"
17 metadata['operator'] = ctx.caller
18 metadata['rebase_balance_limit'] = 0
19
20 def manage_holders_index(address: str):
21 if(not address.startswith("con_")):
22 if(balances[address] > metadata['rebase_balance_limit']):
23 if(holders_reverse_index[address] == False):
24 new_holder_amount = holders_amount.get() + 1
25 holders_amount.set(new_holder_amount)
26 reverse_holders_index[address] = new_holder_amount
27 forward_holders_index[new_holder_amount] = address
28 else:
29 if (reverse_holders_index[address] != False):
30 reverse_holders_index[address] = False
31 forward_holders_index[new_holder_amount] = False
32
33 @export
34 def rebase_everyone(rebase_type: str, amount: float, start: int=None, end: int=None):
35 assert ctx.caller == metadata['operator'], 'Only operator can rebase!'
36 #start end for batches if too many stamps
37 if start == None:
38 start = 1
39 if end == None:
40 end = holders_amount.get() + 1
41
42 if(rebase_type == "multiply"):
43 for holder_id in range(start, end):
44 if (forward_holders_index[holder_id] != False):
45 balances[forward_holders_index[holder_id]] *= amount
46 else:
47 for holder_id in range(start, end):
48 if (forward_holders_index[holder_id] != False):
49 balances[forward_holders_index[holder_id]] /= amount
50
51 @export
52 def change_metadata(key: str, value: Any):
53 assert ctx.caller == metadata['operator'], 'Only operator can set metadata!'
54 metadata[key] = value
55
56
57 @export
58 def transfer(amount: float, to: str):
59 assert amount > 0, 'Cannot send negative balances!'
60 assert balances[ctx.caller] >= amount, 'Not enough coins to send!'
61 balances[ctx.caller] -= amount
62 balances[to] += amount
63 manage_holders_index(ctx.caller)
64 manage_holders_index(to)
65
66
67 @export
68 def approve(amount: float, to: str):
69 assert amount > 0, 'Cannot send negative balances!'
70 balances[ctx.caller, to] += amount
71
72
73 @export
74 def transfer_from(amount: float, to: str, main_account: str):
75 assert amount > 0, 'Cannot send negative balances!'
76 assert balances[main_account, ctx.caller] >= amount, 'Not enough coins approved to send! You have {} and are trying to spend {}'\
77 .format(balances[main_account, ctx.caller], amount)
78 assert balances[main_account] >= amount, 'Not enough coins to send!'
79
80 balances[main_account, ctx.caller] -= amount
81 balances[main_account] -= amount
82 balances[to] += amount
83 manage_holders_index(main_account)
84 manage_holders_index(to)

Byte Code

e3000000000000000000000000070000004000000073dc000000650064006401640264038d035a01650064046401640564038d035a02650064046401640664038d035a0365046401640764088d025a0565006401640964088d025a06640a640b84005a076508640c9c01640d640e84045a09650a64018301641e6508650b650c650c64109c0464116412840583015a0d650a640183016508650e64139c0264146415840483015a0f650a64018301650b650864169c0264176418840483015a10650a64018301650b650864169c026419641a840483015a11650a64018301650b65086508641b9c03641c641d840483015a12640f5300291fe900000000da12636f6e5f7265626173655f6578616d706c65da0862616c616e6365732903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d6546da15686f6c646572735f666f72776172645f696e646578da15686f6c646572735f726576657273655f696e646578da0e686f6c646572735f616d6f756e74290272050000007206000000da086d65746164617461630000000000000000000000000300000043000000734e0000006401740074016a023c0074036a0464028301010074016a02740564023c006402740674016a023c006403740764043c006405740764063c0074016a02740764073c006408740764093c0064005300290a4e6900e1f505e9010000007a0e526562617365204578616d706c65da0a746f6b656e5f6e616d65da0452454245da0c746f6b656e5f73796d626f6cda086f70657261746f727201000000da147265626173655f62616c616e63655f6c696d69742908da0a5f5f62616c616e636573da03637478da0663616c6c6572da105f5f686f6c646572735f616d6f756e74da03736574da175f5f686f6c646572735f666f72776172645f696e646578da175f5f686f6c646572735f726576657273655f696e646578da0a5f5f6d65746164617461a90072190000007219000000da00da045f5f5f5f0c000000731000000000010a010a010a010a01080108010a01721b0000002901da0761646472657373630100000000000000020000000300000043000000736e0000007c006a0064018301736a74017c0019007402640219006b04724e74037c00190064036b02726a74046a058300640417007d0174046a067c01830101007c0174077c003c007c0074087c013c006e1c74077c00190064036b03726a640374077c003c00640374087c013c006400530029054eda04636f6e5f721000000046720b0000002909da0a737461727473776974687211000000721800000072170000007214000000da036765747215000000da15726576657273655f686f6c646572735f696e646578da15666f72776172645f686f6c646572735f696e6465782902721c000000da116e65775f686f6c6465725f616d6f756e7472190000007219000000721a000000da165f5f6d616e6167655f686f6c646572735f696e64657817000000731400000000010a0110010c010c010a0108010a010c01080172230000004e2904da0b7265626173655f74797065da06616d6f756e74da057374617274da03656e6463040000000000000005000000050000004300000073ac00000074006a017402640119006b02731674036402830182017c0264006b02722264037d027c0364006b02723674046a058300640317007d037c0064046b027274786874067c027c03830244005d247d0474077c04190064056b03724a740874077c041900050019007c01390003003c00714a57006e34783274067c027c03830244005d247d0474077c04190064056b037280740874077c041900050019007c011d0003003c00718057006400530029064e720f0000007a194f6e6c79206f70657261746f722063616e2072656261736521720b000000da086d756c7469706c79462909721200000072130000007218000000da0e417373657274696f6e4572726f727214000000721f000000da0572616e67657221000000721100000029057224000000722500000072260000007227000000da09686f6c6465725f696472190000007219000000721a000000da0f7265626173655f65766572796f6e65240000007318000000000316010801040108010c01080110010c011a0210010c01722c0000002902da036b6579da0576616c7565630200000000000000020000000300000043000000732200000074006a017402640119006b02731674036402830182017c0174027c003c006400530029034e720f0000007a1f4f6e6c79206f70657261746f722063616e20736574206d6574616461746121290472120000007213000000721800000072290000002902722d000000722e00000072190000007219000000721a000000da0f6368616e67655f6d65746164617461360000007306000000000210010601722f00000029027225000000da02746f630200000000000000020000000400000043000000735e0000007c0064016b0473107400640283018201740174026a0319007c006b0573267400640383018201740174026a03050019007c00380003003c0074017c01050019007c00370003003c00740474026a038301010074047c01830101006400530029044e72010000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a194e6f7420656e6f75676820636f696e7320746f2073656e642129057229000000721100000072120000007213000000722300000029027225000000723000000072190000007219000000721a000000da087472616e736665723d000000730c000000000210011601120110010a017231000000630200000000000000020000000400000043000000732a0000007c0064016b0473107400640283018201740174026a037c016602050019007c00370003003c006400530029034e72010000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573212904722900000072110000007212000000721300000029027225000000723000000072190000007219000000721a000000da07617070726f7665470000007304000000000210017232000000290372250000007230000000da0c6d61696e5f6163636f756e74630300000000000000030000000500000043000000739a0000007c0064016b047310740064028301820174017c0274026a03660219007c006b05733c740064036a0474017c0274026a03660219007c0083028301820174017c0219007c006b057350740064048301820174017c0274026a036602050019007c00380003003c0074017c02050019007c00380003003c0074017c01050019007c00370003003c0074057c028301010074057c01830101006400530029054e72010000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a494e6f7420656e6f75676820636f696e7320617070726f76656420746f2073656e642120596f752068617665207b7d20616e642061726520747279696e6720746f207370656e64207b7d7a194e6f7420656e6f75676820636f696e7320746f2073656e642129067229000000721100000072120000007213000000da06666f726d61747223000000290372250000007230000000723300000072190000007219000000721a000000da0d7472616e736665725f66726f6d4d0000007314000000000210010c010c01140114011601100110010801723500000029024e4e2913da0448617368721100000072160000007217000000da085661726961626c6572140000007218000000721b000000da037374727223000000da085f5f6578706f7274da05666c6f6174da03696e74722c000000da03416e79722f000000723100000072320000007235000000721900000072190000007219000000721a000000da083c6d6f64756c653e01000000732a0000000601080104010a0104010a01040108010c03080b0e0d0601000118100601120606011209060112050601