abstract_core/native/ibc.rs
1use cosmwasm_std::{to_json_binary, wasm_execute, Binary, CosmosMsg, StdResult};
2use polytone::callbacks::Callback;
3use schemars::JsonSchema;
4
5// CallbackInfo from modules, that is turned into an IbcResponseMsg by the ibc client
6#[cosmwasm_schema::cw_serde]
7pub struct CallbackInfo {
8 /// Used to identify the callback that is sent (acts like the reply ID)
9 pub id: String,
10 /// Used to add information to the callback.
11 /// This is usually used to provide information to the ibc callback function for context
12 pub msg: Option<Binary>,
13 /// Contract that will be called with the callback message
14 pub receiver: String,
15}
16
17/// IbcResponseMsg should be de/serialized under `IbcCallback()` variant in a ExecuteMsg
18#[cosmwasm_schema::cw_serde]
19pub struct IbcResponseMsg {
20 /// The ID chosen by the caller in the `callback_info.id`
21 pub id: String,
22 /// The msg sent with the callback request.
23 /// This is usually used to provide information to the ibc callback function for context
24 pub msg: Option<Binary>,
25 pub result: Callback,
26}
27
28impl IbcResponseMsg {
29 /// serializes the message
30 pub fn into_json_binary(self) -> StdResult<Binary> {
31 let msg = IbcCallbackMsg::IbcCallback(self);
32 to_json_binary(&msg)
33 }
34
35 /// creates a cosmos_msg sending this struct to the named contract
36 pub fn into_cosmos_msg<T: Into<String>, C>(self, contract_addr: T) -> StdResult<CosmosMsg<C>>
37 where
38 C: Clone + std::fmt::Debug + PartialEq + JsonSchema,
39 {
40 Ok(wasm_execute(
41 contract_addr.into(),
42 &IbcCallbackMsg::IbcCallback(self),
43 vec![],
44 )?
45 .into())
46 }
47}
48
49/// This is just a helper to properly serialize the above message.
50/// The actual receiver should include this variant in the larger ExecuteMsg enum
51#[cosmwasm_schema::cw_serde]
52enum IbcCallbackMsg {
53 IbcCallback(IbcResponseMsg),
54}