diff --git a/packages/std/Cargo.toml b/packages/std/Cargo.toml index b918e127ef..b6a39fd838 100644 --- a/packages/std/Cargo.toml +++ b/packages/std/Cargo.toml @@ -12,7 +12,7 @@ license = "Apache-2.0" readme = "README.md" [package.metadata.docs.rs] -features = ["stargate", "staking", "ibc3"] +features = ["stargate", "staking", "ibc3", "random"] [features] default = [] @@ -40,6 +40,9 @@ ibc3 = ["stargate"] # the host blockchain to run CosmWasm `1.1.0` or higher. cosmwasm_1_1 = [] +# Add random seed to env in inits and executes +random = [] + [dependencies] base64 = "0.13.0" cosmwasm-derive = { path = "../derive", version = "1.1.9" } diff --git a/packages/std/src/exports.rs b/packages/std/src/exports.rs index a2c986806f..86b075409f 100644 --- a/packages/std/src/exports.rs +++ b/packages/std/src/exports.rs @@ -29,6 +29,10 @@ use crate::serde::{from_slice, to_vec}; use crate::types::Env; use crate::{CustomMsg, Deps, DepsMut, MessageInfo}; +#[cfg(feature = "random")] +#[no_mangle] +extern "C" fn requires_random() -> () {} + #[cfg(feature = "iterator")] #[no_mangle] extern "C" fn requires_iterator() -> () {} diff --git a/packages/std/src/testing/mock.rs b/packages/std/src/testing/mock.rs index feca7fa89a..2aa611d2d6 100644 --- a/packages/std/src/testing/mock.rs +++ b/packages/std/src/testing/mock.rs @@ -248,6 +248,10 @@ pub fn mock_env() -> Env { height: 12_345, time: Timestamp::from_nanos(1_571_797_419_879_305_533), chain_id: "cosmos-testnet-14002".to_string(), + #[cfg(feature = "random")] + random: Some( + Binary::from_base64("wLsKdf/sYqvSMI0G0aWRjob25mrIB0VQVjTjDXnDafk=").unwrap(), + ), }, transaction: Some(TransactionInfo { index: 3 }), contract: ContractInfo { diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index e97accfd17..4af1b54894 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -1,4 +1,8 @@ use serde::{de::DeserializeOwned, Serialize}; + +#[cfg(feature = "random")] +use serde::Deserialize; + use std::marker::PhantomData; use std::ops::Deref; diff --git a/packages/std/src/types.rs b/packages/std/src/types.rs index ff3a3394c6..936e84d85c 100644 --- a/packages/std/src/types.rs +++ b/packages/std/src/types.rs @@ -5,6 +5,9 @@ use crate::addresses::Addr; use crate::coin::Coin; use crate::timestamp::Timestamp; +#[cfg(feature = "random")] +use crate::Binary; + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct Env { pub block: BlockInfo, @@ -46,7 +49,7 @@ pub struct BlockInfo { /// # height: 12_345, /// # time: Timestamp::from_nanos(1_571_797_419_879_305_533), /// # chain_id: "cosmos-testnet-14002".to_string(), - /// # }, + /// # }, /// # transaction: Some(TransactionInfo { index: 3 }), /// # contract: ContractInfo { /// # address: Addr::unchecked("contract"), @@ -55,6 +58,7 @@ pub struct BlockInfo { /// # }; /// # extern crate chrono; /// use chrono::NaiveDateTime; + /// use cosmwasm_std::Binary; /// let seconds = env.block.time.seconds(); /// let nsecs = env.block.time.subsec_nanos(); /// let dt = NaiveDateTime::from_timestamp(seconds as i64, nsecs as u32); @@ -63,6 +67,7 @@ pub struct BlockInfo { /// Creating a simple millisecond-precision timestamp (as used in JavaScript): /// /// ``` + /// # use cosmwasm_std::Binary; /// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo}; /// # let env = Env { /// # block: BlockInfo { @@ -80,6 +85,9 @@ pub struct BlockInfo { /// ``` pub time: Timestamp, pub chain_id: String, + #[cfg(feature = "random")] + #[serde(skip_serializing_if = "Option::is_none")] + pub random: Option, } /// Additional information from [MsgInstantiateContract] and [MsgExecuteContract], which is passed