1

I am analyzing the slashing module in Staking pallet and found that whenever slash happens NegativeImbalance is generating.

// 1
fn apply_slash() {

     do_slash();
     payout_reporters();
}

// 2 Imbalance is generating
fn do_slash() {
    ...
    let (imbalance, missing) = T::Currency::slash(stash, value);
    slashed_imbalance.subsume(imbalance);
}

// 3 payout reporters when there are reporters otherwise goes to Treasury
fn payout_reporters() {
    
    if reward_payout.is_zero() || reporters.is_empty() {
        
        // T::Slash = Treasury 
        // Question: How exactly does this imbalance go to Treasury? What is really happening on back side?
        T::Slash::on_unbalanced(slashed_imbalance); 
        return
    }
}

// 4 Inside Treasury Pallet
impl<T: Config<I>, I: 'static> OnUnbalanced<NegativeImbalanceOf<T, I>> for Pallet<T, I> {
fn on_nonzero_unbalanced(amount: NegativeImbalanceOf<T, I>) {
    let numeric_amount = amount.peek();

    // Must resolve into existing but better to be safe.
    let _ = T::Currency::resolve_creating(&Self::account_id(), amount);

    Self::deposit_event(Event::Deposit { value: numeric_amount });
}

}

Whenever Imbalance is dropped in Substrate, I know that TotalIssuance is increasing when PositiveImbalance is dropped and decreasing when NegativeImbalance is dropped.

Question

  1. How T::Slash::on_unbalanced(slashed_imbalance) makes NegativeImbalance, which is amount of slash goes to Treasury? I don't see any custom on_balance() method in OnUnbalance trait implementation inside Treasury pallet. It just seems like drop(slashed_imbalance) which only affects TotalIssuance not Treasury.

  2. How can I check whether its balance goes to Treasury or not?

1 Answer 1

2

No, it does NOT always go to the treasury pallet. This is something that is configured when setting up the pallets for your chain.

In staking there is a configuration:

/// Handler for the unbalanced reduction when slashing a staker.
type Slash: OnUnbalanced<NegativeImbalanceOf<Self>>;

In the runtime configuration this is configured as:

type Slash = Treasury; // send the slashed funds to the treasury.

So you can completely control what you want to do with the slashed funds. If you set it to (), then it will simply be burned.

4
  • Yeah I got your point. Then if it is (), TotalIssuance would be decreased, and if it is configured to Treasury, TotalIssuance would be increased? Another question is how Treasury pallet handle its slashed balance, since I can only see the default feature of on_unbalanced of OnUnbalanced trait, which is just drop. Does it automatically go to pot()? Thank you.
    – coco_yoon
    Commented Jul 30, 2022 at 16:36
  • Total issuance wouldn't be "increased"; it would stay the same. Yes, treasury has code to put the tokens into the treasury account: github.com/paritytech/substrate/blob/master/frame/treasury/src/…
    – Shawn Tabrizi
    Commented Jul 30, 2022 at 17:43
  • Ah, yes. Now I understand how Substrate handle its NegativeImbalance in terms of Slashing. One more last question is, in case of burn() in Balance Pallet, TotalIssuance is decreased with amount of burn and it returns PositiveImbalance. When it is dropped, TotalIssuance is increased, where does PositiveImbalance go? Does it go to Treasury Pallet?
    – coco_yoon
    Commented Jul 31, 2022 at 4:22
  • The behavior of PositiveImbalance and NegativeImbalance, when dropped, is to adjust the total issuance. So if you don't do anything with the imbalance, everything is automatically adjusted.
    – Shawn Tabrizi
    Commented Jul 31, 2022 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.