@@ -10,7 +10,9 @@ use super::storage::MockStorage;
10
10
use crate :: backend:: unwrap_or_return_with_gas;
11
11
use crate :: { Backend , BackendApi , BackendError , BackendResult , GasInfo } ;
12
12
13
- pub const MOCK_CONTRACT_ADDR : & str = "cosmwasmcontract" ; // TODO: use correct address
13
+ pub const MOCK_CONTRACT_ADDR : & str =
14
+ "cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs" ;
15
+
14
16
/// Default gas multiplier in wasmd.
15
17
/// See https://github.com/CosmWasm/wasmd/blob/v0.51.0/x/wasm/types/gas_register.go#L34
16
18
const WASMD_GAS_MULTIPLIER : u64 = 140_000 ;
@@ -216,12 +218,62 @@ fn validate_length(bytes: &[u8]) -> Result<(), BackendError> {
216
218
}
217
219
}
218
220
219
- /// Returns a default environment with height, time, chain_id, and contract address
221
+ /// Returns a default environment with height, time, chain_id, and contract address.
220
222
/// You can submit as is to most contracts, or modify height/time if you want to
221
223
/// test for expiration.
222
224
///
223
225
/// This is intended for use in test code only.
226
+ ///
227
+ /// The contract address uses the same bech32 prefix as [`MockApi`](crate::testing::MockApi). While
228
+ /// this is good for the majority of users, you might need to create your `Env`s
229
+ /// differently if you need a valid address using a different prefix.
230
+ ///
231
+ /// ## Examples
232
+ ///
233
+ /// Create an env:
234
+ ///
235
+ /// ```
236
+ /// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, Timestamp, TransactionInfo};
237
+ /// use cosmwasm_vm::testing::mock_env;
238
+ ///
239
+ /// let env = mock_env();
240
+ /// assert_eq!(env, Env {
241
+ /// block: BlockInfo {
242
+ /// height: 12_345,
243
+ /// time: Timestamp::from_nanos(1_571_797_419_879_305_533),
244
+ /// chain_id: "cosmos-testnet-14002".to_string(),
245
+ /// },
246
+ /// transaction: Some(TransactionInfo { index: 3 }),
247
+ /// contract: ContractInfo {
248
+ /// address: Addr::unchecked("cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs"),
249
+ /// },
250
+ /// });
251
+ /// ```
252
+ ///
253
+ /// Mutate and reuse environment:
254
+ ///
255
+ /// ```
256
+ /// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, Timestamp, TransactionInfo};
257
+ /// use cosmwasm_vm::testing::mock_env;
258
+ ///
259
+ /// let env1 = mock_env();
260
+ ///
261
+ /// // First test with `env1`
262
+ ///
263
+ /// let mut env2 = env1.clone();
264
+ /// env2.block.height += 1;
265
+ /// env2.block.time = env1.block.time.plus_seconds(6);
266
+ ///
267
+ /// // `env2` is one block and 6 seconds later
268
+ ///
269
+ /// let mut env3 = env2.clone();
270
+ /// env3.block.height += 1;
271
+ /// env3.block.time = env2.block.time.plus_nanos(5_500_000_000);
272
+ ///
273
+ /// // `env3` is one block and 5.5 seconds later
274
+ /// ```
224
275
pub fn mock_env ( ) -> Env {
276
+ let contract_addr = MockApi :: default ( ) . addr_make ( "cosmos2contract" ) ;
225
277
Env {
226
278
block : BlockInfo {
227
279
height : 12_345 ,
@@ -230,7 +282,7 @@ pub fn mock_env() -> Env {
230
282
} ,
231
283
transaction : Some ( TransactionInfo { index : 3 } ) ,
232
284
contract : ContractInfo {
233
- address : Addr :: unchecked ( MOCK_CONTRACT_ADDR ) ,
285
+ address : Addr :: unchecked ( contract_addr ) ,
234
286
} ,
235
287
}
236
288
}
@@ -249,6 +301,12 @@ mod tests {
249
301
use super :: * ;
250
302
use cosmwasm_std:: coins;
251
303
304
+ #[ test]
305
+ fn mock_env_matches_mock_contract_addr ( ) {
306
+ let contract_address = mock_env ( ) . contract . address ;
307
+ assert_eq ! ( contract_address, Addr :: unchecked( MOCK_CONTRACT_ADDR ) ) ;
308
+ }
309
+
252
310
#[ test]
253
311
fn mock_info_works ( ) {
254
312
let info = mock_info ( "my name" , & coins ( 100 , "atom" ) ) ;
0 commit comments