Skip to content

Commit

Permalink
feat(test): add pool_insufficient_balance_for_swap test
Browse files Browse the repository at this point in the history
  • Loading branch information
TropicalDog17 committed Dec 2, 2023
1 parent 5715f32 commit 860d91d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,11 @@ pub mod execute {

// Precheck if enough balance in pool for user swap request
let available_offer = offer.query_pool(&deps.querier, pool_address.clone())?;
// TODO: Use a better error type
if available_offer < amount.into() {
return Err(ContractError::Unauthorized {});
return Err(ContractError::InsufficientFunds {
asked_amount: amount.into(),
available_amount: available_offer,
});
}

let asked_asset = if offer.equal(&asset_infos[0]) {
Expand Down
8 changes: 7 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use cosmwasm_std::StdError;
use cosmwasm_std::Uint128;
use cw_utils::PaymentError;
use thiserror::Error;
#[derive(Error, Debug)]
#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),
Expand All @@ -20,4 +21,9 @@ pub enum ContractError {
PoolNotExist {},
#[error("Offer and ask token should not be identical")]
DoublingAssets {},
#[error("Insufficient funds available in the pool to complete the swap: {asked_amount} > {available_amount}")]
InsufficientFunds {
asked_amount: Uint128,
available_amount: Uint128,
},
}
72 changes: 72 additions & 0 deletions src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,76 @@ mod tests {
Uint128::from(500u128)
);
}
#[test]
fn test_pool_insufficient_balance_for_swap() {
let owner = Addr::unchecked(ADMIN);
let user = Addr::unchecked(USER);
let (mut app, cw_contract) = proper_instantiate();
let (token_contract, factory_contract, pair_contract) = instantiate_contracts(&mut app);
let my_contract = instantiate_my_contract(&mut app);
let inj_amount = Uint128::new(1_000_000);
let ttt_amount = Uint128::new(1_000_000);
let inj_offer = Uint128::new(10_000_000);

let (msg, coins) =
provide_liquidity_msg(ttt_amount, inj_amount, None, None, &token_contract);

app.execute_contract(owner.clone(), pair_contract.addr(), &msg, &coins)
.unwrap();
// Enable pool balance tracking
let msg = PairExecuteMsg::UpdateConfig {
params: to_json_binary(&XYKPoolUpdateParams::EnableAssetBalancesTracking).unwrap(),
};
app.execute_contract(owner.clone(), pair_contract.addr(), &msg, &[])
.unwrap();

app.update_block(|b| b.height += 1);

// Check pool balances
let res: Option<Uint128> = app
.wrap()
.query_wasm_smart(
pair_contract.addr(),
&PairQueryMsg::AssetBalanceAt {
asset_info: AssetInfo::NativeToken {
denom: "ttt".to_string(),
},
block_height: app.block_info().height.into(),
},
)
.unwrap();
assert_eq!(res.unwrap(), Uint128::new(1_000_000));
// Check pool balances
let res: Option<Uint128> = app
.wrap()
.query_wasm_smart(
pair_contract.addr(),
&PairQueryMsg::AssetBalanceAt {
asset_info: AssetInfo::NativeToken {
denom: "inj".to_owned(),
},
block_height: app.block_info().height.into(),
},
)
.unwrap();
assert_eq!(res.unwrap(), Uint128::new(1_000_000));
// Perform swap of two native tokens
let swap_msg = crate::msg::ExecuteMsg::MySwap {
pool_address: pair_contract.addr(),
};
let send_funds = vec![Coin {
denom: "inj".to_string(),
amount: inj_offer,
}];
let res = app
.execute_contract(owner.clone(), my_contract.addr(), &swap_msg, &send_funds)
.unwrap_err();
assert_eq!(
crate::error::ContractError::InsufficientFunds {
asked_amount: inj_offer,
available_amount: Uint128::new(1_000_000)
},
res.downcast().unwrap(),
);
}
}

0 comments on commit 860d91d

Please sign in to comment.