kabu_types_events/
tx_compose.rs

1use crate::{Message, TxState};
2use alloy_primitives::{Address, BlockNumber, Bytes, TxHash, U256};
3use kabu_types_entities::LoomTxSigner;
4use kabu_types_swap::Swap;
5use reth_ethereum_primitives::EthPrimitives;
6use reth_node_types::NodePrimitives;
7use std::sync::Arc;
8
9#[derive(Debug, Clone)]
10pub enum RlpState {
11    Stuffing(Bytes),
12    Backrun(Bytes),
13    None,
14}
15
16impl RlpState {
17    pub fn is_none(&self) -> bool {
18        matches!(self, RlpState::None)
19    }
20
21    pub fn unwrap(&self) -> Bytes {
22        match self.clone() {
23            RlpState::Backrun(val) | RlpState::Stuffing(val) => val,
24            RlpState::None => Bytes::new(),
25        }
26    }
27}
28
29#[derive(Clone, Debug)]
30pub enum TxComposeMessageType<N: NodePrimitives = EthPrimitives> {
31    Sign(TxComposeData<N>),
32    Broadcast(TxComposeData<N>),
33}
34
35#[derive(Clone, Debug)]
36pub struct TxComposeData<N: NodePrimitives = EthPrimitives> {
37    /// The EOA address that will be used to sign the transaction.
38    /// If this is None, the transaction will be signed by a random signer.
39    pub eoa: Option<Address>,
40    pub signer: Option<Arc<dyn LoomTxSigner<N>>>,
41    pub nonce: u64,
42    pub eth_balance: U256,
43    pub value: U256,
44    pub gas: u64,
45    pub priority_gas_fee: u64,
46    pub stuffing_txs_hashes: Vec<TxHash>,
47    pub stuffing_txs: Vec<N::SignedTx>,
48    pub next_block_number: BlockNumber,
49    pub next_block_timestamp: u64,
50    pub next_block_base_fee: u64,
51    pub tx_bundle: Option<Vec<TxState<N>>>,
52    pub rlp_bundle: Option<Vec<RlpState>>,
53    pub origin: Option<String>,
54    pub swap: Option<Swap>,
55    pub tips: Option<U256>,
56}
57
58impl<N: NodePrimitives> Default for TxComposeData<N> {
59    fn default() -> Self {
60        Self {
61            eoa: None,
62            signer: None,
63            nonce: Default::default(),
64            eth_balance: Default::default(),
65            next_block_base_fee: Default::default(),
66            value: Default::default(),
67            gas: Default::default(),
68            priority_gas_fee: Default::default(),
69            stuffing_txs_hashes: Vec::new(),
70            stuffing_txs: Vec::new(),
71            next_block_number: Default::default(),
72            next_block_timestamp: Default::default(),
73            tx_bundle: None,
74            rlp_bundle: None,
75            origin: None,
76            swap: None,
77            tips: None,
78        }
79    }
80}
81
82pub type MessageTxCompose<N = EthPrimitives> = Message<TxComposeMessageType<N>>;
83
84impl<N: NodePrimitives> MessageTxCompose<N> {
85    pub fn sign(data: TxComposeData<N>) -> Self {
86        Message::new(TxComposeMessageType::Sign(data))
87    }
88
89    pub fn broadcast(data: TxComposeData<N>) -> Self {
90        Message::new(TxComposeMessageType::Broadcast(data))
91    }
92}
93
94#[cfg(test)]
95mod test {
96    use super::*;
97
98    #[test]
99    fn test() {
100        let v = [RlpState::Stuffing(Bytes::from(vec![1])), RlpState::Backrun(Bytes::from(vec![2]))];
101
102        let b: Vec<Bytes> = v.iter().filter(|i| matches!(i, RlpState::Backrun(_))).map(|i| i.unwrap()).collect();
103
104        for c in b {
105            println!("{c:?}");
106        }
107    }
108}