1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use schemars::JsonSchema;
use cosmwasm_std::{to_binary, wasm_execute, Binary, CosmosMsg, StdResult};
use crate::StdAck;
#[cosmwasm_schema::cw_serde]
pub struct IbcResponseMsg {
pub id: String,
pub msg: StdAck,
}
impl IbcResponseMsg {
pub fn into_binary(self) -> StdResult<Binary> {
let msg = IbcCallbackMsg::IbcCallback(self);
to_binary(&msg)
}
pub fn into_cosmos_msg<T: Into<String>, C>(self, contract_addr: T) -> StdResult<CosmosMsg<C>>
where
C: Clone + std::fmt::Debug + PartialEq + JsonSchema,
{
Ok(wasm_execute(
contract_addr.into(),
&IbcCallbackMsg::IbcCallback(self),
vec![],
)?
.into())
}
}
#[cosmwasm_schema::cw_serde]
enum IbcCallbackMsg {
IbcCallback(IbcResponseMsg),
}
#[cfg(test)]
mod test {
use super::*;
use cosmwasm_std::WasmMsg;
use speculoos::prelude::*;
#[test]
fn into_binary_should_wrap_in_callback() {
let msg = IbcResponseMsg {
id: "my-id".to_string(),
msg: StdAck::Result(Binary::default()),
};
let actual = msg.clone().into_binary().unwrap();
let expected = to_binary(&IbcCallbackMsg::IbcCallback(msg)).unwrap();
assert_that(&actual).is_equal_to(&expected);
}
#[test]
fn into_cosmos_msg_should_build_wasm_execute() {
let msg = IbcResponseMsg {
id: "my-id".to_string(),
msg: StdAck::Result(Binary::default()),
};
let actual = msg.clone().into_cosmos_msg("my-addr").unwrap();
let funds = vec![];
let payload = to_binary(&IbcCallbackMsg::IbcCallback(msg)).unwrap();
let expected: CosmosMsg = WasmMsg::Execute {
contract_addr: "my-addr".into(),
msg: payload,
funds,
}
.into();
assert_that(&actual).is_equal_to(&expected);
}
}