Skip to content

Commit

Permalink
Add transaction index to Env
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Sep 8, 2021
1 parent 13bfda8 commit 45ef88c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to

## [Unreleased]

### Added

- cosmwasm-std: New field `Env::transaction` containing info of the transaction
the contract call was executed in.

### Changed

- cosmwasm-std: Make `iterator` a required feature if the `iterator` feature
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub use crate::serde::{from_binary, from_slice, to_binary, to_vec};
pub use crate::storage::MemoryStorage;
pub use crate::timestamp::Timestamp;
pub use crate::traits::{Api, Querier, QuerierResult, QuerierWrapper, Storage};
pub use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo};
pub use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo, TransactionInfo};

// Exposed in wasm build only

Expand Down
3 changes: 2 additions & 1 deletion packages/std/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::serde::{from_slice, to_binary};
use crate::storage::MemoryStorage;
use crate::timestamp::Timestamp;
use crate::traits::{Api, Querier, QuerierResult};
use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo};
use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo, TransactionInfo};
use crate::Attribute;

pub const MOCK_CONTRACT_ADDR: &str = "cosmos2contract";
Expand Down Expand Up @@ -206,6 +206,7 @@ pub fn mock_env() -> Env {
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(MOCK_CONTRACT_ADDR),
},
Expand Down
21 changes: 19 additions & 2 deletions packages/std/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ use crate::timestamp::Timestamp;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Env {
pub block: BlockInfo,
/// Information on the transaction this message was executed in.
/// The field is unset when the `MsgExecuteContract`/`MsgInstantiateContract`/`MsgMigrateContract`
/// is not executed as part of a transaction.
pub transaction: Option<TransactionInfo>,
pub contract: ContractInfo,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct TransactionInfo {
/// The position of this transaction in the block. The first
/// transaction has index 0.
///
/// This allows you to get a unique transaction indentifier in this chain
/// using the pair (`env.block.height`, `env.transaction.index`).
///
pub index: u32,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct BlockInfo {
/// The height of a block is the number of blocks preceding it in the blockchain.
Expand All @@ -24,13 +39,14 @@ pub struct BlockInfo {
/// Using chrono:
///
/// ```
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp};
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo};
/// # let env = Env {
/// # block: 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 @@ -45,13 +61,14 @@ pub struct BlockInfo {
/// Creating a simple millisecond-precision timestamp (as used in JavaScript):
///
/// ```
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp};
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo};
/// # let env = Env {
/// # block: 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 Down
5 changes: 4 additions & 1 deletion packages/vm/src/testing/mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use cosmwasm_std::testing::{digit_sum, riffle_shuffle};
use cosmwasm_std::{Addr, BlockInfo, Coin, ContractInfo, Env, MessageInfo, Timestamp};
use cosmwasm_std::{
Addr, BlockInfo, Coin, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo,
};

use super::querier::MockQuerier;
use super::storage::MockStorage;
Expand Down Expand Up @@ -160,6 +162,7 @@ pub fn mock_env() -> Env {
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(MOCK_CONTRACT_ADDR),
},
Expand Down

0 comments on commit 45ef88c

Please sign in to comment.