feat: Support manual router address setting in builder

This commit is contained in:
TAMARA LIPOWSKI
2025-04-02 01:12:02 +02:00
parent 99adaa9796
commit c336a28905

View File

@@ -1,4 +1,4 @@
use tycho_common::models::Chain; use tycho_common::{models::Chain, Bytes};
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
@@ -17,6 +17,7 @@ pub struct EVMEncoderBuilder {
strategy: Option<Box<dyn StrategyEncoder>>, strategy: Option<Box<dyn StrategyEncoder>>,
chain: Option<Chain>, chain: Option<Chain>,
executors_file_path: Option<String>, executors_file_path: Option<String>,
router_address: Option<Bytes>,
} }
impl Default for EVMEncoderBuilder { impl Default for EVMEncoderBuilder {
@@ -27,7 +28,12 @@ impl Default for EVMEncoderBuilder {
impl EVMEncoderBuilder { impl EVMEncoderBuilder {
pub fn new() -> Self { pub fn new() -> Self {
EVMEncoderBuilder { chain: None, strategy: None, executors_file_path: None } EVMEncoderBuilder {
chain: None,
strategy: None,
executors_file_path: None,
router_address: None,
}
} }
pub fn chain(mut self, chain: Chain) -> Self { pub fn chain(mut self, chain: Chain) -> Self {
self.chain = Some(chain); self.chain = Some(chain);
@@ -41,6 +47,13 @@ impl EVMEncoderBuilder {
self self
} }
/// Sets the `router_address` manually.
/// If it's not set, the default router address will be used (config/router_addresses.json)
pub fn router_address(mut self, router_address: Bytes) -> Self {
self.router_address = Some(router_address);
self
}
/// Sets the `strategy_encoder` manually. /// Sets the `strategy_encoder` manually.
/// ///
/// **Note**: This method should not be used in combination with `tycho_router` or /// **Note**: This method should not be used in combination with `tycho_router` or
@@ -56,12 +69,17 @@ impl EVMEncoderBuilder {
if let Some(chain) = self.chain { if let Some(chain) = self.chain {
let swap_encoder_registry = let swap_encoder_registry =
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?; SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?;
let strategy = let strategy = Box::new(SplitSwapStrategyEncoder::new(
Box::new(SplitSwapStrategyEncoder::new(chain, swap_encoder_registry, None, None)?); chain,
swap_encoder_registry,
None,
self.router_address.clone(),
)?);
Ok(EVMEncoderBuilder { Ok(EVMEncoderBuilder {
chain: Some(chain), chain: Some(chain),
strategy: Some(strategy), strategy: Some(strategy),
executors_file_path: self.executors_file_path, executors_file_path: self.executors_file_path,
router_address: self.router_address,
}) })
} else { } else {
Err(EncodingError::FatalError( Err(EncodingError::FatalError(
@@ -83,12 +101,13 @@ impl EVMEncoderBuilder {
chain, chain,
swap_encoder_registry, swap_encoder_registry,
Some(swapper_pk), Some(swapper_pk),
None, self.router_address.clone(),
)?); )?);
Ok(EVMEncoderBuilder { Ok(EVMEncoderBuilder {
chain: Some(chain), chain: Some(chain),
strategy: Some(strategy), strategy: Some(strategy),
executors_file_path: self.executors_file_path, executors_file_path: self.executors_file_path,
router_address: self.router_address,
}) })
} else { } else {
Err(EncodingError::FatalError( Err(EncodingError::FatalError(
@@ -108,6 +127,7 @@ impl EVMEncoderBuilder {
chain: Some(chain), chain: Some(chain),
strategy: Some(strategy), strategy: Some(strategy),
executors_file_path: self.executors_file_path, executors_file_path: self.executors_file_path,
router_address: self.router_address,
}) })
} else { } else {
Err(EncodingError::FatalError( Err(EncodingError::FatalError(