Skip to content

Commit

Permalink
feat(smart-contracts): add tests for add & remove swap routes
Browse files Browse the repository at this point in the history
  • Loading branch information
nseguias committed Apr 19, 2024
1 parent dbcf946 commit 5b8f22a
Show file tree
Hide file tree
Showing 2 changed files with 328 additions and 1 deletion.
291 changes: 291 additions & 0 deletions contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ mod pair_creation_failures {

mod router {
use cosmwasm_std::Event;
use white_whale_std::pool_manager::SwapRoute;

use super::*;
#[test]
Expand Down Expand Up @@ -1181,6 +1182,296 @@ mod router {
},
);
}

#[test]
fn add_swap_routes() {
let mut suite = TestingSuite::default_with_balances(vec![
coin(1_000_000_001u128, "uwhale".to_string()),
coin(1_000_000_000u128, "uluna".to_string()),
coin(1_000_000_001u128, "uusd".to_string()),
]);
let creator = suite.creator();
let _other = suite.senders[1].clone();
let _unauthorized = suite.senders[2].clone();

let first_pair = vec!["uwhale".to_string(), "uluna".to_string()];
let second_pair = vec!["uluna".to_string(), "uusd".to_string()];

#[cfg(not(feature = "osmosis"))]
let pool_fees = PoolFee {
protocol_fee: Fee {
share: Decimal::bps(50), // 0.5%
},
swap_fee: Fee {
share: Decimal::bps(50), // 0.5%
},
burn_fee: Fee {
share: Decimal::bps(50), // 0.5%
},
extra_fees: vec![],
};
#[cfg(feature = "osmosis")]
let pool_fees = PoolFee {
protocol_fee: Fee {
share: Decimal::bps(50),
},
swap_fee: Fee {
share: Decimal::bps(50),
},
burn_fee: Fee {
share: Decimal::bps(50),
},
osmosis_fee: Fee {
share: Decimal::bps(50),
},
extra_fees: vec![],
};

// Create a pair
suite
.instantiate_default()
.create_pair(
creator.clone(),
first_pair,
vec![6u8, 6u8],
pool_fees.clone(),
white_whale_std::pool_network::asset::PairType::ConstantProduct,
Some("whale-uluna".to_string()),
vec![coin(1000, "uusd")],
|result| {
result.unwrap();
},
)
.create_pair(
creator.clone(),
second_pair,
vec![6u8, 6u8],
pool_fees,
white_whale_std::pool_network::asset::PairType::ConstantProduct,
Some("uluna-uusd".to_string()),
vec![coin(1000, "uusd")],
|result| {
result.unwrap();
},
)
.provide_liquidity(
creator.clone(),
"whale-uluna".to_string(),
vec![
Coin {
denom: "uwhale".to_string(),
amount: Uint128::from(1000000u128),
},
Coin {
denom: "uluna".to_string(),
amount: Uint128::from(1000000u128),
},
],
|result| {
result.unwrap();
},
)
.provide_liquidity(
creator.clone(),
"uluna-uusd".to_string(),
vec![
Coin {
denom: "uluna".to_string(),
amount: Uint128::from(1000000u128),
},
Coin {
denom: "uusd".to_string(),
amount: Uint128::from(1000000u128),
},
],
|result| {
result.unwrap();
},
);

// Lets try to add a swap route
let swap_route_1 = SwapRoute {
offer_asset_denom: "uwhale".to_string(),
ask_asset_denom: "uusd".to_string(),
swap_operations: vec![
white_whale_std::pool_manager::SwapOperation::WhaleSwap {
token_in_denom: "uwhale".to_string(),
token_out_denom: "uluna".to_string(),
pool_identifier: "whale-uluna".to_string(),
},
white_whale_std::pool_manager::SwapOperation::WhaleSwap {
token_in_denom: "uluna".to_string(),
token_out_denom: "uusd".to_string(),
pool_identifier: "uluna-uusd".to_string(),
},
],
};

suite.add_swap_routes(creator.clone(), vec![swap_route_1.clone()], |result| {
assert!(result.unwrap().events.into_iter().any(|attr| {
attr.attributes
.iter()
.any(|attr| attr.value == "add_swap_routes")
}));
});

// Let's query for the swap route
suite.query_swap_routes(|result| {
assert_eq!(result.unwrap().swap_routes[0], swap_route_1);
});
}

#[test]
fn remove_swap_routes() {
let mut suite = TestingSuite::default_with_balances(vec![
coin(1_000_000_001u128, "uwhale".to_string()),
coin(1_000_000_000u128, "uluna".to_string()),
coin(1_000_000_001u128, "uusd".to_string()),
]);
let creator = suite.creator();
let _other = suite.senders[1].clone();
let _unauthorized = suite.senders[2].clone();

let first_pair = vec!["uwhale".to_string(), "uluna".to_string()];
let second_pair = vec!["uluna".to_string(), "uusd".to_string()];

#[cfg(not(feature = "osmosis"))]
let pool_fees = PoolFee {
protocol_fee: Fee {
share: Decimal::bps(50), // 0.5%
},
swap_fee: Fee {
share: Decimal::bps(50), // 0.5%
},
burn_fee: Fee {
share: Decimal::bps(50), // 0.5%
},
extra_fees: vec![],
};
#[cfg(feature = "osmosis")]
let pool_fees = PoolFee {
protocol_fee: Fee {
share: Decimal::bps(50),
},
swap_fee: Fee {
share: Decimal::bps(50),
},
burn_fee: Fee {
share: Decimal::bps(50),
},
osmosis_fee: Fee {
share: Decimal::bps(50),
},
extra_fees: vec![],
};

// Create a pair
suite
.instantiate_default()
.create_pair(
creator.clone(),
first_pair,
vec![6u8, 6u8],
pool_fees.clone(),
white_whale_std::pool_network::asset::PairType::ConstantProduct,
Some("whale-uluna".to_string()),
vec![coin(1000, "uusd")],
|result| {
result.unwrap();
},
)
.create_pair(
creator.clone(),
second_pair,
vec![6u8, 6u8],
pool_fees,
white_whale_std::pool_network::asset::PairType::ConstantProduct,
Some("uluna-uusd".to_string()),
vec![coin(1000, "uusd")],
|result| {
result.unwrap();
},
)
.provide_liquidity(
creator.clone(),
"whale-uluna".to_string(),
vec![
Coin {
denom: "uwhale".to_string(),
amount: Uint128::from(1000000u128),
},
Coin {
denom: "uluna".to_string(),
amount: Uint128::from(1000000u128),
},
],
|result| {
result.unwrap();
},
)
.provide_liquidity(
creator.clone(),
"uluna-uusd".to_string(),
vec![
Coin {
denom: "uluna".to_string(),
amount: Uint128::from(1000000u128),
},
Coin {
denom: "uusd".to_string(),
amount: Uint128::from(1000000u128),
},
],
|result| {
result.unwrap();
},
);

// Lets try to add a swap route
let swap_route_1 = SwapRoute {
offer_asset_denom: "uwhale".to_string(),
ask_asset_denom: "uusd".to_string(),
swap_operations: vec![
white_whale_std::pool_manager::SwapOperation::WhaleSwap {
token_in_denom: "uwhale".to_string(),
token_out_denom: "uluna".to_string(),
pool_identifier: "whale-uluna".to_string(),
},
white_whale_std::pool_manager::SwapOperation::WhaleSwap {
token_in_denom: "uluna".to_string(),
token_out_denom: "uusd".to_string(),
pool_identifier: "uluna-uusd".to_string(),
},
],
};

suite.add_swap_routes(creator.clone(), vec![swap_route_1.clone()], |result| {
assert!(result.unwrap().events.into_iter().any(|attr| {
attr.attributes
.iter()
.any(|attr| attr.value == "add_swap_routes")
}));
});

// Let's query for the swap route
suite.query_swap_routes(|result| {
assert_eq!(result.unwrap().swap_routes[0], swap_route_1);
});

// Lets try to remove the swap route
suite.remove_swap_routes(creator.clone(), vec![swap_route_1.clone()], |result| {
assert!(result.unwrap().events.into_iter().any(|attr| {
attr.attributes
.iter()
.any(|attr| attr.value == "remove_swap_routes")
}));
});

// Let's query for the swap route
suite.query_swap_routes(|result| {
assert_eq!(result.unwrap().swap_routes.len(), 0);
});
}
}

mod swapping {
Expand Down
38 changes: 37 additions & 1 deletion contracts/liquidity_hub/pool-manager/src/tests/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,42 @@ impl TestingSuite {

self
}

/// Adds swap routes to the pool manager contract.
#[track_caller]
pub(crate) fn add_swap_routes(
&mut self,
sender: Addr,
swap_routes: Vec<white_whale_std::pool_manager::SwapRoute>,
result: impl Fn(Result<AppResponse, anyhow::Error>),
) -> &mut Self {
result(self.app.execute_contract(
sender,
self.pool_manager_addr.clone(),
&white_whale_std::pool_manager::ExecuteMsg::AddSwapRoutes { swap_routes },
&[],

Check warning on line 378 in contracts/liquidity_hub/pool-manager/src/tests/suite.rs

View check run for this annotation

Codecov / codecov/patch

contracts/liquidity_hub/pool-manager/src/tests/suite.rs#L378

Added line #L378 was not covered by tests
));

self
}

/// Removes swap routes from the pool manager contract.
#[track_caller]
pub(crate) fn remove_swap_routes(
&mut self,
sender: Addr,
swap_routes: Vec<white_whale_std::pool_manager::SwapRoute>,
result: impl Fn(Result<AppResponse, anyhow::Error>),
) -> &mut Self {
result(self.app.execute_contract(
sender,
self.pool_manager_addr.clone(),
&white_whale_std::pool_manager::ExecuteMsg::RemoveSwapRoutes { swap_routes },
&[],

Check warning on line 396 in contracts/liquidity_hub/pool-manager/src/tests/suite.rs

View check run for this annotation

Codecov / codecov/patch

contracts/liquidity_hub/pool-manager/src/tests/suite.rs#L396

Added line #L396 was not covered by tests
));

self
}
}

/// queries
Expand Down Expand Up @@ -545,7 +581,7 @@ impl TestingSuite {
}

/// Retrieves the swap routes for a given pair of assets.
pub(crate) fn _query_swap_routes(
pub(crate) fn query_swap_routes(
&mut self,
result: impl Fn(StdResult<SwapRoutesResponse>),
) -> &mut Self {
Expand Down

0 comments on commit 5b8f22a

Please sign in to comment.