diff --git a/examples/quickstart/main.rs b/examples/quickstart/main.rs index b1e0c1c..eefe4e5 100644 --- a/examples/quickstart/main.rs +++ b/examples/quickstart/main.rs @@ -21,7 +21,9 @@ fn main() { .expect("Failed to create user address"); // Initialize the encoder - let encoder = EVMEncoderBuilder::tycho_router(TychoCoreChain::Ethereum, swapper_pk, None) + let encoder = EVMEncoderBuilder::new() + .chain(TychoCoreChain::Ethereum) + .tycho_router(swapper_pk, None) .expect("Failed to create encoder builder") .build() .expect("Failed to build encoder"); diff --git a/src/bin/tycho-encode.rs b/src/bin/tycho-encode.rs index 0949b8d..b297b3b 100644 --- a/src/bin/tycho-encode.rs +++ b/src/bin/tycho-encode.rs @@ -103,14 +103,16 @@ fn encode_swaps( let solution: Solution = serde_json::from_str(input)?; let chain = Chain::Ethereum; - let encoder = if use_tycho_router { + let mut builder = EVMEncoderBuilder::new().chain(chain); + builder = if use_tycho_router { let private_key = swapper_pk.ok_or(EncodingError::FatalError( "Swapper private key is required for tycho_router".to_string(), ))?; - EVMEncoderBuilder::tycho_router(chain, private_key, config_path)?.build()? + builder.tycho_router(private_key, config_path)? } else { - EVMEncoderBuilder::direct_execution(chain, config_path)?.build()? + builder.direct_execution(config_path)? }; + let encoder = builder.build()?; let transactions = encoder.encode_router_calldata(vec![solution])?; diff --git a/src/encoding/evm/encoder_builder.rs b/src/encoding/evm/encoder_builder.rs index a5a0ff7..332d253 100644 --- a/src/encoding/evm/encoder_builder.rs +++ b/src/encoding/evm/encoder_builder.rs @@ -11,34 +11,66 @@ use crate::encoding::{ }; pub struct EVMEncoderBuilder { - strategy: Box, - chain: Chain, + strategy: Option>, + chain: Option, +} + +impl Default for EVMEncoderBuilder { + fn default() -> Self { + Self::new() + } } impl EVMEncoderBuilder { - pub fn new(chain: Chain, strategy: Box) -> Self { - EVMEncoderBuilder { chain, strategy } + pub fn new() -> Self { + EVMEncoderBuilder { chain: None, strategy: None } + } + pub fn chain(mut self, chain: Chain) -> Self { + self.chain = Some(chain); + self + } + pub fn strategy_encoder(mut self, strategy: Box) -> Self { + self.strategy = Some(strategy); + self } pub fn tycho_router( - chain: Chain, + self, swapper_pk: String, executors_file_path: Option, ) -> Result { - let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?; - let strategy = - Box::new(SplitSwapStrategyEncoder::new(swapper_pk, chain, swap_encoder_registry)?); - Ok(EVMEncoderBuilder { chain, strategy }) + if let Some(chain) = self.chain { + let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?; + let strategy = + Box::new(SplitSwapStrategyEncoder::new(swapper_pk, chain, swap_encoder_registry)?); + Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) }) + } else { + Err(EncodingError::FatalError( + "Please set the chain before setting the strategy".to_string(), + )) + } } pub fn direct_execution( - chain: Chain, + self, executors_file_path: Option, ) -> Result { - let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?; - let strategy = Box::new(ExecutorStrategyEncoder::new(swap_encoder_registry)); - Ok(EVMEncoderBuilder { chain, strategy }) + if let Some(chain) = self.chain { + let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?; + let strategy = Box::new(ExecutorStrategyEncoder::new(swap_encoder_registry)); + Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) }) + } else { + Err(EncodingError::FatalError( + "Please set the chain before setting the strategy".to_string(), + )) + } } pub fn build(self) -> Result { - EVMTychoEncoder::new(self.chain, self.strategy) + if let (Some(chain), Some(strategy)) = (self.chain, self.strategy) { + EVMTychoEncoder::new(chain, strategy) + } else { + Err(EncodingError::FatalError( + "Please set the chain and strategy before building the encoder".to_string(), + )) + } } }