Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x committed Dec 6, 2023
1 parent 149890a commit 66793ef
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 59 deletions.
161 changes: 151 additions & 10 deletions contracts/liquidity_hub/fee_collector/src/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn collect_all_factories_cw20_fees_successfully() {
let creator = mock_creator();

let fee_collector_id = store_fee_collector_code(&mut app);
let fee_distributor_id = store_fee_distributor_code(&mut app);
let pool_factory_id = store_pool_factory_code(&mut app);
let pool_router_id = store_pool_router_code(&mut app);
let pair_id = store_pair_code(&mut app);
Expand Down Expand Up @@ -380,6 +381,43 @@ fn collect_all_factories_cw20_fees_successfully() {
let ask_asset = AssetInfo::Token {
contract_addr: cw20_tokens[0].to_string(),
};

// init fee distributor

let fee_distributor_address = app
.instantiate_contract(
fee_distributor_id,
creator.clone().sender,
&white_whale::fee_distributor::InstantiateMsg {
bonding_contract_addr: "whale_lair".clone().to_string(),
fee_collector_addr: fee_collector_address.clone().to_string(),
grace_period: Uint64::new(1),
epoch_config: EpochConfig {
duration: Uint64::new(86_400_000_000_000u64), // a day
genesis_epoch: Uint64::new(1678802400_000000000u64), // March 14, 2023 2:00:00 PM
},
distribution_asset: ask_asset.clone(),
},
&[],
"fee_distributor",
None,
)
.unwrap();

app.execute_contract(
creator.sender.clone(),
fee_collector_address.clone(),
&white_whale::fee_collector::ExecuteMsg::UpdateConfig {
owner: None,
pool_router: None,
fee_distributor: Some(fee_distributor_address.to_string()),
pool_factory: None,
vault_factory: None,
},
&[],
)
.unwrap();

let mut ask_asset_original_balance = Uint128::zero();
for (asset_addr, asset) in assets_collected.clone() {
let balance_res: BalanceResponse = app
Expand Down Expand Up @@ -458,7 +496,6 @@ fn collect_all_factories_cw20_fees_successfully() {
creator.sender,
fee_collector_address.clone(),
&AggregateFees {
asset_info: ask_asset,
aggregate_fees_for: FeesFor::Factory {
factory_addr: pool_factory_address.to_string(),
factory_type: FactoryType::Pool {
Expand Down Expand Up @@ -941,6 +978,7 @@ fn collect_pools_native_fees_successfully() {
let mut app = mock_app_with_balance(balances);

let fee_collector_id = store_fee_collector_code(&mut app);
let fee_distributor_id = store_fee_distributor_code(&mut app);
let pool_factory_id = store_pool_factory_code(&mut app);
let pool_router_id = store_pool_router_code(&mut app);
let pair_id = store_pair_code(&mut app);
Expand Down Expand Up @@ -1285,6 +1323,41 @@ fn collect_pools_native_fees_successfully() {
let ask_asset = AssetInfo::Token {
contract_addr: cw20_tokens[0].to_string(),
};

let fee_distributor_address = app
.instantiate_contract(
fee_distributor_id,
creator.clone().sender,
&white_whale::fee_distributor::InstantiateMsg {
bonding_contract_addr: "whale_lair".clone().to_string(),
fee_collector_addr: fee_collector_address.clone().to_string(),
grace_period: Uint64::new(1),
epoch_config: EpochConfig {
duration: Uint64::new(86_400_000_000_000u64), // a day
genesis_epoch: Uint64::new(1678802400_000000000u64), // March 14, 2023 2:00:00 PM
},
distribution_asset: ask_asset.clone(),
},
&[],
"fee_distributor",
None,
)
.unwrap();

app.execute_contract(
creator.sender.clone(),
fee_collector_address.clone(),
&white_whale::fee_collector::ExecuteMsg::UpdateConfig {
owner: None,
pool_router: None,
fee_distributor: Some(fee_distributor_address.to_string()),
pool_factory: None,
vault_factory: None,
},
&[],
)
.unwrap();

let mut ask_asset_original_balance = Uint128::zero();
for (asset_id, asset) in assets_collected.clone() {
if asset_id == "native" {
Expand Down Expand Up @@ -1435,7 +1508,6 @@ fn collect_pools_native_fees_successfully() {
creator.sender,
fee_collector_address.clone(),
&AggregateFees {
asset_info: ask_asset,
aggregate_fees_for: FeesFor::Factory {
factory_addr: pool_factory_address.to_string(),
factory_type: FactoryType::Pool {
Expand Down Expand Up @@ -2154,6 +2226,7 @@ fn aggregate_fees_for_vault() {
let mut app = mock_app_with_balance(balances);

let fee_collector_id = store_fee_collector_code(&mut app);
let fee_distributor_id = store_fee_distributor_code(&mut app);
let vault_factory_id = store_vault_factory_code(&mut app);
let pool_factory_id = store_pool_factory_code(&mut app);
let pool_router_id = store_pool_router_code(&mut app);
Expand All @@ -2174,6 +2247,42 @@ fn aggregate_fees_for_vault() {
)
.unwrap();

let fee_distributor_address = app
.instantiate_contract(
fee_distributor_id,
creator.clone().sender,
&white_whale::fee_distributor::InstantiateMsg {
bonding_contract_addr: "whale_lair".clone().to_string(),
fee_collector_addr: fee_collector_address.clone().to_string(),
grace_period: Uint64::new(1),
epoch_config: EpochConfig {
duration: Uint64::new(86_400_000_000_000u64), // a day
genesis_epoch: Uint64::new(1678802400_000000000u64), // March 14, 2023 2:00:00 PM
},
distribution_asset: AssetInfo::NativeToken {
denom: "uatom".to_string(),
},
},
&[],
"fee_distributor",
None,
)
.unwrap();

app.execute_contract(
creator.sender.clone(),
fee_collector_address.clone(),
&white_whale::fee_collector::ExecuteMsg::UpdateConfig {
owner: None,
pool_router: None,
fee_distributor: Some(fee_distributor_address.to_string()),
pool_factory: None,
vault_factory: None,
},
&[],
)
.unwrap();

let vault_factory_address = app
.instantiate_contract(
vault_factory_id,
Expand Down Expand Up @@ -2512,7 +2621,6 @@ fn aggregate_fees_for_vault() {
creator.sender.clone(),
fee_collector_address.clone(),
&AggregateFees {
asset_info: ask_asset.clone(),
aggregate_fees_for: FeesFor::Factory {
factory_addr: vault_factory_address.to_string(),
factory_type: FactoryType::Vault {
Expand Down Expand Up @@ -2650,7 +2758,6 @@ fn aggregate_fees_for_vault() {
creator.sender,
fee_collector_address.clone(),
&AggregateFees {
asset_info: ask_asset.clone(),
aggregate_fees_for: FeesFor::Factory {
factory_addr: vault_factory_address.to_string(),
factory_type: FactoryType::Vault {
Expand Down Expand Up @@ -5748,6 +5855,7 @@ fn aggregate_fees_unsuccessfully() {
let mut app = mock_app();

let fee_collector_id = store_fee_collector_code(&mut app);
let fee_distributor_id = store_fee_distributor_code(&mut app);

let fee_collector_address = app
.instantiate_contract(
Expand All @@ -5760,15 +5868,48 @@ fn aggregate_fees_unsuccessfully() {
)
.unwrap();

// try to aggregate fees from an unauthorized address
let fee_distributor_address = app
.instantiate_contract(
fee_distributor_id,
creator.clone().sender,
&white_whale::fee_distributor::InstantiateMsg {
bonding_contract_addr: "whale_lair".to_string(),
fee_collector_addr: fee_collector_address.to_string(),
grace_period: Uint64::new(5),
epoch_config: EpochConfig {
duration: Uint64::new(86400000000000),
genesis_epoch: Default::default(),
},
distribution_asset: AssetInfo::NativeToken {
denom: "uwhale".to_string(),
},
},
&[],
"fee_distributor",
None,
)
.unwrap();

// update the fee_distributor_address on fee collector
app.execute_contract(
creator.sender.clone(),
fee_collector_address.clone(),
&UpdateConfig {
owner: None,
pool_router: None,
fee_distributor: Some(fee_distributor_address.to_string()),
pool_factory: None,
vault_factory: None,
},
&[],
)
.unwrap();

let err = app
.execute_contract(
Addr::unchecked("unauthorized"),
Addr::unchecked("anyone"),
fee_collector_address.clone(),
&AggregateFees {
asset_info: AssetInfo::NativeToken {
denom: "uwhale".to_string(),
},
aggregate_fees_for: FeesFor::Contracts { contracts: vec![] },
},
&[],
Expand All @@ -5777,7 +5918,7 @@ fn aggregate_fees_unsuccessfully() {

assert_eq!(
err.downcast::<ContractError>().unwrap(),
ContractError::Unauthorized {}
ContractError::InvalidContractsFeeAggregation {}
);
}

Expand Down
50 changes: 1 addition & 49 deletions contracts/liquidity_hub/fee_collector/src/tests/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ use cosmwasm_std::testing::{mock_env, mock_info};
use cosmwasm_std::{from_binary, Addr, DepsMut, MessageInfo, Response};
use cw2::{get_contract_version, set_contract_version, ContractVersion};
use std::env;
use white_whale::pool_network::asset::AssetInfo;

use crate::contract::{execute, instantiate, migrate, query};
use white_whale::pool_network::mock_querier::mock_dependencies;

use crate::ContractError;
use white_whale::fee_collector::ExecuteMsg::AggregateFees;
use white_whale::fee_collector::{
Config, ExecuteMsg, FeesFor, InstantiateMsg, MigrateMsg, QueryMsg,
};
use white_whale::fee_collector::{Config, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};

pub fn mock_instantiation(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
let msg = InstantiateMsg {};
Expand All @@ -32,27 +28,6 @@ fn proper_initialization() {
assert_eq!("owner".to_string(), config_res.owner);
}

#[test]
fn collect_fees_unsuccessfully_unauthorized() {
let mut deps = mock_dependencies(&[]);
let info = mock_info("owner", &[]);
mock_instantiation(deps.as_mut(), info).unwrap();

// unauthorized tries collecting fees
let info = mock_info("unauthorized", &[]);
let msg = ExecuteMsg::CollectFees {
collect_fees_for: FeesFor::Contracts { contracts: vec![] },
};

let res = execute(deps.as_mut(), mock_env(), info, msg);

match res {
Ok(_) => panic!("should return ContractError::Unauthorized"),
Err(ContractError::Unauthorized {}) => (),
_ => panic!("should return ContractError::Unauthorized"),
}
}

#[test]
fn test_update_config_successfully() {
let mut deps = mock_dependencies(&[]);
Expand Down Expand Up @@ -149,26 +124,3 @@ fn test_migration() {
_ => panic!("should return ContractError::Std"),
}
}

#[test]
fn test_aggregate_fee_for_contracts_err() {
let mut deps = mock_dependencies(&[]);
let info = mock_info("owner", &[]);
mock_instantiation(deps.as_mut(), info.clone()).unwrap();

// should error, can't collect fees for contracts
let msg = AggregateFees {
asset_info: AssetInfo::NativeToken {
denom: "uluna".to_string(),
},
aggregate_fees_for: FeesFor::Contracts { contracts: vec![] },
};

let res = execute(deps.as_mut(), mock_env(), info, msg);

match res {
Ok(_) => panic!("should return ContractError::InvalidContractsFeeAggregation"),
Err(ContractError::InvalidContractsFeeAggregation {}) => (),
_ => panic!("should return ContractError::InvalidContractsFeeAggregation"),
}
}

0 comments on commit 66793ef

Please sign in to comment.