Skip to content

Commit 5dfdceb

Browse files
authored
configurable max spread (#67)
1 parent dbbc9de commit 5dfdceb

File tree

10 files changed

+286
-142
lines changed

10 files changed

+286
-142
lines changed

contracts/pools/stable_pool/src/contract.rs

Lines changed: 215 additions & 131 deletions
Large diffs are not rendered by default.

contracts/pools/stable_pool/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ pub enum ContractError {
9696

9797
#[error("Scaling factor manager shouldn't be specified if scaling factor is not updatable")]
9898
ScalingFactorManagerSpecified,
99+
100+
#[error("Invalid max allowed spread. Max allowed spread should be positive non-zero value less than 1")]
101+
InvalidMaxAllowedSpread,
99102
}
100103

101104
impl From<OverflowError> for ContractError {

contracts/pools/stable_pool/src/state.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22

33
use cosmwasm_schema::cw_serde;
4-
use cosmwasm_std::{DepsMut, StdResult, Storage, Uint128, Addr, Decimal256};
4+
use cosmwasm_std::{DepsMut, StdResult, Storage, Uint128, Addr, Decimal256, Decimal};
55
use cw_storage_plus::{Item, Map};
66
use dexter::asset::AssetInfo;
77
use dexter::pool::Config;
@@ -35,7 +35,15 @@ pub struct MathConfig {
3535

3636
#[cw_serde]
3737
pub struct StableSwapConfig {
38+
/// Max allowed spread between the price of the asset and the price of the pool.
39+
/// If the spread is greater than this value, the swap will fail.
40+
/// This value is configurable by the Pool Manager.
41+
/// Max allowed spread is in the range (0, 1) non-inclusive.
42+
pub max_allowed_spread: Decimal,
43+
/// If this is true, then the scaling factors can be updated by the scaling_factor_manager.
3844
pub supports_scaling_factors_update: bool,
45+
/// The vector of scaling factors for each asset in the pool.
46+
/// The scaling factor is used to scale the volume of the asset in the pool for the stableswap invariant calculations.
3947
pub scaling_factors: Vec<AssetScalingFactor>,
4048
// This address is allowed to update scaling factors. This address is required if support_scaling_factors_update is true.
4149
pub scaling_factor_manager: Option<Addr>,
@@ -78,6 +86,8 @@ pub struct Twap {
7886
pub struct StablePoolParams {
7987
/// The current stableswap pool amplification
8088
pub amp: u64,
89+
/// Max allowed spread for the trades
90+
pub max_allowed_spread: Decimal,
8191
/// Support scaling factors update
8292
pub supports_scaling_factors_update: bool,
8393
/// Scaling factors
@@ -108,6 +118,7 @@ pub enum StablePoolUpdateParams {
108118
StopChangingAmp {},
109119
UpdateScalingFactorManager { manager: Addr },
110120
UpdateScalingFactor { asset_info: AssetInfo, scaling_factor: Decimal256 },
121+
UpdateMaxAllowedSpread { max_allowed_spread: Decimal },
111122
}
112123

113124
// ----------------x----------------x----------------x----------------

contracts/pools/stable_pool/tests/integration.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,47 @@ fn test_update_config() {
307307

308308
let params: StablePoolParams = from_binary(&res.additional_params.unwrap()).unwrap();
309309
assert_eq!(params.amp, 20u64);
310+
311+
// Change max allowed spread limits for trades
312+
let msg = ExecuteMsg::UpdateConfig {
313+
params: Some(
314+
to_binary(&StablePoolUpdateParams::UpdateMaxAllowedSpread {
315+
max_allowed_spread: Decimal::percent(90),
316+
})
317+
.unwrap(),
318+
),
319+
};
320+
321+
app.execute_contract(owner.clone(), pool_addr.clone(), &msg, &[]).unwrap();
322+
323+
// validate max allowed spread change
324+
let res: ConfigResponse = app
325+
.wrap()
326+
.query_wasm_smart(pool_addr.clone(), &QueryMsg::Config {})
327+
.unwrap();
328+
329+
let params: StablePoolParams = from_binary(&res.additional_params.unwrap()).unwrap();
330+
assert_eq!(params.max_allowed_spread, Decimal::percent(90));
331+
332+
// try updating max spread to an invalid value
333+
let msg = ExecuteMsg::UpdateConfig {
334+
params: Some(
335+
to_binary(&StablePoolUpdateParams::UpdateMaxAllowedSpread {
336+
max_allowed_spread: Decimal::percent(100),
337+
})
338+
.unwrap(),
339+
),
340+
};
341+
342+
let resp = app
343+
.execute_contract(owner.clone(), pool_addr.clone(), &msg, &[])
344+
.unwrap_err();
345+
346+
assert_eq!(
347+
resp.root_cause().to_string(),
348+
"Invalid max allowed spread. Max allowed spread should be positive non-zero value less than 1"
349+
);
350+
310351
}
311352

312353
/// Tests the following -

contracts/pools/stable_pool/tests/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub fn instantiate_contract_generic(
136136
scaling_factors,
137137
supports_scaling_factors_update: false,
138138
scaling_factor_manager: None,
139+
max_allowed_spread: Decimal::from_ratio(50u128, 100u128)
139140
})
140141
.unwrap(),
141142
),

contracts/router/tests/integration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ fn initialize_stable_5_pool(
251251
scaling_factors: vec![],
252252
supports_scaling_factors_update: false,
253253
scaling_factor_manager: None,
254+
max_allowed_spread: Decimal::from_ratio(50u128, 100u128)
254255
}).unwrap()),
255256
fee_info: None,
256257
};

contracts/vault/tests/initialization_and_config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub mod utils;
22

3-
use cosmwasm_std::{attr, coin, Addr, Coin, Uint128, to_binary};
3+
use cosmwasm_std::{attr, coin, Addr, Coin, Uint128, to_binary, Decimal};
44
use cw20::MinterResponse;
55
use cw_multi_test::Executor;
66
use dexter::asset::AssetInfo;
@@ -531,6 +531,7 @@ fn test_pool_config_update() {
531531
supports_scaling_factors_update: false,
532532
scaling_factor_manager: None,
533533
scaling_factors: vec![],
534+
max_allowed_spread: Decimal::from_ratio(50u128, 100u128),
534535
}).unwrap()),
535536
fee_info: None,
536537
};

contracts/vault/tests/pool_creation.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub mod utils;
22

33
use std::vec;
44

5-
use cosmwasm_std::{attr, coin, Addr, Coin, Uint128, to_binary};
5+
use cosmwasm_std::{attr, coin, Addr, Coin, Uint128, to_binary, Decimal};
66
use cw20::MinterResponse;
77
use cw_multi_test::Executor;
88
use dexter::asset::{Asset, AssetInfo};
@@ -101,7 +101,8 @@ fn test_create_pool_instance() {
101101
amp: 100u64,
102102
scaling_factor_manager: None,
103103
supports_scaling_factors_update: false,
104-
scaling_factors: vec![]
104+
scaling_factors: vec![],
105+
max_allowed_spread: Decimal::from_ratio(50u64, 100u64)
105106
}).unwrap()),
106107
fee_info: None,
107108
};
@@ -480,6 +481,7 @@ fn test_pool_creation_fee() {
480481
scaling_factor_manager: None,
481482
supports_scaling_factors_update: false,
482483
scaling_factors: vec![],
484+
max_allowed_spread: Decimal::from_ratio(50u128, 100u128)
483485
}).unwrap()),
484486
fee_info: None,
485487
};

contracts/vault/tests/utils/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ pub fn dummy_pool_creation_msg(asset_infos: &[AssetInfo]) -> ExecuteMsg {
262262
amp: 100u64,
263263
scaling_factor_manager: None,
264264
scaling_factors: vec![],
265-
supports_scaling_factors_update: false
265+
supports_scaling_factors_update: false,
266+
max_allowed_spread: Decimal::from_ratio(50u64, 100u64)
266267
}).unwrap()),
267268
fee_info: Some(FeeInfo {
268269
total_fee_bps: 1_000u16,
@@ -303,7 +304,8 @@ pub fn initialize_stable_5_pool_2_asset(
303304
amp: 10u64,
304305
scaling_factor_manager: None,
305306
scaling_factors: vec![],
306-
supports_scaling_factors_update: false
307+
supports_scaling_factors_update: false,
308+
max_allowed_spread: Decimal::from_ratio(50u64, 100u64)
307309
}).unwrap()),
308310
fee_info: None,
309311
};
@@ -369,7 +371,8 @@ pub fn initialize_stable_5_pool(
369371
amp: 10u64,
370372
scaling_factor_manager: None,
371373
scaling_factors: vec![],
372-
supports_scaling_factors_update: false
374+
supports_scaling_factors_update: false,
375+
max_allowed_spread: Decimal::from_ratio(50u64, 100u64)
373376
}).unwrap()),
374377
fee_info: None,
375378
};

packages/dexter/src/pool.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ use std::fmt::{Display, Formatter, Result};
88
use cw_storage_plus::Item;
99
use crate::helper::{EventExt};
1010

11-
/// The default slippage (0.5%)
11+
/// The default spread (0.5%)
1212
pub const DEFAULT_SPREAD: &str = "0.005";
1313

14-
/// The maximum allowed slippage (50%)
15-
pub const MAX_SPREAD: &str = "0.5";
16-
1714
// ----------------x----------------x----------------x----------------x----------------x----------------
1815
// ----------------x----------------x Gneneric struct Types x----------------x----------------
1916
// ----------------x----------------x----------------x----------------x----------------x----------------

0 commit comments

Comments
 (0)