[cosmwasm] generate schema (#515)

* schema gen

* include bin for schema gen

* update cosmwasm compiler

* update compiler version

* update their sha digest
This commit is contained in:
Dev Kalra 2023-01-20 14:20:15 +05:30 committed by GitHub
parent a7accadce3
commit 18b1479c4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 387 additions and 18 deletions

View File

@ -1 +1,2 @@
artifacts/ artifacts/
!bin

View File

@ -229,6 +229,30 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "cosmwasm-schema"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04135971e2c3b867eb793ca4e832543c077dbf72edaef7672699190f8fcdb619"
dependencies = [
"cosmwasm-schema-derive",
"schemars",
"serde",
"serde_json",
"thiserror",
]
[[package]]
name = "cosmwasm-schema-derive"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06c8f516a13ae481016aa35f0b5c4652459e8aee65b15b6fb51547a07cea5a0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "cosmwasm-std" name = "cosmwasm-std"
version = "1.0.0" version = "1.0.0"
@ -1240,6 +1264,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bigint", "bigint",
"byteorder", "byteorder",
"cosmwasm-schema",
"cosmwasm-std", "cosmwasm-std",
"cosmwasm-storage", "cosmwasm-storage",
"cosmwasm-vm", "cosmwasm-vm",

View File

@ -4,4 +4,4 @@ docker run --rm -v "$(pwd)":/code \
-v $(cd ../../third_party; pwd):/third_party \ -v $(cd ../../third_party; pwd):/third_party \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \ --mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \ --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/workspace-optimizer:0.12.6 cosmwasm/workspace-optimizer:0.12.11

View File

@ -31,6 +31,7 @@ bigint = "4"
p2w-sdk = { path = "../../../../third_party/pyth/p2w-sdk/rust" } p2w-sdk = { path = "../../../../third_party/pyth/p2w-sdk/rust" }
pyth-sdk = "0.7.0" pyth-sdk = "0.7.0"
byteorder = "1.4.3" byteorder = "1.4.3"
cosmwasm-schema = "1.1.9"
[dev-dependencies] [dev-dependencies]
cosmwasm-vm = { version = "1.0.0", default-features = false } cosmwasm-vm = { version = "1.0.0", default-features = false }

View File

@ -0,0 +1,319 @@
{
"contract_name": "pyth-cosmwasm",
"contract_version": "0.1.0",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InstantiateMsg",
"type": "object",
"required": [
"chain_id",
"data_sources",
"fee",
"governance_sequence_number",
"governance_source",
"governance_source_index",
"valid_time_period_secs",
"wormhole_contract"
],
"properties": {
"chain_id": {
"type": "integer",
"format": "uint16",
"minimum": 0.0
},
"data_sources": {
"type": "array",
"items": {
"$ref": "#/definitions/PythDataSource"
}
},
"fee": {
"$ref": "#/definitions/Coin"
},
"governance_sequence_number": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"governance_source": {
"$ref": "#/definitions/PythDataSource"
},
"governance_source_index": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
},
"valid_time_period_secs": {
"type": "integer",
"format": "uint16",
"minimum": 0.0
},
"wormhole_contract": {
"type": "string"
}
},
"additionalProperties": false,
"definitions": {
"Binary": {
"description": "Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>",
"type": "string"
},
"Coin": {
"type": "object",
"required": ["amount", "denom"],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
},
"denom": {
"type": "string"
}
}
},
"PythDataSource": {
"type": "object",
"required": ["chain_id", "emitter"],
"properties": {
"chain_id": {
"type": "integer",
"format": "uint16",
"minimum": 0.0
},
"emitter": {
"$ref": "#/definitions/Binary"
}
}
},
"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"
}
}
},
"execute": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecuteMsg",
"oneOf": [
{
"type": "object",
"required": ["update_price_feeds"],
"properties": {
"update_price_feeds": {
"type": "object",
"required": ["data"],
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/Binary"
}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": ["execute_governance_instruction"],
"properties": {
"execute_governance_instruction": {
"type": "object",
"required": ["data"],
"properties": {
"data": {
"$ref": "#/definitions/Binary"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
],
"definitions": {
"Binary": {
"description": "Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>",
"type": "string"
}
}
},
"query": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "QueryMsg",
"oneOf": [
{
"type": "string",
"enum": ["get_valid_time_period"]
},
{
"type": "object",
"required": ["price_feed"],
"properties": {
"price_feed": {
"type": "object",
"required": ["id"],
"properties": {
"id": {
"$ref": "#/definitions/Identifier"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": ["get_update_fee"],
"properties": {
"get_update_fee": {
"type": "object",
"required": ["vaas"],
"properties": {
"vaas": {
"type": "array",
"items": {
"$ref": "#/definitions/Binary"
}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
],
"definitions": {
"Binary": {
"description": "Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>",
"type": "string"
},
"Identifier": {
"type": "string"
}
}
},
"migrate": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MigrateMsg",
"type": "object",
"additionalProperties": false
},
"sudo": null,
"responses": {
"get_update_fee": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Coin",
"type": "object",
"required": ["amount", "denom"],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
},
"denom": {
"type": "string"
}
},
"definitions": {
"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"
}
}
},
"get_valid_time_period": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Duration",
"type": "object",
"required": ["nanos", "secs"],
"properties": {
"nanos": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
},
"secs": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
}
}
},
"price_feed": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "PriceFeedResponse",
"type": "object",
"required": ["price_feed"],
"properties": {
"price_feed": {
"$ref": "#/definitions/PriceFeed"
}
},
"additionalProperties": false,
"definitions": {
"Identifier": {
"type": "string"
},
"Price": {
"description": "A price with a degree of uncertainty at a certain time, represented as a price +- a confidence interval.\n\nPlease refer to the documentation at https://docs.pyth.network/consumers/best-practices for using this price safely.\n\nThe confidence interval roughly corresponds to the standard error of a normal distribution. Both the price and confidence are stored in a fixed-point numeric representation, `x * 10^expo`, where `expo` is the exponent. For example:\n\n``` use pyth_sdk::Price; Price { price: 12345, conf: 267, expo: -2, publish_time: 100 }; // represents 123.45 +- 2.67 published at UnixTimestamp 100 Price { price: 123, conf: 1, expo: 2, publish_time: 100 }; // represents 12300 +- 100 published at UnixTimestamp 100 ```\n\n`Price` supports a limited set of mathematical operations. All of these operations will propagate any uncertainty in the arguments into the result. However, the uncertainty in the result may overestimate the true uncertainty (by at most a factor of `sqrt(2)`) due to computational limitations. Furthermore, all of these operations may return `None` if their result cannot be represented within the numeric representation (e.g., the exponent is so small that the price does not fit into an i64). Users of these methods should (1) select their exponents to avoid this problem, and (2) handle the `None` case gracefully.",
"type": "object",
"required": ["conf", "expo", "price", "publish_time"],
"properties": {
"conf": {
"description": "Confidence interval.",
"type": "string"
},
"expo": {
"description": "Exponent.",
"type": "integer",
"format": "int32"
},
"price": {
"description": "Price.",
"type": "string"
},
"publish_time": {
"description": "Publish time.",
"type": "integer",
"format": "int64"
}
}
},
"PriceFeed": {
"description": "Represents a current aggregation price from pyth publisher feeds.",
"type": "object",
"required": ["ema_price", "id", "price"],
"properties": {
"ema_price": {
"description": "Exponentially-weighted moving average (EMA) price.",
"allOf": [
{
"$ref": "#/definitions/Price"
}
]
},
"id": {
"description": "Unique identifier for this price.",
"allOf": [
{
"$ref": "#/definitions/Identifier"
}
]
},
"price": {
"description": "Price.",
"allOf": [
{
"$ref": "#/definitions/Price"
}
]
}
}
}
}
}
}
}

View File

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

View File

@ -4,21 +4,24 @@ use {
PriceFeed, PriceFeed,
PriceIdentifier, PriceIdentifier,
}, },
cosmwasm_schema::{
cw_serde,
QueryResponses,
},
cosmwasm_std::{ cosmwasm_std::{
Binary, Binary,
Coin, Coin,
}, },
schemars::JsonSchema, std::time::Duration,
serde::{
Deserialize,
Serialize,
},
}; };
// cw_serde attribute is equivalent to
// #[derive(Serialize, Deserialize, PartialEq, Debug, Clone, JsonSchema)]
// #[serde(rename_all = "snake_case")]
type HumanAddr = String; type HumanAddr = String;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[cw_serde]
#[serde(rename_all = "snake_case")]
pub struct InstantiateMsg { pub struct InstantiateMsg {
pub wormhole_contract: HumanAddr, pub wormhole_contract: HumanAddr,
pub data_sources: Vec<PythDataSource>, pub data_sources: Vec<PythDataSource>,
@ -33,29 +36,31 @@ pub struct InstantiateMsg {
pub fee: Coin, pub fee: Coin,
} }
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[derive(Eq)]
#[serde(rename_all = "snake_case")] #[cw_serde]
pub enum ExecuteMsg { pub enum ExecuteMsg {
// TODO: add UpdatePriceFeeds if necessary // TODO: add UpdatePriceFeeds if necessary
UpdatePriceFeeds { data: Vec<Binary> }, UpdatePriceFeeds { data: Vec<Binary> },
ExecuteGovernanceInstruction { data: Binary }, ExecuteGovernanceInstruction { data: Binary },
} }
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[derive(Eq)]
#[serde(rename_all = "snake_case")] #[cw_serde]
pub struct MigrateMsg {} pub struct MigrateMsg {}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[cw_serde]
#[serde(rename_all = "snake_case")] #[derive(QueryResponses)]
pub enum QueryMsg { pub enum QueryMsg {
#[returns(PriceFeedResponse)]
PriceFeed { id: PriceIdentifier }, PriceFeed { id: PriceIdentifier },
#[returns(Coin)]
GetUpdateFee { vaas: Vec<Binary> }, GetUpdateFee { vaas: Vec<Binary> },
#[returns(Duration)]
GetValidTimePeriod, GetValidTimePeriod,
} }
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[cw_serde]
#[serde(rename_all = "snake_case")]
pub struct PriceFeedResponse { pub struct PriceFeedResponse {
pub price_feed: PriceFeed, pub price_feed: PriceFeed,
} }

View File

@ -1,7 +1,7 @@
# This is a multi-stage docker file, first and second stage builds contracts # This is a multi-stage docker file, first and second stage builds contracts
# And the third one creates node.js environment to deploy them # And the third one creates node.js environment to deploy them
FROM cosmwasm/workspace-optimizer:0.12.6@sha256:e6565a5e87c830ef3e8775a9035006b38ad0aaf0a96319158c802457b1dd1d08 AS wormhole_builder FROM cosmwasm/workspace-optimizer:0.12.11@sha256:b6b0b1d5ed76dafd0ec0d3812a1e7a275316cf862fd6d0a918f14d71c4cb187f AS wormhole_builder
WORKDIR /tmp/wormhole-repo WORKDIR /tmp/wormhole-repo
@ -15,7 +15,7 @@ RUN mv wormhole-${WORMHOLE_REV}/cosmwasm/contracts /code/contracts
WORKDIR /code WORKDIR /code
RUN --mount=type=cache,target=/code/target,id=cosmwasm_wormhole_target --mount=type=cache,target=/usr/local/cargo/registry optimize_workspace.sh RUN --mount=type=cache,target=/code/target,id=cosmwasm_wormhole_target --mount=type=cache,target=/usr/local/cargo/registry optimize_workspace.sh
FROM cosmwasm/workspace-optimizer:0.12.6@sha256:e6565a5e87c830ef3e8775a9035006b38ad0aaf0a96319158c802457b1dd1d08 AS pyth_builder FROM cosmwasm/workspace-optimizer:0.12.11@sha256:b6b0b1d5ed76dafd0ec0d3812a1e7a275316cf862fd6d0a918f14d71c4cb187f AS pyth_builder
COPY target-chains/cosmwasm/Cargo.lock /code/ COPY target-chains/cosmwasm/Cargo.lock /code/
COPY target-chains/cosmwasm/Cargo.toml /code/ COPY target-chains/cosmwasm/Cargo.toml /code/