levana_perpswap_cosmos/
ibc.rs

1//! Ibc helpers
2use cosmwasm_schema::cw_serde;
3use cosmwasm_std::{to_json_binary, Binary};
4
5/// Timeout in seconds for IBC packets
6pub const TIMEOUT_SECONDS: u64 = 60 * 10; // 10 minutes
7
8/// IBC ACK. See:
9/// https://github.com/cosmos/cosmos-sdk/blob/f999b1ff05a4db4a338a855713864497bedd4396/proto/ibc/core/channel/v1/channel.proto#L141-L147
10#[cw_serde]
11enum Ack {
12    Result(Binary),
13    Error(String),
14}
15
16/// IBC ACK success
17pub fn ack_success() -> Binary {
18    to_json_binary(&Ack::Result(b"1".into())).unwrap()
19}
20
21/// IBC ACK failure
22pub fn ack_fail(err: anyhow::Error) -> Binary {
23    to_json_binary(&Ack::Error(err.to_string())).unwrap()
24}
25
26/// Common IBC Events
27pub mod event {
28    use cosmwasm_std::{Event, IbcChannel};
29
30    /// IBC Channel Connect Event
31    #[derive(Debug)]
32    pub struct IbcChannelConnectEvent<'a> {
33        /// The IBC channel
34        pub channel: &'a IbcChannel,
35    }
36
37    impl<'a> From<IbcChannelConnectEvent<'a>> for Event {
38        fn from(src: IbcChannelConnectEvent) -> Self {
39            mixin_ibc_channel(Event::new("ibc-channel-connect"), src.channel)
40        }
41    }
42
43    /// IBC Channel Close Event
44    #[derive(Debug)]
45    pub struct IbcChannelCloseEvent<'a> {
46        /// The IBC channel
47        pub channel: &'a IbcChannel,
48    }
49
50    impl<'a> From<IbcChannelCloseEvent<'a>> for Event {
51        fn from(src: IbcChannelCloseEvent) -> Self {
52            mixin_ibc_channel(Event::new("ibc-channel-close"), src.channel)
53        }
54    }
55
56    fn mixin_ibc_channel(event: Event, channel: &IbcChannel) -> Event {
57        event
58            .add_attribute("endpoint-id", &channel.endpoint.channel_id)
59            .add_attribute("endpoint-port-id", &channel.endpoint.port_id)
60            .add_attribute(
61                "counterparty-endpoint-id",
62                &channel.counterparty_endpoint.channel_id,
63            )
64            .add_attribute(
65                "counterparty-endpoint-port-id",
66                &channel.counterparty_endpoint.port_id,
67            )
68            .add_attribute("order", format!("{:?}", channel.order))
69            .add_attribute("version", &channel.version)
70            .add_attribute("connection-id", &channel.connection_id)
71    }
72}