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