diff --git a/contracts/ibc-reflect-send/schema/ibc/acknowledgement_msg_balances.json b/contracts/ibc-reflect-send/schema/ibc/acknowledgement_msg_balances.json new file mode 100644 index 0000000000..4bde7573d0 --- /dev/null +++ b/contracts/ibc-reflect-send/schema/ibc/acknowledgement_msg_balances.json @@ -0,0 +1,71 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "AcknowledgementMsgBalances", + "description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512", + "oneOf": [ + { + "type": "object", + "required": [ + "ok" + ], + "properties": { + "ok": { + "$ref": "#/definitions/BalancesResponse" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "error" + ], + "properties": { + "error": { + "type": "string" + } + }, + "additionalProperties": false + } + ], + "definitions": { + "BalancesResponse": { + "description": "This is the success response we send on ack for PacketMsg::Balance. Just acknowledge success or error", + "type": "object", + "required": [ + "account", + "balances" + ], + "properties": { + "account": { + "type": "string" + }, + "balances": { + "type": "array", + "items": { + "$ref": "#/definitions/Coin" + } + } + } + }, + "Coin": { + "type": "object", + "required": [ + "amount", + "denom" + ], + "properties": { + "amount": { + "$ref": "#/definitions/Uint128" + }, + "denom": { + "type": "string" + } + } + }, + "Uint128": { + "description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```", + "type": "string" + } + } +} diff --git a/contracts/ibc-reflect-send/schema/ibc/acknowledgement_msg_dispatch.json b/contracts/ibc-reflect-send/schema/ibc/acknowledgement_msg_dispatch.json new file mode 100644 index 0000000000..5c010f5bd1 --- /dev/null +++ b/contracts/ibc-reflect-send/schema/ibc/acknowledgement_msg_dispatch.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "AcknowledgementMsgDispatch", + "description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512", + "oneOf": [ + { + "type": "object", + "required": [ + "ok" + ], + "properties": { + "ok": { + "type": "null" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "error" + ], + "properties": { + "error": { + "type": "string" + } + }, + "additionalProperties": false + } + ] +} diff --git a/contracts/ibc-reflect-send/schema/ibc/acknowledgement_msg_who_am_i.json b/contracts/ibc-reflect-send/schema/ibc/acknowledgement_msg_who_am_i.json new file mode 100644 index 0000000000..e4ad97424b --- /dev/null +++ b/contracts/ibc-reflect-send/schema/ibc/acknowledgement_msg_who_am_i.json @@ -0,0 +1,45 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "AcknowledgementMsgWhoAmI", + "description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512", + "oneOf": [ + { + "type": "object", + "required": [ + "ok" + ], + "properties": { + "ok": { + "$ref": "#/definitions/WhoAmIResponse" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "error" + ], + "properties": { + "error": { + "type": "string" + } + }, + "additionalProperties": false + } + ], + "definitions": { + "WhoAmIResponse": { + "description": "This is the success response we send on ack for PacketMsg::WhoAmI. Return the caller's account address on the remote chain", + "type": "object", + "required": [ + "account" + ], + "properties": { + "account": { + "type": "string" + } + } + } + } +} diff --git a/contracts/ibc-reflect-send/schema/packet_msg.json b/contracts/ibc-reflect-send/schema/ibc/packet_msg.json similarity index 100% rename from contracts/ibc-reflect-send/schema/packet_msg.json rename to contracts/ibc-reflect-send/schema/ibc/packet_msg.json diff --git a/contracts/ibc-reflect-send/src/bin/schema.rs b/contracts/ibc-reflect-send/src/bin/schema.rs index 2f8a8d3e72..cb7ea07166 100644 --- a/contracts/ibc-reflect-send/src/bin/schema.rs +++ b/contracts/ibc-reflect-send/src/bin/schema.rs @@ -1,8 +1,10 @@ use std::env::current_dir; -use cosmwasm_schema::{export_schema, schema_for, write_api}; +use cosmwasm_schema::{export_schema, export_schema_with_title, schema_for, write_api}; -use ibc_reflect_send::ibc_msg::PacketMsg; +use ibc_reflect_send::ibc_msg::{ + AcknowledgementMsg, BalancesResponse, DispatchResponse, PacketMsg, WhoAmIResponse, +}; use ibc_reflect_send::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; fn main() { @@ -16,5 +18,21 @@ fn main() { // Schemas for inter-contract communication let mut out_dir = current_dir().unwrap(); out_dir.push("schema"); + out_dir.push("ibc"); export_schema(&schema_for!(PacketMsg), &out_dir); + export_schema_with_title( + &schema_for!(AcknowledgementMsg), + &out_dir, + "AcknowledgementMsgBalances", + ); + export_schema_with_title( + &schema_for!(AcknowledgementMsg), + &out_dir, + "AcknowledgementMsgDispatch", + ); + export_schema_with_title( + &schema_for!(AcknowledgementMsg), + &out_dir, + "AcknowledgementMsgWhoAmI", + ); } diff --git a/contracts/ibc-reflect-send/src/ibc.rs b/contracts/ibc-reflect-send/src/ibc.rs index adede549eb..634fed3e81 100644 --- a/contracts/ibc-reflect-send/src/ibc.rs +++ b/contracts/ibc-reflect-send/src/ibc.rs @@ -146,7 +146,7 @@ fn acknowledge_who_am_i( // ignore errors (but mention in log) let WhoAmIResponse { account } = match ack { AcknowledgementMsg::Ok(res) => res, - AcknowledgementMsg::Err(e) => { + AcknowledgementMsg::Error(e) => { return Ok(IbcBasicResponse::new() .add_attribute("action", "acknowledge_who_am_i") .add_attribute("error", e)) @@ -176,7 +176,7 @@ fn acknowledge_balances( // ignore errors (but mention in log) let BalancesResponse { account, balances } = match ack { AcknowledgementMsg::Ok(res) => res, - AcknowledgementMsg::Err(e) => { + AcknowledgementMsg::Error(e) => { return Ok(IbcBasicResponse::new() .add_attribute("action", "acknowledge_balances") .add_attribute("error", e)) diff --git a/contracts/ibc-reflect-send/src/ibc_msg.rs b/contracts/ibc-reflect-send/src/ibc_msg.rs index d395388c3a..ca6041218c 100644 --- a/contracts/ibc-reflect-send/src/ibc_msg.rs +++ b/contracts/ibc-reflect-send/src/ibc_msg.rs @@ -1,4 +1,5 @@ -use cosmwasm_std::{Coin, ContractResult, CosmosMsg}; +use cosmwasm_schema::cw_serde; +use cosmwasm_std::{Coin, CosmosMsg}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -11,9 +12,35 @@ pub enum PacketMsg { Balances {}, } -/// All IBC acknowledgements are wrapped in `ContractResult`. -/// The success value depends on the PacketMsg variant. -pub type AcknowledgementMsg = ContractResult; +/// A custom acknowledgement type. +/// The success type `T` depends on the PacketMsg variant. +/// +/// This could be refactored to use [StdAck] at some point. However, +/// it has a different success variant name ("ok" vs. "result") and +/// a JSON payload instead of a binary payload. +/// +/// [StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512 +#[cw_serde] +pub enum AcknowledgementMsg { + Ok(S), + Error(String), +} + +impl AcknowledgementMsg { + pub fn unwrap(self) -> S { + match self { + AcknowledgementMsg::Ok(data) => data, + AcknowledgementMsg::Error(err) => panic!("{}", err), + } + } + + pub fn unwrap_err(self) -> String { + match self { + AcknowledgementMsg::Ok(_) => panic!("not an error"), + AcknowledgementMsg::Error(err) => err, + } + } +} /// This is the success response we send on ack for PacketMsg::Dispatch. /// Just acknowledge success or error diff --git a/contracts/ibc-reflect/schema/acknowledgement_msg_dispatch.json b/contracts/ibc-reflect/schema/acknowledgement_msg_dispatch.json deleted file mode 100644 index e89f54b65d..0000000000 --- a/contracts/ibc-reflect/schema/acknowledgement_msg_dispatch.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-07/schema#", - "title": "AcknowledgementMsgDispatch", - "description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"events\":[],\"data\":null}}\"#); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#); ```", - "oneOf": [ - { - "type": "object", - "required": [ - "ok" - ], - "properties": { - "ok": { - "type": "null" - } - }, - "additionalProperties": false - }, - { - "description": "An error type that every custom error created by contract developers can be converted to. This could potientially have more structure, but String is the easiest.", - "type": "object", - "required": [ - "error" - ], - "properties": { - "error": { - "type": "string" - } - }, - "additionalProperties": false - } - ] -} diff --git a/contracts/ibc-reflect/schema/acknowledgement_msg_who_am_i.json b/contracts/ibc-reflect/schema/acknowledgement_msg_who_am_i.json deleted file mode 100644 index 57c0933f8c..0000000000 --- a/contracts/ibc-reflect/schema/acknowledgement_msg_who_am_i.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-07/schema#", - "title": "AcknowledgementMsgWhoAmI", - "description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"events\":[],\"data\":null}}\"#); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#); ```", - "oneOf": [ - { - "type": "object", - "required": [ - "ok" - ], - "properties": { - "ok": { - "$ref": "#/definitions/WhoAmIResponse" - } - }, - "additionalProperties": false - }, - { - "description": "An error type that every custom error created by contract developers can be converted to. This could potientially have more structure, but String is the easiest.", - "type": "object", - "required": [ - "error" - ], - "properties": { - "error": { - "type": "string" - } - }, - "additionalProperties": false - } - ], - "definitions": { - "WhoAmIResponse": { - "description": "This is the success response we send on ack for PacketMsg::WhoAmI. Return the caller's account address on the remote chain", - "type": "object", - "required": [ - "account" - ], - "properties": { - "account": { - "type": "string" - } - }, - "additionalProperties": false - } - } -} diff --git a/contracts/ibc-reflect/schema/acknowledgement_msg_balances.json b/contracts/ibc-reflect/schema/ibc/acknowledgement_msg_balances.json similarity index 59% rename from contracts/ibc-reflect/schema/acknowledgement_msg_balances.json rename to contracts/ibc-reflect/schema/ibc/acknowledgement_msg_balances.json index 20e956cd3f..b181c28559 100644 --- a/contracts/ibc-reflect/schema/acknowledgement_msg_balances.json +++ b/contracts/ibc-reflect/schema/ibc/acknowledgement_msg_balances.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "title": "AcknowledgementMsgBalances", - "description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"events\":[],\"data\":null}}\"#); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#); ```", + "description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512", "oneOf": [ { "type": "object", @@ -16,7 +16,6 @@ "additionalProperties": false }, { - "description": "An error type that every custom error created by contract developers can be converted to. This could potientially have more structure, but String is the easiest.", "type": "object", "required": [ "error" diff --git a/contracts/ibc-reflect/schema/ibc/acknowledgement_msg_dispatch.json b/contracts/ibc-reflect/schema/ibc/acknowledgement_msg_dispatch.json new file mode 100644 index 0000000000..5c010f5bd1 --- /dev/null +++ b/contracts/ibc-reflect/schema/ibc/acknowledgement_msg_dispatch.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "AcknowledgementMsgDispatch", + "description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512", + "oneOf": [ + { + "type": "object", + "required": [ + "ok" + ], + "properties": { + "ok": { + "type": "null" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "error" + ], + "properties": { + "error": { + "type": "string" + } + }, + "additionalProperties": false + } + ] +} diff --git a/contracts/ibc-reflect/schema/ibc/acknowledgement_msg_who_am_i.json b/contracts/ibc-reflect/schema/ibc/acknowledgement_msg_who_am_i.json new file mode 100644 index 0000000000..f706c655de --- /dev/null +++ b/contracts/ibc-reflect/schema/ibc/acknowledgement_msg_who_am_i.json @@ -0,0 +1,46 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "AcknowledgementMsgWhoAmI", + "description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512", + "oneOf": [ + { + "type": "object", + "required": [ + "ok" + ], + "properties": { + "ok": { + "$ref": "#/definitions/WhoAmIResponse" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "error" + ], + "properties": { + "error": { + "type": "string" + } + }, + "additionalProperties": false + } + ], + "definitions": { + "WhoAmIResponse": { + "description": "This is the success response we send on ack for PacketMsg::WhoAmI. Return the caller's account address on the remote chain", + "type": "object", + "required": [ + "account" + ], + "properties": { + "account": { + "type": "string" + } + }, + "additionalProperties": false + } + } +} diff --git a/contracts/ibc-reflect/schema/packet_msg.json b/contracts/ibc-reflect/schema/ibc/packet_msg.json similarity index 100% rename from contracts/ibc-reflect/schema/packet_msg.json rename to contracts/ibc-reflect/schema/ibc/packet_msg.json diff --git a/contracts/ibc-reflect/src/bin/schema.rs b/contracts/ibc-reflect/src/bin/schema.rs index 74b3408387..5103b778f8 100644 --- a/contracts/ibc-reflect/src/bin/schema.rs +++ b/contracts/ibc-reflect/src/bin/schema.rs @@ -19,6 +19,7 @@ fn main() { // Schemas for inter-contract communication let mut out_dir = current_dir().unwrap(); out_dir.push("schema"); + out_dir.push("ibc"); export_schema(&schema_for!(PacketMsg), &out_dir); export_schema_with_title( &schema_for!(AcknowledgementMsg), diff --git a/contracts/ibc-reflect/src/contract.rs b/contracts/ibc-reflect/src/contract.rs index 23fa7046a3..1af5d90e91 100644 --- a/contracts/ibc-reflect/src/contract.rs +++ b/contracts/ibc-reflect/src/contract.rs @@ -224,7 +224,7 @@ pub fn migrate(_deps: DepsMut, _env: Env, _msg: Empty) -> StdResult { // this encode an error or error message into a proper acknowledgement to the recevier fn encode_ibc_error(msg: impl Into) -> Binary { // this cannot error, unwrap to keep the interface simple - to_binary(&AcknowledgementMsg::<()>::Err(msg.into())).unwrap() + to_binary(&AcknowledgementMsg::<()>::Error(msg.into())).unwrap() } #[entry_point] diff --git a/contracts/ibc-reflect/src/msg.rs b/contracts/ibc-reflect/src/msg.rs index acd49baf63..6f72005f1c 100644 --- a/contracts/ibc-reflect/src/msg.rs +++ b/contracts/ibc-reflect/src/msg.rs @@ -1,5 +1,5 @@ use cosmwasm_schema::{cw_serde, QueryResponses}; -use cosmwasm_std::{Coin, ContractResult, CosmosMsg}; +use cosmwasm_std::{Coin, CosmosMsg}; /// Just needs to know the code_id of a reflect contract to spawn sub-accounts #[cw_serde] @@ -51,9 +51,35 @@ pub enum PacketMsg { ReturnMsgs { msgs: Vec }, } -/// All acknowledgements are wrapped in `ContractResult`. -/// The success value depends on the PacketMsg variant. -pub type AcknowledgementMsg = ContractResult; +/// A custom acknowledgement type. +/// The success type `T` depends on the PacketMsg variant. +/// +/// This could be refactored to use [StdAck] at some point. However, +/// it has a different success variant name ("ok" vs. "result") and +/// a JSON payload instead of a binary payload. +/// +/// [StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512 +#[cw_serde] +pub enum AcknowledgementMsg { + Ok(S), + Error(String), +} + +impl AcknowledgementMsg { + pub fn unwrap(self) -> S { + match self { + AcknowledgementMsg::Ok(data) => data, + AcknowledgementMsg::Error(err) => panic!("{}", err), + } + } + + pub fn unwrap_err(self) -> String { + match self { + AcknowledgementMsg::Ok(_) => panic!("not an error"), + AcknowledgementMsg::Error(err) => err, + } + } +} /// This is the success response we send on ack for PacketMsg::Dispatch. /// Just acknowledge success or error