kabu_types_events/
tx_compose.rs

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