referrerpolicy=no-referrer-when-downgrade
frame_support::pallet_macros

Attribute Macro weight

Source
#[weight]
Expand description

Allows specifying the weight of a call.

Each dispatchable needs to define a weight. This attribute allows to define a weight using the expression: #[pallet::weight($expr)] Note that argument of the call are available inside the expression.

If not defined explicitly, the weight can be implicitly inferred from the weight info defined in the attribute pallet::call: #[pallet::call(weight = $WeightInfo)]. Or it can be simply ignored when the pallet is in dev_mode.

§Example

#[frame_support::pallet]
mod pallet {
 	use frame_support::pallet_prelude::*;
 	use frame_system::pallet_prelude::*;

	#[pallet::pallet]
	pub struct Pallet<T>(_);

 	#[pallet::config]
 	pub trait Config: frame_system::Config {
        /// Type for specifying dispatchable weights.
        type WeightInfo: WeightInfo;
    }

	#[pallet::call(weight = <T as Config>::WeightInfo)]
	impl<T: Config> Pallet<T> {
		// Explicit weight definition
		#[pallet::weight(<T as Config>::WeightInfo::do_something())]
		#[pallet::call_index(0)]
		pub fn do_something(
			origin: OriginFor<T>,
			foo: u32,
		) -> DispatchResult {
			Ok(())
		}

            // Implicit weight definition, the macro looks up to the weight info defined in
            // `#[pallet::call(weight = $WeightInfo)]` attribute. Then use
            // `$WeightInfo::do_something_else` as the weight function.
            #[pallet::call_index(1)]
            pub fn do_something_else(
                origin: OriginFor<T>,
                bar: u64,
            ) -> DispatchResult {
                Ok(())
            }
    }

    /// The `WeightInfo` trait defines weight functions for dispatchable calls.
    pub trait WeightInfo {
        fn do_something() -> Weight;
        fn do_something_else() -> Weight;
    }
}

Documentation for this macro can be found at frame_support::pallet_macros::weight.