Skip to content

Commit

Permalink
Further simplify funds validation during lock_tokens()
Browse files Browse the repository at this point in the history
  • Loading branch information
dusan-maksimovic committed Jul 16, 2024
1 parent d4fa931 commit 2d572c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
26 changes: 5 additions & 21 deletions contracts/atom_wars/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,9 @@ fn lock_tokens(
lock_duration: u64,
) -> Result<Response, ContractError> {
let constants = CONSTANTS.load(deps.storage)?;
validate_lock_duration(constants.lock_epoch_length, lock_duration)?;

// Validate that sent funds are the required denom
if info.funds.len() != 1 {
return Err(ContractError::Std(StdError::generic_err(
"Must send exactly one coin",
)));
}

let sent_funds = info.funds[0].clone();

// validate that the sent funds are the required denom using must_pay
must_pay(&info, &constants.denom).map_err(|_| {
ContractError::Std(StdError::generic_err(format!(
"Must send {} tokens",
constants.denom
)))
})?;
validate_lock_duration(constants.lock_epoch_length, lock_duration)?;
must_pay(&info, &constants.denom)?;

// validate that the user does not have too many locks
if get_lock_count(deps.as_ref(), info.sender.clone()) >= MAX_LOCK_ENTRIES {
Expand All @@ -170,9 +155,8 @@ fn lock_tokens(
))));
}

// Create entry in LocksMap
let lock_entry = LockEntry {
funds: sent_funds,
funds: info.funds[0].clone(),
lock_start: env.block.time,
lock_end: env.block.time.plus_nanos(lock_duration),
};
Expand Down Expand Up @@ -600,7 +584,7 @@ fn add_to_whitelist(
// Validate that the sender is a whitelist admin
let whitelist_admins = WHITELIST_ADMINS.load(deps.storage)?;
if !whitelist_admins.contains(&info.sender) {
return Err(ContractError::Unauthorized {});
return Err(ContractError::Unauthorized);
}

// Add covenant_params to whitelist
Expand Down Expand Up @@ -628,7 +612,7 @@ fn remove_from_whitelist(
// Validate that the sender is a whitelist admin
let whitelist_admins = WHITELIST_ADMINS.load(deps.storage)?;
if !whitelist_admins.contains(&info.sender) {
return Err(ContractError::Unauthorized {});
return Err(ContractError::Unauthorized);
}

// Remove covenant_params from whitelist
Expand Down
6 changes: 5 additions & 1 deletion contracts/atom_wars/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmwasm_std::{OverflowError, StdError};
use cw_utils::PaymentError;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -10,5 +11,8 @@ pub enum ContractError {
OverflowError(#[from] OverflowError),

#[error("Unauthorized")]
Unauthorized {},
Unauthorized,

#[error("{0}")]
PaymentError(#[from] PaymentError),
}

0 comments on commit 2d572c3

Please sign in to comment.