cosmwasm_std/
types.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use crate::coin::Coin;
5use crate::prelude::*;
6use crate::Binary;
7use crate::{Addr, Timestamp};
8
9use crate::utils::impl_hidden_constructor;
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)]
22#[non_exhaustive]
23pub struct TransactionInfo {
24    /// The position of this transaction in the block. The first
25    /// transaction has index 0.
26    ///
27    /// This allows you to get a unique transaction identifier in this chain
28    /// using the pair (`env.block.height`, `env.transaction.index`).
29    ///
30    pub index: u32,
31
32    /// Hash of the transaction.
33    ///
34    /// If the blockchain's CosmWasm version is below 3.0, this field
35    /// will default to being empty.
36    #[serde(default)]
37    pub hash: Binary,
38}
39
40impl_hidden_constructor!(TransactionInfo, index: u32, hash: Binary);
41
42#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
43pub struct BlockInfo {
44    /// The height of a block is the number of blocks preceding it in the blockchain.
45    pub height: u64,
46    /// Absolute time of the block creation in seconds since the UNIX epoch (00:00:00 on 1970-01-01 UTC).
47    ///
48    /// The source of this is the [BFT Time in Tendermint](https://github.com/tendermint/tendermint/blob/58dc1726/spec/consensus/bft-time.md),
49    /// which has the same nanosecond precision as the `Timestamp` type.
50    ///
51    /// # Examples
52    ///
53    /// Using chrono:
54    ///
55    /// ```
56    /// # use cosmwasm_std::{Addr, Binary, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo};
57    /// # let env = Env {
58    /// #     block: BlockInfo {
59    /// #         height: 12_345,
60    /// #         time: Timestamp::from_nanos(1_571_797_419_879_305_533),
61    /// #         chain_id: "cosmos-testnet-14002".to_string(),
62    /// #     },
63    /// #     transaction: Some(TransactionInfo::new(3, Binary::from_hex("E5469DACEC17CEF8A260FD37675ED87E7FB6A2B5AD95193C51308006C7E494B3").unwrap())),
64    /// #     contract: ContractInfo {
65    /// #         address: Addr::unchecked("contract"),
66    /// #     },
67    /// # };
68    /// # extern crate chrono;
69    /// use chrono::NaiveDateTime;
70    /// let seconds = env.block.time.seconds();
71    /// let nsecs = env.block.time.subsec_nanos();
72    /// let dt = NaiveDateTime::from_timestamp(seconds as i64, nsecs as u32);
73    /// ```
74    ///
75    /// Creating a simple millisecond-precision timestamp (as used in JavaScript):
76    ///
77    /// ```
78    /// # use cosmwasm_std::{Addr, Binary, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo};
79    /// # let env = Env {
80    /// #     block: BlockInfo {
81    /// #         height: 12_345,
82    /// #         time: Timestamp::from_nanos(1_571_797_419_879_305_533),
83    /// #         chain_id: "cosmos-testnet-14002".to_string(),
84    /// #     },
85    /// #     transaction: Some(TransactionInfo::new(3, Binary::from_hex("E5469DACEC17CEF8A260FD37675ED87E7FB6A2B5AD95193C51308006C7E494B3").unwrap())),
86    /// #     contract: ContractInfo {
87    /// #         address: Addr::unchecked("contract"),
88    /// #     },
89    /// # };
90    /// let millis = env.block.time.nanos() / 1_000_000;
91    /// ```
92    pub time: Timestamp,
93    pub chain_id: String,
94}
95
96/// Additional information from [MsgInstantiateContract] and [MsgExecuteContract], which is passed
97/// along with the contract execution message into the `instantiate` and `execute` entry points.
98///
99/// It contains the essential info for authorization - identity of the call, and payment.
100///
101/// [MsgInstantiateContract]: https://github.com/CosmWasm/wasmd/blob/v0.15.0/x/wasm/internal/types/tx.proto#L47-L61
102/// [MsgExecuteContract]: https://github.com/CosmWasm/wasmd/blob/v0.15.0/x/wasm/internal/types/tx.proto#L68-L78
103#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
104pub struct MessageInfo {
105    /// The `sender` field from `MsgInstantiateContract` and `MsgExecuteContract`.
106    /// You can think of this as the address that initiated the action (i.e. the message). What that
107    /// means exactly heavily depends on the application.
108    ///
109    /// The x/wasm module ensures that the sender address signed the transaction or
110    /// is otherwise authorized to send the message.
111    ///
112    /// Additional signers of the transaction that are either needed for other messages or contain unnecessary
113    /// signatures are not propagated into the contract.
114    pub sender: Addr,
115    /// The funds that are sent to the contract as part of `MsgInstantiateContract`
116    /// or `MsgExecuteContract`. The transfer is processed in bank before the contract
117    /// is executed such that the new balance is visible during contract execution.
118    pub funds: Vec<Coin>,
119}
120
121#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
122pub struct ContractInfo {
123    pub address: Addr,
124}
125
126/// The structure contains additional information related to the
127/// contract's migration procedure - the sender address and
128/// the contract's migrate version currently stored on the blockchain.
129/// The `old_migrate_version` is optional, since there is no guarantee
130/// that the currently stored contract's binary contains that information.
131#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
132pub struct MigrateInfo {
133    /// Address of the sender.
134    ///
135    /// This is the `sender` field from [`MsgMigrateContract`](https://github.com/CosmWasm/wasmd/blob/v0.53.0/proto/cosmwasm/wasm/v1/tx.proto#L217-L233).
136    pub sender: Addr,
137    /// Migrate version of the previous contract. It's optional, since
138    /// adding the version number to the binary is not a mandatory feature.
139    pub old_migrate_version: Option<u64>,
140}