secret_cosmwasm_std/types.rs
1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use crate::addresses::Addr;
5use crate::coin::Coin;
6use crate::timestamp::Timestamp;
7
8#[cfg(feature = "random")]
9use crate::Binary;
10
11#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
12pub struct Env {
13 pub block: BlockInfo,
14 /// Information on the transaction this message was executed in.
15 /// The field is unset when the `MsgExecuteContract`/`MsgInstantiateContract`/`MsgMigrateContract`
16 /// is not executed as part of a transaction.
17 pub transaction: Option<TransactionInfo>,
18 pub contract: ContractInfo,
19}
20
21#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
22pub struct TransactionInfo {
23 /// The position of this transaction in the block. The first
24 /// transaction has index 0.
25 ///
26 /// This allows you to get a unique transaction indentifier in this chain
27 /// using the pair (`env.block.height`, `env.transaction.index`).
28 ///
29 pub index: u32,
30 #[serde(default)]
31 /// The hash of the current transaction bytes.
32 /// aka txhash or transaction_id
33 /// hash = sha256(tx_bytes)
34 pub hash: String,
35}
36
37#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
38pub struct BlockInfo {
39 /// The height of a block is the number of blocks preceding it in the blockchain.
40 pub height: u64,
41 /// Absolute time of the block creation in seconds since the UNIX epoch (00:00:00 on 1970-01-01 UTC).
42 ///
43 /// The source of this is the [BFT Time in Tendermint](https://github.com/tendermint/tendermint/blob/58dc1726/spec/consensus/bft-time.md),
44 /// which has the same nanosecond precision as the `Timestamp` type.
45 ///
46 /// # Examples
47 ///
48 /// Using chrono:
49 ///
50 /// ```
51 /// # use secret_cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo};
52 /// # let env = Env {
53 /// # block: BlockInfo {
54 /// # height: 12_345,
55 /// # time: Timestamp::from_nanos(1_571_797_419_879_305_533),
56 /// # chain_id: "cosmos-testnet-14002".to_string(),
57 /// # },
58 /// # transaction: Some(TransactionInfo { index: 3, hash: "".to_string() }),
59 /// # contract: ContractInfo {
60 /// # address: Addr::unchecked("contract"),
61 /// # code_hash: "".to_string()
62 /// # },
63 /// # };
64 /// # extern crate chrono;
65 /// use chrono::NaiveDateTime;
66 /// use secret_cosmwasm_std::Binary;
67 /// let seconds = env.block.time.seconds();
68 /// let nsecs = env.block.time.subsec_nanos();
69 /// let dt = NaiveDateTime::from_timestamp(seconds as i64, nsecs as u32);
70 /// ```
71 ///
72 /// Creating a simple millisecond-precision timestamp (as used in JavaScript):
73 ///
74 /// ```
75 /// # use secret_cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo};
76 /// # use secret_cosmwasm_std::Binary;
77 /// # let env = Env {
78 /// # block: BlockInfo {
79 /// # height: 12_345,
80 /// # time: Timestamp::from_nanos(1_571_797_419_879_305_533),
81 /// # chain_id: "cosmos-testnet-14002".to_string(),
82 /// # },
83 /// # transaction: Some(TransactionInfo { index: 3, hash: "".to_string() }),
84 /// # contract: ContractInfo {
85 /// # address: Addr::unchecked("contract"),
86 /// # code_hash: "".to_string()
87 /// },
88 /// # };
89 /// let millis = env.block.time.nanos() / 1_000_000;
90 /// ```
91 pub time: Timestamp,
92 pub chain_id: String,
93 #[cfg(feature = "random")]
94 #[serde(skip_serializing_if = "Option::is_none")]
95 pub random: Option<Binary>,
96}
97
98/// Additional information from [MsgInstantiateContract] and [MsgExecuteContract], which is passed
99/// along with the contract execution message into the `instantiate` and `execute` entry points.
100///
101/// It contains the essential info for authorization - identity of the call, and payment.
102///
103/// [MsgInstantiateContract]: https://github.com/CosmWasm/wasmd/blob/v0.15.0/x/wasm/internal/types/tx.proto#L47-L61
104/// [MsgExecuteContract]: https://github.com/CosmWasm/wasmd/blob/v0.15.0/x/wasm/internal/types/tx.proto#L68-L78
105#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
106pub struct MessageInfo {
107 /// The `sender` field from `MsgInstantiateContract` and `MsgExecuteContract`.
108 /// You can think of this as the address that initiated the action (i.e. the message). What that
109 /// means exactly heavily depends on the application.
110 ///
111 /// The x/wasm module ensures that the sender address signed the transaction or
112 /// is otherwise authorized to send the message.
113 ///
114 /// Additional signers of the transaction that are either needed for other messages or contain unnecessary
115 /// signatures are not propagated into the contract.
116 pub sender: Addr,
117 /// The funds that are sent to the contract as part of `MsgInstantiateContract`
118 /// or `MsgExecuteContract`. The transfer is processed in bank before the contract
119 /// is executed such that the new balance is visible during contract execution.
120 pub funds: Vec<Coin>,
121}
122
123#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
124pub struct ContractInfo {
125 pub address: Addr,
126 #[serde(default)]
127 pub code_hash: String,
128}