From 8943e2dde198d755aa90b6776eb3260035fdf81b Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Wed, 7 Dec 2022 21:33:29 +0800 Subject: [PATCH] Improve code (#111) --- pallet/deposit/src/lib.rs | 16 ++++----- pallet/staking/src/lib.rs | 70 +++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 49 deletions(-) diff --git a/pallet/deposit/src/lib.rs b/pallet/deposit/src/lib.rs index 6f43e31e1..97babfa07 100644 --- a/pallet/deposit/src/lib.rs +++ b/pallet/deposit/src/lib.rs @@ -214,7 +214,7 @@ pub mod pallet { let now = T::UnixTime::now().as_millis(); let mut claimed = 0; let _ = >::try_mutate(&who, |maybe_ds| { - let Some(ds) = maybe_ds else { return Err(()); }; + let ds = maybe_ds.as_mut().ok_or(())?; ds.retain(|d| { if d.expired_time <= now && !d.in_use { @@ -232,7 +232,7 @@ pub mod pallet { *maybe_ds = None; } - Ok(()) + >::Ok(()) }); T::Ring::transfer(&Self::account_id(), &who, claimed, AllowDeath)?; @@ -265,10 +265,8 @@ where fn stake(who: &Self::AccountId, item: Self::Item) -> DispatchResult { >::try_mutate(who, |ds| { - let Some(ds) = ds else { return Err(>::DepositNotFound)?; }; - let Some(d) = ds.iter_mut().find(|d| d.id == item) else { - return Err(>::DepositNotFound)?; - }; + let ds = ds.as_mut().ok_or(>::DepositNotFound)?; + let d = ds.iter_mut().find(|d| d.id == item).ok_or(>::DepositNotFound)?; if d.in_use { Err(>::DepositInUse)? @@ -282,10 +280,8 @@ where fn unstake(who: &Self::AccountId, item: Self::Item) -> DispatchResult { >::try_mutate(who, |ds| { - let Some(ds) = ds else { return Err(>::DepositNotFound)?; }; - let Some(d) = ds.iter_mut().find(|d| d.id == item) else { - return Err(>::DepositNotFound)?; - }; + let ds = ds.as_mut().ok_or(>::DepositNotFound)?; + let d = ds.iter_mut().find(|d| d.id == item).ok_or(>::DepositNotFound)?; if d.in_use { d.in_use = false; diff --git a/pallet/staking/src/lib.rs b/pallet/staking/src/lib.rs index 91b78e9be..e095e9e11 100644 --- a/pallet/staking/src/lib.rs +++ b/pallet/staking/src/lib.rs @@ -18,6 +18,8 @@ //! # Darwinia parachain staking pallet //! +//! ## Overview +//! //! This is a completely specialized stake pallet designed only for Darwinia parachain. //! So, this pallet will eliminate the generic parameters as much as possible. //! @@ -415,9 +417,7 @@ pub mod pallet { } >::try_mutate(&who, |l| { - let Some(l) = l else { - return Err(>::NotStaker)?; - }; + let l = l.as_mut().ok_or(>::NotStaker)?; if ring_amount != 0 { Self::unstake_ring(l, ring_amount)?; @@ -513,22 +513,14 @@ pub mod pallet { P: frame_support::StorageValue, { P::try_mutate(|p| { - let p_new = if increase { - let Some(p_new) = p.checked_add(amount) else { - return Err("[pallet::staking] `u128` must not be overflowed; qed")?; - }; - - p_new + *p = if increase { + p.checked_add(amount) + .ok_or("[pallet::staking] `u128` must not be overflowed; qed")? } else { - let Some(p_new) = p.checked_sub(amount) else { - return Err("[pallet::staking] `u128` must not be overflowed; qed")?; - }; - - p_new + p.checked_sub(amount) + .ok_or("[pallet::staking] `u128` must not be overflowed; qed")? }; - *p = p_new; - Ok(()) }) } @@ -536,11 +528,10 @@ pub mod pallet { fn stake_ring(ledger: &mut Ledger, amount: Balance) -> DispatchResult { T::Ring::stake(&ledger.account, amount)?; - ledger.staked_ring = if let Some(r) = ledger.staked_ring.checked_add(amount) { - r - } else { - return Err("[pallet::staking] `u128` must not be overflowed; qed")?; - }; + ledger.staked_ring = ledger + .staked_ring + .checked_add(amount) + .ok_or("[pallet::staking] `u128` must not be overflowed; qed")?; Self::update_pool::>(true, amount)?; @@ -550,11 +541,10 @@ pub mod pallet { fn stake_kton(ledger: &mut Ledger, amount: Balance) -> DispatchResult { T::Kton::stake(&ledger.account, amount)?; - ledger.staked_kton = if let Some(k) = ledger.staked_kton.checked_add(amount) { - k - } else { - return Err("[pallet::staking] `u128` must not be overflowed; qed")?; - }; + ledger.staked_kton = ledger + .staked_kton + .checked_add(amount) + .ok_or("[pallet::staking] `u128` must not be overflowed; qed")?; Self::update_pool::>(true, amount)?; @@ -572,9 +562,10 @@ pub mod pallet { } fn unstake_ring(ledger: &mut Ledger, amount: Balance) -> DispatchResult { - let Some(nr) = ledger.staked_ring.checked_sub(amount) else { - return Err("[pallet::staking] `u128` must not be overflowed; qed")?; - }; + let nr = ledger + .staked_ring + .checked_sub(amount) + .ok_or("[pallet::staking] `u128` must not be overflowed; qed")?; ledger.staked_ring = nr; ledger @@ -591,9 +582,10 @@ pub mod pallet { } fn unstake_kton(ledger: &mut Ledger, amount: Balance) -> DispatchResult { - let Some(nk) = ledger.staked_kton.checked_sub(amount) else { - return Err("[pallet::staking] `u128` must not be overflowed; qed")?; - }; + let nk = ledger + .staked_kton + .checked_sub(amount) + .ok_or("[pallet::staking] `u128` must not be overflowed; qed")?; ledger.staked_kton = nk; ledger @@ -610,9 +602,9 @@ pub mod pallet { } fn unstake_deposit(ledger: &mut Ledger, deposit: DepositId) -> DispatchResult { - let Some(i) = ledger.staked_deposits.iter().position(|d| d == &deposit) else { - return Err("[pallet::staking] deposit id must be existed, due to previous unstake OP; qed")?; - }; + let i = ledger.staked_deposits.iter().position(|d| d == &deposit).ok_or( + "[pallet::staking] deposit id must be existed, due to previous unstake OP; qed", + )?; ledger .unstaking_deposits @@ -629,9 +621,7 @@ pub mod pallet { fn claim_unstakings(who: &T::AccountId) -> DispatchResult { >::try_mutate(who, |l| { - let Some(l) = l else { - return Err(>::NotStaker)?; - }; + let l = l.as_mut().ok_or(>::NotStaker)?; let now = >::block_number(); let claim = |u: &mut BoundedVec<_, _>, c: &mut Balance| { u.retain(|(a, t)| { @@ -676,7 +666,7 @@ pub mod pallet { fn try_clean_ledger_of(who: &T::AccountId) { let _ = >::try_mutate(who, |maybe_l| { - let Some(l) = maybe_l else { return Err(()); }; + let l = maybe_l.as_mut().ok_or(())?; if l.is_empty() { *maybe_l = None; @@ -752,6 +742,8 @@ pub mod pallet { session_duration, elapsed_time, ) else { + log::error!("[pallet::staking] failed to calculate the inflation"); + return; }; let payout = T::PayoutFraction::get() * inflation;