Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add EurekaMsg #2340

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to

- cosmwasm-schema: The schema export now doesn't overwrite existing
`additionalProperties` values anymore ([#2310])
- cosmwasm-std: Added new `EurekaMsg` and `CosmosMsg::Eureka` variant ([#2340])
jawoznia marked this conversation as resolved.
Show resolved Hide resolved

[#2310]: https://github.com/CosmWasm/cosmwasm/pull/2310
jawoznia marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
53 changes: 53 additions & 0 deletions packages/std/src/eureka.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{Binary, Timestamp};

/// Payload value should be encoded in a format defined by the channel version,
/// and the module on the other side should know how to parse this.
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct EurekaPayload {
/// The port id on the chain where the packet is sent to (external chain).
pub destination_port: String,
/// Version of the receiving contract.
pub version: String,
/// Encoding used to serialize the [EurekaPayload::value].
pub encoding: String,
/// Encoded payload data.
pub value: Binary,
}

/// These are messages in the IBC lifecycle using the new Eureka approach. Only usable by IBC-enabled contracts
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum EurekaMsg {
/// Sends an IBC packet with given payloads over the existing channel.
SendPacket {
/// existing channel to send the tokens over
channel_id: String,
timeout: Timestamp,
payloads: Vec<EurekaPayload>,
},
}

#[cfg(test)]
mod tests {
use serde_json::to_string;

use crate::EurekaPayload;

#[test]
fn eureka_payload_serialize() {
let packet = EurekaPayload {
destination_port: "receiving-contract-port".to_string(),
version: "v1".to_string(),
encoding: "json".to_string(),
value: b"foo".into(),
};
let expected = r#"{"destination_port":"receiving-contract-port","version":"v1","encoding":"json","value":"Zm9v"}"#;
assert_eq!(to_string(&packet).unwrap(), expected);
}
}
2 changes: 2 additions & 0 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod conversion;
mod deps;
mod encoding;
mod errors;
mod eureka;
mod forward_ref;
mod hex_binary;
mod ibc;
Expand Down Expand Up @@ -65,6 +66,7 @@ pub use crate::errors::{
RecoverPubkeyError, RoundDownOverflowError, RoundUpOverflowError, StdError, StdResult,
SystemError, VerificationError,
};
pub use crate::eureka::{EurekaMsg, EurekaPayload};
pub use crate::hex_binary::HexBinary;
pub use crate::ibc::IbcChannelOpenResponse;
pub use crate::ibc::{
Expand Down
9 changes: 9 additions & 0 deletions packages/std/src/results/cosmos_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::coin::Coin;
use crate::eureka::EurekaMsg;
#[cfg(feature = "stargate")]
use crate::ibc::IbcMsg;
use crate::prelude::*;
Expand Down Expand Up @@ -84,6 +85,7 @@ pub enum CosmosMsg<T = Empty> {
Wasm(WasmMsg),
#[cfg(feature = "stargate")]
Gov(GovMsg),
Eureka(EurekaMsg),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add a capability for this and feature-gate this line, since Eureka will probably not be available yet on all chains when we release the implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

}

impl<T> CosmosMsg<T> {
Expand All @@ -108,6 +110,7 @@ impl<T> CosmosMsg<T> {
CosmosMsg::Wasm(msg) => CosmosMsg::Wasm(msg),
#[cfg(feature = "stargate")]
CosmosMsg::Gov(msg) => CosmosMsg::Gov(msg),
CosmosMsg::Eureka(msg) => CosmosMsg::Eureka(msg),
})
}
}
Expand Down Expand Up @@ -485,6 +488,12 @@ impl<T> From<GovMsg> for CosmosMsg<T> {
}
}

impl<T> From<EurekaMsg> for CosmosMsg<T> {
fn from(msg: EurekaMsg) -> Self {
CosmosMsg::Eureka(msg)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading