tycho_execution/encoding/evm/
encoder_builders.rs1use 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
13pub 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 pub fn router_address(mut self, router_address: Bytes) -> Self {
45 self.router_address = Some(router_address);
46 self
47 }
48
49 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
74pub 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 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}