Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random yo #16

Merged
merged 11 commits into from
Apr 16, 2023
5 changes: 4 additions & 1 deletion packages/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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" }
Expand Down
4 changes: 4 additions & 0 deletions packages/std/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() -> () {}
Expand Down
4 changes: 4 additions & 0 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions packages/std/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use serde::{de::DeserializeOwned, Serialize};

#[cfg(feature = "random")]
use serde::Deserialize;

use std::marker::PhantomData;
use std::ops::Deref;

Expand Down
10 changes: 9 additions & 1 deletion packages/std/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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<Binary>,
}

/// Additional information from [MsgInstantiateContract] and [MsgExecuteContract], which is passed
Expand Down