Skip to content

Commit

Permalink
Merge pull request #96 from propeller-heads/encoding/dc/ENG-4286-rena…
Browse files Browse the repository at this point in the history
…me-default-functions-builder

feat: Rename shortcut methods of encoder builder
  • Loading branch information
dianacarvalho1 authored Mar 3, 2025
2 parents 5a2474c + 6f572ee commit f0f476e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/encoding-example/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
// Initialize the encoder
let encoder = EVMEncoderBuilder::new()
.chain(Chain::Ethereum)
.tycho_router_with_permit2(None, swapper_pk)
.initialize_tycho_router_with_permit2(swapper_pk)
.expect("Failed to create encoder builder")
.build()
.expect("Failed to build encoder");
Expand Down
26 changes: 12 additions & 14 deletions src/bin/tycho-encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,21 @@ use tycho_execution::encoding::{
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
#[arg(short, long)]
executors_file_path: Option<String>,
}

#[derive(Subcommand)]
pub enum Commands {
/// Use the Tycho router encoding strategy
TychoRouter {
#[arg(short, long)]
config_path: Option<String>,
},
TychoRouter,
/// Use the Tycho router encoding strategy with Permit2 approval and token in transfer
TychoRouterPermit2 {
#[arg(short, long)]
config_path: Option<String>,
#[arg(short, long)]
swapper_pk: String,
},
/// Use the direct execution encoding strategy
DirectExecution {
#[arg(short, long)]
config_path: Option<String>,
},
DirectExecution,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -82,12 +76,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let mut builder = EVMEncoderBuilder::new().chain(chain);

if let Some(config_path) = cli.executors_file_path {
builder = builder.executors_file_path(config_path);
}

builder = match cli.command {
Commands::TychoRouter { config_path } => builder.tycho_router(config_path)?,
Commands::TychoRouterPermit2 { config_path, swapper_pk } => {
builder.tycho_router_with_permit2(config_path, swapper_pk)?
Commands::TychoRouter => builder.initialize_tycho_router()?,
Commands::TychoRouterPermit2 { swapper_pk } => {
builder.initialize_tycho_router_with_permit2(swapper_pk)?
}
Commands::DirectExecution { config_path } => builder.direct_execution(config_path)?,
Commands::DirectExecution => builder.initialize_direct_execution()?,
};
let encoder = builder.build()?;
let transactions = encoder.encode_router_calldata(vec![solution])?;
Expand Down
47 changes: 33 additions & 14 deletions src/encoding/evm/encoder_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::encoding::{
pub struct EVMEncoderBuilder {
strategy: Option<Box<dyn StrategyEncoder>>,
chain: Option<Chain>,
executors_file_path: Option<String>,
}

impl Default for EVMEncoderBuilder {
Expand All @@ -26,13 +27,20 @@ impl Default for EVMEncoderBuilder {

impl EVMEncoderBuilder {
pub fn new() -> Self {
EVMEncoderBuilder { chain: None, strategy: None }
EVMEncoderBuilder { chain: None, strategy: None, executors_file_path: None }
}
pub fn chain(mut self, chain: Chain) -> Self {
self.chain = Some(chain);
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);
self
}

/// Sets the `strategy_encoder` manually.
///
/// **Note**: This method should not be used in combination with `tycho_router` or
Expand All @@ -44,12 +52,17 @@ impl EVMEncoderBuilder {

/// Shortcut method to initialize a `SplitSwapStrategyEncoder` without any approval nor token in
/// transfer. **Note**: Should not be used at the same time as `strategy_encoder`.
pub fn tycho_router(self, executors_file_path: Option<String>) -> Result<Self, EncodingError> {
pub fn initialize_tycho_router(self) -> Result<Self, EncodingError> {
if let Some(chain) = self.chain {
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?;
let swap_encoder_registry =
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?;
let strategy =
Box::new(SplitSwapStrategyEncoder::new(chain, swap_encoder_registry, None)?);
Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) })
Ok(EVMEncoderBuilder {
chain: Some(chain),
strategy: Some(strategy),
executors_file_path: self.executors_file_path,
})
} else {
Err(EncodingError::FatalError(
"Please set the chain before setting the tycho router".to_string(),
Expand All @@ -59,19 +72,23 @@ impl EVMEncoderBuilder {

/// Shortcut method to initialize a `SplitSwapStrategyEncoder` with Permit2 approval and token
/// in transfer. **Note**: Should not be used at the same time as `strategy_encoder`.
pub fn tycho_router_with_permit2(
pub fn initialize_tycho_router_with_permit2(
self,
executors_file_path: Option<String>,
swapper_pk: String,
) -> Result<Self, EncodingError> {
if let Some(chain) = self.chain {
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?;
let swap_encoder_registry =
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?;
let strategy = Box::new(SplitSwapStrategyEncoder::new(
chain,
swap_encoder_registry,
Some(swapper_pk),
)?);
Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) })
Ok(EVMEncoderBuilder {
chain: Some(chain),
strategy: Some(strategy),
executors_file_path: self.executors_file_path,
})
} else {
Err(EncodingError::FatalError(
"Please set the chain before setting the tycho router".to_string(),
Expand All @@ -81,14 +98,16 @@ impl EVMEncoderBuilder {

/// Shortcut method to initialize an `ExecutorStrategyEncoder`.
/// **Note**: Should not be used at the same time as `strategy_encoder`.
pub fn direct_execution(
self,
executors_file_path: Option<String>,
) -> Result<Self, EncodingError> {
pub fn initialize_direct_execution(self) -> Result<Self, EncodingError> {
if let Some(chain) = self.chain {
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?;
let swap_encoder_registry =
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?;
let strategy = Box::new(ExecutorStrategyEncoder::new(swap_encoder_registry));
Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) })
Ok(EVMEncoderBuilder {
chain: Some(chain),
strategy: Some(strategy),
executors_file_path: self.executors_file_path,
})
} else {
Err(EncodingError::FatalError(
"Please set the chain before setting the strategy".to_string(),
Expand Down

0 comments on commit f0f476e

Please sign in to comment.