From b417530f608380c18fced25146f4f1950163ccbb Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 31 Mar 2021 15:11:31 +0200 Subject: [PATCH 1/5] Add reply_on field to SubMsg --- packages/std/src/lib.rs | 4 ++-- packages/std/src/results/mod.rs | 2 +- packages/std/src/results/response.rs | 5 ++++- packages/std/src/results/subcall.rs | 14 ++++++++++++++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/std/src/lib.rs b/packages/std/src/lib.rs index 9118e13ffa..0bce6cf7c7 100644 --- a/packages/std/src/lib.rs +++ b/packages/std/src/lib.rs @@ -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}; diff --git a/packages/std/src/results/mod.rs b/packages/std/src/results/mod.rs index 5b1fcb7382..3075b0ca99 100644 --- a/packages/std/src/results/mod.rs +++ b/packages/std/src/results/mod.rs @@ -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.")] diff --git a/packages/std/src/results/response.rs b/packages/std/src/results/response.rs index 39d5363739..f50d5a1334 100644 --- a/packages/std/src/results/response.rs +++ b/packages/std/src/results/response.rs @@ -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; @@ -125,11 +125,13 @@ where id: u64, msg: U, gas_limit: Option, + reply_on: ReplyOn, ) { let sub = SubMsg { id, msg: msg.into(), gas_limit, + reply_on, }; self.submessages.push(sub); } @@ -157,6 +159,7 @@ mod tests { } .into(), gas_limit: Some(12345u64), + reply_on: ReplyOn::Always, }], messages: vec![BankMsg::Send { to_address: HumanAddr::from("you"), diff --git a/packages/std/src/results/subcall.rs b/packages/std/src/results/subcall.rs index ba5641ee60..b6ee8e0666 100644 --- a/packages/std/src/results/subcall.rs +++ b/packages/std/src/results/subcall.rs @@ -6,6 +6,19 @@ 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)] +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, +} + /// 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 @@ -18,6 +31,7 @@ where pub id: u64, pub msg: CosmosMsg, pub gas_limit: Option, + pub reply_on: ReplyOn, } /// The Result object returned to subcall_response. We always get the same id back From 69e9b8523928da5884fb8d4700a99174e321d77f Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 31 Mar 2021 15:15:05 +0200 Subject: [PATCH 2/5] Update reflect contract --- contracts/reflect/schema/execute_msg.json | 15 ++++++++++++++- .../reflect/schema/response_for__custom_msg.json | 15 ++++++++++++++- contracts/reflect/src/contract.rs | 3 ++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/contracts/reflect/schema/execute_msg.json b/contracts/reflect/schema/execute_msg.json index f8ac0a7c9b..3a49f5eb5a 100644 --- a/contracts/reflect/schema/execute_msg.json +++ b/contracts/reflect/schema/execute_msg.json @@ -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": [ @@ -517,7 +526,8 @@ "type": "object", "required": [ "id", - "msg" + "msg", + "reply_on" ], "properties": { "gas_limit": { @@ -535,6 +545,9 @@ }, "msg": { "$ref": "#/definitions/CosmosMsg_for_CustomMsg" + }, + "reply_on": { + "$ref": "#/definitions/ReplyOn" } } }, diff --git a/contracts/reflect/schema/response_for__custom_msg.json b/contracts/reflect/schema/response_for__custom_msg.json index b0160784b7..8c6b732f74 100644 --- a/contracts/reflect/schema/response_for__custom_msg.json +++ b/contracts/reflect/schema/response_for__custom_msg.json @@ -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": [ @@ -505,7 +514,8 @@ "type": "object", "required": [ "id", - "msg" + "msg", + "reply_on" ], "properties": { "gas_limit": { @@ -523,6 +533,9 @@ }, "msg": { "$ref": "#/definitions/CosmosMsg_for_CustomMsg" + }, + "reply_on": { + "$ref": "#/definitions/ReplyOn" } } }, diff --git a/contracts/reflect/src/contract.rs b/contracts/reflect/src/contract.rs index bac48b5ff1..a37ebfce8c 100644 --- a/contracts/reflect/src/contract.rs +++ b/contracts/reflect/src/contract.rs @@ -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] @@ -466,6 +466,7 @@ mod tests { amount: coins(1, "token"), } .into(), + reply_on: ReplyOn::Always, }; let msg = ExecuteMsg::ReflectSubCall { From afd2c7089a3dd66912719c19c081fe85a1a0abfc Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 31 Mar 2021 15:22:58 +0200 Subject: [PATCH 3/5] Add default and update integration test --- contracts/reflect/src/contract.rs | 2 +- contracts/reflect/tests/integration.rs | 1 + packages/std/src/results/subcall.rs | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/contracts/reflect/src/contract.rs b/contracts/reflect/src/contract.rs index a37ebfce8c..bff304510f 100644 --- a/contracts/reflect/src/contract.rs +++ b/contracts/reflect/src/contract.rs @@ -466,7 +466,7 @@ mod tests { amount: coins(1, "token"), } .into(), - reply_on: ReplyOn::Always, + reply_on: ReplyOn::default(), }; let msg = ExecuteMsg::ReflectSubCall { diff --git a/contracts/reflect/tests/integration.rs b/contracts/reflect/tests/integration.rs index 97070882e5..ee744cadd5 100644 --- a/contracts/reflect/tests/integration.rs +++ b/contracts/reflect/tests/integration.rs @@ -204,6 +204,7 @@ fn reflect_subcall() { amount: coins(1, "token"), } .into(), + reply_on: Default::default(), }; let msg = ExecuteMsg::ReflectSubCall { diff --git a/packages/std/src/results/subcall.rs b/packages/std/src/results/subcall.rs index b6ee8e0666..5064159ed4 100644 --- a/packages/std/src/results/subcall.rs +++ b/packages/std/src/results/subcall.rs @@ -19,6 +19,12 @@ pub enum ReplyOn { 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 From 41a2578683c25fadf6df73c4bba1a29be7158b79 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 31 Mar 2021 16:07:45 +0200 Subject: [PATCH 4/5] Update packages/std/src/results/subcall.rs Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> --- packages/std/src/results/subcall.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/std/src/results/subcall.rs b/packages/std/src/results/subcall.rs index 5064159ed4..8447dc0e40 100644 --- a/packages/std/src/results/subcall.rs +++ b/packages/std/src/results/subcall.rs @@ -10,6 +10,7 @@ use super::{Attribute, CosmosMsg, Empty}; /// 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, From db5d208e1af90dc203def68e3f91b0e3aef6d5e7 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 31 Mar 2021 17:04:58 +0200 Subject: [PATCH 5/5] Update schema --- contracts/reflect/schema/execute_msg.json | 6 +++--- contracts/reflect/schema/response_for__custom_msg.json | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/contracts/reflect/schema/execute_msg.json b/contracts/reflect/schema/execute_msg.json index 3a49f5eb5a..5fb601b79d 100644 --- a/contracts/reflect/schema/execute_msg.json +++ b/contracts/reflect/schema/execute_msg.json @@ -400,9 +400,9 @@ "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" + "always", + "error", + "success" ] }, "StakingMsg": { diff --git a/contracts/reflect/schema/response_for__custom_msg.json b/contracts/reflect/schema/response_for__custom_msg.json index 8c6b732f74..50c25700d7 100644 --- a/contracts/reflect/schema/response_for__custom_msg.json +++ b/contracts/reflect/schema/response_for__custom_msg.json @@ -388,9 +388,9 @@ "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" + "always", + "error", + "success" ] }, "StakingMsg": {