use core::cmp::{Ord, Ordering, PartialOrd};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::coin::Coin;
use crate::prelude::*;
use crate::results::{Attribute, CosmosMsg, Empty, Event, SubMsg};
use crate::StdResult;
use crate::{to_json_binary, Binary};
use crate::{Addr, Timestamp};
mod callbacks;
mod transfer_msg_builder;
pub use callbacks::*;
pub use transfer_msg_builder::*;
#[non_exhaustive]
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[serde(rename_all = "snake_case")]
pub enum IbcMsg {
Transfer {
channel_id: String,
to_address: String,
amount: Coin,
timeout: IbcTimeout,
memo: Option<String>,
},
SendPacket {
channel_id: String,
data: Binary,
timeout: IbcTimeout,
},
#[cfg(feature = "cosmwasm_2_1")]
WriteAcknowledgement {
channel_id: String,
packet_sequence: u64,
ack: IbcAcknowledgement,
},
CloseChannel { channel_id: String },
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
pub struct IbcEndpoint {
pub port_id: String,
pub channel_id: String,
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[serde(rename_all = "snake_case")]
pub struct IbcTimeout {
block: Option<IbcTimeoutBlock>,
timestamp: Option<Timestamp>,
}
impl IbcTimeout {
pub fn with_block(block: IbcTimeoutBlock) -> Self {
IbcTimeout {
block: Some(block),
timestamp: None,
}
}
pub fn with_timestamp(timestamp: Timestamp) -> Self {
IbcTimeout {
block: None,
timestamp: Some(timestamp),
}
}
pub fn with_both(block: IbcTimeoutBlock, timestamp: Timestamp) -> Self {
IbcTimeout {
block: Some(block),
timestamp: Some(timestamp),
}
}
pub fn block(&self) -> Option<IbcTimeoutBlock> {
self.block
}
pub fn timestamp(&self) -> Option<Timestamp> {
self.timestamp
}
}
impl From<Timestamp> for IbcTimeout {
fn from(timestamp: Timestamp) -> IbcTimeout {
IbcTimeout::with_timestamp(timestamp)
}
}
impl From<IbcTimeoutBlock> for IbcTimeout {
fn from(original: IbcTimeoutBlock) -> IbcTimeout {
IbcTimeout::with_block(original)
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[non_exhaustive]
pub struct IbcChannel {
pub endpoint: IbcEndpoint,
pub counterparty_endpoint: IbcEndpoint,
pub order: IbcOrder,
pub version: String,
pub connection_id: String,
}
impl IbcChannel {
pub fn new(
endpoint: IbcEndpoint,
counterparty_endpoint: IbcEndpoint,
order: IbcOrder,
version: impl Into<String>,
connection_id: impl Into<String>,
) -> Self {
Self {
endpoint,
counterparty_endpoint,
order,
version: version.into(),
connection_id: connection_id.into(),
}
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
pub enum IbcOrder {
#[serde(rename = "ORDER_UNORDERED")]
Unordered,
#[serde(rename = "ORDER_ORDERED")]
Ordered,
}
#[derive(
Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
pub struct IbcTimeoutBlock {
pub revision: u64,
pub height: u64,
}
impl IbcTimeoutBlock {
pub fn is_zero(&self) -> bool {
self.revision == 0 && self.height == 0
}
}
impl PartialOrd for IbcTimeoutBlock {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for IbcTimeoutBlock {
fn cmp(&self, other: &Self) -> Ordering {
match self.revision.cmp(&other.revision) {
Ordering::Equal => self.height.cmp(&other.height),
other => other,
}
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[non_exhaustive]
pub struct IbcPacket {
pub data: Binary,
pub src: IbcEndpoint,
pub dest: IbcEndpoint,
pub sequence: u64,
pub timeout: IbcTimeout,
}
impl IbcPacket {
pub fn new(
data: impl Into<Binary>,
src: IbcEndpoint,
dest: IbcEndpoint,
sequence: u64,
timeout: IbcTimeout,
) -> Self {
Self {
data: data.into(),
src,
dest,
sequence,
timeout,
}
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[non_exhaustive]
pub struct IbcAcknowledgement {
pub data: Binary,
}
impl IbcAcknowledgement {
pub fn new(data: impl Into<Binary>) -> Self {
IbcAcknowledgement { data: data.into() }
}
pub fn encode_json(data: &impl Serialize) -> StdResult<Self> {
Ok(IbcAcknowledgement {
data: to_json_binary(data)?,
})
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[serde(rename_all = "snake_case")]
pub enum IbcChannelOpenMsg {
OpenInit { channel: IbcChannel },
OpenTry {
channel: IbcChannel,
counterparty_version: String,
},
}
impl IbcChannelOpenMsg {
pub fn new_init(channel: IbcChannel) -> Self {
Self::OpenInit { channel }
}
pub fn new_try(channel: IbcChannel, counterparty_version: impl Into<String>) -> Self {
Self::OpenTry {
channel,
counterparty_version: counterparty_version.into(),
}
}
pub fn channel(&self) -> &IbcChannel {
match self {
Self::OpenInit { channel } => channel,
Self::OpenTry { channel, .. } => channel,
}
}
pub fn counterparty_version(&self) -> Option<&str> {
match self {
Self::OpenTry {
counterparty_version,
..
} => Some(counterparty_version),
_ => None,
}
}
}
impl From<IbcChannelOpenMsg> for IbcChannel {
fn from(msg: IbcChannelOpenMsg) -> IbcChannel {
match msg {
IbcChannelOpenMsg::OpenInit { channel } => channel,
IbcChannelOpenMsg::OpenTry { channel, .. } => channel,
}
}
}
pub type IbcChannelOpenResponse = Option<Ibc3ChannelOpenResponse>;
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
pub struct Ibc3ChannelOpenResponse {
pub version: String,
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[serde(rename_all = "snake_case")]
pub enum IbcChannelConnectMsg {
OpenAck {
channel: IbcChannel,
counterparty_version: String,
},
OpenConfirm { channel: IbcChannel },
}
impl IbcChannelConnectMsg {
pub fn new_ack(channel: IbcChannel, counterparty_version: impl Into<String>) -> Self {
Self::OpenAck {
channel,
counterparty_version: counterparty_version.into(),
}
}
pub fn new_confirm(channel: IbcChannel) -> Self {
Self::OpenConfirm { channel }
}
pub fn channel(&self) -> &IbcChannel {
match self {
Self::OpenAck { channel, .. } => channel,
Self::OpenConfirm { channel } => channel,
}
}
pub fn counterparty_version(&self) -> Option<&str> {
match self {
Self::OpenAck {
counterparty_version,
..
} => Some(counterparty_version),
_ => None,
}
}
}
impl From<IbcChannelConnectMsg> for IbcChannel {
fn from(msg: IbcChannelConnectMsg) -> IbcChannel {
match msg {
IbcChannelConnectMsg::OpenAck { channel, .. } => channel,
IbcChannelConnectMsg::OpenConfirm { channel } => channel,
}
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[serde(rename_all = "snake_case")]
pub enum IbcChannelCloseMsg {
CloseInit { channel: IbcChannel },
CloseConfirm { channel: IbcChannel }, }
impl IbcChannelCloseMsg {
pub fn new_init(channel: IbcChannel) -> Self {
Self::CloseInit { channel }
}
pub fn new_confirm(channel: IbcChannel) -> Self {
Self::CloseConfirm { channel }
}
pub fn channel(&self) -> &IbcChannel {
match self {
Self::CloseInit { channel } => channel,
Self::CloseConfirm { channel } => channel,
}
}
}
impl From<IbcChannelCloseMsg> for IbcChannel {
fn from(msg: IbcChannelCloseMsg) -> IbcChannel {
match msg {
IbcChannelCloseMsg::CloseInit { channel } => channel,
IbcChannelCloseMsg::CloseConfirm { channel } => channel,
}
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[non_exhaustive]
pub struct IbcPacketReceiveMsg {
pub packet: IbcPacket,
pub relayer: Addr,
}
impl IbcPacketReceiveMsg {
pub fn new(packet: IbcPacket, relayer: Addr) -> Self {
Self { packet, relayer }
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[non_exhaustive]
pub struct IbcPacketAckMsg {
pub acknowledgement: IbcAcknowledgement,
pub original_packet: IbcPacket,
pub relayer: Addr,
}
impl IbcPacketAckMsg {
pub fn new(
acknowledgement: IbcAcknowledgement,
original_packet: IbcPacket,
relayer: Addr,
) -> Self {
Self {
acknowledgement,
original_packet,
relayer,
}
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[non_exhaustive]
pub struct IbcPacketTimeoutMsg {
pub packet: IbcPacket,
pub relayer: Addr,
}
impl IbcPacketTimeoutMsg {
pub fn new(packet: IbcPacket, relayer: Addr) -> Self {
Self { packet, relayer }
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[non_exhaustive]
pub struct IbcBasicResponse<T = Empty> {
pub messages: Vec<SubMsg<T>>,
pub attributes: Vec<Attribute>,
pub events: Vec<Event>,
}
impl<T> Default for IbcBasicResponse<T> {
fn default() -> Self {
IbcBasicResponse {
messages: vec![],
attributes: vec![],
events: vec![],
}
}
}
impl<T> IbcBasicResponse<T> {
pub fn new() -> Self {
Self::default()
}
pub fn add_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.attributes.push(Attribute::new(key, value));
self
}
pub fn add_message(mut self, msg: impl Into<CosmosMsg<T>>) -> Self {
self.messages.push(SubMsg::new(msg));
self
}
pub fn add_submessage(mut self, msg: SubMsg<T>) -> Self {
self.messages.push(msg);
self
}
pub fn add_event(mut self, event: Event) -> Self {
self.events.push(event);
self
}
pub fn add_attributes<A: Into<Attribute>>(
mut self,
attrs: impl IntoIterator<Item = A>,
) -> Self {
self.attributes.extend(attrs.into_iter().map(A::into));
self
}
pub fn add_messages<M: Into<CosmosMsg<T>>>(self, msgs: impl IntoIterator<Item = M>) -> Self {
self.add_submessages(msgs.into_iter().map(SubMsg::new))
}
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg<T>>) -> Self {
self.messages.extend(msgs);
self
}
pub fn add_events(mut self, events: impl IntoIterator<Item = Event>) -> Self {
self.events.extend(events);
self
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
)]
#[non_exhaustive]
pub struct IbcReceiveResponse<T = Empty> {
pub acknowledgement: Option<Binary>,
pub messages: Vec<SubMsg<T>>,
pub attributes: Vec<Attribute>,
pub events: Vec<Event>,
}
impl<T> IbcReceiveResponse<T> {
pub fn new(ack: impl Into<Binary>) -> Self {
Self {
acknowledgement: Some(ack.into()),
messages: vec![],
attributes: vec![],
events: vec![],
}
}
pub fn without_ack() -> Self {
Self {
acknowledgement: None,
messages: vec![],
attributes: vec![],
events: vec![],
}
}
pub fn add_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.attributes.push(Attribute::new(key, value));
self
}
pub fn add_message(mut self, msg: impl Into<CosmosMsg<T>>) -> Self {
self.messages.push(SubMsg::new(msg));
self
}
pub fn add_submessage(mut self, msg: SubMsg<T>) -> Self {
self.messages.push(msg);
self
}
pub fn add_event(mut self, event: Event) -> Self {
self.events.push(event);
self
}
pub fn add_attributes<A: Into<Attribute>>(
mut self,
attrs: impl IntoIterator<Item = A>,
) -> Self {
self.attributes.extend(attrs.into_iter().map(A::into));
self
}
pub fn add_messages<M: Into<CosmosMsg<T>>>(self, msgs: impl IntoIterator<Item = M>) -> Self {
self.add_submessages(msgs.into_iter().map(SubMsg::new))
}
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg<T>>) -> Self {
self.messages.extend(msgs);
self
}
pub fn add_events(mut self, events: impl IntoIterator<Item = Event>) -> Self {
self.events.extend(events);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::to_json_string;
#[test]
fn serialize_msg() {
let msg = IbcMsg::Transfer {
channel_id: "channel-123".to_string(),
to_address: "my-special-addr".into(),
amount: Coin::new(12345678u128, "uatom"),
timeout: IbcTimeout::with_timestamp(Timestamp::from_nanos(1234567890)),
memo: None,
};
let encoded = to_json_string(&msg).unwrap();
let expected = r#"{"transfer":{"channel_id":"channel-123","to_address":"my-special-addr","amount":{"denom":"uatom","amount":"12345678"},"timeout":{"block":null,"timestamp":"1234567890"},"memo":null}}"#;
assert_eq!(encoded.as_str(), expected);
}
#[test]
fn ibc_timeout_serialize() {
let timestamp = IbcTimeout::with_timestamp(Timestamp::from_nanos(684816844));
let expected = r#"{"block":null,"timestamp":"684816844"}"#;
assert_eq!(to_json_string(×tamp).unwrap(), expected);
let block = IbcTimeout::with_block(IbcTimeoutBlock {
revision: 12,
height: 129,
});
let expected = r#"{"block":{"revision":12,"height":129},"timestamp":null}"#;
assert_eq!(to_json_string(&block).unwrap(), expected);
let both = IbcTimeout::with_both(
IbcTimeoutBlock {
revision: 12,
height: 129,
},
Timestamp::from_nanos(684816844),
);
let expected = r#"{"block":{"revision":12,"height":129},"timestamp":"684816844"}"#;
assert_eq!(to_json_string(&both).unwrap(), expected);
}
#[test]
#[allow(clippy::eq_op)]
fn ibc_timeout_block_ord() {
let epoch1a = IbcTimeoutBlock {
revision: 1,
height: 1000,
};
let epoch1b = IbcTimeoutBlock {
revision: 1,
height: 3000,
};
let epoch2a = IbcTimeoutBlock {
revision: 2,
height: 500,
};
let epoch2b = IbcTimeoutBlock {
revision: 2,
height: 2500,
};
assert_eq!(epoch1a, epoch1a);
assert!(epoch1a < epoch1b);
assert!(epoch1b > epoch1a);
assert!(epoch2a > epoch1a);
assert!(epoch2b > epoch1a);
assert!(epoch1b > epoch1a);
assert!(epoch2a > epoch1b);
assert!(epoch2b > epoch2a);
assert!(epoch2b > epoch1b);
assert!(epoch1a < epoch1b);
assert!(epoch1b < epoch2a);
assert!(epoch2a < epoch2b);
assert!(epoch1b < epoch2b);
}
#[test]
fn ibc_packet_serialize() {
let packet = IbcPacket {
data: b"foo".into(),
src: IbcEndpoint {
port_id: "their-port".to_string(),
channel_id: "channel-1234".to_string(),
},
dest: IbcEndpoint {
port_id: "our-port".to_string(),
channel_id: "chan33".into(),
},
sequence: 27,
timeout: IbcTimeout::with_both(
IbcTimeoutBlock {
revision: 1,
height: 12345678,
},
Timestamp::from_nanos(4611686018427387904),
),
};
let expected = r#"{"data":"Zm9v","src":{"port_id":"their-port","channel_id":"channel-1234"},"dest":{"port_id":"our-port","channel_id":"chan33"},"sequence":27,"timeout":{"block":{"revision":1,"height":12345678},"timestamp":"4611686018427387904"}}"#;
assert_eq!(to_json_string(&packet).unwrap(), expected);
let no_timestamp = IbcPacket {
data: b"foo".into(),
src: IbcEndpoint {
port_id: "their-port".to_string(),
channel_id: "channel-1234".to_string(),
},
dest: IbcEndpoint {
port_id: "our-port".to_string(),
channel_id: "chan33".into(),
},
sequence: 27,
timeout: IbcTimeout::with_block(IbcTimeoutBlock {
revision: 1,
height: 12345678,
}),
};
let expected = r#"{"data":"Zm9v","src":{"port_id":"their-port","channel_id":"channel-1234"},"dest":{"port_id":"our-port","channel_id":"chan33"},"sequence":27,"timeout":{"block":{"revision":1,"height":12345678},"timestamp":null}}"#;
assert_eq!(to_json_string(&no_timestamp).unwrap(), expected);
}
}