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