hpl_interface/core/
mailbox.rs

1use cosmwasm_schema::{cw_serde, QueryResponses};
2use cosmwasm_std::{wasm_execute, Addr, Api, Coin, CosmosMsg, HexBinary, StdResult};
3
4#[allow(unused_imports)]
5use crate::{
6    hook::QuoteDispatchResponse,
7    ownable::{OwnableMsg, OwnableQueryMsg},
8    types,
9};
10
11#[cw_serde]
12pub struct InstantiateMsg {
13    pub hrp: String,
14    pub owner: String,
15    pub domain: u32,
16}
17
18#[cw_serde]
19pub struct DispatchMsg {
20    pub dest_domain: u32,
21    pub recipient_addr: HexBinary,
22    pub msg_body: HexBinary,
23    pub hook: Option<String>,
24    pub metadata: Option<HexBinary>,
25}
26
27impl DispatchMsg {
28    pub fn new(
29        dest_domain: u32,
30        recipient_addr: impl Into<HexBinary>,
31        msg_body: impl Into<HexBinary>,
32    ) -> Self {
33        Self {
34            dest_domain,
35            recipient_addr: recipient_addr.into(),
36            msg_body: msg_body.into(),
37            hook: None,
38            metadata: None,
39        }
40    }
41
42    pub fn with_hook(mut self, hook: impl Into<String>) -> Self {
43        self.hook = Some(hook.into());
44        self
45    }
46
47    pub fn with_metadata(mut self, metadata: impl Into<HexBinary>) -> Self {
48        self.metadata = Some(metadata.into());
49        self
50    }
51
52    pub fn to_msg(
53        self,
54        version: u8,
55        nonce: u32,
56        origin_domain: u32,
57        sender: impl Into<String>,
58    ) -> StdResult<types::Message> {
59        Ok(types::Message {
60            version,
61            nonce,
62            origin_domain,
63            sender: types::bech32_to_h256(&sender.into())?.to_vec().into(),
64            dest_domain: self.dest_domain,
65            recipient: self.recipient_addr,
66            body: self.msg_body,
67        })
68    }
69
70    pub fn get_hook_addr(&self, api: &dyn Api, default: Addr) -> StdResult<Addr> {
71        Ok(self
72            .hook
73            .as_ref()
74            .map(|v| api.addr_validate(v))
75            .transpose()?
76            .unwrap_or(default))
77    }
78}
79
80#[cw_serde]
81pub enum ExecuteMsg {
82    // overrides
83    Ownable(OwnableMsg),
84
85    // Mailbox
86    SetDefaultIsm {
87        ism: String,
88    },
89
90    SetDefaultHook {
91        hook: String,
92    },
93
94    SetRequiredHook {
95        hook: String,
96    },
97
98    Dispatch(DispatchMsg),
99
100    Process {
101        metadata: HexBinary,
102        message: HexBinary,
103    },
104}
105
106pub fn dispatch(
107    mailbox: impl Into<String>,
108    dest_domain: u32,
109    recipient_addr: HexBinary,
110    msg_body: HexBinary,
111    hook: Option<String>,
112    metadata: Option<HexBinary>,
113    funds: Vec<Coin>,
114) -> StdResult<CosmosMsg> {
115    Ok(wasm_execute(
116        mailbox,
117        &ExecuteMsg::Dispatch(DispatchMsg {
118            dest_domain,
119            recipient_addr,
120            msg_body,
121            hook,
122            metadata,
123        }),
124        funds,
125    )?
126    .into())
127}
128
129pub fn process(mailbox: impl Into<String>, metadata: HexBinary, message: HexBinary) -> CosmosMsg {
130    wasm_execute(mailbox, &ExecuteMsg::Process { metadata, message }, vec![])
131        .unwrap()
132        .into()
133}
134
135#[cw_serde]
136pub struct DispatchResponse {
137    pub message_id: HexBinary,
138}
139
140#[cw_serde]
141#[derive(QueryResponses)]
142#[query_responses(nested)]
143pub enum QueryMsg {
144    // overrides
145    Ownable(OwnableQueryMsg),
146
147    Hook(MailboxHookQueryMsg),
148
149    // mailbox
150    Mailbox(MailboxQueryMsg),
151}
152
153#[cw_serde]
154#[derive(QueryResponses)]
155pub enum MailboxHookQueryMsg {
156    #[returns(QuoteDispatchResponse)]
157    QuoteDispatch { sender: String, msg: DispatchMsg },
158}
159
160#[cw_serde]
161#[derive(QueryResponses)]
162pub enum MailboxQueryMsg {
163    #[returns(HrpResponse)]
164    Hrp {},
165
166    #[returns(LocalDomainResponse)]
167    LocalDomain {},
168
169    #[returns(MessageDeliveredResponse)]
170    MessageDelivered { id: HexBinary },
171
172    #[returns(DefaultIsmResponse)]
173    DefaultIsm {},
174
175    #[returns(DefaultHookResponse)]
176    DefaultHook {},
177
178    #[returns(RequiredHookResponse)]
179    RequiredHook {},
180
181    #[returns(NonceResponse)]
182    Nonce {},
183
184    #[returns(RecipientIsmResponse)]
185    RecipientIsm { recipient_addr: String },
186
187    #[returns(LatestDispatchedIdResponse)]
188    LatestDispatchId {},
189}
190impl MailboxQueryMsg {
191    pub fn wrap(self) -> QueryMsg {
192        QueryMsg::Mailbox(self)
193    }
194}
195
196#[cw_serde]
197pub struct HrpResponse {
198    pub hrp: String,
199}
200
201#[cw_serde]
202pub struct LocalDomainResponse {
203    pub local_domain: u32,
204}
205
206#[cw_serde]
207pub struct MessageDeliveredResponse {
208    pub delivered: bool,
209}
210
211#[cw_serde]
212pub struct DefaultIsmResponse {
213    pub default_ism: String,
214}
215
216#[cw_serde]
217pub struct DefaultHookResponse {
218    pub default_hook: String,
219}
220
221#[cw_serde]
222pub struct RequiredHookResponse {
223    pub required_hook: String,
224}
225
226#[cw_serde]
227pub struct RecipientIsmResponse {
228    pub ism: String,
229}
230
231#[cw_serde]
232pub struct NonceResponse {
233    pub nonce: u32,
234}
235
236#[cw_serde]
237pub struct LatestDispatchedIdResponse {
238    pub message_id: HexBinary,
239}