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

46 fee splitter tests #47

Merged
merged 16 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dex = { path = "./packages/dex", default-features = false }
dex-factory = { path = "./contracts/factory", default-features = false }
dex-pool = { path = "./contracts/pool", default-features = false }
dex-stake = { path = "./contracts/stake", default-features = false }
dex-fee-splitter = { path = "./contracts/fee_splitter", default-features = false }
itertools = "0.10"
proptest = "1.0"
schemars = "0.8"
Expand Down
2 changes: 1 addition & 1 deletion contracts/fee_splitter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ anyhow = { workspace = true }
bindings-test = { workspace = true }
cw-multi-test = { workspace = true }
cw20-base = { workspace = true }
# dex-factory = { workspace = true }
dex-factory = { workspace = true }
dex-pool = { workspace = true }
dex-stake = { workspace = true }
21 changes: 4 additions & 17 deletions contracts/fee_splitter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ use cosmwasm_std::{
DepsMut, Env, MessageInfo, StdError, StdResult, WasmMsg,
};
use cw20::{BalanceResponse, Cw20ExecuteMsg, Cw20QueryMsg};
use cw_storage_plus::Item;

use crate::{
error::ContractError,
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
state::Config,
state::{Config, CONFIG},
};

/// Saves factory settings
pub const CONFIG: Item<Config> = Item::new("config");

pub type Response = cosmwasm_std::Response<CoreumMsg>;
pub type SubMsg = cosmwasm_std::SubMsg<CoreumMsg>;

Expand All @@ -38,7 +34,7 @@ pub fn instantiate(
.iter()
.map(|&(_, weight)| weight)
.fold(Decimal::zero(), |acc, x| acc + x)
.le(&Decimal::from_ratio(1u32, 1u32));
.le(&Decimal::percent(100u64));
gangov marked this conversation as resolved.
Show resolved Hide resolved

if !is_weights_valid {
return Err(ContractError::InvalidWeights {});
Expand All @@ -50,7 +46,7 @@ pub fn instantiate(

CONFIG.save(deps.storage, &config)?;

Ok(Response::new().add_attribute("initialized", "contract"))
Ok(Response::new().add_attribute("initialized", "fee_splitter contract"))
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand All @@ -74,7 +70,7 @@ fn execute_send_tokens(
native_denoms: Vec<String>,
cw20_addresses: Vec<String>,
) -> Result<Response, ContractError> {
let config = query_config(deps.as_ref())?;
let config = CONFIG.load(deps.storage)?;

let contract_address = env.contract.address.to_string();
// gather balances of native tokens, either from function parameter or all
Expand Down Expand Up @@ -163,12 +159,3 @@ pub fn query_config(deps: Deps<CoreumQueries>) -> StdResult<Config> {

Ok(resp)
}

#[cfg(test)]
mod tests {
#[test]
#[ignore]
fn instantiate_with_invalid_weights_should_throw_error() {
todo!()
}
}
3 changes: 3 additions & 0 deletions contracts/fee_splitter/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Decimal;
use cw_storage_plus::Item;

#[cw_serde]
pub struct Config {
// List of addresses and their weights.
// Weights must sum up to 1.0
pub addresses: Vec<(String, Decimal)>,
}

pub const CONFIG: Item<Config> = Item::new("config");
200 changes: 200 additions & 0 deletions contracts/fee_splitter/src/testing.rs
Original file line number Diff line number Diff line change
@@ -1 +1,201 @@
use bindings_test::mock_coreum_deps;
use cosmwasm_std::{
from_json,
testing::{mock_env, mock_info, MOCK_CONTRACT_ADDR},
to_json_binary, Attribute, BankMsg, Coin, CosmosMsg, Decimal, ReplyOn, Uint128, WasmMsg,
};
use cw20::Cw20ExecuteMsg;

use crate::{
contract::{execute, instantiate, query, SubMsg},
error::ContractError,
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
state::Config,
};

const SENDER: &str = "addr0000";
const FIRST_RECIPIENT: &str = "address0000";
const SECOND_RECIPIENT: &str = "address0001";
const ATOM: &str = "ATOM";
const TIA: &str = "TIA";
const USDT: &str = "USDT";
const CW20_ASSET_ONE: &str = "asset0000";
const CW20_ASSET_TWO: &str = "asset0001";

#[test]
fn init_works() {
let mut deps = mock_coreum_deps(&[]);
let env = mock_env();
let info = mock_info(SENDER, &[]);

let first_addr_percent = (FIRST_RECIPIENT.to_string(), Decimal::percent(50u64));
let second_addr_percent = (SECOND_RECIPIENT.to_string(), Decimal::percent(50u64));
let msg = InstantiateMsg {
addresses: vec![first_addr_percent.clone(), second_addr_percent.clone()],
cw20_contracts: vec![USDT.to_string()],
};

let res = instantiate(deps.as_mut(), env, info, msg).unwrap();

assert_eq!(
res.attributes,
vec![Attribute {
key: "initialized".to_string(),
value: "fee_splitter contract".to_string(),
}]
);
}

#[test]
fn fails_to_init_because_weights_not_correct() {
let mut deps = mock_coreum_deps(&[]);
let env = mock_env();
let info = mock_info(SENDER, &[]);

let first_addr_percent = (FIRST_RECIPIENT.to_string(), Decimal::percent(50u64));
let second_addr_percent = (SECOND_RECIPIENT.to_string(), Decimal::percent(60u64));
let msg = InstantiateMsg {
addresses: vec![first_addr_percent.clone(), second_addr_percent.clone()],
cw20_contracts: vec![USDT.to_string()],
};

let res = instantiate(deps.as_mut(), env, info, msg).unwrap_err();
assert_eq!(res, ContractError::InvalidWeights {});
}

#[test]
fn should_send_tokens_in_correct_amount() {
let mut deps = mock_coreum_deps(&[]);

deps.querier.with_token_balances(&[(
&String::from(CW20_ASSET_ONE),
&[(&String::from(MOCK_CONTRACT_ADDR), &Uint128::new(100_000))],
)]);

deps.querier.with_balance(&[(
&String::from(MOCK_CONTRACT_ADDR),
&[
Coin {
denom: ATOM.to_string(),
amount: Uint128::new(100_000),
},
Coin {
denom: TIA.to_string(),
amount: Uint128::new(100_000),
},
],
)]);

let env = mock_env();

let info = mock_info(SENDER, &[]);
let msg = InstantiateMsg {
addresses: vec![
(FIRST_RECIPIENT.to_string(), Decimal::percent(60u64)),
(SECOND_RECIPIENT.to_string(), Decimal::percent(40u64)),
],
cw20_contracts: vec![CW20_ASSET_ONE.to_string(), CW20_ASSET_TWO.to_string()],
};

let fee_splitter_instance = instantiate(deps.as_mut(), env.clone(), info.clone(), msg).unwrap();

assert_eq!(
fee_splitter_instance.attributes,
vec![Attribute {
key: "initialized".to_string(),
value: "fee_splitter contract".to_string(),
}]
);

let msg = ExecuteMsg::SendTokens {
native_denoms: vec![ATOM.to_string(), TIA.to_string()],
cw20_addresses: vec![CW20_ASSET_ONE.to_string()],
};

let res = execute(deps.as_mut(), env.clone(), info, msg).unwrap();

assert_eq!(
res.messages,
vec![
SubMsg {
id: 0,
msg: CosmosMsg::Bank(BankMsg::Send {
to_address: FIRST_RECIPIENT.to_string(),
amount: vec![
Coin {
denom: ATOM.to_string(),
amount: Uint128::new(60_000),
},
Coin {
denom: TIA.to_string(),
amount: Uint128::new(60_000),
}
]
}),
gas_limit: None,
reply_on: ReplyOn::Never
},
SubMsg {
id: 0,
msg: CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: CW20_ASSET_ONE.to_string(),
msg: to_json_binary(&Cw20ExecuteMsg::Transfer {
recipient: FIRST_RECIPIENT.to_string(),
amount: Uint128::new(60_000),
})
.unwrap(),
funds: vec![]
}),
gas_limit: None,
reply_on: ReplyOn::Never
},
SubMsg {
id: 0,
msg: CosmosMsg::Bank(BankMsg::Send {
to_address: SECOND_RECIPIENT.to_string(),
amount: vec![
Coin {
denom: ATOM.to_string(),
amount: Uint128::new(40_000),
},
Coin {
denom: TIA.to_string(),
amount: Uint128::new(40_000),
}
]
}),
gas_limit: None,
reply_on: ReplyOn::Never
},
SubMsg {
id: 0,
msg: CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: CW20_ASSET_ONE.to_string(),
msg: to_json_binary(&Cw20ExecuteMsg::Transfer {
recipient: SECOND_RECIPIENT.to_string(),
amount: Uint128::new(40_000),
})
.unwrap(),
funds: vec![]
}),
gas_limit: None,
reply_on: ReplyOn::Never
},
]
);

let msg = QueryMsg::Config {};

let query_result = query(deps.as_ref(), env, msg).unwrap();
let config_res: Config = from_json(query_result).unwrap();

assert_eq!(
config_res,
Config {
addresses: vec![
(FIRST_RECIPIENT.to_string(), Decimal::percent(60)),
(SECOND_RECIPIENT.to_string(), Decimal::percent(40))
],
}
);
}
3 changes: 2 additions & 1 deletion packages/bindings-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ cosmwasm-std = { workspace = true }
cw-multi-test = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }

dex = { workspace = true }
cw20 = { workspace = true }

7 changes: 4 additions & 3 deletions packages/bindings-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod multitest;
mod testing;

pub use multitest::{
mock_coreum_deps, CoreumApp, CoreumAppWrapped, CoreumDeps, CoreumModule, BLOCK_TIME,
};
pub use multitest::{CoreumApp, CoreumAppWrapped, CoreumModule, BLOCK_TIME};

pub use testing::{mock_coreum_deps, CoreumDeps};
16 changes: 2 additions & 14 deletions packages/bindings-test/src/multitest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
cmp::max,
fmt::Debug,
marker::PhantomData,
ops::{Deref, DerefMut},
};

Expand All @@ -14,9 +13,9 @@ use coreum_wasm_sdk::{
core::{CoreumMsg, CoreumQueries},
};
use cosmwasm_std::{
testing::{MockApi, MockQuerier, MockStorage},
testing::{MockApi, MockStorage},
to_json_binary, Addr, Api, BalanceResponse, BankMsg, BankQuery, Binary, BlockInfo, CustomQuery,
Empty, OwnedDeps, Querier, QuerierWrapper, QueryRequest, Storage,
Empty, Querier, QuerierWrapper, QueryRequest, Storage,
};
use cw_multi_test::{
App, AppResponse, BankKeeper, BankSudo, BasicAppBuilder, CosmosRouter, Module, WasmKeeper,
Expand All @@ -26,17 +25,6 @@ use cw_multi_test::{
/// (when we increment block.height, use this multiplier for block.time)
pub const BLOCK_TIME: u64 = 5;

pub type CoreumDeps = OwnedDeps<MockStorage, MockApi, MockQuerier, CoreumQueries>;

pub fn mock_coreum_deps() -> CoreumDeps {
CoreumDeps {
storage: MockStorage::default(),
api: MockApi::default(),
querier: MockQuerier::default(),
custom_query_type: PhantomData,
}
}

pub struct CoreumModule {}

impl Module for CoreumModule {
Expand Down
Loading
Loading