From 5c396512cf695dab3b0d0fec16f71b916661d54d Mon Sep 17 00:00:00 2001 From: royvardhan Date: Wed, 29 Jan 2025 21:20:22 +0530 Subject: [PATCH] feat: update ExecutorEncoder interface and relevant types --- src/encoding/evm/router_encoder.rs | 11 ++++++++--- src/encoding/evm/strategy_encoder/encoder.rs | 19 +++++++++++-------- src/encoding/evm/strategy_encoder/selector.rs | 4 ++-- src/encoding/models.rs | 3 +++ src/encoding/strategy_encoder.rs | 4 +++- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/encoding/evm/router_encoder.rs b/src/encoding/evm/router_encoder.rs index 53ab437..a564e3e 100644 --- a/src/encoding/evm/router_encoder.rs +++ b/src/encoding/evm/router_encoder.rs @@ -30,7 +30,7 @@ impl RouterEncoder for EVMRo &self, solutions: Vec, ) -> Result, EncodingError> { - let _approvals_calldata = self.handle_approvals(&solutions)?; // TODO: where should we append this? + let _approvals_calldata = self.handle_approvals(&solutions)?; let mut transactions: Vec = Vec::new(); for solution in solutions.iter() { let exact_out = solution.exact_out; @@ -39,7 +39,8 @@ impl RouterEncoder for EVMRo let strategy = self .strategy_selector .select_strategy(solution); - let method_calldata = strategy.encode_strategy((*solution).clone())?; + let (method_calldata, target_address) = + strategy.encode_strategy((*solution).clone())?; let contract_interaction = if straight_to_pool { method_calldata @@ -52,7 +53,11 @@ impl RouterEncoder for EVMRo } else { BigUint::ZERO }; - transactions.push(Transaction { value, data: contract_interaction }); + transactions.push(Transaction { + value, + data: contract_interaction, + to: target_address, + }); } Ok(transactions) } diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index 7068608..35c1b19 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use alloy_primitives::Address; use alloy_sol_types::SolValue; @@ -27,7 +29,7 @@ pub trait EVMStrategyEncoder: StrategyEncoder { pub struct SplitSwapStrategyEncoder {} impl EVMStrategyEncoder for SplitSwapStrategyEncoder {} impl StrategyEncoder for SplitSwapStrategyEncoder { - fn encode_strategy(&self, _solution: Solution) -> Result, EncodingError> { + fn encode_strategy(&self, _solution: Solution) -> Result<(Vec, Address), EncodingError> { todo!() } fn selector(&self, _exact_out: bool) -> &str { @@ -37,10 +39,10 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { /// This strategy encoder is used for solutions that are sent directly to the pool. /// Only 1 solution with 1 swap is supported. -pub struct StraightToPoolStrategyEncoder {} -impl EVMStrategyEncoder for StraightToPoolStrategyEncoder {} -impl StrategyEncoder for StraightToPoolStrategyEncoder { - fn encode_strategy(&self, solution: Solution) -> Result, EncodingError> { +pub struct ExecutorEncoder {} +impl EVMStrategyEncoder for ExecutorEncoder {} +impl StrategyEncoder for ExecutorEncoder { + fn encode_strategy(&self, solution: Solution) -> Result<(Vec, Address), EncodingError> { if solution.router_address.is_none() { return Err(EncodingError::InvalidInput( "Router address is required for straight to pool solutions".to_string(), @@ -68,10 +70,11 @@ impl StrategyEncoder for StraightToPoolStrategyEncoder { router_address, }; let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?; - // TODO: here we need to pass also the address of the executor to be used - Ok(protocol_data) + let executor_address = Address::from_str(swap_encoder.executor_address()) + .map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?; + Ok((protocol_data, executor_address)) } fn selector(&self, _exact_out: bool) -> &str { - unimplemented!(); + "swap(uint256, bytes)" } } diff --git a/src/encoding/evm/strategy_encoder/selector.rs b/src/encoding/evm/strategy_encoder/selector.rs index 37e397c..5ede40a 100644 --- a/src/encoding/evm/strategy_encoder/selector.rs +++ b/src/encoding/evm/strategy_encoder/selector.rs @@ -1,5 +1,5 @@ use crate::encoding::{ - evm::strategy_encoder::encoder::{SplitSwapStrategyEncoder, StraightToPoolStrategyEncoder}, + evm::strategy_encoder::encoder::{ExecutorEncoder, SplitSwapStrategyEncoder}, models::Solution, strategy_encoder::{StrategyEncoder, StrategySelector}, }; @@ -9,7 +9,7 @@ pub struct EVMStrategySelector; impl StrategySelector for EVMStrategySelector { fn select_strategy(&self, solution: &Solution) -> Box { if solution.straight_to_pool { - Box::new(StraightToPoolStrategyEncoder {}) + Box::new(ExecutorEncoder {}) } else { Box::new(SplitSwapStrategyEncoder {}) } diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 4e2cbbd..431cdc5 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -1,3 +1,4 @@ +use alloy_primitives::Address; use num_bigint::BigUint; use tycho_core::{dto::ProtocolComponent, Bytes}; @@ -60,6 +61,8 @@ pub struct Transaction { pub data: Vec, // ETH value to be sent with the transaction. pub value: BigUint, + // Address of the contract to call with the calldata + pub to: Address, } #[allow(dead_code)] diff --git a/src/encoding/strategy_encoder.rs b/src/encoding/strategy_encoder.rs index 2b4e348..fb3aa41 100644 --- a/src/encoding/strategy_encoder.rs +++ b/src/encoding/strategy_encoder.rs @@ -1,8 +1,10 @@ +use alloy_primitives::Address; + use crate::encoding::{errors::EncodingError, models::Solution}; #[allow(dead_code)] pub trait StrategyEncoder { - fn encode_strategy(&self, to_encode: Solution) -> Result, EncodingError>; + fn encode_strategy(&self, to_encode: Solution) -> Result<(Vec, Address), EncodingError>; fn selector(&self, exact_out: bool) -> &str; }