Skip to content

Commit

Permalink
add integration test and fix edgecase
Browse files Browse the repository at this point in the history
  • Loading branch information
iboss-ptk committed Aug 2, 2024
1 parent d1d0362 commit dfa6262
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 3 deletions.
6 changes: 3 additions & 3 deletions contracts/transmuter/src/limiter/limiters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,14 @@ impl<'a> Limiters<'a> {
) -> Result<(), ContractError> {
for (denom, (prev_value, value)) in denom_value_pairs {
let limiters = self.list_limiters_by_denom(storage, denom.as_str())?;
let is_value_increasing = value > prev_value;
let is_not_decreasing = value >= prev_value;

for (label, limiter) in limiters {
// Enforce limiter only if value is increasing, because if the value is decreasing from the previous value,
// for the specific denom, it is a balancing act to move away from the limit.
let limiter = match limiter {
Limiter::ChangeLimiter(limiter) => Limiter::ChangeLimiter({
if is_value_increasing {
if is_not_decreasing {
limiter
.ensure_upper_limit(block_time, denom.as_str(), value)?
.update(block_time, value)?
Expand All @@ -542,7 +542,7 @@ impl<'a> Limiters<'a> {
}
}),
Limiter::StaticLimiter(limiter) => Limiter::StaticLimiter({
if is_value_increasing {
if is_not_decreasing {
limiter.ensure_upper_limit(denom.as_str(), value)?
} else {
limiter
Expand Down
188 changes: 188 additions & 0 deletions contracts/transmuter/src/test/cases/scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,3 +1275,191 @@ fn test_limiters() {
err,
);
}

#[test]
fn test_register_limiter_after_having_liquidity() {
let app = OsmosisTestApp::new();
let cp = CosmwasmPool::new(&app);

let admin = app
.init_account(&[Coin::new(100_000u128, "uosmo")])
.unwrap();

let t = TestEnvBuilder::new()
.with_account(
"alice",
vec![
Coin::new(1_000_000, AXL_USDC),
Coin::new(1_000_000, COSMOS_USDC),
],
)
.with_account(
"bob",
vec![
Coin::new(1_000_000, AXL_USDC),
Coin::new(1_000_000, COSMOS_USDC),
],
)
.with_account("admin", vec![])
.with_account(
"provider",
vec![
Coin::new(1_000_000, AXL_USDC),
Coin::new(1_000_000, COSMOS_USDC),
],
)
.with_instantiate_msg(InstantiateMsg {
pool_asset_configs: vec![
AssetConfig::from_denom_str(AXL_USDC),
AssetConfig::from_denom_str(COSMOS_USDC),
],
alloyed_asset_subdenom: "usdc".to_string(),
alloyed_asset_normalization_factor: Uint128::one(),
admin: Some(admin.address()),
moderator: "osmo1cyyzpxplxdzkeea7kwsydadg87357qnahakaks".to_string(),
})
.build(&app);

// query share denom
let GetShareDenomResponse { share_denom } =
t.contract.query(&QueryMsg::GetShareDenom {}).unwrap();

let alloyed_denom = share_denom;

cp.swap_exact_amount_in(
MsgSwapExactAmountIn {
sender: t.accounts["provider"].address(),
token_in: Some(Coin::new(200_000, COSMOS_USDC).into()),
routes: vec![SwapAmountInRoute {
pool_id: t.contract.pool_id,
token_out_denom: alloyed_denom.clone(),
}],
token_out_min_amount: Uint128::from(200_000u128).to_string(),
},
&t.accounts["provider"],
)
.unwrap();

t.contract
.execute(
&ExecMsg::RegisterLimiter {
denom: COSMOS_USDC.to_string(),
label: "static".to_string(),
limiter_params: LimiterParams::StaticLimiter {
upper_limit: Decimal::percent(60),
},
},
&[],
&t.accounts["admin"],
)
.unwrap();

let err = cp
.swap_exact_amount_in(
MsgSwapExactAmountIn {
sender: t.accounts["provider"].address(),
token_in: Some(Coin::new(1, COSMOS_USDC).into()),
routes: vec![SwapAmountInRoute {
pool_id: t.contract.pool_id,
token_out_denom: alloyed_denom.clone(),
}],
token_out_min_amount: Uint128::from(1u128).to_string(),
},
&t.accounts["provider"],
)
.unwrap_err();

assert_contract_err(
ContractError::UpperLimitExceeded {
denom: COSMOS_USDC.to_string(),
upper_limit: Decimal::from_str("0.6").unwrap(),
value: Decimal::from_str("1").unwrap(),
},
err,
);

// Swap AXL USDC to alloyed asset should be successful
cp.swap_exact_amount_in(
MsgSwapExactAmountIn {
sender: t.accounts["provider"].address(),
token_in: Some(Coin::new(1, AXL_USDC).into()),
routes: vec![SwapAmountInRoute {
pool_id: t.contract.pool_id,
token_out_denom: alloyed_denom.clone(),
}],
token_out_min_amount: Uint128::from(1u128).to_string(),
},
&t.accounts["provider"],
)
.unwrap();

// Swap alloyed asset to COSMOS USDC should be successful
cp.swap_exact_amount_in(
MsgSwapExactAmountIn {
sender: t.accounts["provider"].address(),
token_in: Some(Coin::new(1, &alloyed_denom).into()),
routes: vec![SwapAmountInRoute {
pool_id: t.contract.pool_id,
token_out_denom: COSMOS_USDC.to_string(),
}],
token_out_min_amount: Uint128::from(1u128).to_string(),
},
&t.accounts["provider"],
)
.unwrap();

// Swap AXL USDC to COSMOS USDC should be successful and reduce COSMOS USDC composition
cp.swap_exact_amount_in(
MsgSwapExactAmountIn {
sender: t.accounts["provider"].address(),
token_in: Some(Coin::new(1, AXL_USDC).into()),
routes: vec![SwapAmountInRoute {
pool_id: t.contract.pool_id,
token_out_denom: COSMOS_USDC.to_string(),
}],
token_out_min_amount: Uint128::from(1u128).to_string(),
},
&t.accounts["provider"],
)
.unwrap();

// Swap the other way around should fail
let err = cp
.swap_exact_amount_in(
MsgSwapExactAmountIn {
sender: t.accounts["provider"].address(),
token_in: Some(Coin::new(1, COSMOS_USDC).into()),
routes: vec![SwapAmountInRoute {
pool_id: t.contract.pool_id,
token_out_denom: AXL_USDC.to_string(),
}],
token_out_min_amount: Uint128::from(1u128).to_string(),
},
&t.accounts["provider"],
)
.unwrap_err();

assert_contract_err(
ContractError::UpperLimitExceeded {
denom: COSMOS_USDC.to_string(),
upper_limit: Decimal::from_str("0.6").unwrap(),
value: Decimal::from_str("0.999995").unwrap(),
},
err,
);

// Swap alloyed asset to COSMOS USDC should be successful
cp.swap_exact_amount_in(
MsgSwapExactAmountIn {
sender: t.accounts["provider"].address(),
token_in: Some(Coin::new(1, alloyed_denom).into()),
routes: vec![SwapAmountInRoute {
pool_id: t.contract.pool_id,
token_out_denom: COSMOS_USDC.to_string(),
}],
token_out_min_amount: Uint128::from(1u128).to_string(),
},
&t.accounts["provider"],
)
.unwrap();
}

0 comments on commit dfa6262

Please sign in to comment.