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:
@@ -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<dyn std::error::Error>> {
|
||||
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);
|
||||
|
||||
@@ -20,7 +20,7 @@ use crate::encoding::{
|
||||
pub struct TychoRouterEncoderBuilder {
|
||||
chain: Option<Chain>,
|
||||
user_transfer_type: Option<UserTransferType>,
|
||||
executors_file_path: Option<String>,
|
||||
executors_addresses: Option<String>,
|
||||
router_address: Option<Bytes>,
|
||||
swapper_pk: Option<String>,
|
||||
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<Chain>,
|
||||
executors_file_path: Option<String>,
|
||||
executors_addresses: Option<String>,
|
||||
}
|
||||
|
||||
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<Box<dyn TychoEncoder>, 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(
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<String>, chain: Chain) -> Result<Self, EncodingError> {
|
||||
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<String>, chain: Chain) -> Result<Self, EncodingError> {
|
||||
let config_str = if let Some(addresses) = executors_addresses {
|
||||
addresses
|
||||
} else {
|
||||
DEFAULT_EXECUTORS_JSON.to_string()
|
||||
};
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<dyn TychoEncoder> {
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user