Skip to content

Commit

Permalink
Merge pull request #857 from CosmWasm/854-submsg-reply-control
Browse files Browse the repository at this point in the history
854 submsg reply control
  • Loading branch information
webmaster128 authored Mar 31, 2021
2 parents 20842b4 + db5d208 commit 21adf0d
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 7 deletions.
15 changes: 14 additions & 1 deletion contracts/reflect/schema/execute_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,15 @@
}
}
},
"ReplyOn": {
"description": "Use this to define when the contract gets a response callback. If you only need it for errors or success you can select just those in order to save gas.",
"type": "string",
"enum": [
"always",
"error",
"success"
]
},
"StakingMsg": {
"description": "The message types of the staking module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/tx.proto",
"anyOf": [
Expand Down Expand Up @@ -517,7 +526,8 @@
"type": "object",
"required": [
"id",
"msg"
"msg",
"reply_on"
],
"properties": {
"gas_limit": {
Expand All @@ -535,6 +545,9 @@
},
"msg": {
"$ref": "#/definitions/CosmosMsg_for_CustomMsg"
},
"reply_on": {
"$ref": "#/definitions/ReplyOn"
}
}
},
Expand Down
15 changes: 14 additions & 1 deletion contracts/reflect/schema/response_for__custom_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,15 @@
}
}
},
"ReplyOn": {
"description": "Use this to define when the contract gets a response callback. If you only need it for errors or success you can select just those in order to save gas.",
"type": "string",
"enum": [
"always",
"error",
"success"
]
},
"StakingMsg": {
"description": "The message types of the staking module.\n\nSee https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/tx.proto",
"anyOf": [
Expand Down Expand Up @@ -505,7 +514,8 @@
"type": "object",
"required": [
"id",
"msg"
"msg",
"reply_on"
],
"properties": {
"gas_limit": {
Expand All @@ -523,6 +533,9 @@
},
"msg": {
"$ref": "#/definitions/CosmosMsg_for_CustomMsg"
},
"reply_on": {
"$ref": "#/definitions/ReplyOn"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion contracts/reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ mod tests {
use cosmwasm_std::testing::{mock_env, mock_info, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{
coin, coins, from_binary, AllBalanceResponse, Api, BankMsg, BankQuery, Binary,
ContractResult, Event, StakingMsg, StdError, SubcallResponse,
ContractResult, Event, ReplyOn, StakingMsg, StdError, SubcallResponse,
};

#[test]
Expand Down Expand Up @@ -466,6 +466,7 @@ mod tests {
amount: coins(1, "token"),
}
.into(),
reply_on: ReplyOn::default(),
};

let msg = ExecuteMsg::ReflectSubCall {
Expand Down
1 change: 1 addition & 0 deletions contracts/reflect/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ fn reflect_subcall() {
amount: coins(1, "token"),
}
.into(),
reply_on: Default::default(),
};

let msg = ExecuteMsg::ReflectSubCall {
Expand Down
4 changes: 2 additions & 2 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub use crate::query::{
};
pub use crate::results::{
attr, wasm_execute, wasm_instantiate, Attribute, BankMsg, ContractResult, CosmosMsg, Empty,
Event, QueryResponse, Reply, Response, StakingMsg, SubMsg, SubcallResponse, SystemResult,
WasmMsg,
Event, QueryResponse, Reply, ReplyOn, Response, StakingMsg, SubMsg, SubcallResponse,
SystemResult, WasmMsg,
};
#[allow(deprecated)]
pub use crate::results::{Context, HandleResponse, InitResponse, MigrateResponse};
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/results/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use cosmos_msg::{wasm_execute, wasm_instantiate, BankMsg, CosmosMsg, Staking
pub use empty::Empty;
pub use query::QueryResponse;
pub use response::Response;
pub use subcall::{Event, Reply, SubMsg, SubcallResponse};
pub use subcall::{Event, Reply, ReplyOn, SubMsg, SubcallResponse};
pub use system_result::SystemResult;

#[deprecated(since = "0.14.0", note = "Renamed to Response.")]
Expand Down
5 changes: 4 additions & 1 deletion packages/std/src/results/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;

use crate::Binary;
use crate::{Binary, ReplyOn};

use super::{Attribute, CosmosMsg, Empty};
use crate::results::SubMsg;
Expand Down Expand Up @@ -125,11 +125,13 @@ where
id: u64,
msg: U,
gas_limit: Option<u64>,
reply_on: ReplyOn,
) {
let sub = SubMsg {
id,
msg: msg.into(),
gas_limit,
reply_on,
};
self.submessages.push(sub);
}
Expand Down Expand Up @@ -157,6 +159,7 @@ mod tests {
}
.into(),
gas_limit: Some(12345u64),
reply_on: ReplyOn::Always,
}],
messages: vec![BankMsg::Send {
to_address: HumanAddr::from("you"),
Expand Down
21 changes: 21 additions & 0 deletions packages/std/src/results/subcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ use crate::{Binary, ContractResult};

use super::{Attribute, CosmosMsg, Empty};

/// Use this to define when the contract gets a response callback.
/// If you only need it for errors or success you can select just those in order
/// to save gas.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ReplyOn {
/// Always perform a callback after SubMsg is processed
Always,
/// Only callback if SubMsg returned an error, no callback on success case
Error,
/// Only callback if SubMsg was successful, no callback on error case
Success,
}

impl Default for ReplyOn {
fn default() -> Self {
ReplyOn::Always
}
}

/// A sub-message that will guarantee a subcall_response callback on success or error
/// Note on error the subcall will revert any partial state changes due to this message,
/// but not revert any state changes in the calling contract (that must be done in the
Expand All @@ -18,6 +38,7 @@ where
pub id: u64,
pub msg: CosmosMsg<T>,
pub gas_limit: Option<u64>,
pub reply_on: ReplyOn,
}

/// The Result object returned to subcall_response. We always get the same id back
Expand Down

0 comments on commit 21adf0d

Please sign in to comment.