Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show proposal status #43

Merged
merged 11 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions elections/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use near_sdk::{serde::Serialize, AccountId};
use near_sdk::{serde::Serialize};
use serde_json::json;

use common::{EventPayload, NearEvent};
Expand All @@ -21,7 +21,7 @@ pub(crate) fn emit_vote(prop_id: u32) {

#[cfg(test)]
mod unit_tests {
use near_sdk::test_utils;
use near_sdk::{test_utils, AccountId};

use super::*;

Expand Down
27 changes: 27 additions & 0 deletions elections/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,33 @@ mod unit_tests {
assert_eq!(res.unwrap(), policy1());
}

#[test]
fn proposal_status_query() {
let (mut ctx, mut ctr) = setup(&admin());

let prop_id = mk_proposal(&mut ctr);
let mut res = ctr.proposal_status(prop_id);
assert_eq!(res, ProposalStatus::BEFORE);

ctx.block_timestamp = (START + 2) * MSECOND;
testing_env!(ctx.clone());

res = ctr.proposal_status(prop_id);
assert_eq!(res, ProposalStatus::ONGOING);

ctx.block_timestamp = (START + 11) * MSECOND;
testing_env!(ctx.clone());

res = ctr.proposal_status(prop_id);
assert_eq!(res, ProposalStatus::COOLDOWN);

ctx.block_timestamp = (START + 111) * MSECOND;
testing_env!(ctx.clone());

res = ctr.proposal_status(prop_id);
assert_eq!(res, ProposalStatus::ENDED);
}

#[test]
#[should_panic(expected = "double vote for the same candidate")]
fn vote_double_vote_same_candidate() {
Expand Down
10 changes: 10 additions & 0 deletions elections/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ pub enum HouseType {
TransparencyCommission,
}

#[derive(Serialize, Deserialize)]
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(test, derive(Debug, PartialEq))]
pub enum ProposalStatus {
BEFORE,
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
ONGOING,
COOLDOWN,
ENDED
}

#[derive(BorshDeserialize, BorshSerialize)]
#[cfg_attr(test, derive(Debug))]
pub struct Proposal {
Expand Down
17 changes: 16 additions & 1 deletion elections/src/view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use near_sdk::{near_bindgen, AccountId};
use near_sdk::{near_bindgen, AccountId, env};
use uint::hex;

use crate::proposal::*;
Expand Down Expand Up @@ -31,4 +31,19 @@ impl Contract {
.get(&user)
.map(|policy| hex::encode(policy))
}

/// Returns the proposal status
pub fn proposal_status(&self, prop_id: u32) -> ProposalStatus {
let proposal = self._proposal(prop_id);
let current_time = env::block_timestamp_ms();
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
if current_time < proposal.start {
return ProposalStatus::BEFORE;
} else if current_time >= proposal.start && current_time <= proposal.end {
return ProposalStatus::ONGOING;
} else if current_time > proposal.end && current_time <= (proposal.cooldown + proposal.end) {
return ProposalStatus::COOLDOWN;
} else {
return ProposalStatus::ENDED;
}
}
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading