Skip to content

Commit d65889e

Browse files
authored
fix(PCL): don't update LP profits on withdrawal (#469)
1 parent f7201e8 commit d65889e

4 files changed

Lines changed: 51 additions & 41 deletions

File tree

contracts/pair_concentrated/src/contract.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use astroport_pcl_common::utils::{
4141
accumulate_prices, assert_max_spread, before_swap_check, calc_last_prices, check_asset_infos,
4242
check_cw20_in_pool, compute_swap, get_share_in_assets, mint_liquidity_token_message,
4343
};
44-
use astroport_pcl_common::{calc_d, get_xcp};
4544

4645
use crate::error::ContractError;
4746
use crate::state::{BALANCES, CONFIG, OBSERVATIONS, OWNERSHIP_PROPOSAL};
@@ -559,7 +558,7 @@ fn withdraw_liquidity(
559558
info: MessageInfo,
560559
assets: Vec<Asset>,
561560
) -> Result<Response, ContractError> {
562-
let mut config = CONFIG.load(deps.storage)?;
561+
let config = CONFIG.load(deps.storage)?;
563562

564563
let Coin { amount, denom } = one_coin(&info)?;
565564

@@ -587,18 +586,6 @@ fn withdraw_liquidity(
587586
return Err(StdError::generic_err("Imbalanced withdraw is currently disabled").into());
588587
};
589588

590-
// decrease XCP
591-
let mut xs = pools.iter().map(|a| a.amount).collect_vec();
592-
593-
xs[0] -= refund_assets[0].amount;
594-
xs[1] -= refund_assets[1].amount;
595-
xs[1] *= config.pool_state.price_state.price_scale;
596-
let amp_gamma = config.pool_state.get_amp_gamma(&env);
597-
let d = calc_d(&xs, &amp_gamma)?;
598-
config.pool_state.price_state.xcp_profit_real =
599-
get_xcp(d, config.pool_state.price_state.price_scale)
600-
/ (total_share - amount).to_decimal256(LP_TOKEN_PRECISION)?;
601-
602589
let refund_assets = refund_assets
603590
.into_iter()
604591
.map(|asset| {
@@ -637,8 +624,6 @@ fn withdraw_liquidity(
637624
}
638625
}
639626

640-
CONFIG.save(deps.storage, &config)?;
641-
642627
Ok(Response::new().add_messages(messages).add_attributes(vec![
643628
attr("action", "withdraw_liquidity"),
644629
attr("sender", info.sender),

contracts/pair_concentrated/tests/pair_concentrated_integration.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,55 @@ fn check_correct_fee_share() {
17131713
assert!(config.fee_share.is_none());
17141714
}
17151715

1716+
#[test]
1717+
fn test_lsd_corner_case() {
1718+
let owner = Addr::unchecked("owner");
1719+
1720+
let test_coins = vec![TestCoin::native("uusd"), TestCoin::native("uluna")];
1721+
1722+
let params = ConcentratedPoolParams {
1723+
amp: f64_to_dec(500.0),
1724+
gamma: f64_to_dec(0.01),
1725+
mid_fee: f64_to_dec(0.0003),
1726+
out_fee: f64_to_dec(0.0045),
1727+
fee_gamma: f64_to_dec(0.3),
1728+
repeg_profit_threshold: f64_to_dec(0.00000001),
1729+
min_price_scale_delta: f64_to_dec(0.0000055),
1730+
price_scale: f64_to_dec(1.5915113196549202),
1731+
..common_pcl_params()
1732+
};
1733+
1734+
let mut helper = Helper::new(&owner, test_coins.clone(), params).unwrap();
1735+
1736+
helper.app.next_block(61200);
1737+
1738+
let assets = vec![
1739+
helper.assets[&test_coins[0]].with_balance(10000u128),
1740+
helper.assets[&test_coins[1]].with_balance(10000u128),
1741+
];
1742+
helper.provide_liquidity(&owner, &assets).unwrap();
1743+
1744+
helper.app.next_block(240);
1745+
let assets = vec![
1746+
helper.assets[&test_coins[0]].with_balance(20000u128),
1747+
helper.assets[&test_coins[1]].with_balance(20000u128),
1748+
];
1749+
helper.provide_liquidity(&owner, &assets).unwrap();
1750+
1751+
helper.app.next_block(6000);
1752+
helper.withdraw_liquidity(&owner, 19519, vec![]).unwrap();
1753+
1754+
assert!(
1755+
helper
1756+
.query_config()
1757+
.unwrap()
1758+
.pool_state
1759+
.price_state
1760+
.xcp_profit_real
1761+
>= Decimal256::one()
1762+
);
1763+
}
1764+
17161765
#[test]
17171766
fn check_small_trades() {
17181767
let owner = Addr::unchecked("owner");

contracts/pair_concentrated_duality/src/execute.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use astroport_pcl_common::utils::{
2323
accumulate_prices, assert_max_spread, before_swap_check, calc_last_prices, compute_swap,
2424
get_share_in_assets, mint_liquidity_token_message,
2525
};
26-
use astroport_pcl_common::{calc_d, get_xcp};
2726

2827
use crate::error::ContractError;
2928
use crate::instantiate::LP_TOKEN_PRECISION;
@@ -399,14 +398,6 @@ fn withdraw_liquidity(
399398
);
400399
ob_state.save(deps.storage)?;
401400

402-
// decrease XCP
403-
xs[1] *= config.pool_state.price_state.price_scale;
404-
let amp_gamma = config.pool_state.get_amp_gamma(&env);
405-
let d = calc_d(&xs, &amp_gamma)?;
406-
config.pool_state.price_state.xcp_profit_real =
407-
get_xcp(d, config.pool_state.price_state.price_scale)
408-
/ (total_share - amount).to_decimal256(LP_TOKEN_PRECISION)?;
409-
410401
CONFIG.save(deps.storage, &config)?;
411402

412403
Ok(response.add_submessages(submsgs).add_attributes([

contracts/pair_concentrated_sale_tax/src/contract.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use astroport_pcl_common::utils::{
4242
accumulate_prices, assert_max_spread, before_swap_check, calc_last_prices, check_asset_infos,
4343
check_cw20_in_pool, get_share_in_assets, mint_liquidity_token_message,
4444
};
45-
use astroport_pcl_common::{calc_d, get_xcp};
4645

4746
use crate::error::ContractError;
4847
use crate::state::{BALANCES, CONFIG, OBSERVATIONS, OWNERSHIP_PROPOSAL, TAX_CONFIG};
@@ -569,7 +568,7 @@ fn withdraw_liquidity(
569568
info: MessageInfo,
570569
assets: Vec<Asset>,
571570
) -> Result<Response, ContractError> {
572-
let mut config = CONFIG.load(deps.storage)?;
571+
let config = CONFIG.load(deps.storage)?;
573572

574573
let Coin { amount, denom } = one_coin(&info)?;
575574

@@ -597,18 +596,6 @@ fn withdraw_liquidity(
597596
return Err(StdError::generic_err("Imbalanced withdraw is currently disabled").into());
598597
};
599598

600-
// decrease XCP
601-
let mut xs = pools.iter().map(|a| a.amount).collect_vec();
602-
603-
xs[0] -= refund_assets[0].amount;
604-
xs[1] -= refund_assets[1].amount;
605-
xs[1] *= config.pool_state.price_state.price_scale;
606-
let amp_gamma = config.pool_state.get_amp_gamma(&env);
607-
let d = calc_d(&xs, &amp_gamma)?;
608-
config.pool_state.price_state.xcp_profit_real =
609-
get_xcp(d, config.pool_state.price_state.price_scale)
610-
/ (total_share - amount).to_decimal256(LP_TOKEN_PRECISION)?;
611-
612599
let refund_assets = refund_assets
613600
.into_iter()
614601
.map(|asset| {
@@ -647,8 +634,6 @@ fn withdraw_liquidity(
647634
}
648635
}
649636

650-
CONFIG.save(deps.storage, &config)?;
651-
652637
Ok(Response::new().add_messages(messages).add_attributes(vec![
653638
attr("action", "withdraw_liquidity"),
654639
attr("sender", info.sender),

0 commit comments

Comments
 (0)