Skip to content

Commit

Permalink
Fix astroport LPer limit handling (#74)
Browse files Browse the repository at this point in the history
* fix limit

* add extra check
  • Loading branch information
keyleu authored Oct 17, 2024
1 parent 3e8eb90 commit d97ff81
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
6 changes: 2 additions & 4 deletions contracts/services/astroport-lper/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ mod actions {
)?;

// Check which asset is being provided and get its balance
let (asset_balance, other_asset) = if asset == cfg.lp_config.asset_data.asset1 {
let (mut asset_balance, other_asset) = if asset == cfg.lp_config.asset_data.asset1 {
(balance_asset1.clone(), balance_asset2.clone())
} else if asset == cfg.lp_config.asset_data.asset2 {
(balance_asset2.clone(), balance_asset1.clone())
Expand All @@ -295,9 +295,7 @@ mod actions {
// Check limit if provided
if let Some(limit) = limit {
if limit < asset_balance.amount {
return Err(ServiceError::ExecutionError(
"Asset amount is greater than the limit".to_string(),
));
asset_balance.amount = limit;
}
}

Expand Down
55 changes: 37 additions & 18 deletions contracts/services/astroport-lper/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::Uint128;
use neutron_test_tube::{
neutron_std::types::cosmos::{
bank::v1beta1::{MsgSend, QueryAllBalancesRequest},
bank::v1beta1::{MsgSend, QueryAllBalancesRequest, QueryBalanceRequest},
base::v1beta1::Coin as BankCoin,
},
Account, Bank, Module, Wasm,
Expand Down Expand Up @@ -688,23 +688,42 @@ fn provide_single_sided_liquidity_cw20_lp_token() {
fn test_limit_single_sided_liquidity() {
let setup = LPerTestSuite::default();
let wasm = Wasm::new(&setup.inner.app);
let bank = Bank::new(&setup.inner.app);

let error = wasm
.execute::<ExecuteMsg<ActionsMsgs, OptionalServiceConfig>>(
&setup.lper_addr,
&ExecuteMsg::ProcessAction(ActionsMsgs::ProvideSingleSidedLiquidity {
asset: setup.inner.pool_asset1.clone(),
limit: Some(Uint128::new(1)),
expected_pool_ratio_range: None,
}),
&[],
setup.inner.processor_acc(),
)
.unwrap_err();
let query_balance = |address: &str| -> u128 {
bank.query_balance(&QueryBalanceRequest {
address: address.to_string(),
denom: setup.inner.pool_asset1.clone(),
})
.unwrap()
.balance
.unwrap()
.amount
.parse()
.unwrap()
};

assert!(error.to_string().contains(
ServiceError::ExecutionError("Asset amount is greater than the limit".to_string())
.to_string()
.as_str(),
),);
let input_acc_balance_before = query_balance(&setup.input_acc);

let liquidity_provided = 500_000u128;

wasm.execute::<ExecuteMsg<ActionsMsgs, OptionalServiceConfig>>(
&setup.lper_addr,
&ExecuteMsg::ProcessAction(ActionsMsgs::ProvideSingleSidedLiquidity {
asset: setup.inner.pool_asset1.clone(),
limit: Some(Uint128::new(liquidity_provided)),
expected_pool_ratio_range: None,
}),
&[],
setup.inner.processor_acc(),
)
.unwrap();

let input_acc_balance_after = query_balance(&setup.input_acc);

assert!(input_acc_balance_after > 0);
assert_eq!(
input_acc_balance_before - liquidity_provided,
input_acc_balance_after
);
}

0 comments on commit d97ff81

Please sign in to comment.