prov_cosmwasm_std/
types.rs

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