Skip to content

Commit

Permalink
de-dupe redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Aug 2, 2024
1 parent 6dff386 commit 99b451e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 67 deletions.
86 changes: 19 additions & 67 deletions packages/dao-pre-propose-base/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use serde::Serialize;

use crate::{
error::PreProposeError,
helpers::add_and_remove_addresses,
msg::{DepositInfoResponse, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg},
state::{Config, PreProposeContract},
};
Expand Down Expand Up @@ -257,28 +258,12 @@ where
));
}

// Add to denylist.
if let Some(denylist_add) = denylist_add {
// Validate addresses.
let mut addrs = denylist_add
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<StdResult<Vec<Addr>>>()?;

denylist.append(&mut addrs);
denylist.dedup();
}

// Remove from denylist.
if let Some(denylist_remove) = denylist_remove {
// Validate addresses.
let addrs = denylist_remove
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<StdResult<Vec<Addr>>>()?;

denylist.retain(|a| !addrs.contains(a));
}
add_and_remove_addresses(
deps.as_ref(),
&mut denylist,
denylist_add,
denylist_remove,
)?;

config.submission_policy = PreProposeSubmissionPolicy::Anyone { denylist };
}
Expand All @@ -293,51 +278,18 @@ where
dao_members
};

// Add to allowlist.
if let Some(allowlist_add) = allowlist_add {
// Validate addresses.
let mut addrs = allowlist_add
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<StdResult<Vec<Addr>>>()?;

allowlist.append(&mut addrs);
allowlist.dedup();
}

// Remove from allowlist.
if let Some(allowlist_remove) = allowlist_remove {
// Validate addresses.
let addrs = allowlist_remove
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<StdResult<Vec<Addr>>>()?;

allowlist.retain(|a| !addrs.contains(a));
}

// Add to denylist.
if let Some(denylist_add) = denylist_add {
// Validate addresses.
let mut addrs = denylist_add
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<StdResult<Vec<Addr>>>()?;

denylist.append(&mut addrs);
denylist.dedup();
}

// Remove from denylist.
if let Some(denylist_remove) = denylist_remove {
// Validate addresses.
let addrs = denylist_remove
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<StdResult<Vec<Addr>>>()?;

denylist.retain(|a| !addrs.contains(a));
}
add_and_remove_addresses(
deps.as_ref(),
&mut allowlist,
allowlist_add,
allowlist_remove,
)?;
add_and_remove_addresses(
deps.as_ref(),
&mut denylist,
denylist_add,
denylist_remove,
)?;

config.submission_policy = PreProposeSubmissionPolicy::Specific {
dao_members,
Expand Down
32 changes: 32 additions & 0 deletions packages/dao-pre-propose-base/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use cosmwasm_std::{Addr, Deps, StdResult};

/// validate addresses and add to and/or remove from an existing list of
/// addresses, removing any duplicates. mutates the original list.
pub fn add_and_remove_addresses(
deps: Deps,
list: &mut Vec<Addr>,
add: Option<Vec<String>>,
remove: Option<Vec<String>>,
) -> StdResult<()> {
if let Some(add) = add {
let mut addrs = add
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<StdResult<Vec<Addr>>>()?;

list.append(&mut addrs);
list.sort();
list.dedup();
}

if let Some(remove) = remove {
let addrs = remove
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<StdResult<Vec<Addr>>>()?;

list.retain(|a| !addrs.contains(a));
}

Ok(())
}
1 change: 1 addition & 0 deletions packages/dao-pre-propose-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub mod error;
pub mod execute;
pub mod helpers;
pub mod msg;
pub mod state;

Expand Down

0 comments on commit 99b451e

Please sign in to comment.