Contract stamp_cost

Created On NaN secs ago - Invalid Date UTC+0

Contract Code


  
1 import election_house
2 __S = Hash(contract='stamp_cost', name='S')
3
4
5 def ____(initial_rate: int=100, master_contract='masternodes',
6 delegate_contract='delegates', election_max_length=datetime.DAYS * 1):
7 __S['value'] = initial_rate
8 __S['master_contract'] = master_contract
9 __S['delegate_contract'] = delegate_contract
10 __S['election_max_length'] = election_max_length
11 __S['vote_count'] = 1
12 __reset_current_votes()
13
14
15 def __reset_current_votes():
16 __S['current_total'] = __S['value']
17
18
19 @__export('stamp_cost')
20 def current_value():
21 return __S['value']
22
23
24 @__export('stamp_cost')
25 def vote(vk: str, obj: int):
26 if __S['election_start'] is None:
27 total_nodes = len(election_house.current_value_for_policy(__S[
28 'master_contract'])) + len(election_house.
29 current_value_for_policy(__S['delegate_contract']))
30 __S['vote_count'] = 1
31 __S['min_votes_required'] = total_nodes * 2 // 3 + 1
32 __S['election_start'] = now
33 __S.clear('has_voted')
34 __reset_current_votes()
35 __tally_vote(vk, obj)
36 else:
37 __tally_vote(vk, obj)
38 if __election_is_over():
39 __update_value()
40 __S['election_start'] = None
41
42
43 def __update_value():
44 __S['value'] = int(__S['current_total'] / __S['vote_count']) or 1
45
46
47 def __election_is_over():
48 return __S['vote_count'] >= __S['min_votes_required'] or now - __S[
49 'election_start'] >= __S['election_max_length']
50
51
52 def __tally_vote(vk: str, obj: int):
53 __validate_vote(vk, obj)
54 __S['current_total'] += obj
55 __S['has_voted', vk] = True
56 __S['vote_count'] += 1
57
58
59 def __validate_vote(vk: str, obj: int):
60 assert vk in election_house.current_value_for_policy(__S['master_contract']
61 ) or vk in election_house.current_value_for_policy(__S[
62 'delegate_contract']), 'Not allowed to vote!'
63 assert type(obj) == int, 'Pass an int!'
64 assert obj > 0, 'No negatives!'
65 assert __S['value'] / 2 <= obj <= __S['value'] * 2, 'Out of range!'
66 assert __S['votes', vk] is None, 'Already voted!'
67