abstract_sdk/base/endpoints/
modules_ibc.rs

1use crate::features::ModuleIdentification;
2use crate::{base::Handler, AbstractSdkError};
3use abstract_std::ibc::ModuleIbcMsg;
4use cosmwasm_std::{Addr, Deps, DepsMut, Env, MessageInfo, Response};
5
6/// Trait for a contract to call itself on an IBC counterpart.
7pub trait ModuleIbcEndpoint: Handler {
8    /// Get the address of the ibc host associated with this module
9    fn ibc_host(&self, deps: Deps) -> Result<Addr, Self::Error>;
10
11    /// Handler for the `ExecuteMsg::ModuleIbc(ModuleIbcMsg)` variant.
12    fn module_ibc(
13        self,
14        deps: DepsMut,
15        env: Env,
16        info: MessageInfo,
17        msg: ModuleIbcMsg,
18    ) -> Result<Response, Self::Error> {
19        // Only an IBC host can call this endpoint
20        let ibc_host = self.ibc_host(deps.as_ref())?;
21        if info.sender.ne(&ibc_host) {
22            return Err(AbstractSdkError::ModuleIbcNotCalledByHost {
23                caller: info.sender,
24                host_addr: ibc_host,
25                module: self.info().0.to_string(),
26            }
27            .into());
28        };
29
30        // If there is no handler and this endpoint is called we need to error
31        let handler =
32            self.maybe_module_ibc_handler()
33                .ok_or(AbstractSdkError::NoModuleIbcHandler(
34                    self.module_id().to_string(),
35                ))?;
36        handler(deps, env, self, msg.src_module_info, msg.msg)
37    }
38}