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(
61                chain,
62                swap_encoder_registry,
63                tycho_router_address,
64            )?))
65        } else {
66            Err(EncodingError::FatalError(
67                "Please set the chain and swap encoder registry before building the encoder"
68                    .to_string(),
69            ))
70        }
71    }
72}
73
74/// Builder pattern for constructing a `TychoExecutorEncoder` with customizable options.
75pub struct TychoExecutorEncoderBuilder {
76    swap_encoder_registry: Option<SwapEncoderRegistry>,
77}
78
79impl Default for TychoExecutorEncoderBuilder {
80    fn default() -> Self {
81        Self::new()
82    }
83}
84
85impl TychoExecutorEncoderBuilder {
86    pub fn new() -> Self {
87        TychoExecutorEncoderBuilder { swap_encoder_registry: None }
88    }
89
90    pub fn swap_encoder_registry(mut self, swap_encoder_registry: SwapEncoderRegistry) -> Self {
91        self.swap_encoder_registry = Some(swap_encoder_registry);
92        self
93    }
94
95    /// Builds the `TychoExecutorEncoder` instance using the configured chain and strategy.
96    /// Returns an error if either the chain or strategy has not been set.
97    pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> {
98        if let Some(swap_encoder_registry) = self.swap_encoder_registry {
99            Ok(Box::new(TychoExecutorEncoder::new(swap_encoder_registry)?))
100        } else {
101            Err(EncodingError::FatalError(
102                "Please set the swap encoder registry before building the encoder".to_string(),
103            ))
104        }
105    }
106}