-
Notifications
You must be signed in to change notification settings - Fork 3
/
proposals_action.rs
131 lines (118 loc) · 3.89 KB
/
proposals_action.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::*;
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(crate = "near_sdk::serde")]
pub enum Action {
/// Vote to approve given proposal
VoteApprove,
/// Vote to reject given proposal
VoteReject,
/// Vote to nonsense given proposal(because it's spam).
VoteNonsense,
/// Vote to farm id given proposal
VoteFarm { farm_id: usize },
/// Vote to poll id given proposal
VotePoll { poll_id: usize }
}
impl Action {
pub fn get_index(&self) -> usize {
match self {
Action::VoteFarm { farm_id } => {
*farm_id
},
Action::VotePoll { poll_id } => {
*poll_id
},
Action::VoteApprove => Vote::Approve as usize,
Action::VoteReject => Vote::Reject as usize,
Action::VoteNonsense => Vote::Nonsense as usize,
}
}
}
/// Votes recorded in the proposal.
#[derive(BorshSerialize, BorshDeserialize, Serialize, Clone)]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug))]
#[serde(crate = "near_sdk::serde")]
pub enum Vote {
Approve = 0x0,
Reject = 0x1,
Nonsense = 0x2,
}
impl From<&Action> for Vote {
fn from(action: &Action) -> Self {
match action {
Action::VoteApprove => Vote::Approve,
Action::VoteReject => Vote::Reject,
Action::VoteNonsense => Vote::Nonsense,
Action::VoteFarm { .. } => env::panic_str(E201_INVALID_VOTE),
Action::VotePoll { .. } => env::panic_str(E201_INVALID_VOTE),
}
}
}
impl Proposal {
pub fn update_votes(
&mut self,
action: &Action,
amount: Balance,
is_increased: bool
) {
let index = action.get_index();
require!(index < self.votes.len(), E201_INVALID_VOTE);
if is_increased {
self.votes[index].total_ballots += amount;
} else {
self.votes[index].total_ballots -= amount;
}
}
}
impl Contract {
pub fn internal_append_vote(
&mut self,
proposal_id: u32,
action: &Action,
amount: Balance,
) {
let mut proposal = self.internal_unwrap_proposal(proposal_id);
require!(action != &Action::VoteNonsense, E201_INVALID_VOTE);
// check proposal is inprogress
match proposal.status {
Some(ProposalStatus::InProgress) => {
// update proposal result
proposal.update_votes(
action,
amount,
true
);
proposal.ve_amount_at_last_action = self.data().cur_total_ve_lpt;
proposal.votes[action.get_index()].participants += 1;
proposal.participants += 1;
self.data_mut()
.proposals
.insert(&proposal_id, &proposal.into());
},
_ => env::panic_str(E205_NOT_VOTABLE)
}
}
pub fn internal_cancel_vote(
&mut self,
proposal_id: u32,
vote_detail: &VoteDetail
) {
let mut proposal = self.internal_unwrap_proposal(proposal_id);
// check proposal is inprogress
match proposal.status {
Some(ProposalStatus::InProgress) => {
// update proposal result
proposal.update_votes(
&vote_detail.action,
vote_detail.amount,
false
);
proposal.ve_amount_at_last_action = self.data().cur_total_ve_lpt;
proposal.votes[vote_detail.action.get_index()].participants -= 1;
proposal.participants -= 1;
self.internal_set_proposal(proposal_id, proposal.into());
},
_ => env::panic_str(E204_VOTE_CAN_NOT_CANCEL)
}
}
}