Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some refactors to airdrop contract #111

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions contracts/airdrop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,48 @@
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(
deps: DepsMut,
_env: Env,
_msg: Empty,
) -> Result<Response, StdError> {
let new_version: Version = CONTRACT_VERSION
.parse()
.map_err(|_| StdError::generic_err("Invalid contract version format"))?;
let current_version = get_contract_version(deps.storage)?;

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

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L55-L63

Added lines #L55 - L63 were not covered by tests

if current_version.contract != CONTRACT_NAME {
return Err(StdError::generic_err(
"Can only upgrade from same contract type",
));
}

if current_version.version.parse::<Version>().unwrap() >= new_version {
return Err(StdError::generic_err(
"Cannot upgrade from a newer contract version",
));
}

set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

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

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L65-L77

Added lines #L65 - L77 were not covered by tests

Ok(Response::new().add_attribute("method", "migrate"))
}

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

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L79-L80

Added lines #L79 - L80 were not covered by tests

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, StdError> {
match msg {
ExecuteMsg::RewardUsers { requests } => {
reward_users(deps, env, info, requests)

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

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L83-L91

Added lines #L83 - L91 were not covered by tests
}
ExecuteMsg::Claim {} => claim(deps, env, info),
ExecuteMsg::Withdraw { amount } => withdraw(deps, env, info, amount),

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

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L93-L94

Added lines #L93 - L94 were not covered by tests
}
}

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

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L96

Added line #L96 was not covered by tests

pub fn reward_users(
deps: DepsMut,
Expand All @@ -103,39 +103,32 @@
) -> 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 107 in contracts/airdrop/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L107

Added line #L107 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 113 in contracts/airdrop/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L113

Added line #L113 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",
));
}

match USER_REWARDS.may_load(deps.storage, req.user_address.clone())? {
Some(mut user_reward) => {
user_reward += req.amount;
USER_REWARDS.save(
deps.storage,
req.user_address.clone(),
&user_reward,
)?;

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

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L125-L131

Added lines #L125 - L131 were not covered by tests
}
None => {
USER_REWARDS.save(
Expand Down Expand Up @@ -224,20 +217,20 @@
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Campaign {} => query_campaign(deps, env),
QueryMsg::GetUserReward { user_address } => {
query_user_reward(deps, env, user_address)

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

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L220-L224

Added lines #L220 - L224 were not covered by tests
}
}
}

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

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L227

Added line #L227 was not covered by tests

pub fn query_campaign(deps: Deps, _env: Env) -> StdResult<Binary> {
match CAMPAIGN.load(deps.storage) {
Ok(campaign) => return to_json_binary(&campaign),
Err(_) => {
return Err(StdError::generic_err("Failed to load campaign data"))

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

View check run for this annotation

Codecov / codecov/patch

contracts/airdrop/src/contract.rs#L233

Added line #L233 was not covered by tests
}
}
}
Expand Down
45 changes: 44 additions & 1 deletion contracts/airdrop/src/tests/execute/reward_users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::contract::{instantiate, reward_users};
use crate::msg::{InstantiateMsg, RewardUserRequest, RewardUserResponse};
use crate::state::{Campaign, CAMPAIGN, USER_REWARDS};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{coins, from_json, Addr, Uint128};
use cosmwasm_std::{coins, from_json, Addr, Uint128, StdError};
use std::vec;

#[test]
Expand Down Expand Up @@ -171,3 +171,46 @@ fn test_reward_users_as_manager() {
Uint128::new(250)
);
}

#[test]
fn test_fails_when_we_try_to_allocate_more_than_available() {
let mut deps = mock_dependencies();
let env = mock_env();

instantiate(
deps.as_mut(),
env.clone(),
mock_info("owner", &coins(1000, "")),
InstantiateMsg {
campaign_id: "campaign_id".to_string(),
campaign_name: "campaign_name".to_string(),
campaign_description: "campaign_description".to_string(),
managers: vec![Addr::unchecked("manager1"), Addr::unchecked("manager2")],
},
)
.unwrap();

let resp = reward_users(
deps.as_mut(),
env.clone(),
mock_info("manager1", &[]),
vec![
RewardUserRequest {
user_address: Addr::unchecked("user1"),
amount: Uint128::new(750),
},
RewardUserRequest {
user_address: Addr::unchecked("user2"),
amount: Uint128::new(250),
},
RewardUserRequest {
user_address: Addr::unchecked("user3"),
amount: Uint128::new(251),
},
],
);

assert_eq!(resp, Err(StdError::generic_err(
"Not enough funds in the campaign",
)));
}
2 changes: 0 additions & 2 deletions nibiru-std/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ mod type_url_cosmos;
mod type_url_nibiru;

pub use traits::*;
pub use type_url_cosmos::*;
pub use type_url_nibiru::*;

pub mod cosmos {
/// Authentication of accounts and transactions.
Expand Down
Loading