Skip to main content

tycho_execution/encoding/evm/
encoder_builders.rs

1use tycho_common::{models::Chain, Bytes};
2
3use crate::encoding::{
4    errors::EncodingError,
5    evm::{
6        constants::get_router_address,
7        swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
8        tycho_encoders::{TychoExecutorEncoder, TychoRouterEncoder},
9    },
10    tycho_encoder::TychoEncoder,
11};
12
13/// Builder pattern for constructing a `TychoRouterEncoder` with customizable options.
14///
15/// This struct allows setting a chain and strategy encoder before building the final encoder.
16pub struct TychoRouterEncoderBuilder {
17    chain: Option<Chain>,
18    swap_encoder_registry: Option<SwapEncoderRegistry>,
19    router_address: Option<Bytes>,
20}
21
22impl Default for TychoRouterEncoderBuilder {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28impl TychoRouterEncoderBuilder {
29    pub fn new() -> Self {
30        TychoRouterEncoderBuilder { chain: None, swap_encoder_registry: None, router_address: None }
31    }
32    pub fn chain(mut self, chain: Chain) -> Self {
33        self.chain = Some(chain);
34        self
35    }
36
37    pub fn swap_encoder_registry(mut self, swap_encoder_registry: SwapEncoderRegistry) -> Self {
38        self.swap_encoder_registry = Some(swap_encoder_registry);
39        self
40    }
41
42    /// Sets the `router_address` manually.
43    /// If it's not set, the default router address will be used (config/router_addresses.json)
44    pub fn router_address(mut self, router_address: Bytes) -> Self {
45        self.router_address = Some(router_address);
46        self
47    }
48
49    /// Builds the `TychoRouterEncoder` instance using the configured chain.
50    /// Returns an error if either the chain has not been set.
51    pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> {
52        if let (Some(chain), Some(swap_encoder_registry)) = (self.chain, self.swap_encoder_registry)
53        {
54            let tycho_router_address = if let Some(address) = self.router_address {
55                address
56            } else {
57                get_router_address(&chain)?.clone()
58            };
59
60            Ok(Box::new(TychoRouterEncoder::new(swap_encoder_registry, tycho_router_address)?))
61        } else {
62            Err(EncodingError::FatalError(
63                "Please set the chain and swap encoder registry before building the encoder"
64                    .to_string(),
65            ))
66        }
67    }
68}
69
70/// Builder pattern for constructing a `TychoExecutorEncoder` with customizable options.
71pub struct TychoExecutorEncoderBuilder {
72    swap_encoder_registry: Option<SwapEncoderRegistry>,
73}
74
75impl Default for TychoExecutorEncoderBuilder {
76    fn default() -> Self {
77        Self::new()
78    }
79}
80
81impl TychoExecutorEncoderBuilder {
82    pub fn new() -> Self {
83        TychoExecutorEncoderBuilder { swap_encoder_registry: None }
84    }
85
86    pub fn swap_encoder_registry(mut self, swap_encoder_registry: SwapEncoderRegistry) -> Self {
87        self.swap_encoder_registry = Some(swap_encoder_registry);
88        self
89    }
90
91    /// Builds the `TychoExecutorEncoder` instance using the configured chain and strategy.
92    /// Returns an error if either the chain or strategy has not been set.
93    pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> {
94        if let Some(swap_encoder_registry) = self.swap_encoder_registry {
95            Ok(Box::new(TychoExecutorEncoder::new(swap_encoder_registry)?))
96        } else {
97            Err(EncodingError::FatalError(
98                "Please set the swap encoder registry before building the encoder".to_string(),
99            ))
100        }
101    }
102}