Skip to content

Commit

Permalink
Validate unstaking duration on instantiation and update config
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeHartnell committed Sep 25, 2023
1 parent f01cc59 commit 1763c90
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions contracts/voting/dao-voting-cw721-staked/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use cw_storage_plus::Bound;
use cw_utils::{parse_reply_execute_data, parse_reply_instantiate_data, Duration};
use dao_hooks::nft_stake::{stake_nft_hook_msgs, unstake_nft_hook_msgs};
use dao_interface::{nft::NftFactoryCallback, voting::IsActiveResponse};
use dao_voting::duration::validate_duration;
use dao_voting::threshold::{
assert_valid_absolute_count_threshold, assert_valid_percentage_threshold, ActiveThreshold,
ActiveThresholdResponse,
Expand Down Expand Up @@ -74,6 +75,10 @@ pub fn instantiate(

DAO.save(deps.storage, &info.sender)?;

// Validate unstaking duration
validate_duration(msg.unstaking_duration)?;

// Validate active threshold if configured
if let Some(active_threshold) = msg.active_threshold.as_ref() {
match active_threshold {
ActiveThreshold::Percentage { percent } => {
Expand Down Expand Up @@ -376,6 +381,9 @@ pub fn execute_update_config(
return Err(ContractError::Unauthorized {});
}

// Validate unstaking duration
validate_duration(duration)?;

config.unstaking_duration = duration;
CONFIG.save(deps.storage, &config)?;

Expand Down
3 changes: 3 additions & 0 deletions contracts/voting/dao-voting-cw721-staked/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub enum ContractError {
#[error(transparent)]
ParseReplyError(#[from] ParseReplyError),

#[error(transparent)]
UnstakingDurationError(#[from] dao_voting::duration::UnstakingDurationError),

#[error("Can not stake that which has already been staked")]
AlreadyStaked {},

Expand Down
46 changes: 46 additions & 0 deletions contracts/voting/dao-voting-cw721-staked/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ fn test_update_config() -> anyhow::Result<()> {
}
);

// Update config to invalid duration fails
let err = update_config(&mut app, &module, CREATOR_ADDR, Some(Duration::Time(0))).unwrap_err();
assert_eq!(
err.root_cause().to_string(),
"Invalid unstaking duration, unstaking duration cannot be 0".to_string()
);

// Update duration
update_config(&mut app, &module, CREATOR_ADDR, Some(Duration::Time(1)))?;

Expand Down Expand Up @@ -411,6 +418,45 @@ fn test_add_remove_hooks() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn test_instantiate_with_invalid_duration_fails() {
let mut app = App::default();
let module_id = app.store_code(voting_cw721_staked_contract());
let cw721_id = app.store_code(cw721_base_contract());

let err = app
.instantiate_contract(
module_id,
Addr::unchecked(CREATOR_ADDR),
&InstantiateMsg {
nft_contract: NftContract::New {
code_id: cw721_id,
label: "Test NFT".to_string(),
msg: to_binary(&Cw721InstantiateMsg {
name: "Test NFT".to_string(),
symbol: "TEST".to_string(),
minter: CREATOR_ADDR.to_string(),
})
.unwrap(),
initial_nfts: vec![to_binary(&Cw721ExecuteMsg::<Empty, Empty>::Extension {
msg: Empty {},
})
.unwrap()],
},
unstaking_duration: None,
active_threshold: None,
},
&[],
"cw721_voting",
None,
)
.unwrap_err();
assert_eq!(
err.root_cause().to_string(),
"New NFT contract must be instantiated with at least one NFT".to_string()
);
}

#[test]
#[should_panic(expected = "Active threshold count must be greater than zero")]
fn test_instantiate_zero_active_threshold_count() {
Expand Down

0 comments on commit 1763c90

Please sign in to comment.