Skip to content

Commit

Permalink
Improve code (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier authored Dec 7, 2022
1 parent 4d167f0 commit 8943e2d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
16 changes: 6 additions & 10 deletions pallet/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub mod pallet {
let now = T::UnixTime::now().as_millis();
let mut claimed = 0;
let _ = <Deposits<T>>::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 {
Expand All @@ -232,7 +232,7 @@ pub mod pallet {
*maybe_ds = None;
}

Ok(())
<Result<(), ()>>::Ok(())
});

T::Ring::transfer(&Self::account_id(), &who, claimed, AllowDeath)?;
Expand Down Expand Up @@ -265,10 +265,8 @@ where

fn stake(who: &Self::AccountId, item: Self::Item) -> DispatchResult {
<Deposits<T>>::try_mutate(who, |ds| {
let Some(ds) = ds else { return Err(<Error<T>>::DepositNotFound)?; };
let Some(d) = ds.iter_mut().find(|d| d.id == item) else {
return Err(<Error<T>>::DepositNotFound)?;
};
let ds = ds.as_mut().ok_or(<Error<T>>::DepositNotFound)?;
let d = ds.iter_mut().find(|d| d.id == item).ok_or(<Error<T>>::DepositNotFound)?;

if d.in_use {
Err(<Error<T>>::DepositInUse)?
Expand All @@ -282,10 +280,8 @@ where

fn unstake(who: &Self::AccountId, item: Self::Item) -> DispatchResult {
<Deposits<T>>::try_mutate(who, |ds| {
let Some(ds) = ds else { return Err(<Error<T>>::DepositNotFound)?; };
let Some(d) = ds.iter_mut().find(|d| d.id == item) else {
return Err(<Error<T>>::DepositNotFound)?;
};
let ds = ds.as_mut().ok_or(<Error<T>>::DepositNotFound)?;
let d = ds.iter_mut().find(|d| d.id == item).ok_or(<Error<T>>::DepositNotFound)?;

if d.in_use {
d.in_use = false;
Expand Down
70 changes: 31 additions & 39 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//!
Expand Down Expand Up @@ -415,9 +417,7 @@ pub mod pallet {
}

<Ledgers<T>>::try_mutate(&who, |l| {
let Some(l) = l else {
return Err(<Error<T>>::NotStaker)?;
};
let l = l.as_mut().ok_or(<Error<T>>::NotStaker)?;

if ring_amount != 0 {
Self::unstake_ring(l, ring_amount)?;
Expand Down Expand Up @@ -513,34 +513,25 @@ pub mod pallet {
P: frame_support::StorageValue<Balance, Query = Balance>,
{
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(())
})
}

fn stake_ring(ledger: &mut Ledger<T>, 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::<RingPool<T>>(true, amount)?;

Expand All @@ -550,11 +541,10 @@ pub mod pallet {
fn stake_kton(ledger: &mut Ledger<T>, 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::<KtonPool<T>>(true, amount)?;

Expand All @@ -572,9 +562,10 @@ pub mod pallet {
}

fn unstake_ring(ledger: &mut Ledger<T>, 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
Expand All @@ -591,9 +582,10 @@ pub mod pallet {
}

fn unstake_kton(ledger: &mut Ledger<T>, 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
Expand All @@ -610,9 +602,9 @@ pub mod pallet {
}

fn unstake_deposit(ledger: &mut Ledger<T>, deposit: DepositId<T>) -> 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
Expand All @@ -629,9 +621,7 @@ pub mod pallet {

fn claim_unstakings(who: &T::AccountId) -> DispatchResult {
<Ledgers<T>>::try_mutate(who, |l| {
let Some(l) = l else {
return Err(<Error<T>>::NotStaker)?;
};
let l = l.as_mut().ok_or(<Error<T>>::NotStaker)?;
let now = <frame_system::Pallet<T>>::block_number();
let claim = |u: &mut BoundedVec<_, _>, c: &mut Balance| {
u.retain(|(a, t)| {
Expand Down Expand Up @@ -676,7 +666,7 @@ pub mod pallet {

fn try_clean_ledger_of(who: &T::AccountId) {
let _ = <Ledgers<T>>::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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8943e2d

Please sign in to comment.