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