Skip to main content

tycho_execution/encoding/evm/
encoder_builders.rs

1use std::{collections::HashMap, str::FromStr};
2
3use alloy::{primitives::B256, signers::local::PrivateKeySigner};
4use tycho_common::{models::Chain, Bytes};
5
6use crate::encoding::{
7    errors::EncodingError,
8    evm::{
9        constants::DEFAULT_ROUTERS_JSON,
10        swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
11        tycho_encoders::{TychoExecutorEncoder, TychoRouterEncoder},
12    },
13    models::UserTransferType,
14    tycho_encoder::TychoEncoder,
15};
16
17/// Builder pattern for constructing a `TychoRouterEncoder` with customizable options.
18///
19/// This struct allows setting a chain and strategy encoder before building the final encoder.
20pub struct TychoRouterEncoderBuilder {
21    chain: Option<Chain>,
22    user_transfer_type: Option<UserTransferType>,
23    swap_encoder_registry: Option<SwapEncoderRegistry>,
24    router_address: Option<Bytes>,
25    swapper_pk: Option<String>,
26    historical_trade: bool,
27}
28
29impl Default for TychoRouterEncoderBuilder {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl TychoRouterEncoderBuilder {
36    pub fn new() -> Self {
37        TychoRouterEncoderBuilder {
38            chain: None,
39            swap_encoder_registry: None,
40            router_address: None,
41            swapper_pk: None,
42            user_transfer_type: None,
43            historical_trade: false,
44        }
45    }
46    pub fn chain(mut self, chain: Chain) -> Self {
47        self.chain = Some(chain);
48        self
49    }
50
51    pub fn user_transfer_type(mut self, user_transfer_type: UserTransferType) -> Self {
52        self.user_transfer_type = Some(user_transfer_type);
53        self
54    }
55
56    pub fn swap_encoder_registry(mut self, swap_encoder_registry: SwapEncoderRegistry) -> Self {
57        self.swap_encoder_registry = Some(swap_encoder_registry);
58        self
59    }
60
61    /// Sets the `router_address` manually.
62    /// If it's not set, the default router address will be used (config/router_addresses.json)
63    pub fn router_address(mut self, router_address: Bytes) -> Self {
64        self.router_address = Some(router_address);
65        self
66    }
67
68    /// Sets the `historical_trade` manually to true.
69    /// If set to true, it means that the encoded trade will be used in an historical block (as a
70    /// test) and not in the current one. This is relevant for checking token approvals in some
71    /// protocols (like Balancer v2).
72    pub fn historical_trade(mut self) -> Self {
73        self.historical_trade = true;
74        self
75    }
76
77    /// Sets the `swapper_pk` for the encoder. This is used to sign permit2 objects. This is only
78    /// needed if you intend to get the full calldata for the transfer. We do not recommend
79    /// using this option, you should sign and create the function calldata entirely on your
80    /// own.
81    #[deprecated(
82        note = "This is deprecated and will be removed in the future. You should sign and create the function calldata on your own."
83    )]
84    pub fn swapper_pk(mut self, swapper_pk: String) -> Self {
85        self.swapper_pk = Some(swapper_pk);
86        self
87    }
88
89    /// Builds the `TychoRouterEncoder` instance using the configured chain.
90    /// Returns an error if either the chain has not been set.
91    pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> {
92        if let (Some(chain), Some(user_transfer_type), Some(swap_encoder_registry)) =
93            (self.chain, self.user_transfer_type, self.swap_encoder_registry)
94        {
95            let tycho_router_address;
96            if let Some(address) = self.router_address {
97                tycho_router_address = address;
98            } else {
99                let default_routers: HashMap<Chain, Bytes> =
100                    serde_json::from_str(DEFAULT_ROUTERS_JSON)?;
101                tycho_router_address = default_routers
102                    .get(&chain)
103                    .ok_or(EncodingError::FatalError(
104                        "No default router address found for chain".to_string(),
105                    ))?
106                    .to_owned();
107            }
108
109            let signer = if let Some(pk) = self.swapper_pk {
110                let pk = B256::from_str(&pk).map_err(|_| {
111                    EncodingError::FatalError("Invalid swapper private key provided".to_string())
112                })?;
113                Some(PrivateKeySigner::from_bytes(&pk).map_err(|_| {
114                    EncodingError::FatalError("Failed to create signer".to_string())
115                })?)
116            } else {
117                None
118            };
119
120            Ok(Box::new(TychoRouterEncoder::new(
121                chain,
122                swap_encoder_registry,
123                tycho_router_address,
124                user_transfer_type,
125                signer,
126                self.historical_trade,
127            )?))
128        } else {
129            Err(EncodingError::FatalError(
130                "Please set the chain, user transfer type and swap encoder registry before building the encoder"
131                    .to_string(),
132            ))
133        }
134    }
135}
136
137/// Builder pattern for constructing a `TychoExecutorEncoder` with customizable options.
138pub struct TychoExecutorEncoderBuilder {
139    swap_encoder_registry: Option<SwapEncoderRegistry>,
140}
141
142impl Default for TychoExecutorEncoderBuilder {
143    fn default() -> Self {
144        Self::new()
145    }
146}
147
148impl TychoExecutorEncoderBuilder {
149    pub fn new() -> Self {
150        TychoExecutorEncoderBuilder { swap_encoder_registry: None }
151    }
152
153    pub fn swap_encoder_registry(mut self, swap_encoder_registry: SwapEncoderRegistry) -> Self {
154        self.swap_encoder_registry = Some(swap_encoder_registry);
155        self
156    }
157
158    /// Builds the `TychoExecutorEncoder` instance using the configured chain and strategy.
159    /// Returns an error if either the chain or strategy has not been set.
160    pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> {
161        if let Some(swap_encoder_registry) = self.swap_encoder_registry {
162            Ok(Box::new(TychoExecutorEncoder::new(swap_encoder_registry)?))
163        } else {
164            Err(EncodingError::FatalError(
165                "Please set the swap encoder registry before building the encoder".to_string(),
166            ))
167        }
168    }
169}