Browse Source

cosmwasm: add support for json schemas for all contracts (#4118)

* cosmwasm: add support for json schemas for all contracts

* cosmwasm: fixed formatting as per the linter
Kaku 1 năm trước cách đây
mục cha
commit
c35940ae96

+ 4 - 0
cosmwasm/Cargo.lock

@@ -611,6 +611,7 @@ dependencies = [
 name = "cw20-wrapped-2"
 name = "cw20-wrapped-2"
 version = "0.1.0"
 version = "0.1.0"
 dependencies = [
 dependencies = [
+ "cosmwasm-schema",
  "cosmwasm-std",
  "cosmwasm-std",
  "cosmwasm-storage",
  "cosmwasm-storage",
  "cw2 0.13.4",
  "cw2 0.13.4",
@@ -1244,6 +1245,7 @@ dependencies = [
 name = "mock-bridge-integration-2"
 name = "mock-bridge-integration-2"
 version = "0.1.0"
 version = "0.1.0"
 dependencies = [
 dependencies = [
+ "cosmwasm-schema",
  "cosmwasm-std",
  "cosmwasm-std",
  "cosmwasm-storage",
  "cosmwasm-storage",
  "cosmwasm-vm",
  "cosmwasm-vm",
@@ -1976,6 +1978,7 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
 name = "token-bridge-cosmwasm"
 name = "token-bridge-cosmwasm"
 version = "0.1.0"
 version = "0.1.0"
 dependencies = [
 dependencies = [
+ "cosmwasm-schema",
  "cosmwasm-std",
  "cosmwasm-std",
  "cosmwasm-storage",
  "cosmwasm-storage",
  "cw20",
  "cw20",
@@ -2627,6 +2630,7 @@ dependencies = [
 name = "wormhole-cosmwasm"
 name = "wormhole-cosmwasm"
 version = "0.1.0"
 version = "0.1.0"
 dependencies = [
 dependencies = [
+ "cosmwasm-schema",
  "cosmwasm-std",
  "cosmwasm-std",
  "cosmwasm-storage",
  "cosmwasm-storage",
  "generic-array",
  "generic-array",

+ 1 - 0
cosmwasm/contracts/cw20-wrapped/Cargo.toml

@@ -16,6 +16,7 @@ library = []
 [dependencies]
 [dependencies]
 cosmwasm-std = { version = "1.0.0" }
 cosmwasm-std = { version = "1.0.0" }
 cosmwasm-storage = { version = "1.0.0" }
 cosmwasm-storage = { version = "1.0.0" }
+cosmwasm-schema = { version = "1.0.0" }
 schemars = "0.8.8"
 schemars = "0.8.8"
 serde = { version = "1.0.137", default-features = false, features = ["derive"] }
 serde = { version = "1.0.137", default-features = false, features = ["derive"] }
 cw2 = { version = "0.13.2" } 
 cw2 = { version = "0.13.2" } 

+ 10 - 0
cosmwasm/contracts/cw20-wrapped/src/examples/cw20_wrapped_schema.rs

@@ -0,0 +1,10 @@
+use cosmwasm_schema::write_api;
+use cw20_wrapped_2::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
+
+fn main() {
+    write_api! {
+        instantiate: InstantiateMsg,
+        execute: ExecuteMsg,
+        query: QueryMsg,
+    }
+}

+ 19 - 17
cosmwasm/contracts/cw20-wrapped/src/msg.rs

@@ -1,13 +1,12 @@
 #![allow(clippy::field_reassign_with_default)]
 #![allow(clippy::field_reassign_with_default)]
-use schemars::JsonSchema;
-use serde::{Deserialize, Serialize};
+use cosmwasm_schema::{cw_serde, QueryResponses};
 
 
 use cosmwasm_std::{Addr, Binary, Uint128};
 use cosmwasm_std::{Addr, Binary, Uint128};
-use cw20::Expiration;
+use cw20::{AllowanceResponse, BalanceResponse, Expiration, TokenInfoResponse};
 
 
 type HumanAddr = String;
 type HumanAddr = String;
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
+#[cw_serde]
 pub struct InstantiateMsg {
 pub struct InstantiateMsg {
     pub name: String,
     pub name: String,
     pub symbol: String,
     pub symbol: String,
@@ -18,24 +17,22 @@ pub struct InstantiateMsg {
     pub init_hook: Option<InitHook>,
     pub init_hook: Option<InitHook>,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
+#[cw_serde]
 pub struct InitHook {
 pub struct InitHook {
     pub msg: Binary,
     pub msg: Binary,
     pub contract_addr: HumanAddr,
     pub contract_addr: HumanAddr,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
+#[cw_serde]
 pub struct InitMint {
 pub struct InitMint {
     pub recipient: HumanAddr,
     pub recipient: HumanAddr,
     pub amount: Uint128,
     pub amount: Uint128,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct MigrateMsg {}
 pub struct MigrateMsg {}
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub enum ExecuteMsg {
 pub enum ExecuteMsg {
     /// Implements CW20. Transfer is a base message to move tokens to another account without triggering actions
     /// Implements CW20. Transfer is a base message to move tokens to another account without triggering actions
     Transfer {
     Transfer {
@@ -94,17 +91,22 @@ pub enum ExecuteMsg {
     UpdateMetadata { name: String, symbol: String },
     UpdateMetadata { name: String, symbol: String },
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
+#[derive(QueryResponses)]
 pub enum QueryMsg {
 pub enum QueryMsg {
-    // Generic information about the wrapped asset
+    #[returns(WrappedAssetInfoResponse)]
+    /// Generic information about the wrapped asset
     WrappedAssetInfo {},
     WrappedAssetInfo {},
+
+    #[returns(BalanceResponse)]
     /// Implements CW20. Returns the current balance of the given address, 0 if unset.
     /// Implements CW20. Returns the current balance of the given address, 0 if unset.
-    Balance {
-        address: HumanAddr,
-    },
+    Balance { address: HumanAddr },
+
+    #[returns(TokenInfoResponse)]
     /// Implements CW20. Returns metadata on the contract - name, decimals, supply, etc.
     /// Implements CW20. Returns metadata on the contract - name, decimals, supply, etc.
     TokenInfo {},
     TokenInfo {},
+
+    #[returns(AllowanceResponse)]
     /// Implements CW20 "allowance" extension.
     /// Implements CW20 "allowance" extension.
     /// Returns how much spender can use from owner account, 0 if unset.
     /// Returns how much spender can use from owner account, 0 if unset.
     Allowance {
     Allowance {
@@ -113,7 +115,7 @@ pub enum QueryMsg {
     },
     },
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
+#[cw_serde]
 pub struct WrappedAssetInfoResponse {
 pub struct WrappedAssetInfoResponse {
     pub asset_chain: u16,      // Asset chain id
     pub asset_chain: u16,      // Asset chain id
     pub asset_address: Binary, // Asset smart contract address in the original chain
     pub asset_address: Binary, // Asset smart contract address in the original chain

+ 10 - 0
cosmwasm/contracts/ibc-translator/src/examples/ibc_translator_schema.rs

@@ -0,0 +1,10 @@
+use cosmwasm_schema::write_api;
+use ibc_translator::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
+
+fn main() {
+    write_api! {
+        instantiate: InstantiateMsg,
+        execute: ExecuteMsg,
+        query: QueryMsg,
+    }
+}

+ 1 - 0
cosmwasm/contracts/mock-bridge-integration/Cargo.toml

@@ -15,6 +15,7 @@ library = []
 [dependencies]
 [dependencies]
 cosmwasm-std = { version = "1.0.0" }
 cosmwasm-std = { version = "1.0.0" }
 cosmwasm-storage = { version = "1.0.0" }
 cosmwasm-storage = { version = "1.0.0" }
+cosmwasm-schema = { version = "1.0.0" }
 schemars = "0.8.8"
 schemars = "0.8.8"
 serde = { version = "1.0.137", default-features = false, features = ["derive"] }
 serde = { version = "1.0.137", default-features = false, features = ["derive"] }
 
 

+ 7 - 9
cosmwasm/contracts/mock-bridge-integration/src/msg.rs

@@ -1,26 +1,24 @@
+use cosmwasm_schema::{cw_serde, QueryResponses};
 use cosmwasm_std::Binary;
 use cosmwasm_std::Binary;
-use schemars::JsonSchema;
-use serde::{Deserialize, Serialize};
 
 
 type HumanAddr = String;
 type HumanAddr = String;
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
+#[cw_serde]
 pub struct InstantiateMsg {
 pub struct InstantiateMsg {
     pub token_bridge_contract: HumanAddr,
     pub token_bridge_contract: HumanAddr,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub enum ExecuteMsg {
 pub enum ExecuteMsg {
     CompleteTransferWithPayload { data: Binary },
     CompleteTransferWithPayload { data: Binary },
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct MigrateMsg {}
 pub struct MigrateMsg {}
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
+#[derive(QueryResponses)]
 pub enum QueryMsg {
 pub enum QueryMsg {
+    #[returns(())]
     WrappedRegistry { chain: u16, address: Binary },
     WrappedRegistry { chain: u16, address: Binary },
 }
 }

+ 1 - 0
cosmwasm/contracts/token-bridge/Cargo.toml

@@ -21,6 +21,7 @@ default = ["full"]
 [dependencies]
 [dependencies]
 cosmwasm-std = { version = "1.0.0" }
 cosmwasm-std = { version = "1.0.0" }
 cosmwasm-storage = { version = "1.0.0" }
 cosmwasm-storage = { version = "1.0.0" }
+cosmwasm-schema = { version = "1.0.0" }
 schemars = "0.8.8"
 schemars = "0.8.8"
 serde = { version = "1.0.137", default-features = false, features = ["derive"] }
 serde = { version = "1.0.137", default-features = false, features = ["derive"] }
 cw20 = "0.13.2"
 cw20 = "0.13.2"

+ 19 - 24
cosmwasm/contracts/token-bridge/src/msg.rs

@@ -1,6 +1,5 @@
+use cosmwasm_schema::{cw_serde, QueryResponses};
 use cosmwasm_std::{Binary, Uint128};
 use cosmwasm_std::{Binary, Uint128};
-use schemars::JsonSchema;
-use serde::{Deserialize, Serialize};
 
 
 use crate::token_address::{ExternalTokenId, TokenId};
 use crate::token_address::{ExternalTokenId, TokenId};
 
 
@@ -8,7 +7,7 @@ type HumanAddr = String;
 
 
 /// The instantiation parameters of the token bridge contract. See
 /// The instantiation parameters of the token bridge contract. See
 /// [`crate::state::ConfigInfo`] for more details on what these fields mean.
 /// [`crate::state::ConfigInfo`] for more details on what these fields mean.
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
+#[cw_serde]
 pub struct InstantiateMsg {
 pub struct InstantiateMsg {
     pub gov_chain: u16,
     pub gov_chain: u16,
     pub gov_address: Binary,
     pub gov_address: Binary,
@@ -22,8 +21,7 @@ pub struct InstantiateMsg {
     pub native_decimals: u8,
     pub native_decimals: u8,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub enum ExecuteMsg {
 pub enum ExecuteMsg {
     RegisterAssetHook {
     RegisterAssetHook {
         chain: u16,
         chain: u16,
@@ -67,28 +65,30 @@ pub enum ExecuteMsg {
     },
     },
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct MigrateMsg {}
 pub struct MigrateMsg {}
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
+#[derive(QueryResponses)]
 pub enum QueryMsg {
 pub enum QueryMsg {
+    #[returns(WrappedRegistryResponse)]
     WrappedRegistry { chain: u16, address: Binary },
     WrappedRegistry { chain: u16, address: Binary },
+    #[returns(TransferInfoResponse)]
     TransferInfo { vaa: Binary },
     TransferInfo { vaa: Binary },
+    #[returns(ExternalIdResponse)]
     ExternalId { external_id: Binary },
     ExternalId { external_id: Binary },
+    #[returns(IsVaaRedeemedResponse)]
     IsVaaRedeemed { vaa: Binary },
     IsVaaRedeemed { vaa: Binary },
+    #[returns(ChainRegistrationResponse)]
     ChainRegistration { chain: u16 },
     ChainRegistration { chain: u16 },
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct WrappedRegistryResponse {
 pub struct WrappedRegistryResponse {
     pub address: HumanAddr,
     pub address: HumanAddr,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct TransferInfoResponse {
 pub struct TransferInfoResponse {
     pub amount: Uint128,
     pub amount: Uint128,
     pub token_address: [u8; 32],
     pub token_address: [u8; 32],
@@ -99,26 +99,22 @@ pub struct TransferInfoResponse {
     pub payload: Vec<u8>,
     pub payload: Vec<u8>,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct ExternalIdResponse {
 pub struct ExternalIdResponse {
     pub token_id: TokenId,
     pub token_id: TokenId,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct IsVaaRedeemedResponse {
 pub struct IsVaaRedeemedResponse {
     pub is_redeemed: bool,
     pub is_redeemed: bool,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct ChainRegistrationResponse {
 pub struct ChainRegistrationResponse {
     pub address: Binary,
     pub address: Binary,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct CompleteTransferResponse {
 pub struct CompleteTransferResponse {
     // All addresses are bech32-encoded strings.
     // All addresses are bech32-encoded strings.
 
 
@@ -132,7 +128,7 @@ pub struct CompleteTransferResponse {
     pub fee: Uint128,
     pub fee: Uint128,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
+#[cw_serde]
 pub struct Asset {
 pub struct Asset {
     pub info: AssetInfo,
     pub info: AssetInfo,
     pub amount: Uint128,
     pub amount: Uint128,
@@ -140,8 +136,7 @@ pub struct Asset {
 
 
 /// AssetInfo contract_addr is usually passed from the cw20 hook
 /// AssetInfo contract_addr is usually passed from the cw20 hook
 /// so we can trust the contract_addr is properly validated.
 /// so we can trust the contract_addr is properly validated.
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub enum AssetInfo {
 pub enum AssetInfo {
     Token { contract_addr: String },
     Token { contract_addr: String },
     NativeToken { denom: String },
     NativeToken { denom: String },

+ 11 - 0
cosmwasm/contracts/wormchain-ibc-receiver/src/examples/wormchain_ibc_receiver_schema.rs

@@ -0,0 +1,11 @@
+use cosmwasm_schema::write_api;
+use cosmwasm_std::Empty;
+use wormchain_ibc_receiver::msg::{ExecuteMsg, QueryMsg};
+
+fn main() {
+    write_api! {
+        instantiate: Empty,
+        execute: ExecuteMsg,
+        query: QueryMsg,
+    }
+}

+ 11 - 0
cosmwasm/contracts/wormhole-ibc/src/examples/wormhole_ibc_schema.rs

@@ -0,0 +1,11 @@
+use cosmwasm_schema::write_api;
+use cw_wormhole::msg::{InstantiateMsg, QueryMsg};
+use wormhole_ibc::msg::ExecuteMsg;
+
+fn main() {
+    write_api! {
+        instantiate: InstantiateMsg,
+        execute: ExecuteMsg,
+        query: QueryMsg,
+    }
+}

+ 1 - 0
cosmwasm/contracts/wormhole/Cargo.toml

@@ -21,6 +21,7 @@ default = ["full"]
 [dependencies]
 [dependencies]
 cosmwasm-std = { version = "1.0.0" }
 cosmwasm-std = { version = "1.0.0" }
 cosmwasm-storage = { version = "1.0.0" }
 cosmwasm-storage = { version = "1.0.0" }
+cosmwasm-schema = { version = "1.0.0" }
 schemars = "0.8.8"
 schemars = "0.8.8"
 serde = { version = "1.0.137", default-features = false, features = ["derive"] }
 serde = { version = "1.0.137", default-features = false, features = ["derive"] }
 thiserror = { version = "1.0.31" }
 thiserror = { version = "1.0.31" }

+ 10 - 0
cosmwasm/contracts/wormhole/src/examples/wormhole_schema.rs

@@ -0,0 +1,10 @@
+use cosmwasm_schema::write_api;
+use cw_wormhole::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
+
+fn main() {
+    write_api! {
+        instantiate: InstantiateMsg,
+        execute: ExecuteMsg,
+        query: QueryMsg,
+    }
+}

+ 15 - 18
cosmwasm/contracts/wormhole/src/msg.rs

@@ -1,14 +1,13 @@
+use cosmwasm_schema::{cw_serde, QueryResponses};
 use cosmwasm_std::{Binary, Coin};
 use cosmwasm_std::{Binary, Coin};
-use schemars::JsonSchema;
-use serde::{Deserialize, Serialize};
 
 
-use crate::state::{GuardianAddress, GuardianSetInfo};
+use crate::state::{GuardianAddress, GuardianSetInfo, ParsedVAA};
 
 
 type HumanAddr = String;
 type HumanAddr = String;
 
 
 /// The instantiation parameters of the core bridge contract. See
 /// The instantiation parameters of the core bridge contract. See
 /// [`crate::state::ConfigInfo`] for more details on what these fields mean.
 /// [`crate::state::ConfigInfo`] for more details on what these fields mean.
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
+#[cw_serde]
 pub struct InstantiateMsg {
 pub struct InstantiateMsg {
     pub gov_chain: u16,
     pub gov_chain: u16,
     pub gov_address: Binary,
     pub gov_address: Binary,
@@ -21,47 +20,45 @@ pub struct InstantiateMsg {
     pub fee_denom: String,
     pub fee_denom: String,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub enum ExecuteMsg {
 pub enum ExecuteMsg {
     SubmitVAA { vaa: Binary },
     SubmitVAA { vaa: Binary },
     PostMessage { message: Binary, nonce: u32 },
     PostMessage { message: Binary, nonce: u32 },
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct MigrateMsg {}
 pub struct MigrateMsg {}
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
+#[derive(QueryResponses)]
 pub enum QueryMsg {
 pub enum QueryMsg {
+    #[returns(GuardianSetInfoResponse)]
     GuardianSetInfo {},
     GuardianSetInfo {},
+    #[returns(ParsedVAA)]
     VerifyVAA { vaa: Binary, block_time: u64 },
     VerifyVAA { vaa: Binary, block_time: u64 },
+    #[returns(GetStateResponse)]
     GetState {},
     GetState {},
+    #[returns(GetAddressHexResponse)]
     QueryAddressHex { address: HumanAddr },
     QueryAddressHex { address: HumanAddr },
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct GuardianSetInfoResponse {
 pub struct GuardianSetInfoResponse {
     pub guardian_set_index: u32,         // Current guardian set index
     pub guardian_set_index: u32,         // Current guardian set index
     pub addresses: Vec<GuardianAddress>, // List of querdian addresses
     pub addresses: Vec<GuardianAddress>, // List of querdian addresses
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct WrappedRegistryResponse {
 pub struct WrappedRegistryResponse {
     pub address: HumanAddr,
     pub address: HumanAddr,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct GetStateResponse {
 pub struct GetStateResponse {
     pub fee: Coin,
     pub fee: Coin,
 }
 }
 
 
-#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
-#[serde(rename_all = "snake_case")]
+#[cw_serde]
 pub struct GetAddressHexResponse {
 pub struct GetAddressHexResponse {
     pub hex: String,
     pub hex: String,
 }
 }