Contract elect_delegates

Created On NaN secs ago - Invalid Date UTC+0

Contract Code


  
1 import currency
2 import election_house
3 __candidate_state = Hash(contract='elect_delegates', name='candidate_state')
4 __top_candidate = Variable(contract='elect_delegates', name='top_candidate')
5 __no_confidence_state = Hash(contract='elect_delegates', name=
6 'no_confidence_state')
7 __last_candidate = Variable(contract='elect_delegates', name='last_candidate')
8 __to_be_relinquished = Variable(contract='elect_delegates', name=
9 'to_be_relinquished')
10 STAMP_COST = 20
11 __member_cost = Variable(contract='elect_delegates', name='member_cost')
12 __controller = Variable(contract='elect_delegates', name='controller')
13
14
15 def ____(policy: str='members', cost: int=100000):
16 __controller.set(policy)
17 __member_cost.set(cost)
18
19
20 @__export('elect_delegates')
21 def register():
22 assert not __candidate_state['registered', ctx.caller
23 ], 'Already registered.'
24 currency.transfer_from(__member_cost.get(), ctx.this, ctx.caller)
25 __candidate_state['registered', ctx.caller] = True
26 __candidate_state['votes', ctx.caller] = 0
27 if __top_candidate.get() is None:
28 __top_candidate.set(ctx.caller)
29
30
31 @__export('elect_delegates')
32 def unregister():
33 mns = election_house.current_value_for_policy(__controller.get())
34 assert __candidate_state['registered', ctx.caller], 'Not registered.'
35 assert ctx.caller not in mns, "Can't unstake if in governance."
36 currency.transfer(__member_cost.get(), ctx.caller)
37 __candidate_state['registered', ctx.caller] = False
38 __candidate_state['votes', ctx.caller] = 0
39
40
41 @__export('elect_delegates')
42 def vote_candidate(address: str):
43 assert __candidate_state['registered', address
44 ], 'Candidate not registered.'
45 v = __candidate_state['last_voted', ctx.caller]
46 assert v is None or now - v > datetime.DAYS * 1, 'Voting again too soon.'
47 vote_cost = STAMP_COST / election_house.current_value_for_policy(
48 'stamp_cost')
49 currency.transfer_from(vote_cost, 'blackhole', ctx.caller)
50 __candidate_state['last_voted', ctx.caller] = now
51 votes = __candidate_state['votes', address]
52 if votes is None:
53 __candidate_state['votes', address] = 1
54 else:
55 __candidate_state['votes', address] += 1
56 if __top_candidate.get() is not None:
57 if __candidate_state['votes', address] > __candidate_state['votes',
58 __top_candidate.get()]:
59 __top_candidate.set(address)
60
61
62 @__export('elect_delegates')
63 def top_member():
64 return __top_candidate.get()
65
66
67 @__export('elect_delegates')
68 def pop_top():
69 assert ctx.caller == __controller.get(), 'Wrong smart contract caller.'
70 top = top_member()
71 if top is None:
72 return None
73 __candidate_state.clear('votes')
74 __top_candidate.set(None)
75
76
77 @__export('elect_delegates')
78 def vote_no_confidence(address: str):
79 assert address in election_house.current_value_for_policy(__controller.
80 get()), 'Cannot vote against a non-committee member'
81 v = __no_confidence_state['last_voted', ctx.caller]
82 assert v is None or now - v > datetime.DAYS * 1, 'Voting again too soon.'
83 vote_cost = STAMP_COST / election_house.current_value_for_policy(
84 'stamp_cost')
85 currency.transfer_from(vote_cost, 'blackhole', ctx.caller)
86 __no_confidence_state['last_voted', ctx.caller] = now
87 if __no_confidence_state['votes', address] is None:
88 __no_confidence_state['votes', address] = 1
89 else:
90 __no_confidence_state['votes', address] += 1
91 if __last_candidate.get() is None:
92 __last_candidate.set(address)
93 elif __no_confidence_state['votes', address] > __no_confidence_state[
94 'votes', __last_candidate.get()]:
95 __last_candidate.set(address)
96
97
98 @__export('elect_delegates')
99 def last_member():
100 r = __to_be_relinquished.get()
101 if r is not None:
102 return r
103 return __last_candidate.get()
104
105
106 @__export('elect_delegates')
107 def pop_last():
108 assert ctx.caller == __controller.get(), 'Wrong smart contract caller.'
109 r = __to_be_relinquished.get()
110 if r is not None:
111 __no_confidence_state['votes', r] = 0
112 __to_be_relinquished.set(None)
113 else:
114 __no_confidence_state.clear('votes')
115 __candidate_state['registered', __last_candidate.get()] = False
116 __last_candidate.set(None)
117
118
119 @__export('elect_delegates')
120 def force_removal(address: str):
121 assert ctx.caller == __controller.get(), 'Wrong smart contract caller.'
122 __candidate_state['registered', address] = False
123
124
125 @__export('elect_delegates')
126 def relinquish():
127 assert ctx.caller in election_house.current_value_for_policy(__controller
128 .get())
129 r = __to_be_relinquished.get()
130 assert r is None, 'Someone is already trying to relinquish!'
131 __to_be_relinquished.set(ctx.caller)
132