feat: Add methods to builder to set chain and strategy independently

--- don't change below this line ---
ENG-4246 Took 15 minutes
This commit is contained in:
Diana Carvalho
2025-02-19 11:15:27 +00:00
parent 4741e628b8
commit 684de4fa60
3 changed files with 54 additions and 18 deletions

View File

@@ -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");

View File

@@ -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])?;

View File

@@ -11,34 +11,66 @@ use crate::encoding::{
};
pub struct EVMEncoderBuilder {
strategy: Box<dyn StrategyEncoder>,
chain: Chain,
strategy: Option<Box<dyn StrategyEncoder>>,
chain: Option<Chain>,
}
impl Default for EVMEncoderBuilder {
fn default() -> Self {
Self::new()
}
}
impl EVMEncoderBuilder {
pub fn new(chain: Chain, strategy: Box<dyn StrategyEncoder>) -> 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<dyn StrategyEncoder>) -> Self {
self.strategy = Some(strategy);
self
}
pub fn tycho_router(
chain: Chain,
self,
swapper_pk: String,
executors_file_path: Option<String>,
) -> Result<Self, EncodingError> {
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<String>,
) -> Result<Self, EncodingError> {
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, EncodingError> {
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(),
))
}
}
}