use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
use crate::{Binary, ContractResult};
use super::{CosmosMsg, Empty, Event};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ReplyOn {
Always,
Error,
Success,
Never,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct SubMsg<T = Empty>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
pub id: u64,
pub msg: CosmosMsg<T>,
pub gas_limit: Option<u64>,
pub reply_on: ReplyOn,
}
pub const UNUSED_MSG_ID: u64 = 0;
impl<T> SubMsg<T>
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
pub fn new(msg: impl Into<CosmosMsg<T>>) -> Self {
SubMsg {
id: UNUSED_MSG_ID,
msg: msg.into(),
reply_on: ReplyOn::Never,
gas_limit: None,
}
}
pub fn reply_on_success(msg: impl Into<CosmosMsg<T>>, id: u64) -> Self {
Self::reply_on(msg.into(), id, ReplyOn::Success)
}
pub fn reply_on_error(msg: impl Into<CosmosMsg<T>>, id: u64) -> Self {
Self::reply_on(msg.into(), id, ReplyOn::Error)
}
pub fn reply_always(msg: impl Into<CosmosMsg<T>>, id: u64) -> Self {
Self::reply_on(msg.into(), id, ReplyOn::Always)
}
pub fn with_gas_limit(mut self, limit: u64) -> Self {
self.gas_limit = Some(limit);
self
}
fn reply_on(msg: CosmosMsg<T>, id: u64, reply_on: ReplyOn) -> Self {
SubMsg {
id,
msg,
reply_on,
gas_limit: None,
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Reply {
pub id: u64,
pub result: ContractResult<SubMsgExecutionResponse>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct SubMsgExecutionResponse {
pub events: Vec<Event>,
pub data: Option<Binary>,
}