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