feat: Refactor TychoEncoder

We have a trait TychoEncoder and then two implementations: TychoRouterEncoder and TychoExecutorEncoder.
This way we go a level above with the decision if it is a direct execution or if it should use the tycho router.

- Created two builders: one for each tycho encoder
- Delete ExecutorStrategyEncoder and move code straight into the TychoExecutorEncoder
- Add validate_solution to trait TychoEncoder
- Move group_swaps.rs a level up
- Update tests and usage cases

Doing this we get rid of all that weird stuff we were doing before

--- don't change below this line ---
ENG-4306 Took 2 hours 6 minutes


Took 12 seconds
This commit is contained in:
Diana Carvalho
2025-04-07 18:41:14 +01:00
parent 899f0efeff
commit 8b4b79b353
11 changed files with 1217 additions and 1178 deletions

View File

@@ -3,7 +3,9 @@ use std::io::{self, Read};
use clap::{Parser, Subcommand};
use tycho_common::{hex_bytes::Bytes, models::Chain};
use tycho_execution::encoding::{
evm::encoder_builder::EVMEncoderBuilder, models::Solution, tycho_encoder::TychoEncoder,
evm::encoder_builders::{TychoExecutorEncoderBuilder, TychoRouterEncoderBuilder},
models::Solution,
tycho_encoder::TychoEncoder,
};
#[derive(Parser)]
@@ -45,19 +47,16 @@ pub struct Cli {
executors_file_path: Option<String>,
#[arg(short, long)]
router_address: Option<Bytes>,
#[arg(short, long)]
swapper_pk: Option<String>,
}
#[derive(Subcommand)]
pub enum Commands {
/// Use the Tycho router encoding strategy
/// Use Tycho router encoding
TychoRouter,
/// Use the Tycho router encoding strategy with Permit2 approval and token in transfer
TychoRouterPermit2 {
#[arg(short, long)]
swapper_pk: String,
},
/// Use the direct execution encoding strategy
DirectExecution,
/// Use direct execution encoding
TychoExecutor,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -75,24 +74,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
let solution: Solution = serde_json::from_str(&buffer)?;
let mut builder = EVMEncoderBuilder::new().chain(chain);
if let Some(config_path) = cli.executors_file_path {
builder = builder.executors_file_path(config_path);
}
if let Some(router_address) = cli.router_address {
builder = builder.router_address(router_address);
}
builder = match cli.command {
Commands::TychoRouter => builder.initialize_tycho_router()?,
Commands::TychoRouterPermit2 { swapper_pk } => {
builder.initialize_tycho_router_with_permit2(swapper_pk)?
let encoder: Box<dyn TychoEncoder> = match cli.command {
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);
}
if let Some(router_address) = cli.router_address {
builder = builder.router_address(router_address);
}
if let Some(swapper_pk) = cli.swapper_pk {
builder = builder.swapper_pk(swapper_pk);
}
builder.build()?
}
Commands::DirectExecution => builder.initialize_direct_execution()?,
Commands::TychoExecutor => TychoExecutorEncoderBuilder::new()
.chain(chain)
.build()?,
};
let encoder = builder.build()?;
let transactions = encoder.encode_router_calldata(vec![solution])?;
let transactions = encoder.encode_calldata(vec![solution])?;
let encoded = serde_json::json!({
"to": format!("0x{}", hex::encode(&transactions[0].to)),
"value": format!("0x{}", hex::encode(transactions[0].value.to_bytes_be())),