Contract con_stamp_cost_test


Contract Code


  
1 import election_house
2
3 S = Hash()
4
5 @construct
6 def seed(initial_rate: int=100,
7 master_contract='masternodes',
8 delegate_contract='delegates',
9 election_max_length=datetime.DAYS * 1):
10
11 S['value'] = initial_rate
12 S['master_contract'] = master_contract
13 S['delegate_contract'] = delegate_contract
14 S['election_max_length'] = election_max_length
15
16 S['vote_count'] = 1
17
18 reset_current_votes()
19
20
21 def reset_current_votes():
22 S['current_total'] = S['value']
23
24 @export
25 def current_value():
26 return S['value']
27
28 @export
29 def vote(vk: str, obj: int):
30 # Start a new election
31 if S['election_start'] is None:
32 total_nodes = len(election_house.current_value_for_policy(S['master_contract'])) + \
33 len(election_house.current_value_for_policy(S['delegate_contract']))
34
35 S['vote_count'] = 1
36 S['min_votes_required'] = (total_nodes * 2 // 3) + 1
37
38 # Set start to now
39 S['election_start'] = now
40 S.clear('has_voted')
41
42 reset_current_votes()
43
44 tally_vote(vk, obj)
45
46 else:
47 tally_vote(vk, obj)
48
49 if election_is_over():
50 update_value()
51
52 # Reset everything
53 S['election_start'] = None
54
55
56 def update_value():
57 S['value'] = int(S['current_total'] / S['vote_count']) or 1
58
59
60 def election_is_over():
61 return S['vote_count'] >= S['min_votes_required'] or \
62 now - S['election_start'] >= S['election_max_length']
63
64
65 def tally_vote(vk: str, obj: int):
66 validate_vote(vk, obj)
67
68 S['current_total'] += obj
69
70 S['has_voted', vk] = True
71 S['vote_count'] += 1
72
73
74 def validate_vote(vk: str, obj: int):
75 assert vk in election_house.current_value_for_policy(S['master_contract']) or \
76 vk in election_house.current_value_for_policy(S['delegate_contract']), 'Not allowed to vote!'
77
78 assert type(obj) == int, 'Pass an int!'
79 assert obj > 0, 'No negatives!'
80
81 assert S['value'] / 2 <= obj <= S['value'] * 2, 'Out of range!'
82
83 assert S['votes', vk] is None, 'Already voted!'

Byte Code

e300000000000000000000000005000000400000007396000000640064016c005a0065016402640364048d025a0264056406640765036a04640814006604650564099c01640a640b84055a06640c640d84005a07650864028301640e640f840083015a09650864028301650a650564109c0264116412840483015a0b6413641484005a0c6415641684005a0d650a650564109c026417641884045a0e650a650564109c026419641a84045a0f64015300291be9000000004eda13636f6e5f7374616d705f636f73745f74657374da01532902da08636f6e7472616374da046e616d65e964000000da0b6d61737465726e6f646573da0964656c656761746573e9010000002901da0c696e697469616c5f7261746563040000000000000004000000030000004300000073320000007c00740064013c007c01740064023c007c02740064033c007c03740064043c006405740064063c007401830001006400530029074eda0576616c7565da0f6d61737465725f636f6e7472616374da1164656c65676174655f636f6e7472616374da13656c656374696f6e5f6d61785f6c656e6774687209000000da0a766f74655f636f756e742902da035f5f53da155f5f72657365745f63757272656e745f766f7465732904720a000000720c000000720d000000720e000000a9007212000000da00da045f5f5f5f05000000730c00000000020801080108010801080172140000006300000000000000000000000003000000430000007310000000740064011900740064023c006400530029034e720b000000da0d63757272656e745f746f74616c29017210000000721200000072120000007212000000721300000072110000000f0000007302000000000172110000006300000000000000000000000002000000430000007308000000740064011900530029024e720b000000290172100000007212000000721200000072120000007213000000da0d63757272656e745f76616c7565130000007302000000000272160000002902da02766bda036f626a630200000000000000030000000500000043000000739200000074006401190064006b087270740174026a0374006402190083018301740174026a037400640319008301830117007d026404740064053c007c026406140064071a0064041700740064083c007404740064013c0074006a0564098301010074068300010074077c007c01830201006e1e74077c007c018302010074088300728e7409830001006400740064013c0064005300290a4eda0e656c656374696f6e5f7374617274720c000000720d0000007209000000720f000000e902000000e903000000da126d696e5f766f7465735f7265717569726564da096861735f766f746564290a7210000000da036c656eda0e656c656374696f6e5f686f757365da1863757272656e745f76616c75655f666f725f706f6c696379da036e6f77da05636c6561727211000000da0c5f5f74616c6c795f766f7465da125f5f656c656374696f6e5f69735f6f766572da0e5f5f7570646174655f76616c7565290372170000007218000000da0b746f74616c5f6e6f646573721200000072120000007213000000da04766f746518000000731c00000000020c0108010e010e010801140108010a0106010c020a01060106017227000000630000000000000000000000000400000043000000732000000074007401640119007401640219001b00830170166403740164043c006400530029054e7215000000720f0000007209000000720b0000002902da03696e747210000000721200000072120000007212000000721300000072250000002b00000073020000000001722500000063000000000000000000000000030000004300000073240000007400640119007400640219006b057022740174006403190018007400640419006b05530029054e720f000000721c0000007219000000720e000000290272100000007221000000721200000072120000007212000000721300000072240000002f0000007304000000000114017224000000630200000000000000020000000400000043000000733a00000074007c007c018302010074016401050019007c01370003003c006402740164037c0066023c0074016404050019006405370003003c006400530029064e721500000054721d000000720f00000072090000002902da0f5f5f76616c69646174655f766f74657210000000290272170000007218000000721200000072120000007213000000722300000034000000730800000000010a0110010c017223000000630200000000000000020000000400000043000000739c0000007c0074006a0174026401190083016b06732c7c0074006a0174026402190083016b06732c740364038301820174047c01830174056b02734074036404830182017c0164056b047350740364068301820174026407190064081b007c01040003006b016f72740264071900640814006b016e0402000100738074036409830182017402640a7c006602190064006b0873987403640b8301820164005300290c4e720c000000720d0000007a144e6f7420616c6c6f77656420746f20766f7465217a0c5061737320616e20696e742172010000007a0d4e6f206e656761746976657321720b000000721a0000007a0d4f7574206f662072616e676521da05766f7465737a0e416c726561647920766f746564212906721f00000072200000007210000000da0e417373657274696f6e4572726f72da0474797065722800000029027217000000721800000072120000007212000000721300000072290000003b000000730e000000000112010801120114011001300172290000002910721f000000da04486173687210000000da086461746574696d65da0444415953722800000072140000007211000000da085f5f6578706f72747216000000da03737472722700000072250000007224000000722300000072290000007212000000721200000072120000007213000000da083c6d6f64756c653e01000000731600000008010c0304011a090804100506011212080408051007