Skip to content

Commit 88294ec

Browse files
committed
Generate JSON Schema for BlockInfo
1 parent f2cd7a8 commit 88294ec

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

packages/std/examples/schema.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use std::env::current_dir;
22
use std::fs::create_dir_all;
33

44
use cosmwasm_schema::{export_schema, export_schema_with_title, remove_schemas, schema_for};
5-
use cosmwasm_std::{CosmosMsg, Empty, QueryRequest, Timestamp};
5+
use cosmwasm_std::{BlockInfo, CosmosMsg, Empty, QueryRequest, Timestamp};
66

77
fn main() {
88
let mut out_dir = current_dir().unwrap();
99
out_dir.push("schema");
1010
create_dir_all(&out_dir).unwrap();
1111
remove_schemas(&out_dir).unwrap();
1212

13+
export_schema(&schema_for!(BlockInfo), &out_dir);
1314
export_schema(&schema_for!(Timestamp), &out_dir);
1415
export_schema_with_title(&schema_for!(CosmosMsg), &out_dir, "CosmosMsg");
1516
export_schema_with_title(&schema_for!(QueryRequest<Empty>), &out_dir, "QueryRequest");

packages/std/schema/block_info.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "BlockInfo",
4+
"type": "object",
5+
"required": [
6+
"chain_id",
7+
"height",
8+
"time"
9+
],
10+
"properties": {
11+
"chain_id": {
12+
"type": "string"
13+
},
14+
"height": {
15+
"description": "The height of a block is the number of blocks preceding it in the blockchain.",
16+
"type": "integer",
17+
"format": "uint64",
18+
"minimum": 0.0
19+
},
20+
"time": {
21+
"description": "Absolute time of the block creation in seconds since the UNIX epoch (00:00:00 on 1970-01-01 UTC).\n\nThe source of this is the [BFT Time in Tendermint](https://github.com/tendermint/tendermint/blob/58dc1726/spec/consensus/bft-time.md), which has the same nanosecond precision as the `Timestamp` type.\n\n# Examples\n\nUsing chrono:\n\n``` # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo}; # let env = Env { # block: BlockInfo { # height: 12_345, # time: Timestamp::from_nanos(1_571_797_419_879_305_533), # chain_id: \"cosmos-testnet-14002\".to_string(), # }, # transaction: Some(TransactionInfo { index: 3 }), # contract: ContractInfo { # address: Addr::unchecked(\"contract\"), # }, # }; # extern crate chrono; use chrono::NaiveDateTime; let seconds = env.block.time.seconds(); let nsecs = env.block.time.subsec_nanos(); let dt = NaiveDateTime::from_timestamp(seconds as i64, nsecs as u32); ```\n\nCreating a simple millisecond-precision timestamp (as used in JavaScript):\n\n``` # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo}; # let env = Env { # block: BlockInfo { # height: 12_345, # time: Timestamp::from_nanos(1_571_797_419_879_305_533), # chain_id: \"cosmos-testnet-14002\".to_string(), # }, # transaction: Some(TransactionInfo { index: 3 }), # contract: ContractInfo { # address: Addr::unchecked(\"contract\"), # }, # }; let millis = env.block.time.nanos() / 1_000_000; ```",
22+
"allOf": [
23+
{
24+
"$ref": "#/definitions/Timestamp"
25+
}
26+
]
27+
}
28+
},
29+
"definitions": {
30+
"Timestamp": {
31+
"description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```",
32+
"allOf": [
33+
{
34+
"$ref": "#/definitions/Uint64"
35+
}
36+
]
37+
},
38+
"Uint64": {
39+
"description": "A thin wrapper around u64 that is using strings for JSON encoding/decoding, such that the full u64 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 `u64` to get the value out:\n\n``` # use cosmwasm_std::Uint64; let a = Uint64::from(42u64); assert_eq!(a.u64(), 42);\n\nlet b = Uint64::from(70u32); assert_eq!(b.u64(), 70); ```",
40+
"type": "string"
41+
}
42+
}
43+
}

packages/std/src/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use schemars::JsonSchema;
12
use serde::{Deserialize, Serialize};
23

34
use crate::addresses::Addr;
@@ -25,7 +26,7 @@ pub struct TransactionInfo {
2526
pub index: u32,
2627
}
2728

28-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
29+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
2930
pub struct BlockInfo {
3031
/// The height of a block is the number of blocks preceding it in the blockchain.
3132
pub height: u64,

0 commit comments

Comments
 (0)