diff --git a/src/bin/tycho-encode.rs b/src/bin/tycho-encode.rs index 161f7ae..d0252dc 100644 --- a/src/bin/tycho-encode.rs +++ b/src/bin/tycho-encode.rs @@ -1,9 +1,13 @@ -use std::io::{self, Read}; +use std::{ + fs, + io::{self, Read}, +}; use alloy::sol_types::SolValue; use clap::{Parser, Subcommand}; use tycho_common::{hex_bytes::Bytes, models::Chain}; use tycho_execution::encoding::{ + errors::EncodingError, evm::{ approvals::permit2::PermitSingle, encoder_builders::{TychoExecutorEncoderBuilder, TychoRouterEncoderBuilder}, @@ -83,7 +87,12 @@ fn main() -> Result<(), Box> { Commands::TychoRouter => { let mut builder = TychoRouterEncoderBuilder::new().chain(chain); if let Some(config_path) = cli.executors_file_path { - builder = builder.executors_file_path(config_path); + let executors_addresses = fs::read_to_string(&config_path).map_err(|e| { + EncodingError::FatalError(format!( + "Error reading executors file from {config_path:?}: {e}", + )) + })?; + builder = builder.executors_addresses(executors_addresses); } if let Some(router_address) = cli.router_address { builder = builder.router_address(router_address); diff --git a/src/encoding/evm/encoder_builders.rs b/src/encoding/evm/encoder_builders.rs index 90f641b..703f083 100644 --- a/src/encoding/evm/encoder_builders.rs +++ b/src/encoding/evm/encoder_builders.rs @@ -20,7 +20,7 @@ use crate::encoding::{ pub struct TychoRouterEncoderBuilder { chain: Option, user_transfer_type: Option, - executors_file_path: Option, + executors_addresses: Option, router_address: Option, swapper_pk: Option, historical_trade: bool, @@ -36,7 +36,7 @@ impl TychoRouterEncoderBuilder { pub fn new() -> Self { TychoRouterEncoderBuilder { chain: None, - executors_file_path: None, + executors_addresses: None, router_address: None, swapper_pk: None, user_transfer_type: None, @@ -53,10 +53,10 @@ impl TychoRouterEncoderBuilder { self } - /// Sets the `executors_file_path` manually. - /// If it's not set, the default path will be used (config/executor_addresses.json) - pub fn executors_file_path(mut self, executors_file_path: String) -> Self { - self.executors_file_path = Some(executors_file_path); + /// Sets the `executors_addresses` manually. + /// If it's not set, the default value will be used (contents of config/executor_addresses.json) + pub fn executors_addresses(mut self, executors_addresses: String) -> Self { + self.executors_addresses = Some(executors_addresses); self } @@ -107,7 +107,7 @@ impl TychoRouterEncoderBuilder { } let swap_encoder_registry = - SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?; + SwapEncoderRegistry::new(self.executors_addresses.clone(), chain)?; let signer = if let Some(pk) = self.swapper_pk { let pk = B256::from_str(&pk).map_err(|_| { @@ -140,7 +140,7 @@ impl TychoRouterEncoderBuilder { /// Builder pattern for constructing a `TychoExecutorEncoder` with customizable options. pub struct TychoExecutorEncoderBuilder { chain: Option, - executors_file_path: Option, + executors_addresses: Option, } impl Default for TychoExecutorEncoderBuilder { @@ -151,17 +151,17 @@ impl Default for TychoExecutorEncoderBuilder { impl TychoExecutorEncoderBuilder { pub fn new() -> Self { - TychoExecutorEncoderBuilder { chain: None, executors_file_path: None } + TychoExecutorEncoderBuilder { chain: None, executors_addresses: None } } pub fn chain(mut self, chain: Chain) -> Self { self.chain = Some(chain); self } - /// Sets the `executors_file_path` manually. + /// Sets the `executors_addresses` manually. /// If it's not set, the default path will be used (config/executor_addresses.json) - pub fn executors_file_path(mut self, executors_file_path: String) -> Self { - self.executors_file_path = Some(executors_file_path); + pub fn executors_addresses(mut self, executors_addresses: String) -> Self { + self.executors_addresses = Some(executors_addresses); self } @@ -170,7 +170,7 @@ impl TychoExecutorEncoderBuilder { pub fn build(self) -> Result, EncodingError> { if let Some(chain) = self.chain { let swap_encoder_registry = - SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?; + SwapEncoderRegistry::new(self.executors_addresses.clone(), chain)?; Ok(Box::new(TychoExecutorEncoder::new(swap_encoder_registry)?)) } else { Err(EncodingError::FatalError( diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index e0c1255..44f5503 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -553,7 +553,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { #[cfg(test)] mod tests { - use std::{collections::HashMap, str::FromStr}; + use std::{collections::HashMap, fs, str::FromStr}; use alloy::{hex::encode, primitives::hex}; use num_bigint::{BigInt, BigUint}; @@ -573,9 +573,10 @@ mod tests { } fn get_swap_encoder_registry() -> SwapEncoderRegistry { + let executors_addresses = + fs::read_to_string("config/test_executor_addresses.json").unwrap(); let eth_chain = eth_chain(); - SwapEncoderRegistry::new(Some("config/test_executor_addresses.json".to_string()), eth_chain) - .unwrap() + SwapEncoderRegistry::new(Some(executors_addresses), eth_chain).unwrap() } fn router_address() -> Bytes { diff --git a/src/encoding/evm/swap_encoder/swap_encoder_registry.rs b/src/encoding/evm/swap_encoder/swap_encoder_registry.rs index 221ea76..a9955a3 100644 --- a/src/encoding/evm/swap_encoder/swap_encoder_registry.rs +++ b/src/encoding/evm/swap_encoder/swap_encoder_registry.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, fs}; +use std::collections::HashMap; use tycho_common::models::Chain; @@ -21,13 +21,9 @@ pub struct SwapEncoderRegistry { impl SwapEncoderRegistry { /// Populates the registry with the `SwapEncoders` for the given blockchain by parsing the /// executors' addresses in the file at the given path. - pub fn new(executors_file_path: Option, chain: Chain) -> Result { - let config_str = if let Some(ref path) = executors_file_path { - fs::read_to_string(path).map_err(|e| { - EncodingError::FatalError(format!( - "Error reading executors file from {executors_file_path:?}: {e}", - )) - })? + pub fn new(executors_addresses: Option, chain: Chain) -> Result { + let config_str = if let Some(addresses) = executors_addresses { + addresses } else { DEFAULT_EXECUTORS_JSON.to_string() }; diff --git a/src/encoding/evm/tycho_encoders.rs b/src/encoding/evm/tycho_encoders.rs index 79632db..e6cae0d 100644 --- a/src/encoding/evm/tycho_encoders.rs +++ b/src/encoding/evm/tycho_encoders.rs @@ -67,14 +67,14 @@ impl TychoRouterEncoder { swap_encoder_registry.clone(), user_transfer_type.clone(), router_address.clone(), - historical_trade.clone(), + historical_trade, )?, sequential_swap_strategy: SequentialSwapStrategyEncoder::new( chain, swap_encoder_registry.clone(), user_transfer_type.clone(), router_address.clone(), - historical_trade.clone(), + historical_trade, )?, split_swap_strategy: SplitSwapStrategyEncoder::new( chain, @@ -405,7 +405,7 @@ impl TychoEncoder for TychoExecutorEncoder { #[cfg(test)] mod tests { - use std::{collections::HashMap, str::FromStr}; + use std::{collections::HashMap, fs, str::FromStr}; use num_bigint::{BigInt, BigUint}; use tycho_common::models::{protocol::ProtocolComponent, Chain}; @@ -489,11 +489,9 @@ mod tests { } fn get_swap_encoder_registry() -> SwapEncoderRegistry { - SwapEncoderRegistry::new( - Some("config/test_executor_addresses.json".to_string()), - eth_chain(), - ) - .unwrap() + let executors_addresses = + fs::read_to_string("config/test_executor_addresses.json").unwrap(); + SwapEncoderRegistry::new(Some(executors_addresses), eth_chain()).unwrap() } fn get_tycho_router_encoder(user_transfer_type: UserTransferType) -> TychoRouterEncoder { diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 0949b7e..a6be289 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] pub mod encoding; -use std::str::FromStr; +use std::{fs, str::FromStr}; use alloy::{ primitives::{B256, U256}, @@ -71,10 +71,11 @@ pub fn get_signer() -> PrivateKeySigner { } pub fn get_tycho_router_encoder(user_transfer_type: UserTransferType) -> Box { + let executors_addresses = fs::read_to_string("config/test_executor_addresses.json").unwrap(); TychoRouterEncoderBuilder::new() .chain(Chain::Ethereum) .user_transfer_type(user_transfer_type) - .executors_file_path("config/test_executor_addresses.json".to_string()) + .executors_addresses(executors_addresses) .router_address(router_address()) .build() .expect("Failed to build encoder")