eth_tx_manager/
transaction.rs

1use ethers::core::types::Bytes;
2use ethers::types::transaction::eip2930::AccessList;
3use ethers::types::{
4    transaction::eip2718::TypedTransaction, Address, Eip1559TransactionRequest, NameOrAddress,
5    TransactionRequest, H256, U256,
6};
7use serde::{Deserialize, Serialize};
8
9use crate::gas_oracle::GasInfo;
10use crate::Chain;
11
12#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
13pub enum Priority {
14    Low,
15    Normal,
16    High,
17    ASAP,
18}
19
20#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
21pub struct Transaction {
22    pub from: Address,
23    pub to: Address,
24    pub value: Value,
25    pub call_data: Option<Bytes>, // smart contract payload
26}
27
28#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
29pub enum Value {
30    Number(U256),
31    Nothing,
32    // All,
33}
34
35impl From<Value> for U256 {
36    fn from(value: Value) -> Self {
37        match value {
38            Value::Number(v) => v,
39            Value::Nothing => 0.into(),
40        }
41    }
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
45pub struct StaticTxData {
46    /// Nonce of the current transaction.
47    pub nonce: U256,
48
49    /// Information about the transaction being currently processed.
50    pub transaction: Transaction,
51
52    pub confirmations: usize,
53    pub priority: Priority,
54}
55
56impl StaticTxData {
57    pub fn to_typed_transaction(&self, chain: &Chain, gas_info: GasInfo) -> TypedTransaction {
58        let from = Some(self.transaction.from);
59        let to = Some(NameOrAddress::Address(self.transaction.to));
60        let value = Some(self.transaction.value.into());
61        let data = self.transaction.call_data.clone();
62        let nonce = Some(self.nonce);
63        let chain_id = Some(chain.id.into());
64
65        match gas_info {
66            GasInfo::Legacy(legacy_gas_info) => {
67                TypedTransaction::Legacy(TransactionRequest {
68                    from,
69                    to,
70                    gas: None, // must be set after
71                    gas_price: Some(legacy_gas_info.gas_price),
72                    value,
73                    data,
74                    nonce,
75                    chain_id,
76                })
77            }
78            GasInfo::EIP1559(eip1559_gas_info) => {
79                TypedTransaction::Eip1559(Eip1559TransactionRequest {
80                    from,
81                    to,
82                    gas: None, // must be set after
83                    value,
84                    data,
85                    nonce,
86                    access_list: AccessList::default(),
87                    // max_priority_fee must be set (guaranteed by get_gas_oracle_info)
88                    max_priority_fee_per_gas: Some(eip1559_gas_info.max_priority_fee.unwrap()),
89                    max_fee_per_gas: Some(eip1559_gas_info.max_fee),
90                    chain_id,
91                })
92            }
93        }
94    }
95}
96
97#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
98pub struct SubmittedTxs {
99    /// Hashes of the pending transactions sent to the transaction pool.
100    pub txs_hashes: Vec<H256>,
101}
102
103impl<'a> IntoIterator for &'a SubmittedTxs {
104    type Item = &'a H256;
105    type IntoIter = std::slice::Iter<'a, H256>;
106
107    fn into_iter(self) -> Self::IntoIter {
108        self.txs_hashes.iter()
109    }
110}
111
112impl SubmittedTxs {
113    pub fn new() -> Self {
114        Self {
115            txs_hashes: Vec::new(),
116        }
117    }
118
119    pub fn contains(&mut self, hash: H256) -> bool {
120        self.txs_hashes.contains(&hash)
121    }
122
123    pub fn add(&mut self, hash: H256) {
124        self.txs_hashes.push(hash);
125    }
126
127    pub fn len(&self) -> usize {
128        self.txs_hashes.len()
129    }
130
131    pub fn is_empty(&self) -> bool {
132        self.txs_hashes.is_empty()
133    }
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
137pub struct PersistentState {
138    /// Information about the transaction being currently processed.
139    pub tx_data: StaticTxData,
140
141    /// Hashes of the pending transactions sent to the transaction pool.
142    pub submitted_txs: SubmittedTxs,
143}