Files
tycho-execution/src/encoding/evm/encoder_builder.rs
Diana Carvalho 359bb806f7 docs: Add docstrings to EVMEncoderBuilder
--- don't change below this line ---
ENG-4246 Took 41 minutes
2025-02-19 17:03:25 +00:00

93 lines
3.3 KiB
Rust

use tycho_core::dto::Chain;
use crate::encoding::{
errors::EncodingError,
evm::{
strategy_encoder::strategy_encoders::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder},
swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
tycho_encoder::EVMTychoEncoder,
},
strategy_encoder::StrategyEncoder,
};
/// Builder pattern for constructing an `EVMTychoEncoder` with customizable options.
///
/// This struct allows setting a chain and strategy encoder before building the final encoder.
pub struct EVMEncoderBuilder {
strategy: Option<Box<dyn StrategyEncoder>>,
chain: Option<Chain>,
}
impl Default for EVMEncoderBuilder {
fn default() -> Self {
Self::new()
}
}
impl EVMEncoderBuilder {
pub fn new() -> Self {
EVMEncoderBuilder { chain: None, strategy: None }
}
pub fn chain(mut self, chain: Chain) -> Self {
self.chain = Some(chain);
self
}
/// Sets the `strategy_encoder` manually.
///
/// **Note**: This method should not be used in combination with `tycho_router` or
/// `direct_execution`.
pub fn strategy_encoder(mut self, strategy: Box<dyn StrategyEncoder>) -> Self {
self.strategy = Some(strategy);
self
}
/// Shortcut method to initialize a `SplitSwapStrategyEncoder` with a given `swapper_pk`.
/// **Note**: Should not be used at the same time as `strategy_encoder`.
pub fn tycho_router(
self,
swapper_pk: String,
executors_file_path: Option<String>,
) -> Result<Self, EncodingError> {
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 tycho router".to_string(),
))
}
}
/// Shortcut method to initialize an `ExecutorStrategyEncoder`.
/// **Note**: Should not be used at the same time as `strategy_encoder`.
pub fn direct_execution(
self,
executors_file_path: Option<String>,
) -> Result<Self, EncodingError> {
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(),
))
}
}
/// Builds the `EVMTychoEncoder` instance using the configured chain and strategy.
/// Returns an error if either the chain or strategy has not been set.
pub fn build(self) -> Result<EVMTychoEncoder, EncodingError> {
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(),
))
}
}
}