Skip to content

Commit 28edbfb

Browse files
authored
MP-3528 pass external routes to swapper (#352)
* 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.
1 parent b2c9677 commit 28edbfb

File tree

74 files changed

+2699
-311
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2699
-311
lines changed

Cargo.lock

+7-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/credit-manager/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mars-credit-manager"
3-
version = "2.0.1"
3+
version = "2.0.2"
44
authors = { workspace = true }
55
license = { workspace = true }
66
edition = { workspace = true }

contracts/credit-manager/src/contract.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,6 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> ContractResult<Binary> {
128128
pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> ContractResult<Response> {
129129
match msg {
130130
MigrateMsg::V1_0_0ToV2_0_0(updates) => migrations::v2_0_0::migrate(deps, env, updates),
131-
MigrateMsg::V2_0_0ToV2_0_1 {} => migrations::v2_0_1::migrate(deps),
131+
MigrateMsg::V2_0_1ToV2_0_2 {} => migrations::v2_0_2::migrate(deps),
132132
}
133133
}

contracts/credit-manager/src/execute.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,14 @@ pub fn dispatch_actions(
204204
coin_in,
205205
denom_out,
206206
slippage,
207+
route,
207208
} => {
208209
callbacks.push(CallbackMsg::SwapExactIn {
209210
account_id: account_id.to_string(),
210211
coin_in,
211212
denom_out: denom_out.clone(),
212213
slippage,
214+
route,
213215
});
214216
// check the deposit cap of the swap output denom
215217
denoms_for_cap_check.insert(denom_out);
@@ -416,7 +418,8 @@ pub fn execute_callback(
416418
coin_in,
417419
denom_out,
418420
slippage,
419-
} => swap_exact_in(deps, env, &account_id, &coin_in, &denom_out, slippage),
421+
route,
422+
} => swap_exact_in(deps, env, &account_id, &coin_in, &denom_out, slippage, route),
420423
CallbackMsg::UpdateCoinBalance {
421424
account_id,
422425
previous_balance,
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pub mod v2_0_0;
2-
pub mod v2_0_1;
2+
pub mod v2_0_2;

contracts/credit-manager/src/migrations/v2_0_1.rs contracts/credit-manager/src/migrations/v2_0_2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
error::ContractError,
77
};
88

9-
const FROM_VERSION: &str = "2.0.0";
9+
const FROM_VERSION: &str = "2.0.1";
1010

1111
pub fn migrate(deps: DepsMut) -> Result<Response, ContractError> {
1212
// make sure we're migrating the correct contract and from the correct version

contracts/credit-manager/src/swap.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use cosmwasm_std::{Coin, Decimal, DepsMut, Env, Response, Uint128};
2-
use mars_types::credit_manager::{ActionAmount, ActionCoin, ChangeExpected};
2+
use mars_types::{
3+
credit_manager::{ActionAmount, ActionCoin, ChangeExpected},
4+
swapper::SwapperRoute,
5+
};
36

47
use crate::{
58
error::{ContractError, ContractResult},
@@ -16,6 +19,7 @@ pub fn swap_exact_in(
1619
coin_in: &ActionCoin,
1720
denom_out: &str,
1821
slippage: Decimal,
22+
route: Option<SwapperRoute>,
1923
) -> ContractResult<Response> {
2024
assert_slippage(deps.storage, slippage)?;
2125

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

5155
Ok(Response::new()
52-
.add_message(swapper.swap_exact_in_msg(&coin_in_to_trade, denom_out, slippage)?)
56+
.add_message(swapper.swap_exact_in_msg(&coin_in_to_trade, denom_out, slippage, route)?)
5357
.add_message(update_coin_balance_msg)
5458
.add_attribute("action", "swapper")
5559
.add_attribute("account_id", account_id)

contracts/credit-manager/tests/tests/helpers/mock_env.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use mars_types::{
5959
},
6060
swapper::{
6161
EstimateExactInSwapResponse, InstantiateMsg as SwapperInstantiateMsg,
62-
QueryMsg::EstimateExactInSwap,
62+
QueryMsg::EstimateExactInSwap, SwapperRoute,
6363
},
6464
};
6565
use mars_zapper_mock::msg::{InstantiateMsg as ZapperInstantiateMsg, LpConfig};
@@ -684,6 +684,7 @@ impl MockEnv {
684684
&self,
685685
coin_in: &Coin,
686686
denom_out: &str,
687+
route: SwapperRoute,
687688
) -> EstimateExactInSwapResponse {
688689
let config = self.query_config();
689690
self.app
@@ -693,6 +694,7 @@ impl MockEnv {
693694
&EstimateExactInSwap {
694695
coin_in: coin_in.clone(),
695696
denom_out: denom_out.to_string(),
697+
route: Some(route),
696698
},
697699
)
698700
.unwrap()

contracts/credit-manager/tests/tests/test_deposit_cap.rs

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use mars_credit_manager::error::ContractError;
55
use mars_types::{
66
credit_manager::{Action, ActionAmount, ActionCoin},
77
params::{AssetParams, AssetParamsUpdate},
8+
swapper::{OsmoRoute, OsmoSwap, SwapperRoute},
89
};
910
use test_case::test_case;
1011

@@ -57,6 +58,12 @@ use super::helpers::{uatom_info, uosmo_info, AccountToFund, MockEnv};
5758
},
5859
denom_out: "uosmo".into(),
5960
slippage: Decimal::percent(5),
61+
route: Some(SwapperRoute::Osmo(OsmoRoute{swaps: vec![
62+
OsmoSwap {
63+
pool_id: 101,
64+
to: "uosmo".into(),
65+
}
66+
]}))
6067
}
6168
],
6269
true;
@@ -78,6 +85,12 @@ use super::helpers::{uatom_info, uosmo_info, AccountToFund, MockEnv};
7885
},
7986
denom_out: "uosmo".into(),
8087
slippage: Decimal::percent(5),
88+
route: Some(SwapperRoute::Osmo(OsmoRoute{swaps: vec![
89+
OsmoSwap {
90+
pool_id: 101,
91+
to: "uosmo".into(),
92+
}
93+
]}))
8194
}
8295
],
8396
false;

contracts/credit-manager/tests/tests/test_migration_v2.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,24 @@ fn successful_migration() {
151151
}
152152

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

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

161161
assert_eq!(res.messages, vec![]);
162162
assert_eq!(res.events, vec![] as Vec<Event>);
163163
assert!(res.data.is_none());
164164
assert_eq!(
165165
res.attributes,
166-
vec![attr("action", "migrate"), attr("from_version", "2.0.0"), attr("to_version", "2.0.1")]
166+
vec![attr("action", "migrate"), attr("from_version", "2.0.1"), attr("to_version", "2.0.2")]
167167
);
168168

169169
let new_contract_version = ContractVersion {
170170
contract: "crates.io:mars-credit-manager".to_string(),
171-
version: "2.0.1".to_string(),
171+
version: "2.0.2".to_string(),
172172
};
173173
assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version);
174174
}

contracts/credit-manager/tests/tests/test_swap.rs

+62-9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ use std::str::FromStr;
33
use cosmwasm_std::{coins, Addr, Coin, Decimal, OverflowError, OverflowOperation::Sub, Uint128};
44
use mars_credit_manager::error::ContractError;
55
use mars_swapper_mock::contract::MOCK_SWAP_RESULT;
6-
use mars_types::credit_manager::{
7-
Action::{Deposit, SwapExactIn},
8-
ActionAmount, ActionCoin,
6+
use mars_types::{
7+
credit_manager::{
8+
Action::{Deposit, SwapExactIn},
9+
ActionAmount, ActionCoin,
10+
},
11+
swapper::{OsmoRoute, OsmoSwap, SwapperRoute},
912
};
1013

1114
use super::helpers::{
@@ -29,6 +32,12 @@ fn only_token_owner_can_swap_for_account() {
2932
},
3033
denom_out: "osmo".to_string(),
3134
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
35+
route: Some(SwapperRoute::Osmo(OsmoRoute {
36+
swaps: vec![OsmoSwap {
37+
pool_id: 101,
38+
to: "osmo".to_string(),
39+
}],
40+
})),
3241
}],
3342
&[],
3443
);
@@ -57,6 +66,12 @@ fn denom_out_must_be_whitelisted() {
5766
coin_in: blacklisted_coin.to_action_coin(10_000),
5867
denom_out: "ujake".to_string(),
5968
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
69+
route: Some(SwapperRoute::Osmo(OsmoRoute {
70+
swaps: vec![OsmoSwap {
71+
pool_id: 101,
72+
to: "ujake".to_string(),
73+
}],
74+
})),
6075
}],
6176
&[],
6277
);
@@ -79,8 +94,14 @@ fn no_amount_sent() {
7994
&user,
8095
vec![SwapExactIn {
8196
coin_in: osmo_info.to_action_coin(0),
82-
denom_out: atom_info.denom,
97+
denom_out: atom_info.denom.clone(),
8398
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
99+
route: Some(SwapperRoute::Osmo(OsmoRoute {
100+
swaps: vec![OsmoSwap {
101+
pool_id: 101,
102+
to: atom_info.denom,
103+
}],
104+
})),
84105
}],
85106
&[],
86107
);
@@ -103,8 +124,14 @@ fn user_has_zero_balance_for_swap_req() {
103124
&user,
104125
vec![SwapExactIn {
105126
coin_in: osmo_info.to_action_coin(10_000),
106-
denom_out: atom_info.denom,
127+
denom_out: atom_info.denom.clone(),
107128
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
129+
route: Some(SwapperRoute::Osmo(OsmoRoute {
130+
swaps: vec![OsmoSwap {
131+
pool_id: 101,
132+
to: atom_info.denom,
133+
}],
134+
})),
108135
}],
109136
&[],
110137
);
@@ -139,8 +166,14 @@ fn slippage_too_high() {
139166
&user,
140167
vec![SwapExactIn {
141168
coin_in: osmo_info.to_action_coin(10_000),
142-
denom_out: atom_info.denom,
169+
denom_out: atom_info.denom.clone(),
143170
slippage,
171+
route: Some(SwapperRoute::Osmo(OsmoRoute {
172+
swaps: vec![OsmoSwap {
173+
pool_id: 101,
174+
to: atom_info.denom,
175+
}],
176+
})),
144177
}],
145178
&[],
146179
);
@@ -177,8 +210,14 @@ fn user_does_not_have_enough_balance_for_swap_req() {
177210
Deposit(osmo_info.to_coin(100)),
178211
SwapExactIn {
179212
coin_in: osmo_info.to_action_coin(10_000),
180-
denom_out: atom_info.denom,
213+
denom_out: atom_info.denom.clone(),
181214
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
215+
route: Some(SwapperRoute::Osmo(OsmoRoute {
216+
swaps: vec![OsmoSwap {
217+
pool_id: 101,
218+
to: atom_info.denom,
219+
}],
220+
})),
182221
},
183222
],
184223
&[osmo_info.to_coin(100)],
@@ -209,7 +248,13 @@ fn swap_success_with_specified_amount() {
209248
.build()
210249
.unwrap();
211250

212-
let res = mock.query_swap_estimate(&atom_info.to_coin(10_000), &osmo_info.denom);
251+
let route = SwapperRoute::Osmo(OsmoRoute {
252+
swaps: vec![OsmoSwap {
253+
pool_id: 101,
254+
to: osmo_info.denom.clone(),
255+
}],
256+
});
257+
let res = mock.query_swap_estimate(&atom_info.to_coin(10_000), &osmo_info.denom, route.clone());
213258
assert_eq!(res.amount, MOCK_SWAP_RESULT);
214259

215260
let account_id = mock.create_credit_account(&user).unwrap();
@@ -222,6 +267,7 @@ fn swap_success_with_specified_amount() {
222267
coin_in: atom_info.to_action_coin(10_000),
223268
denom_out: osmo_info.denom.clone(),
224269
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
270+
route: Some(route),
225271
},
226272
],
227273
&[atom_info.to_coin(10_000)],
@@ -256,7 +302,13 @@ fn swap_success_with_amount_none() {
256302
.build()
257303
.unwrap();
258304

259-
let res = mock.query_swap_estimate(&atom_info.to_coin(10_000), &osmo_info.denom);
305+
let route = SwapperRoute::Osmo(OsmoRoute {
306+
swaps: vec![OsmoSwap {
307+
pool_id: 101,
308+
to: osmo_info.denom.clone(),
309+
}],
310+
});
311+
let res = mock.query_swap_estimate(&atom_info.to_coin(10_000), &osmo_info.denom, route.clone());
260312
assert_eq!(res.amount, MOCK_SWAP_RESULT);
261313

262314
let account_id = mock.create_credit_account(&user).unwrap();
@@ -269,6 +321,7 @@ fn swap_success_with_amount_none() {
269321
coin_in: atom_info.to_action_coin_full_balance(),
270322
denom_out: osmo_info.denom.clone(),
271323
slippage: Decimal::from_atomics(6u128, 1).unwrap(),
324+
route: Some(route),
272325
},
273326
],
274327
&[atom_info.to_coin(10_000)],

0 commit comments

Comments
 (0)