kabu_types_events/
best_tx_compose.rs1use crate::SwapComposeData;
2use alloy_primitives::U256;
3use reth_ethereum_primitives::EthPrimitives;
4use reth_node_types::NodePrimitives;
5
6#[derive(Default)]
7pub struct BestTxSwapCompose<DB, N: NodePrimitives = EthPrimitives> {
8 validity_pct: Option<U256>,
9 best_profit_swap: Option<SwapComposeData<DB, N>>,
10 best_profit_gas_ratio_swap: Option<SwapComposeData<DB, N>>,
11 best_tips_swap: Option<SwapComposeData<DB, N>>,
12 best_tips_gas_ratio_swap: Option<SwapComposeData<DB, N>>,
13}
14
15impl<DB: Clone + Default + 'static, N: NodePrimitives> BestTxSwapCompose<DB, N> {
16 pub fn new_with_pct<T: Into<U256>>(validity_pct: T) -> Self {
17 BestTxSwapCompose {
18 validity_pct: Some(validity_pct.into()),
19 best_profit_swap: None,
20 best_profit_gas_ratio_swap: None,
21 best_tips_swap: None,
22 best_tips_gas_ratio_swap: None,
23 }
24 }
25
26 pub fn check(&mut self, request: &SwapComposeData<DB, N>) -> bool {
27 let mut is_ok = false;
28
29 match &self.best_profit_swap {
30 None => {
31 self.best_profit_swap = Some(request.clone());
32 is_ok = true;
33 }
34 Some(best_swap) => {
35 if best_swap.swap.arb_profit_eth() < request.swap.arb_profit_eth() {
36 self.best_profit_swap = Some(request.clone());
37 is_ok = true;
38 } else if let Some(pct) = self.validity_pct
39 && (best_swap.swap.arb_profit_eth() * pct) / U256::from(10000) < request.swap.arb_profit_eth()
40 {
41 is_ok = true
42 }
43 }
44 }
45
46 if !is_ok && request.tips.is_some() {
47 match &self.best_tips_swap {
48 Some(best_swap) => {
49 if best_swap.tips.unwrap_or_default() < request.tips.unwrap_or_default() {
50 self.best_tips_swap = Some(request.clone());
51 is_ok = true;
52 } else if let Some(pct) = self.validity_pct
53 && (best_swap.tips.unwrap_or_default() * pct) / U256::from(10000) < request.tips.unwrap_or_default()
54 {
55 is_ok = true
56 }
57 }
58 None => {
59 self.best_tips_swap = Some(request.clone());
60 is_ok = true;
61 }
62 }
63 }
64
65 if !is_ok && request.tx_compose.gas != 0 {
66 match &self.best_tips_gas_ratio_swap {
67 Some(best_swap) => {
68 if best_swap.tips_gas_ratio() < request.tips_gas_ratio() {
69 self.best_tips_gas_ratio_swap = Some(request.clone());
70 is_ok = true;
71 } else if let Some(pct) = self.validity_pct
72 && (best_swap.tips_gas_ratio() * pct) / U256::from(10000) < request.tips_gas_ratio()
73 {
74 is_ok = true
75 }
76 }
77 None => {
78 self.best_tips_gas_ratio_swap = Some(request.clone());
79 is_ok = true;
80 }
81 }
82
83 match &self.best_profit_gas_ratio_swap {
84 Some(best_swap) => {
85 if best_swap.profit_eth_gas_ratio() < request.profit_eth_gas_ratio() {
86 self.best_profit_gas_ratio_swap = Some(request.clone());
87 is_ok = true;
88 } else if let Some(pct) = self.validity_pct
89 && (best_swap.profit_eth_gas_ratio() * pct) / U256::from(10000) < request.profit_eth_gas_ratio()
90 {
91 is_ok = true
92 }
93 }
94 None => {
95 self.best_profit_gas_ratio_swap = Some(request.clone());
96 is_ok = true;
97 }
98 }
99 }
100 is_ok
101 }
102}