Skip to content

Commit

Permalink
Deduplicate whitelist and admins
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Jul 1, 2024
1 parent 24f4353 commit b1cb576
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
18 changes: 13 additions & 5 deletions contracts/atom_wars/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,20 @@ pub fn instantiate(
PROP_ID.save(deps.storage, &0)?;

let mut whitelist_admins: Vec<Addr> = vec![];
let mut whitelist: Vec<CovenantParams> = vec![];
for admin in msg.whitelist_admins {
whitelist_admins.push(deps.api.addr_validate(&admin)?);
let admin_addr = deps.api.addr_validate(&admin)?;
if !whitelist_admins.contains(&admin_addr) {
whitelist_admins.push(admin_addr.clone());
}
}
for covenant in msg.initial_whitelist {
if !whitelist.contains(&covenant) {
whitelist.push(covenant.clone());
}
}

WHITELIST_ADMINS.save(deps.storage, &whitelist_admins)?;
WHITELIST.save(deps.storage, &msg.initial_whitelist)?;
WHITELIST.save(deps.storage, &whitelist)?;

// For each tranche, create a tranche in the TRANCHE_MAP and set the total power to 0
let mut tranche_ids = std::collections::HashSet::new();
Expand Down Expand Up @@ -875,11 +883,11 @@ fn query_user_lockups(
.collect()
}

fn query_whitelist(deps: Deps) -> StdResult<Vec<CovenantParams>> {
pub fn query_whitelist(deps: Deps) -> StdResult<Vec<CovenantParams>> {
WHITELIST.load(deps.storage)
}

fn query_whitelist_admins(deps: Deps) -> StdResult<Vec<Addr>> {
pub fn query_whitelist_admins(deps: Deps) -> StdResult<Vec<Addr>> {
WHITELIST_ADMINS.load(deps.storage)
}

Expand Down
29 changes: 28 additions & 1 deletion contracts/atom_wars/src/testing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::contract::MAX_LOCK_ENTRIES;
use crate::contract::{query_whitelist, query_whitelist_admins, MAX_LOCK_ENTRIES};
use crate::query::QueryMsg;
use crate::state::Tranche;
use crate::{
Expand Down Expand Up @@ -59,6 +59,33 @@ fn instantiate_test() {
assert_eq!(msg.round_length, constants.round_length);
}

#[test]
fn deduplicate_whitelist_admins_test() {
let (mut deps, env, info) = (mock_dependencies(), mock_env(), mock_info("addr0000", &[]));
let mut msg = get_default_instantiate_msg();
msg.initial_whitelist = vec![
get_default_covenant_params(),
get_default_covenant_params(),
get_default_covenant_params(),
];
msg.whitelist_admins = vec![
"admin3".to_string(),
"admin2".to_string(),
"admin3".to_string(),
];
let res = instantiate(deps.as_mut(), env, info, msg);
assert!(res.is_ok());
let whitelist = query_whitelist(deps.as_ref()).unwrap();
let whitelist_admins = query_whitelist_admins(deps.as_ref()).unwrap();

assert_eq!(whitelist.len(), 1);
assert_eq!(whitelist[0], get_default_covenant_params());

assert_eq!(whitelist_admins.len(), 2);
assert_eq!(whitelist_admins[0], "admin3");
assert_eq!(whitelist_admins[1], "admin2");
}

#[test]
fn lock_tokens_basic_test() {
let user_address = "addr0000";
Expand Down

0 comments on commit b1cb576

Please sign in to comment.