From dc65a0a34d0ea483001e3672a69693aa2e8ff015 Mon Sep 17 00:00:00 2001 From: librelois <c@elo.tf> Date: Sun, 3 Jul 2022 03:14:06 +0200 Subject: [PATCH] manual ud: not emit event if 0 UDs claimed --- pallets/universal-dividend/src/lib.rs | 57 ++++++++++++------------- pallets/universal-dividend/src/tests.rs | 9 +--- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/pallets/universal-dividend/src/lib.rs b/pallets/universal-dividend/src/lib.rs index b81811449..3e12e61f7 100644 --- a/pallets/universal-dividend/src/lib.rs +++ b/pallets/universal-dividend/src/lib.rs @@ -274,38 +274,35 @@ pub mod pallet { total_weight } fn do_claim_uds(who: &T::AccountId) -> DispatchResultWithPostInfo { - let (uds_count, uds_total) = - T::MembersStorage::try_mutate_exists(who, |maybe_first_eligible_ud| { - if let Some(FirstEligibleUd(Some(ref mut first_ud_index))) = - maybe_first_eligible_ud - { - let current_ud_index = CurrentUdIndex::<T>::get(); - if first_ud_index.get() >= current_ud_index { - Ok((0, Zero::zero())) - } else { - let (uds_count, uds_total) = compute_claim_uds::compute_claim_uds( - current_ud_index, - first_ud_index.get(), - PastReevals::<T>::get().into_iter(), - ); - let _ = core::mem::replace( - first_ud_index, - core::num::NonZeroU16::new(current_ud_index) - .expect("unrechable because current_ud_index is never zero."), - ); - Ok((uds_count, uds_total)) - } + T::MembersStorage::try_mutate_exists(who, |maybe_first_eligible_ud| { + if let Some(FirstEligibleUd(Some(ref mut first_ud_index))) = maybe_first_eligible_ud + { + let current_ud_index = CurrentUdIndex::<T>::get(); + if first_ud_index.get() >= current_ud_index { + DispatchResultWithPostInfo::Ok(().into()) } else { - Err::<_, DispatchError>(Error::<T>::AccountNotAllowedToClaimUds.into()) + let (uds_count, uds_total) = compute_claim_uds::compute_claim_uds( + current_ud_index, + first_ud_index.get(), + PastReevals::<T>::get().into_iter(), + ); + let _ = core::mem::replace( + first_ud_index, + core::num::NonZeroU16::new(current_ud_index) + .expect("unrechable because current_ud_index is never zero."), + ); + T::Currency::deposit_creating(who, uds_total); + Self::deposit_event(Event::UdsClaimed { + count: uds_count, + total: uds_total, + who: who.clone(), + }); + Ok(().into()) } - })?; - T::Currency::deposit_creating(who, uds_total); - Self::deposit_event(Event::UdsClaimed { - count: uds_count, - total: uds_total, - who: who.clone(), - }); - Ok(().into()) + } else { + Err(Error::<T>::AccountNotAllowedToClaimUds.into()) + } + }) } fn do_transfer_ud( origin: OriginFor<T>, diff --git a/pallets/universal-dividend/src/tests.rs b/pallets/universal-dividend/src/tests.rs index 518dfd77f..9cfbfe1a7 100644 --- a/pallets/universal-dividend/src/tests.rs +++ b/pallets/universal-dividend/src/tests.rs @@ -15,7 +15,7 @@ // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. use crate::mock::*; -use frame_support::{assert_err, assert_ok}; +use frame_support::{assert_err, assert_ok, assert_storage_noop}; #[test] fn test_claim_uds() { @@ -35,12 +35,7 @@ fn test_claim_uds() { // Alice can claim UDs, but this should be a no-op. run_to_block(1); - assert_ok!(UniversalDividend::claim_uds(Origin::signed(1))); - System::assert_has_event(Event::UniversalDividend(crate::Event::UdsClaimed { - count: 0, - total: 0, - who: 1, - })); + assert_storage_noop!(assert_ok!(UniversalDividend::claim_uds(Origin::signed(1)))); assert_eq!(Balances::free_balance(1), 0); // Dave is not a member, he can't claim UDs -- GitLab