Skip to content

Commit

Permalink
Check that all pool join/exit's raise on zero
Browse files Browse the repository at this point in the history
`util::pool` raises if either `pool_amount` or `asset_amount` is zero.
This is an unfortunate result of the abstraction of `pool_join` and
`pool_exit` into `util::pool`, but it does protect the user from paying
for something they get nothing for.

The other four functions only raise an error if what the joining/exiting
user pays is zero. So this protects the LPs, but not the interacting
user.
  • Loading branch information
maltekliemann committed Mar 28, 2022
1 parent 256281a commit 5ac736d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions zrml/swaps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ mod pallet {
pool_amount: BalanceOf<T>,
min_asset_amount: BalanceOf<T>,
) -> DispatchResult {
ensure!(pool_amount != Zero::zero(), Error::<T>::MathApproximation);
let pool = Self::pool_by_id(pool_id)?;
let pool_ref = &pool;
let who = ensure_signed(origin)?;
Expand All @@ -313,6 +314,7 @@ mod pallet {
pool.swap_fee.ok_or(Error::<T>::PoolMissingFee)?.saturated_into(),
)?
.saturated_into();
ensure!(asset_amount != Zero::zero(), Error::<T>::MathApproximation);
ensure!(asset_amount >= min_asset_amount, Error::<T>::LimitOut);
ensure!(
asset_amount
Expand Down Expand Up @@ -1554,6 +1556,7 @@ mod pallet {
asset_amount: BalanceOf<T>,
min_pool_amount: BalanceOf<T>,
) -> Result<Weight, DispatchError> {
ensure!(asset_amount != Zero::zero(), Error::<T>::MathApproximation);
let pool = Pallet::<T>::pool_by_id(pool_id)?;
let pool_ref = &pool;
let pool_account_id = Pallet::<T>::pool_account_id(pool_id);
Expand Down
22 changes: 21 additions & 1 deletion zrml/swaps/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ fn out_amount_must_be_equal_or_less_than_max_out_ratio() {
}

#[test]
fn pool_amount_must_not_be_zero() {
fn pool_join_or_exit_raises_on_zero_value() {
ExtBuilder::default().build().execute_with(|| {
create_initial_pool_with_funds_for_alice(ScoringRule::CPMM, true);

Expand All @@ -568,6 +568,26 @@ fn pool_amount_must_not_be_zero() {
Swaps::pool_exit(alice_signed(), 0, 0, vec!(_1, _1, _1, _1)),
crate::Error::<Runtime>::MathApproximation
);

assert_noop!(
Swaps::pool_join_with_exact_pool_amount(alice_signed(), 0, ASSET_A, 0, 0),
crate::Error::<Runtime>::MathApproximation
);

assert_noop!(
Swaps::pool_join_with_exact_asset_amount(alice_signed(), 0, ASSET_A, 0, 0),
crate::Error::<Runtime>::MathApproximation
);

assert_noop!(
Swaps::pool_exit_with_exact_pool_amount(alice_signed(), 0, ASSET_A, 0, 0),
crate::Error::<Runtime>::MathApproximation
);

assert_noop!(
Swaps::pool_exit_with_exact_asset_amount(alice_signed(), 0, ASSET_A, 0, 0),
crate::Error::<Runtime>::MathApproximation
);
});
}

Expand Down

0 comments on commit 5ac736d

Please sign in to comment.