Skip to content

Commit

Permalink
Merge branch 'release/v2_contracts' into feat/add-remove-swap-routes
Browse files Browse the repository at this point in the history
  • Loading branch information
nseguias authored Apr 23, 2024
2 parents 70976da + f14fb74 commit 4abb16e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 deletions.
3 changes: 2 additions & 1 deletion contracts/liquidity_hub/pool-manager/src/manager/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub const MAX_ASSETS_PER_POOL: usize = 4;
/// "uatom".into(),
/// "uscrt".into(),
/// ];
/// let asset_decimals = vec![6, 6];
/// #[cfg(not(feature = "osmosis"))]
/// let pool_fees = PoolFee {
/// protocol_fee: Fee {
Expand Down Expand Up @@ -68,7 +69,7 @@ pub const MAX_ASSETS_PER_POOL: usize = 4;
/// let pair_type = PairType::ConstantProduct;
/// let token_factory_lp = false;
///
/// let response = create_pair(deps, env, info, asset_infos, pool_fees, pair_type, None)?;
/// let response = create_pair(deps, env, info, asset_infos, asset_decimals, pool_fees, pair_type, None)?;
/// # Ok(response)
/// # }
/// ```
Expand Down
15 changes: 9 additions & 6 deletions contracts/liquidity_hub/pool-manager/src/router/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use cosmwasm_std::{
attr, coin, Addr, BankMsg, Coin, CosmosMsg, Decimal, DepsMut, MessageInfo, Response, Uint128,
attr, coin, to_json_binary, Addr, BankMsg, Coin, CosmosMsg, Decimal, DepsMut, MessageInfo,
Response, Uint128, WasmMsg,
};
use white_whale_std::pool_manager::{SwapOperation, SwapRoute};
use white_whale_std::whale_lair;

use crate::{
helpers::simulate_swap_operations,
Expand Down Expand Up @@ -117,12 +119,13 @@ pub fn execute_swap_operations(
amount: vec![swap_result.burn_fee_asset],
}));
}

// todo this should be not a BankMsg but a fill_rewards msg
if !swap_result.protocol_fee_asset.amount.is_zero() {
fee_messages.push(CosmosMsg::Bank(BankMsg::Send {
to_address: config.whale_lair_addr.to_string(),
amount: vec![swap_result.protocol_fee_asset],
fee_messages.push(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.whale_lair_addr.to_string(),
msg: to_json_binary(&whale_lair::ExecuteMsg::FillRewards {
assets: vec![swap_result.protocol_fee_asset.clone()],
})?,
funds: vec![swap_result.protocol_fee_asset.clone()],
}));
}

Expand Down
17 changes: 11 additions & 6 deletions contracts/liquidity_hub/pool-manager/src/swap/commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::{state::MANAGER_CONFIG, ContractError};
use cosmwasm_std::{Addr, BankMsg, Coin, CosmosMsg, DepsMut, Env, MessageInfo, Response};
use cosmwasm_std::{
to_json_binary, Addr, BankMsg, Coin, CosmosMsg, DepsMut, Env, MessageInfo, Response, WasmMsg,
};

pub const MAX_ASSETS_PER_POOL: usize = 4;

use cosmwasm_std::Decimal;
use white_whale_std::whale_lair;

use super::perform_swap::perform_swap;

Expand Down Expand Up @@ -52,18 +55,20 @@ pub fn swap(
amount: vec![swap_result.return_asset.clone()],
}));
}

// then we add the fees
if !swap_result.burn_fee_asset.amount.is_zero() {
messages.push(CosmosMsg::Bank(BankMsg::Burn {
amount: vec![swap_result.burn_fee_asset.clone()],
}));
}

//todo this should be not a BankMsg but a fill_rewards msg
if !swap_result.protocol_fee_asset.amount.is_zero() {
messages.push(CosmosMsg::Bank(BankMsg::Send {
to_address: config.whale_lair_addr.to_string(),
amount: vec![swap_result.protocol_fee_asset.clone()],
messages.push(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.whale_lair_addr.to_string(),
msg: to_json_binary(&whale_lair::ExecuteMsg::FillRewards {
assets: vec![swap_result.protocol_fee_asset.clone()],
})?,
funds: vec![swap_result.protocol_fee_asset.clone()],
}));
}

Expand Down
50 changes: 23 additions & 27 deletions contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ fn instantiate_normal() {
#[test]
fn deposit_and_withdraw_sanity_check() {
let mut suite = TestingSuite::default_with_balances(vec![
coin(1_000_001u128, "uwhale".to_string()),
coin(1_000_001u128, "uluna".to_string()),
coin(1_000_000u128, "uwhale".to_string()),
coin(1_000_000u128, "uluna".to_string()),
coin(1_000u128, "uusd".to_string()),
]);
let creator = suite.creator();
Expand Down Expand Up @@ -323,9 +323,9 @@ mod router {
#[test]
fn basic_swap_operations_test() {
let mut suite = TestingSuite::default_with_balances(vec![
coin(1_000_000_001u128, "uwhale".to_string()),
coin(1_000_000_000u128, "uwhale".to_string()),
coin(1_000_000_000u128, "uluna".to_string()),
coin(1_000_000_001u128, "uusd".to_string()),
coin(1_000_000_000u128, "uusd".to_string()),
]);
let creator = suite.creator();
let _other = suite.senders[1].clone();
Expand Down Expand Up @@ -451,10 +451,9 @@ mod router {
},
];

// before swap uusd balance = 1_000_000_001
// before swap uusd balance = 1_000_000_000
// - 2*1_000 pair creation fee
// - 1_000_000 liquidity provision
// - 1 for native token creation (for decimal precisions)
// = 998_998_000
let pre_swap_amount = 998_998_000;
suite.query_balance(creator.to_string(), "uusd".to_string(), |amt| {
Expand Down Expand Up @@ -799,9 +798,9 @@ mod router {
#[test]
fn sends_to_correct_receiver() {
let mut suite = TestingSuite::default_with_balances(vec![
coin(1_000_000_001u128, "uwhale".to_string()),
coin(1_000_000_000u128, "uwhale".to_string()),
coin(1_000_000_000u128, "uluna".to_string()),
coin(1_000_000_001u128, "uusd".to_string()),
coin(1_000_000_000u128, "uusd".to_string()),
]);
let creator = suite.creator();
let other = suite.senders[1].clone();
Expand Down Expand Up @@ -931,18 +930,18 @@ mod router {
},
];

// before swap uusd balance = 1_000_000_001
// before swap uwhale balance = 1_000_000_001
// before swap uluna balance = 1_000_000_001
let pre_swap_amount = 1_000_000_001;
// before swap uusd balance = 1_000_000_000
// before swap uwhale balance = 1_000_000_000
// before swap uluna balance = 1_000_000_000
let pre_swap_amount = 1_000_000_000;
suite.query_balance(other.to_string(), "uusd".to_string(), |amt| {
assert_eq!(amt.unwrap().amount.u128(), pre_swap_amount);
});
suite.query_balance(other.to_string(), "uwhale".to_string(), |amt| {
assert_eq!(amt.unwrap().amount.u128(), pre_swap_amount);
});
suite.query_balance(other.to_string(), "uluna".to_string(), |amt| {
assert_eq!(amt.unwrap().amount.u128(), pre_swap_amount - 1);
assert_eq!(amt.unwrap().amount.u128(), pre_swap_amount);
});
// also check the same for unauthorized receiver
suite.query_balance(other.to_string(), "uusd".to_string(), |amt| {
Expand All @@ -952,30 +951,28 @@ mod router {
assert_eq!(amt.unwrap().amount.u128(), pre_swap_amount);
});
suite.query_balance(other.to_string(), "uluna".to_string(), |amt| {
assert_eq!(amt.unwrap().amount.u128(), pre_swap_amount - 1);
assert_eq!(amt.unwrap().amount.u128(), pre_swap_amount);
});
// also check for contract
// when we add tokens to the contract, we must send a fee of 1_u128 so the contract
// can register the native token
suite.query_balance(
suite.pool_manager_addr.to_string(),
"uusd".to_string(),
|amt| {
assert_eq!(amt.unwrap().amount.u128(), liquidity_amount + 1);
assert_eq!(amt.unwrap().amount.u128(), liquidity_amount);
},
);
suite.query_balance(
suite.pool_manager_addr.to_string(),
"uwhale".to_string(),
|amt| {
assert_eq!(amt.unwrap().amount.u128(), liquidity_amount + 1);
assert_eq!(amt.unwrap().amount.u128(), liquidity_amount);
},
);
suite.query_balance(
suite.pool_manager_addr.to_string(),
"uluna".to_string(),
|amt| {
assert_eq!(amt.unwrap().amount.u128(), 2 * liquidity_amount + 1);
assert_eq!(amt.unwrap().amount.u128(), 2 * liquidity_amount);
},
);

Expand Down Expand Up @@ -1003,31 +1000,31 @@ mod router {
suite.pool_manager_addr.to_string(),
"uusd".to_string(),
|amt| {
assert_eq!(amt.unwrap().amount.u128(), liquidity_amount - 998 + 1);
assert_eq!(amt.unwrap().amount.u128(), liquidity_amount - 998);
},
);
suite.query_balance(
suite.pool_manager_addr.to_string(),
"uwhale".to_string(),
|amt| {
assert_eq!(amt.unwrap().amount.u128(), liquidity_amount + 1000 + 1);
assert_eq!(amt.unwrap().amount.u128(), liquidity_amount + 1000);
},
);
suite.query_balance(
suite.pool_manager_addr.to_string(),
"uluna".to_string(),
|amt| {
assert_eq!(amt.unwrap().amount.u128(), 2 * liquidity_amount + 1);
assert_eq!(amt.unwrap().amount.u128(), 2 * liquidity_amount);
},
);
}

#[test]
fn checks_minimum_receive() {
let mut suite = TestingSuite::default_with_balances(vec![
coin(1_000_000_001u128, "uwhale".to_string()),
coin(1_000_000_000u128, "uwhale".to_string()),
coin(1_000_000_000u128, "uluna".to_string()),
coin(1_000_000_001u128, "uusd".to_string()),
coin(1_000_000_000u128, "uusd".to_string()),
]);
let creator = suite.creator();
let _other = suite.senders[1].clone();
Expand Down Expand Up @@ -1153,10 +1150,9 @@ mod router {
},
];

// before swap uusd balance = 1_000_000_001
// before swap uusd balance = 1_000_000_000
// - 2*1_000 pair creation fee
// - 1_000_000 liquidity provision
// - 1 for native token creation (for decimal precisions)
// = 998_998_000
let pre_swap_amount = 998_998_000;
suite.query_balance(creator.to_string(), "uusd".to_string(), |amt| {
Expand Down Expand Up @@ -2093,7 +2089,7 @@ mod swapping {
},
);

// Verify fee collection by querying the address of the fee_collector and checking its balance
// Verify fee collection by querying the address of the whale lair and checking its balance
// Should be 297 uLUNA
suite.query_balance(
suite.whale_lair_addr.to_string(),
Expand Down
4 changes: 2 additions & 2 deletions contracts/liquidity_hub/pool-manager/src/tests/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ impl TestingSuite {
result: impl Fn(StdResult<Uint128>),
) -> &mut Self {
// Get the LP token from Config
let lp_token_response: PairInfo = self
let lp_token_response: PairInfoResponse = self
.app
.wrap()
.query_wasm_smart(
Expand All @@ -525,7 +525,7 @@ impl TestingSuite {
let balance: Uint128 = self
.app
.wrap()
.query_balance(sender, lp_token_response.lp_denom)
.query_balance(sender, lp_token_response.pair_info.lp_denom)
.unwrap()
.amount;

Expand Down

0 comments on commit 4abb16e

Please sign in to comment.