-
Notifications
You must be signed in to change notification settings - Fork 3
/
proposals.rs
127 lines (112 loc) · 3.69 KB
/
proposals.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
use crate::*;
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, Clone)]
#[serde(crate = "near_sdk::serde")]
pub enum ProposalKind {
FarmingReward {
farm_list: Vec<String>,
total_reward: u32
},
Poll {
options: Vec<String>,
},
Common,
}
#[derive(BorshSerialize, BorshDeserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug, Deserialize))]
#[serde(crate = "near_sdk::serde")]
pub enum ProposalStatus {
WarmUp,
InProgress,
/// Expired after period of time.
Expired,
}
#[derive(BorshSerialize, BorshDeserialize, Serialize, Clone, Default)]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug, Deserialize, PartialEq))]
#[serde(crate = "near_sdk::serde")]
pub struct VoteInfo {
#[serde(with = "u128_dec_format")]
pub total_ballots: u128,
pub participants: u64,
}
#[derive(BorshSerialize, BorshDeserialize, Serialize, Clone)]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug, Deserialize))]
#[serde(crate = "near_sdk::serde")]
pub struct Proposal {
pub id: u32,
pub description: String,
/// Original proposer.
pub proposer: AccountId,
/// Kind of proposal with relevant information.
pub kind: ProposalKind,
/// Result of proposal with relevant information.
pub votes: Vec<VoteInfo>,
#[serde(with = "u128_dec_format")]
pub ve_amount_at_last_action: u128,
/// the nano seconds of voting begin time,
/// before this time, proposer can remove this immediately.
#[serde(with = "u64_dec_format")]
pub start_at: Timestamp,
/// the nano seconds of voting end time,
/// An inprogress poposal would change to expired after it.
#[serde(with = "u64_dec_format")]
pub end_at: Timestamp,
#[serde(with = "u64_dec_format")]
pub participants: u64,
/// Incentive of proposal with relevant information.
pub incentive: HashMap<u32, ProposalIncentive>,
#[borsh_skip]
pub status: Option<ProposalStatus>,
#[borsh_skip]
pub is_nonsense: Option<bool>,
}
#[derive(BorshSerialize, BorshDeserialize)]
pub enum VProposal {
Current(Proposal),
}
impl From<VProposal> for Proposal {
fn from(v: VProposal) -> Self {
match v {
VProposal::Current(c) => c,
}
}
}
impl From<Proposal> for VProposal {
fn from(c: Proposal) -> Self {
VProposal::Current(c)
}
}
impl Proposal {
pub fn update_result(&mut self){
if let ProposalKind::Common { .. } = &self.kind {
if self.votes[0].total_ballots + self.votes[1].total_ballots < self.votes[2].total_ballots {
self.is_nonsense = Some(true);
} else {
self.is_nonsense = Some(false);
}
}
}
pub fn update_status(&mut self) {
let now = env::block_timestamp();
if now < self.start_at {
self.status = Some(ProposalStatus::WarmUp);
} else if now >= self.start_at && now < self.end_at {
self.status = Some(ProposalStatus::InProgress);
} else {
self.status = Some(ProposalStatus::Expired);
self.update_result();
}
}
}
impl Contract {
pub fn internal_unwrap_proposal(&self, proposal_id: u32) -> Proposal {
let mut proposal = self.internal_get_proposal(proposal_id).expect(E404_PROPOSAL_NOT_EXIST);
proposal.update_status();
proposal
}
pub fn internal_get_proposal(&self, proposal_id: u32) -> Option<Proposal> {
self.data().proposals.get(&proposal_id).map(|o| o.into())
}
pub fn internal_set_proposal(&mut self, proposal_id: u32, proposal: Proposal) {
self.data_mut().proposals.insert(&proposal_id, &proposal.into());
}
}