Skip to content

Commit

Permalink
add some refactors, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
jgimeno committed Jan 5, 2024
1 parent bd792e4 commit 1d42220
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
34 changes: 14 additions & 20 deletions contracts/airdrop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use cosmwasm_std::{
Empty, Env, MessageInfo, Response, StdError, StdResult, Uint128,
};
use cw2::{get_contract_version, set_contract_version};
use cw2::VersionError::Std;
use semver::Version;

pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
Expand Down Expand Up @@ -103,29 +104,22 @@ pub fn reward_users(
) -> Result<Response, StdError> {
let mut res = vec![];

let mut campaign = CAMPAIGN.load(deps.storage).map_err(|_| {
StdError::generic_err("Failed to load campaign data")

Check warning on line 108 in contracts/airdrop/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L108

Added line #L108 was not covered by tests
})?;

if campaign.owner != info.sender
&& !campaign.managers.contains(&info.sender)
{
return Err(StdError::generic_err("Unauthorized"));

Check warning on line 114 in contracts/airdrop/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L114

Added line #L114 was not covered by tests
}

for req in requests {
let mut campaign = CAMPAIGN.load(deps.storage).map_err(|_| {
StdError::generic_err("Failed to load campaign data")
})?;

if campaign.owner != info.sender
&& !campaign.managers.contains(&info.sender)
{
res.push(RewardUserResponse {
user_address: req.user_address.clone(),
success: false,
error_msg: "Unauthorized".to_string(),
});
continue;
}

if campaign.unallocated_amount < req.amount {
res.push(RewardUserResponse {
user_address: req.user_address.clone(),
success: false,
error_msg: "Not enough funds in campaign".to_string(),
});
continue;
return Err(StdError::generic_err(
"Not enough funds in the campaign",
));

Check warning on line 122 in contracts/airdrop/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L120-L122

Added lines #L120 - L122 were not covered by tests
}

match USER_REWARDS.may_load(deps.storage, req.user_address.clone())? {
Expand Down
5 changes: 5 additions & 0 deletions contracts/airdrop/src/tests/execute/reward_users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,8 @@ fn test_reward_users_as_manager() {
Uint128::new(250)
);
}

fn test_fails_when_we_try_to_allocate_more_than_available() {
let mut deps = mock_dependencies();
let env = mock_env();
}

0 comments on commit 1d42220

Please sign in to comment.