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

add query for accepted policy #40

Merged
merged 6 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
3 changes: 3 additions & 0 deletions elections/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ near view $CTR proposals '{"prop_id": 2}'
# accept fair voting policy
near call $CTR accept_fair_voting_policy '{"policy": "f1c09f8686fe7d0d798517111a66675da0012d8ad1693a47e0e2a7d3ae1c69d4"}' --deposit 0.001 --accountId me.testnet

# query the accepted policy by user it either returns the policy or `None` if user did not accept any policy
sczembor marked this conversation as resolved.
Show resolved Hide resolved
near call $CTR accepted_policy '{"user": "alice.testnet"}' --accountId me.testnet

# vote
near call $CTR vote '{"prop_id": 1, "vote": ["candidate1.testnet", "candidate3.testnet"]}' --gas 70000000000000 --deposit 0.0005 --accountId me.testnet
```
Expand Down
27 changes: 27 additions & 0 deletions elections/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ impl Contract {
)
}

/*****************
* QUERIES
****************/

/// Returns the policy if user has accepted it otherwise returns None
pub fn accepted_policy(&self, user: AccountId) -> Option<[u8; 32]> {
self.accepted_policy.get(&user)
sczembor marked this conversation as resolved.
Show resolved Hide resolved
}

/*****************
* PRIVATE
****************/
Expand Down Expand Up @@ -194,6 +203,7 @@ impl Contract {
#[cfg(all(test, not(target_arch = "wasm32")))]
mod unit_tests {
use near_sdk::{test_utils::VMContextBuilder, testing_env, Gas, VMContext};
use uint::hex;

use crate::*;

Expand Down Expand Up @@ -574,6 +584,23 @@ mod unit_tests {
ctr.vote(prop_id, vec![candidate(1)]);
}

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

let mut res = ctr.accepted_policy(admin());
assert!(res.is_none());
ctx.attached_deposit = ACCEPT_POLICY_COST;
testing_env!(ctx.clone());
ctr.accept_fair_voting_policy(policy1());
sczembor marked this conversation as resolved.
Show resolved Hide resolved
res = ctr.accepted_policy(admin());
assert!(res.is_some());
let mut expected: [u8; 32] = [0u8; 32];
let res1 = hex::decode_to_slice(policy1(), &mut expected);
assert!(res1.is_ok());
assert_eq!(res.unwrap(), expected);
}

#[test]
#[should_panic(expected = "double vote for the same candidate")]
fn vote_double_vote_same_candidate() {
Expand Down
Loading