4

In this code, we can see the pallet::compact is used. So why we need this instruction?

quotes code:

#[pallet::weight(1_000)]
        pub fn transfer(
            origin: OriginFor<T>,
            to: T::AccountId,
            #[pallet::compact] amount: T::Balance,
        ) -> DispatchResultWithPostInfo {
            let sender = ensure_signed(origin)?;
            let sender_balance = Self::get_balance(&sender);
            let receiver_balance = Self::get_balance(&to);

            // Calculate new balances.
            let update_sender = sender_balance.saturating_sub(amount);
            let update_to = receiver_balance.saturating_add(amount);

            // Update both accounts storage.
            <BalanceToAccount<T>>::insert(&sender, update_sender);
            <BalanceToAccount<T>>::insert(&to, update_to);

            // Emit event.
            Self::deposit_event(Event::Transferred(sender, to, amount));
            Ok(().into())
        }

1 Answer 1

6

pallet::compact is just a "shortcut" for writing Compact<T::Balance>.

Compact is a wrapper defined by parity-scale-codec to compact integers when being stored in their encoded format. So, that the entire transaction is a little bit smaller.

2
  • Is there a downside to this? Why is it not default? Commented Apr 14, 2022 at 9:22
  • 2
    Not every type can be compacted, so you can not use it by default. Downside is that you trade smaller storage requirements for more computational requirements (but they are really neglectable)
    – bkchr
    Commented Apr 14, 2022 at 9:53

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.