Skip to content

Commit

Permalink
Validation for basic cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
majusko committed Sep 28, 2023
1 parent 17ae63c commit 0a45b91
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pallets/xyk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,8 @@ impl<T: Config> Pallet<T> {
let (input_reserve, output_reserve) =
Pallet::<T>::get_reserves(sold_token_id, bought_token_id)?;

ensure!(!(Self::is_pool_empty(sold_token_id, bought_token_id)?), Error::<T>::PoolIsEmpty);

Self::calculate_sell_price(input_reserve, output_reserve, sell_amount)
}

Expand All @@ -1352,6 +1354,8 @@ impl<T: Config> Pallet<T> {
let (input_reserve, output_reserve) =
Pallet::<T>::get_reserves(sold_token_id, bought_token_id)?;

ensure!(!(Self::is_pool_empty(sold_token_id, bought_token_id)?), Error::<T>::PoolIsEmpty);

Self::calculate_buy_price(input_reserve, output_reserve, buy_amount)
}

Expand Down Expand Up @@ -1407,6 +1411,8 @@ impl<T: Config> Pallet<T> {
let (first_asset_reserve, second_asset_reserve) =
Pallet::<T>::get_reserves(first_asset_id, second_asset_id)?;

ensure!(!(Self::is_pool_empty(first_asset_id, second_asset_id)?), Error::<T>::PoolIsEmpty);

let (first_asset_amount, second_asset_amount) = Self::get_burn_amount_reserves(
first_asset_reserve,
second_asset_reserve,
Expand Down Expand Up @@ -3061,6 +3067,8 @@ impl<T: Config> XykFunctionsTrait<T::AccountId> for Pallet<T> {
// checks
ensure!(!provided_asset_amount.is_zero(), Error::<T>::ZeroAmount,);

ensure!(!(Self::is_pool_empty(first_asset_id, second_asset_id)?), Error::<T>::PoolIsEmpty);

let (first_reserve, second_reserve) =
Pallet::<T>::get_reserves(first_asset_id, second_asset_id)?;

Expand Down
30 changes: 30 additions & 0 deletions pallets/xyk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,36 @@ fn burn_all_liq_and_mint_it_again() {
Error::<Test>::PoolIsEmpty,
);

// calculate_buy_price_id should fail as pool is empty
assert_err!(
XykStorage::calculate_buy_price_id(
1,
4,
20000
),
Error::<Test>::PoolIsEmpty,
);

// calculate_sell_price_id should fail as pool is empty
assert_err!(
XykStorage::calculate_sell_price_id(
1,
4,
20000
),
Error::<Test>::PoolIsEmpty,
);

// get_burn_amount should fail as pool is empty
assert_err!(
XykStorage::get_burn_amount(
1,
4,
20000
),
Error::<Test>::PoolIsEmpty,
);

let user_assets_1_value_after_sell = XykStorage::balance(1, DUMMY_USER_ID);
let user_assets_4_value_after_sell = XykStorage::balance(4, DUMMY_USER_ID);

Expand Down

0 comments on commit 0a45b91

Please sign in to comment.