Skip to content

Commit

Permalink
feat(bindings-perp): impl and test sudoers
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed May 25, 2023
1 parent e693435 commit 7984d67
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions contracts/bindings-perp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod contract;
pub mod msg;
pub mod query;
pub mod state;
94 changes: 94 additions & 0 deletions contracts/bindings-perp/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::collections::HashSet;

use cosmwasm_schema::cw_serde;
use cw_storage_plus::Item;

pub const SUDOERS: Item<Sudoers> = Item::new("sudoers");

#[cw_serde]
pub struct Sudoers {
pub members: HashSet<String>,
pub admin: String,
}

impl Sudoers {
pub fn has(&self, addr: impl AsRef<str>) -> bool {
let addr = addr.as_ref();
self.members.contains(addr) || self.admin == addr
}

pub fn is_admin(&self, addr: impl AsRef<str>) -> bool {
let addr = addr.as_ref();
self.admin == addr
}

pub fn is_member(&self, addr: impl AsRef<str>) -> bool {
let addr = addr.as_ref();
self.members.contains(addr)
}
}

#[cfg(test)]
pub mod tests {
use cosmwasm_std::testing::MockStorage;

use super::*;

pub fn init_mock_sudoers() -> Sudoers {
let member_names = vec!["alice", "brock", "david"];
let members: HashSet<String> =
member_names.iter().map(|&s| s.to_string()).collect();
let admin: String = "cait".to_string();
Sudoers { members, admin }
}

#[test]
fn sudoers_is_admin() {
let sudoers = init_mock_sudoers();
assert!(!sudoers.is_admin("alice"));
assert!(sudoers.is_admin("cait"));
assert!(!sudoers.is_admin("david"));
assert!(!sudoers.is_admin("brock"));
}

#[test]
fn sudoers_is_member() {
let sudoers = init_mock_sudoers();
assert!(sudoers.is_member("alice"));
assert!(!sudoers.is_member("cait"));
assert!(sudoers.is_member("david"));
assert!(sudoers.is_member("brock"));
}

#[test]
fn sudoers_has() {
let sudoers = init_mock_sudoers();

let sudoersed_names = ["alice", "brock", "cait", "david"];
for name in sudoersed_names.iter() {
assert!(sudoers.has(name));
}

let other_names = ["xxx", "not-sudoersed"];
for name in other_names.iter() {
assert!(!sudoers.has(name));
}
}

#[test]
fn save_and_load() {
let mut store = MockStorage::new();

// Store should start out empty
assert!(SUDOERS.load(&store).is_err());
assert_eq!(SUDOERS.may_load(&store).unwrap(), None);

// save to store
let sudoers = init_mock_sudoers();
let res = SUDOERS.save(&mut store, &sudoers);
assert!(res.is_ok());

// load from store
assert_eq!(sudoers, SUDOERS.load(&store).unwrap());
}
}
2 changes: 1 addition & 1 deletion packages/dummy/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod test {
};

use bindings_perp::msg::{ExecuteMsg as NBExecuteMsg, LiquidationArgs};
use controller::msgs::{ExecuteMsg as ControllerExecuteMsg, MarketParams};
use controller::msgs::ExecuteMsg as ControllerExecuteMsg;
use shifter::msgs::ExecuteMsg as ShifterExecuteMsg;

use cosmwasm_schema::cw_serde;
Expand Down

0 comments on commit 7984d67

Please sign in to comment.