pallets

Pallets that intend to send & receive messages over ISMP must implement the IsmpModule trait, these pallets should also export a unique pallet id. This pallet id is used both by the IsmpRouter to dispatch requests to it as well as other IsmpModules who want to communicate with it.
Ismp modules dispatch requests and responses through the IsmpDispatcher and receive the requests & responses through the IsmpModule implementation. The router identifies a module by it's unique id and dispatches requests and responses to it through its IsmpModule handler.
An implementation of the IsmpModule for a substrate pallet will typically look like:
rust
pub struct YourCustomModule<T: Config>(PhantomData<T>); pub const YOUR_CUSTOM_PALLET_ID: PalletId = *b"_MY_ID_"; impl<T: Config> Default for YourCustomModule<T> { fn default() -> Self { Self(PhantomData) } } impl<T: Config> IsmpModule for YourCustomModule<T> { fn on_accept(&self, request: Post) -> Result<(), ismp::Error> {} fn on_response(&self, response: Response) -> Result<(), ismp::Error> {} fn on_timeout(&self, request: Request) -> Result<(), ismp::Error> {} }
Here’s a demo IsmpModule implementation for a substrate pallet.
The Dispatcher is an implementation of the IsmpDispatcher trait. Here’s an example of it in action.
rust
// Dispatcher can be instantiated using it's default implementation let dispatcher = Dispatcher::<T>::default(); dispatcher .dispatch_request(DispatchRequest::Post(post)) .map_err(|_| Error::<T>::TransferFailed)?;
Now that you have an implementation of IsmpModule, you’ll need to add it to your router. In your runtime config file, modify your router implementation.
rust
pub struct ModuleRouter; impl IsmpRouter for ModuleRouter { fn module_for_id(bytes: Vec<u8>) -> Result<Box<dyn IsmpModule>, ismp::Error> { let module_id = ModuleId::from_bytes(&bytes)?; let handler = match module_id { ModuleId::Pallet(pallet_id) => match pallet_id { YOUR_CUSTOM_PALLET_ID => Box::new(YourCustomModule::<Runtime>::default()), _ => Err(ismp::Error::ModuleNotFound(bytes))?, }, _ => Err(ismp::Error::ModuleNotFound(bytes))?, }; Ok(handler) } }

Powered by Notaku
Share