use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{Addr, Binary, IbcAcknowledgement, Timestamp};
#[non_exhaustive]
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, cw_schema::Schemaifier, JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub struct Ibc2Payload {
pub source_port: String,
pub destination_port: String,
pub version: String,
pub encoding: String,
pub value: Binary,
}
impl Ibc2Payload {
pub fn new(
source_port: String,
destination_port: String,
version: String,
encoding: String,
value: Binary,
) -> Self {
Self {
source_port,
destination_port,
version,
encoding,
value,
}
}
}
#[non_exhaustive]
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, cw_schema::Schemaifier, JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum Ibc2Msg {
SendPacket {
source_client: String,
timeout: Timestamp,
payloads: Vec<Ibc2Payload>,
},
WriteAcknowledgement {
source_client: String,
packet_sequence: u64,
ack: IbcAcknowledgement,
},
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, cw_schema::Schemaifier, JsonSchema,
)]
#[non_exhaustive]
pub struct Ibc2PacketReceiveMsg {
pub payload: Ibc2Payload,
pub relayer: Addr,
pub source_client: String,
pub packet_sequence: u64,
}
impl Ibc2PacketReceiveMsg {
pub fn new(
payload: Ibc2Payload,
relayer: Addr,
source_client: String,
packet_sequence: u64,
) -> Self {
Self {
payload,
relayer,
source_client,
packet_sequence,
}
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, cw_schema::Schemaifier, JsonSchema,
)]
#[non_exhaustive]
pub struct Ibc2PacketTimeoutMsg {
pub payload: Ibc2Payload,
pub source_client: String,
pub destination_client: String,
pub packet_sequence: u64,
pub relayer: Addr,
}
impl Ibc2PacketTimeoutMsg {
pub fn new(
payload: Ibc2Payload,
source_client: String,
destination_client: String,
packet_sequence: u64,
relayer: Addr,
) -> Self {
Self {
payload,
source_client,
destination_client,
packet_sequence,
relayer,
}
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, cw_schema::Schemaifier, JsonSchema,
)]
#[non_exhaustive]
pub struct Ibc2PacketAckMsg {
pub source_client: String,
pub destination_client: String,
pub data: Ibc2Payload,
pub acknowledgement: Binary,
pub relayer: Addr,
}
impl Ibc2PacketAckMsg {
pub fn new(
source_client: String,
destination_client: String,
data: Ibc2Payload,
acknowledgement: Binary,
relayer: Addr,
) -> Self {
Self {
source_client,
destination_client,
data,
acknowledgement,
relayer,
}
}
}
#[derive(
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, cw_schema::Schemaifier, JsonSchema,
)]
#[non_exhaustive]
pub struct Ibc2PacketSendMsg {
pub payload: Ibc2Payload,
pub source_client: String,
pub destination_client: String,
pub packet_sequence: u64,
pub signer: Addr,
}
impl Ibc2PacketSendMsg {
pub fn new(
payload: Ibc2Payload,
source_client: String,
destination_client: String,
packet_sequence: u64,
signer: Addr,
) -> Self {
Self {
payload,
source_client,
destination_client,
packet_sequence,
signer,
}
}
}
#[cfg(test)]
mod tests {
use serde_json::to_string;
use crate::Ibc2Payload;
#[test]
fn ibc2_payload_serialize() {
let packet = Ibc2Payload {
source_port: "sending-contractr-port".to_string(),
destination_port: "receiving-contract-port".to_string(),
version: "v1".to_string(),
encoding: "json".to_string(),
value: b"foo".into(),
};
let expected = r#"{"source_port":"sending-contractr-port","destination_port":"receiving-contract-port","version":"v1","encoding":"json","value":"Zm9v"}"#;
assert_eq!(to_string(&packet).unwrap(), expected);
}
}