From ef0855bcfe8f5e965e10e39ffd6257fb14026cae Mon Sep 17 00:00:00 2001 From: maltekliemann Date: Mon, 28 Mar 2022 19:11:27 +0200 Subject: [PATCH] Return error if asset amount is zero This is primarily for the sake of symmetry (`pool_join_with_exact_pool_amount` raises `MathApproximation` if the asset amount is zero). Furthermore, calling this extrinsic with zero as parameter is rather non-sensical, but, more importantly, this check ensures that on-one can ever get free pool tokens. --- zrml/swaps/src/lib.rs | 1 + zrml/swaps/src/tests.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/zrml/swaps/src/lib.rs b/zrml/swaps/src/lib.rs index 99c24987cf..c4f916b338 100644 --- a/zrml/swaps/src/lib.rs +++ b/zrml/swaps/src/lib.rs @@ -1554,6 +1554,7 @@ mod pallet { asset_amount: BalanceOf, min_pool_amount: BalanceOf, ) -> Result { + ensure!(asset_amount != Zero::zero(), Error::::MathApproximation); let pool = Pallet::::pool_by_id(pool_id)?; let pool_ref = &pool; let pool_account_id = Pallet::::pool_account_id(pool_id); diff --git a/zrml/swaps/src/tests.rs b/zrml/swaps/src/tests.rs index 37788b7903..c2644f9a6b 100644 --- a/zrml/swaps/src/tests.rs +++ b/zrml/swaps/src/tests.rs @@ -571,6 +571,18 @@ fn pool_amount_must_not_be_zero() { }); } +#[test] +fn asset_amount_must_not_be_zero() { + ExtBuilder::default().build().execute_with(|| { + create_initial_pool_with_funds_for_alice(ScoringRule::CPMM, true); + + assert_noop!( + Swaps::pool_join_with_exact_asset_amount(alice_signed(), 0, ASSET_A, 0, 0), + crate::Error::::MathApproximation + ); + }); +} + #[test] fn pool_exit_decreases_correct_pool_parameters() { ExtBuilder::default().build().execute_with(|| {