feat: Pass the file contents instead of the file path for executors

This is better because passing paths around might be really complex when running the code in another environments that is not local

Took 30 minutes
This commit is contained in:
Diana Carvalho
2025-09-23 09:32:46 +01:00
parent c51c6f52a5
commit e78a362894
6 changed files with 41 additions and 36 deletions

View File

@@ -1,9 +1,13 @@
use std::io::{self, Read}; use std::{
fs,
io::{self, Read},
};
use alloy::sol_types::SolValue; use alloy::sol_types::SolValue;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use tycho_common::{hex_bytes::Bytes, models::Chain}; use tycho_common::{hex_bytes::Bytes, models::Chain};
use tycho_execution::encoding::{ use tycho_execution::encoding::{
errors::EncodingError,
evm::{ evm::{
approvals::permit2::PermitSingle, approvals::permit2::PermitSingle,
encoder_builders::{TychoExecutorEncoderBuilder, TychoRouterEncoderBuilder}, encoder_builders::{TychoExecutorEncoderBuilder, TychoRouterEncoderBuilder},
@@ -83,7 +87,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Commands::TychoRouter => { Commands::TychoRouter => {
let mut builder = TychoRouterEncoderBuilder::new().chain(chain); let mut builder = TychoRouterEncoderBuilder::new().chain(chain);
if let Some(config_path) = cli.executors_file_path { 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 { if let Some(router_address) = cli.router_address {
builder = builder.router_address(router_address); builder = builder.router_address(router_address);

View File

@@ -20,7 +20,7 @@ use crate::encoding::{
pub struct TychoRouterEncoderBuilder { pub struct TychoRouterEncoderBuilder {
chain: Option<Chain>, chain: Option<Chain>,
user_transfer_type: Option<UserTransferType>, user_transfer_type: Option<UserTransferType>,
executors_file_path: Option<String>, executors_addresses: Option<String>,
router_address: Option<Bytes>, router_address: Option<Bytes>,
swapper_pk: Option<String>, swapper_pk: Option<String>,
historical_trade: bool, historical_trade: bool,
@@ -36,7 +36,7 @@ impl TychoRouterEncoderBuilder {
pub fn new() -> Self { pub fn new() -> Self {
TychoRouterEncoderBuilder { TychoRouterEncoderBuilder {
chain: None, chain: None,
executors_file_path: None, executors_addresses: None,
router_address: None, router_address: None,
swapper_pk: None, swapper_pk: None,
user_transfer_type: None, user_transfer_type: None,
@@ -53,10 +53,10 @@ impl TychoRouterEncoderBuilder {
self 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) /// If it's not set, the default value will be used (contents of config/executor_addresses.json)
pub fn executors_file_path(mut self, executors_file_path: String) -> Self { pub fn executors_addresses(mut self, executors_addresses: String) -> Self {
self.executors_file_path = Some(executors_file_path); self.executors_addresses = Some(executors_addresses);
self self
} }
@@ -107,7 +107,7 @@ impl TychoRouterEncoderBuilder {
} }
let swap_encoder_registry = 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 signer = if let Some(pk) = self.swapper_pk {
let pk = B256::from_str(&pk).map_err(|_| { let pk = B256::from_str(&pk).map_err(|_| {
@@ -140,7 +140,7 @@ impl TychoRouterEncoderBuilder {
/// Builder pattern for constructing a `TychoExecutorEncoder` with customizable options. /// Builder pattern for constructing a `TychoExecutorEncoder` with customizable options.
pub struct TychoExecutorEncoderBuilder { pub struct TychoExecutorEncoderBuilder {
chain: Option<Chain>, chain: Option<Chain>,
executors_file_path: Option<String>, executors_addresses: Option<String>,
} }
impl Default for TychoExecutorEncoderBuilder { impl Default for TychoExecutorEncoderBuilder {
@@ -151,17 +151,17 @@ impl Default for TychoExecutorEncoderBuilder {
impl TychoExecutorEncoderBuilder { impl TychoExecutorEncoderBuilder {
pub fn new() -> Self { 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 { pub fn chain(mut self, chain: Chain) -> Self {
self.chain = Some(chain); self.chain = Some(chain);
self 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) /// 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 { pub fn executors_addresses(mut self, executors_addresses: String) -> Self {
self.executors_file_path = Some(executors_file_path); self.executors_addresses = Some(executors_addresses);
self self
} }
@@ -170,7 +170,7 @@ impl TychoExecutorEncoderBuilder {
pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> { pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> {
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_addresses.clone(), chain)?;
Ok(Box::new(TychoExecutorEncoder::new(swap_encoder_registry)?)) Ok(Box::new(TychoExecutorEncoder::new(swap_encoder_registry)?))
} else { } else {
Err(EncodingError::FatalError( Err(EncodingError::FatalError(

View File

@@ -553,7 +553,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{collections::HashMap, str::FromStr}; use std::{collections::HashMap, fs, str::FromStr};
use alloy::{hex::encode, primitives::hex}; use alloy::{hex::encode, primitives::hex};
use num_bigint::{BigInt, BigUint}; use num_bigint::{BigInt, BigUint};
@@ -573,9 +573,10 @@ mod tests {
} }
fn get_swap_encoder_registry() -> SwapEncoderRegistry { fn get_swap_encoder_registry() -> SwapEncoderRegistry {
let executors_addresses =
fs::read_to_string("config/test_executor_addresses.json").unwrap();
let eth_chain = eth_chain(); let eth_chain = eth_chain();
SwapEncoderRegistry::new(Some("config/test_executor_addresses.json".to_string()), eth_chain) SwapEncoderRegistry::new(Some(executors_addresses), eth_chain).unwrap()
.unwrap()
} }
fn router_address() -> Bytes { fn router_address() -> Bytes {

View File

@@ -1,4 +1,4 @@
use std::{collections::HashMap, fs}; use std::collections::HashMap;
use tycho_common::models::Chain; use tycho_common::models::Chain;
@@ -21,13 +21,9 @@ pub struct SwapEncoderRegistry {
impl SwapEncoderRegistry { impl SwapEncoderRegistry {
/// Populates the registry with the `SwapEncoders` for the given blockchain by parsing the /// Populates the registry with the `SwapEncoders` for the given blockchain by parsing the
/// executors' addresses in the file at the given path. /// executors' addresses in the file at the given path.
pub fn new(executors_file_path: Option<String>, chain: Chain) -> Result<Self, EncodingError> { pub fn new(executors_addresses: Option<String>, chain: Chain) -> Result<Self, EncodingError> {
let config_str = if let Some(ref path) = executors_file_path { let config_str = if let Some(addresses) = executors_addresses {
fs::read_to_string(path).map_err(|e| { addresses
EncodingError::FatalError(format!(
"Error reading executors file from {executors_file_path:?}: {e}",
))
})?
} else { } else {
DEFAULT_EXECUTORS_JSON.to_string() DEFAULT_EXECUTORS_JSON.to_string()
}; };

View File

@@ -67,14 +67,14 @@ impl TychoRouterEncoder {
swap_encoder_registry.clone(), swap_encoder_registry.clone(),
user_transfer_type.clone(), user_transfer_type.clone(),
router_address.clone(), router_address.clone(),
historical_trade.clone(), historical_trade,
)?, )?,
sequential_swap_strategy: SequentialSwapStrategyEncoder::new( sequential_swap_strategy: SequentialSwapStrategyEncoder::new(
chain, chain,
swap_encoder_registry.clone(), swap_encoder_registry.clone(),
user_transfer_type.clone(), user_transfer_type.clone(),
router_address.clone(), router_address.clone(),
historical_trade.clone(), historical_trade,
)?, )?,
split_swap_strategy: SplitSwapStrategyEncoder::new( split_swap_strategy: SplitSwapStrategyEncoder::new(
chain, chain,
@@ -405,7 +405,7 @@ impl TychoEncoder for TychoExecutorEncoder {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{collections::HashMap, str::FromStr}; use std::{collections::HashMap, fs, str::FromStr};
use num_bigint::{BigInt, BigUint}; use num_bigint::{BigInt, BigUint};
use tycho_common::models::{protocol::ProtocolComponent, Chain}; use tycho_common::models::{protocol::ProtocolComponent, Chain};
@@ -489,11 +489,9 @@ mod tests {
} }
fn get_swap_encoder_registry() -> SwapEncoderRegistry { fn get_swap_encoder_registry() -> SwapEncoderRegistry {
SwapEncoderRegistry::new( let executors_addresses =
Some("config/test_executor_addresses.json".to_string()), fs::read_to_string("config/test_executor_addresses.json").unwrap();
eth_chain(), SwapEncoderRegistry::new(Some(executors_addresses), eth_chain()).unwrap()
)
.unwrap()
} }
fn get_tycho_router_encoder(user_transfer_type: UserTransferType) -> TychoRouterEncoder { fn get_tycho_router_encoder(user_transfer_type: UserTransferType) -> TychoRouterEncoder {

View File

@@ -1,7 +1,7 @@
#![allow(dead_code)] #![allow(dead_code)]
pub mod encoding; pub mod encoding;
use std::str::FromStr; use std::{fs, str::FromStr};
use alloy::{ use alloy::{
primitives::{B256, U256}, primitives::{B256, U256},
@@ -71,10 +71,11 @@ pub fn get_signer() -> PrivateKeySigner {
} }
pub fn get_tycho_router_encoder(user_transfer_type: UserTransferType) -> Box<dyn TychoEncoder> { pub fn get_tycho_router_encoder(user_transfer_type: UserTransferType) -> Box<dyn TychoEncoder> {
let executors_addresses = fs::read_to_string("config/test_executor_addresses.json").unwrap();
TychoRouterEncoderBuilder::new() TychoRouterEncoderBuilder::new()
.chain(Chain::Ethereum) .chain(Chain::Ethereum)
.user_transfer_type(user_transfer_type) .user_transfer_type(user_transfer_type)
.executors_file_path("config/test_executor_addresses.json".to_string()) .executors_addresses(executors_addresses)
.router_address(router_address()) .router_address(router_address())
.build() .build()
.expect("Failed to build encoder") .expect("Failed to build encoder")