-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
272 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use crate::contract::{claim, instantiate, reward_users}; | ||
use crate::msg::{InstantiateMsg, RewardUserRequest}; | ||
use crate::state::{USER_REWARDS}; | ||
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; | ||
use cosmwasm_std::{coins, Addr, BankMsg, CosmosMsg, StdError, SubMsg, Uint128}; | ||
use std::vec; | ||
|
||
#[test] | ||
fn test_claim() { | ||
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(), | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
reward_users( | ||
deps.as_mut(), | ||
env.clone(), | ||
mock_info("owner", &[]), | ||
vec![ | ||
RewardUserRequest { | ||
user_address: Addr::unchecked("user1"), | ||
amount: Uint128::new(750), | ||
}, | ||
RewardUserRequest { | ||
user_address: Addr::unchecked("user2"), | ||
amount: Uint128::new(250), | ||
}, | ||
], | ||
) | ||
.unwrap(); | ||
|
||
// try to claim from user1 | ||
let resp = | ||
claim(deps.as_mut(), env.clone(), mock_info("user1", &[])).unwrap(); | ||
|
||
assert_eq!( | ||
resp.messages, | ||
vec![SubMsg::new(CosmosMsg::Bank(BankMsg::Send { | ||
to_address: "user1".to_string(), | ||
amount: coins(750, ""), | ||
}))] | ||
); | ||
assert_eq!( | ||
USER_REWARDS.has(deps.as_ref().storage, Addr::unchecked("user1")), | ||
false | ||
); | ||
|
||
// try to claim from user2 | ||
let resp = | ||
claim(deps.as_mut(), env.clone(), mock_info("user2", &[])).unwrap(); | ||
|
||
assert_eq!( | ||
resp.messages, | ||
vec![SubMsg::new(CosmosMsg::Bank(BankMsg::Send { | ||
to_address: "user2".to_string(), | ||
amount: coins(250, ""), | ||
}))] | ||
); | ||
assert_eq!( | ||
USER_REWARDS.has(deps.as_ref().storage, Addr::unchecked("user2")), | ||
false | ||
); | ||
|
||
// try to claim from user3 who doesn't exist | ||
let resp = claim(deps.as_mut(), env.clone(), mock_info("user3", &[])); | ||
|
||
assert_eq!(resp, Err(StdError::generic_err("User pool does not exist"))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod claim; | ||
mod reward_all; | ||
mod withdraw; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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 std::vec; | ||
|
||
#[test] | ||
fn test_reward_users_fully_allocated() { | ||
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(), | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
let resp = reward_users( | ||
deps.as_mut(), | ||
env.clone(), | ||
mock_info("owner", &[]), | ||
vec![ | ||
RewardUserRequest { | ||
user_address: Addr::unchecked("user1"), | ||
amount: Uint128::new(750), | ||
}, | ||
RewardUserRequest { | ||
user_address: Addr::unchecked("user2"), | ||
amount: Uint128::new(250), | ||
}, | ||
], | ||
) | ||
.unwrap(); | ||
|
||
// assert response | ||
let user_responses: Vec<RewardUserResponse> = | ||
from_json(resp.data.unwrap()).unwrap(); | ||
assert_eq!( | ||
user_responses, | ||
vec![ | ||
RewardUserResponse { | ||
user_address: Addr::unchecked("user1"), | ||
success: true, | ||
error_msg: "".to_string(), | ||
}, | ||
RewardUserResponse { | ||
user_address: Addr::unchecked("user2"), | ||
success: true, | ||
error_msg: "".to_string(), | ||
}, | ||
] | ||
); | ||
|
||
// assert inner state of the contract | ||
let campaign = CAMPAIGN.load(deps.as_ref().storage).unwrap(); | ||
assert_eq!( | ||
campaign, | ||
Campaign { | ||
owner: Addr::unchecked("owner"), | ||
unallocated_amount: Uint128::zero(), | ||
campaign_id: "campaign_id".to_string(), | ||
campaign_name: "campaign_name".to_string(), | ||
campaign_description: "campaign_description".to_string(), | ||
} | ||
); | ||
|
||
assert_eq!( | ||
USER_REWARDS | ||
.load(deps.as_ref().storage, Addr::unchecked("user1")) | ||
.unwrap(), | ||
Uint128::new(750) | ||
); | ||
|
||
assert_eq!( | ||
USER_REWARDS | ||
.load(deps.as_ref().storage, Addr::unchecked("user2")) | ||
.unwrap(), | ||
Uint128::new(250) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use crate::contract::{instantiate, withdraw}; | ||
use crate::msg::InstantiateMsg; | ||
use cosmwasm_std::testing::{ | ||
mock_dependencies, mock_dependencies_with_balance, mock_env, mock_info, | ||
}; | ||
use cosmwasm_std::{coins, BankMsg, CosmosMsg, StdError, SubMsg, Uint128}; | ||
use std::vec; | ||
|
||
#[test] | ||
fn test_withdraw_ok() { | ||
let mut deps = mock_dependencies_with_balance(&coins(1000, "")); | ||
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(), | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
// try to withdraw | ||
let resp = withdraw( | ||
deps.as_mut(), | ||
env.clone(), | ||
mock_info("owner", &[]), | ||
Uint128::new(1000), | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
resp.messages, | ||
vec![SubMsg::new(CosmosMsg::Bank(BankMsg::Send { | ||
to_address: "owner".to_string(), | ||
amount: coins(1000, ""), | ||
}))] | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_withdraw_too_much() { | ||
let mut deps = mock_dependencies_with_balance(&coins(1000, "")); | ||
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(), | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
// try to withdraw | ||
let resp = withdraw( | ||
deps.as_mut(), | ||
env.clone(), | ||
mock_info("owner", &[]), | ||
Uint128::new(1001), | ||
); | ||
|
||
assert_eq!( | ||
resp, | ||
Err(StdError::generic_err("Not enough funds in the contract")) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_withdraw_unauthorized() { | ||
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(), | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
// try to withdraw | ||
let res = withdraw( | ||
deps.as_mut(), | ||
env.clone(), | ||
mock_info("not_owner", &[]), | ||
Uint128::new(1000), | ||
); | ||
assert_eq!( | ||
res, | ||
Err(StdError::generic_err("Only contract owner can withdraw")) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
mod instantiate; | ||
// mod execute; | ||
mod execute; | ||
mod query; |