stratus/eth/primitives/
transaction_stage.rs

1use super::Address;
2use super::BlockNumber;
3use super::ExecutionResult;
4use super::Index;
5use crate::alias::AlloyReceipt;
6use crate::alias::AlloyTransaction;
7use crate::alias::JsonValue;
8use crate::eth::primitives::TransactionExecution;
9use crate::eth::primitives::TransactionMined;
10use crate::ext::to_json_value;
11
12/// Stages that a transaction can be in.
13#[allow(clippy::large_enum_variant)]
14#[derive(Debug, Clone, derive_new::new)]
15pub enum TransactionStage {
16    /// Transaction was executed, but is awaiting to be mined to a block.
17    Executed(TransactionExecution),
18
19    /// Transaction that was added to a mined block.
20    Mined(TransactionMined),
21}
22
23impl TransactionStage {
24    /// Serializes itself to JSON-RPC transaction format.
25    pub fn to_json_rpc_transaction(self) -> JsonValue {
26        match self {
27            TransactionStage::Executed(tx) => {
28                let json_rpc_payload: AlloyTransaction = tx.input.into();
29                to_json_value(json_rpc_payload)
30            }
31            TransactionStage::Mined(tx) => {
32                let json_rpc_payload: AlloyTransaction = tx.into();
33                to_json_value(json_rpc_payload)
34            }
35        }
36    }
37
38    /// Serializes itself to JSON-RPC receipt format.
39    pub fn to_json_rpc_receipt(self) -> JsonValue {
40        match self {
41            TransactionStage::Executed(_) => JsonValue::Null,
42            TransactionStage::Mined(tx) => {
43                let json_rpc_format: AlloyReceipt = tx.into();
44                to_json_value(json_rpc_format)
45            }
46        }
47    }
48
49    pub fn deployed_contract_address(&self) -> Option<Address> {
50        match self {
51            Self::Executed(tx) => tx.result.execution.deployed_contract_address,
52            Self::Mined(tx) => tx.deployed_contract_address,
53        }
54    }
55
56    pub fn result(&self) -> &ExecutionResult {
57        match self {
58            Self::Executed(tx) => &tx.result.execution.result,
59            Self::Mined(tx) => &tx.result,
60        }
61    }
62
63    pub fn index(&self) -> Option<Index> {
64        match self {
65            Self::Mined(tx) => Some(tx.transaction_index),
66            _ => None,
67        }
68    }
69
70    pub fn block_number(&self) -> BlockNumber {
71        match self {
72            Self::Executed(tx) => tx.evm_input.block_number,
73            Self::Mined(tx) => tx.block_number,
74        }
75    }
76
77    pub fn from(&self) -> Address {
78        match self {
79            Self::Executed(tx) => tx.evm_input.from,
80            Self::Mined(tx) => tx.input.signer,
81        }
82    }
83
84    pub fn to(&self) -> Option<Address> {
85        match self {
86            Self::Executed(tx) => tx.evm_input.to,
87            Self::Mined(tx) => tx.input.to,
88        }
89    }
90}