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