I’m developing a Substrate-based blockchain and I want to create a pallet that intercepts all runtime calls. I have created a pallet that accepts a runtime call as a parameter, but it doesn’t intercept all calls as expected. How can I ensure that every call is funneled through my pallet? I can manually call my extrinsic and pass another runtime call into it, but I want all calls automatically be funneled into that extrinsic.
I followed an approach similar to the feeless pallet by Shawn, as demonstrated in this video. However, while his implementation works, mine does not. Below is the relevant part of my code.
pallet:
pub use pallet::*;
use frame_support::dispatch::GetDispatchInfo;
use frame_support::traits::UnfilteredDispatchable;
use scale_info::prelude::boxed::Box;
use frame_support::dispatch::PostDispatchInfo;
use sp_runtime::traits::Dispatchable;
#[pallet::config(with_default)]
pub trait Config: frame_system::Config {
#[pallet::no_default_bounds]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
#[pallet::no_default_bounds]
type RuntimeCall: Parameter
+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin>
+ From<Call<Self>>;
type WeightInfo: WeightInfo;
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::intercept())]
pub fn intercept(
origin: OriginFor<T>,
call: Box<<T as Config>::RuntimeCall>,
) -> DispatchResultWithPostInfo {
let _res = call.dispatch(origin);
Self::deposit_event(Event::Intercepted);
...
}
}
runtime:
impl pallet_interceptor::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type WeightInfo = pallet_interceptor::weights::SubstrateWeight<Runtime>;
}
construct_runtime!(
pub enum Runtime {
System: frame_system,
Timestamp: pallet_timestamp,
Aura: pallet_aura,
Grandpa: pallet_grandpa,
Balances: pallet_balances,
TransactionPayment: pallet_transaction_payment,
Sudo: pallet_sudo,
Identity: pallet_identity,
Collective: pallet_collective::<Instance1>,
Membership: pallet_membership,
Preimage: pallet_preimage,
Scheduler: pallet_scheduler,
Bounties: pallet_bounties,
Treasury: pallet_treasury,
Nfts: pallet_nfts,
Democracy: pallet_democracy,
Interceptor: pallet_interceptor,
}
);