Skip to content

Commit

Permalink
MP-3528 pass external routes to swapper (#352)
Browse files Browse the repository at this point in the history
* Init swapper extraction.

* Integrate new swapper logic into CM.

* Revert "Integrate new swapper logic into CM."

This reverts commit 68d3047a00368fe3a95c03effb9d04d4fab0b4cf.

* Pass SwapperRoute from CM (and RC) to Swapper contract.

* Clean swapper aimed to be managed by feature flags.

* Fix tests.

* Make fallback to routes in state if no route provided.

* Update schema.

* Increase number of swaps for TWAP tests.

* Make RC swapping backward compatible. Use state routes if not provided.

* Make swapper params more consistent between Astro and Osmo.

* Add custom config for swapper.

* Update schema.

* Review comments.

* Revert wasm file path.

* Migration.

* Add validation for config.
  • Loading branch information
piobab authored Feb 10, 2024
1 parent b2c9677 commit 28edbfb
Show file tree
Hide file tree
Showing 74 changed files with 2,699 additions and 311 deletions.
10 changes: 7 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion contracts/credit-manager/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mars-credit-manager"
version = "2.0.1"
version = "2.0.2"
authors = { workspace = true }
license = { workspace = true }
edition = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion contracts/credit-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> ContractResult<Binary> {
pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> ContractResult<Response> {
match msg {
MigrateMsg::V1_0_0ToV2_0_0(updates) => migrations::v2_0_0::migrate(deps, env, updates),
MigrateMsg::V2_0_0ToV2_0_1 {} => migrations::v2_0_1::migrate(deps),
MigrateMsg::V2_0_1ToV2_0_2 {} => migrations::v2_0_2::migrate(deps),
}
}
5 changes: 4 additions & 1 deletion contracts/credit-manager/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,14 @@ pub fn dispatch_actions(
coin_in,
denom_out,
slippage,
route,
} => {
callbacks.push(CallbackMsg::SwapExactIn {
account_id: account_id.to_string(),
coin_in,
denom_out: denom_out.clone(),
slippage,
route,
});
// check the deposit cap of the swap output denom
denoms_for_cap_check.insert(denom_out);
Expand Down Expand Up @@ -416,7 +418,8 @@ pub fn execute_callback(
coin_in,
denom_out,
slippage,
} => swap_exact_in(deps, env, &account_id, &coin_in, &denom_out, slippage),
route,
} => swap_exact_in(deps, env, &account_id, &coin_in, &denom_out, slippage, route),
CallbackMsg::UpdateCoinBalance {
account_id,
previous_balance,
Expand Down
2 changes: 1 addition & 1 deletion contracts/credit-manager/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod v2_0_0;
pub mod v2_0_1;
pub mod v2_0_2;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
error::ContractError,
};

const FROM_VERSION: &str = "2.0.0";
const FROM_VERSION: &str = "2.0.1";

pub fn migrate(deps: DepsMut) -> Result<Response, ContractError> {
// make sure we're migrating the correct contract and from the correct version
Expand Down
8 changes: 6 additions & 2 deletions contracts/credit-manager/src/swap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use cosmwasm_std::{Coin, Decimal, DepsMut, Env, Response, Uint128};
use mars_types::credit_manager::{ActionAmount, ActionCoin, ChangeExpected};
use mars_types::{
credit_manager::{ActionAmount, ActionCoin, ChangeExpected},
swapper::SwapperRoute,
};

use crate::{
error::{ContractError, ContractResult},
Expand All @@ -16,6 +19,7 @@ pub fn swap_exact_in(
coin_in: &ActionCoin,
denom_out: &str,
slippage: Decimal,
route: Option<SwapperRoute>,
) -> ContractResult<Response> {
assert_slippage(deps.storage, slippage)?;

Expand Down Expand Up @@ -49,7 +53,7 @@ pub fn swap_exact_in(
let swapper = SWAPPER.load(deps.storage)?;

Ok(Response::new()
.add_message(swapper.swap_exact_in_msg(&coin_in_to_trade, denom_out, slippage)?)
.add_message(swapper.swap_exact_in_msg(&coin_in_to_trade, denom_out, slippage, route)?)
.add_message(update_coin_balance_msg)
.add_attribute("action", "swapper")
.add_attribute("account_id", account_id)
Expand Down
4 changes: 3 additions & 1 deletion contracts/credit-manager/tests/tests/helpers/mock_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use mars_types::{
},
swapper::{
EstimateExactInSwapResponse, InstantiateMsg as SwapperInstantiateMsg,
QueryMsg::EstimateExactInSwap,
QueryMsg::EstimateExactInSwap, SwapperRoute,
},
};
use mars_zapper_mock::msg::{InstantiateMsg as ZapperInstantiateMsg, LpConfig};
Expand Down Expand Up @@ -684,6 +684,7 @@ impl MockEnv {
&self,
coin_in: &Coin,
denom_out: &str,
route: SwapperRoute,
) -> EstimateExactInSwapResponse {
let config = self.query_config();
self.app
Expand All @@ -693,6 +694,7 @@ impl MockEnv {
&EstimateExactInSwap {
coin_in: coin_in.clone(),
denom_out: denom_out.to_string(),
route: Some(route),
},
)
.unwrap()
Expand Down
13 changes: 13 additions & 0 deletions contracts/credit-manager/tests/tests/test_deposit_cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use mars_credit_manager::error::ContractError;
use mars_types::{
credit_manager::{Action, ActionAmount, ActionCoin},
params::{AssetParams, AssetParamsUpdate},
swapper::{OsmoRoute, OsmoSwap, SwapperRoute},
};
use test_case::test_case;

Expand Down Expand Up @@ -57,6 +58,12 @@ use super::helpers::{uatom_info, uosmo_info, AccountToFund, MockEnv};
},
denom_out: "uosmo".into(),
slippage: Decimal::percent(5),
route: Some(SwapperRoute::Osmo(OsmoRoute{swaps: vec![
OsmoSwap {
pool_id: 101,
to: "uosmo".into(),
}
]}))
}
],
true;
Expand All @@ -78,6 +85,12 @@ use super::helpers::{uatom_info, uosmo_info, AccountToFund, MockEnv};
},
denom_out: "uosmo".into(),
slippage: Decimal::percent(5),
route: Some(SwapperRoute::Osmo(OsmoRoute{swaps: vec![
OsmoSwap {
pool_id: 101,
to: "uosmo".into(),
}
]}))
}
],
false;
Expand Down
10 changes: 5 additions & 5 deletions contracts/credit-manager/tests/tests/test_migration_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,24 @@ fn successful_migration() {
}

#[test]
fn successful_migration_to_v2_0_1() {
fn successful_migration_to_v2_0_2() {
let mut deps = mock_dependencies();
cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-credit-manager", "2.0.0")
cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-credit-manager", "2.0.1")
.unwrap();

let res = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_0_0ToV2_0_1 {}).unwrap();
let res = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_0_1ToV2_0_2 {}).unwrap();

assert_eq!(res.messages, vec![]);
assert_eq!(res.events, vec![] as Vec<Event>);
assert!(res.data.is_none());
assert_eq!(
res.attributes,
vec![attr("action", "migrate"), attr("from_version", "2.0.0"), attr("to_version", "2.0.1")]
vec![attr("action", "migrate"), attr("from_version", "2.0.1"), attr("to_version", "2.0.2")]
);

let new_contract_version = ContractVersion {
contract: "crates.io:mars-credit-manager".to_string(),
version: "2.0.1".to_string(),
version: "2.0.2".to_string(),
};
assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version);
}
71 changes: 62 additions & 9 deletions contracts/credit-manager/tests/tests/test_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use std::str::FromStr;
use cosmwasm_std::{coins, Addr, Coin, Decimal, OverflowError, OverflowOperation::Sub, Uint128};
use mars_credit_manager::error::ContractError;
use mars_swapper_mock::contract::MOCK_SWAP_RESULT;
use mars_types::credit_manager::{
Action::{Deposit, SwapExactIn},
ActionAmount, ActionCoin,
use mars_types::{
credit_manager::{
Action::{Deposit, SwapExactIn},
ActionAmount, ActionCoin,
},
swapper::{OsmoRoute, OsmoSwap, SwapperRoute},
};

use super::helpers::{
Expand All @@ -29,6 +32,12 @@ fn only_token_owner_can_swap_for_account() {
},
denom_out: "osmo".to_string(),
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
route: Some(SwapperRoute::Osmo(OsmoRoute {
swaps: vec![OsmoSwap {
pool_id: 101,
to: "osmo".to_string(),
}],
})),
}],
&[],
);
Expand Down Expand Up @@ -57,6 +66,12 @@ fn denom_out_must_be_whitelisted() {
coin_in: blacklisted_coin.to_action_coin(10_000),
denom_out: "ujake".to_string(),
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
route: Some(SwapperRoute::Osmo(OsmoRoute {
swaps: vec![OsmoSwap {
pool_id: 101,
to: "ujake".to_string(),
}],
})),
}],
&[],
);
Expand All @@ -79,8 +94,14 @@ fn no_amount_sent() {
&user,
vec![SwapExactIn {
coin_in: osmo_info.to_action_coin(0),
denom_out: atom_info.denom,
denom_out: atom_info.denom.clone(),
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
route: Some(SwapperRoute::Osmo(OsmoRoute {
swaps: vec![OsmoSwap {
pool_id: 101,
to: atom_info.denom,
}],
})),
}],
&[],
);
Expand All @@ -103,8 +124,14 @@ fn user_has_zero_balance_for_swap_req() {
&user,
vec![SwapExactIn {
coin_in: osmo_info.to_action_coin(10_000),
denom_out: atom_info.denom,
denom_out: atom_info.denom.clone(),
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
route: Some(SwapperRoute::Osmo(OsmoRoute {
swaps: vec![OsmoSwap {
pool_id: 101,
to: atom_info.denom,
}],
})),
}],
&[],
);
Expand Down Expand Up @@ -139,8 +166,14 @@ fn slippage_too_high() {
&user,
vec![SwapExactIn {
coin_in: osmo_info.to_action_coin(10_000),
denom_out: atom_info.denom,
denom_out: atom_info.denom.clone(),
slippage,
route: Some(SwapperRoute::Osmo(OsmoRoute {
swaps: vec![OsmoSwap {
pool_id: 101,
to: atom_info.denom,
}],
})),
}],
&[],
);
Expand Down Expand Up @@ -177,8 +210,14 @@ fn user_does_not_have_enough_balance_for_swap_req() {
Deposit(osmo_info.to_coin(100)),
SwapExactIn {
coin_in: osmo_info.to_action_coin(10_000),
denom_out: atom_info.denom,
denom_out: atom_info.denom.clone(),
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
route: Some(SwapperRoute::Osmo(OsmoRoute {
swaps: vec![OsmoSwap {
pool_id: 101,
to: atom_info.denom,
}],
})),
},
],
&[osmo_info.to_coin(100)],
Expand Down Expand Up @@ -209,7 +248,13 @@ fn swap_success_with_specified_amount() {
.build()
.unwrap();

let res = mock.query_swap_estimate(&atom_info.to_coin(10_000), &osmo_info.denom);
let route = SwapperRoute::Osmo(OsmoRoute {
swaps: vec![OsmoSwap {
pool_id: 101,
to: osmo_info.denom.clone(),
}],
});
let res = mock.query_swap_estimate(&atom_info.to_coin(10_000), &osmo_info.denom, route.clone());
assert_eq!(res.amount, MOCK_SWAP_RESULT);

let account_id = mock.create_credit_account(&user).unwrap();
Expand All @@ -222,6 +267,7 @@ fn swap_success_with_specified_amount() {
coin_in: atom_info.to_action_coin(10_000),
denom_out: osmo_info.denom.clone(),
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
route: Some(route),
},
],
&[atom_info.to_coin(10_000)],
Expand Down Expand Up @@ -256,7 +302,13 @@ fn swap_success_with_amount_none() {
.build()
.unwrap();

let res = mock.query_swap_estimate(&atom_info.to_coin(10_000), &osmo_info.denom);
let route = SwapperRoute::Osmo(OsmoRoute {
swaps: vec![OsmoSwap {
pool_id: 101,
to: osmo_info.denom.clone(),
}],
});
let res = mock.query_swap_estimate(&atom_info.to_coin(10_000), &osmo_info.denom, route.clone());
assert_eq!(res.amount, MOCK_SWAP_RESULT);

let account_id = mock.create_credit_account(&user).unwrap();
Expand All @@ -269,6 +321,7 @@ fn swap_success_with_amount_none() {
coin_in: atom_info.to_action_coin_full_balance(),
denom_out: osmo_info.denom.clone(),
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
route: Some(route),
},
],
&[atom_info.to_coin(10_000)],
Expand Down
Loading

0 comments on commit 28edbfb

Please sign in to comment.