feat: update ExecutorEncoder interface and relevant types

This commit is contained in:
royvardhan
2025-01-29 21:20:22 +05:30
parent ee0aafbc4d
commit 5c396512cf
5 changed files with 27 additions and 14 deletions

View File

@@ -30,7 +30,7 @@ impl<S: StrategySelector, A: UserApprovalsManager> RouterEncoder<S, A> for EVMRo
&self, &self,
solutions: Vec<Solution>, solutions: Vec<Solution>,
) -> Result<Vec<Transaction>, EncodingError> { ) -> Result<Vec<Transaction>, 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<Transaction> = Vec::new(); let mut transactions: Vec<Transaction> = Vec::new();
for solution in solutions.iter() { for solution in solutions.iter() {
let exact_out = solution.exact_out; let exact_out = solution.exact_out;
@@ -39,7 +39,8 @@ impl<S: StrategySelector, A: UserApprovalsManager> RouterEncoder<S, A> for EVMRo
let strategy = self let strategy = self
.strategy_selector .strategy_selector
.select_strategy(solution); .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 { let contract_interaction = if straight_to_pool {
method_calldata method_calldata
@@ -52,7 +53,11 @@ impl<S: StrategySelector, A: UserApprovalsManager> RouterEncoder<S, A> for EVMRo
} else { } else {
BigUint::ZERO BigUint::ZERO
}; };
transactions.push(Transaction { value, data: contract_interaction }); transactions.push(Transaction {
value,
data: contract_interaction,
to: target_address,
});
} }
Ok(transactions) Ok(transactions)
} }

View File

@@ -1,3 +1,5 @@
use std::str::FromStr;
use alloy_primitives::Address; use alloy_primitives::Address;
use alloy_sol_types::SolValue; use alloy_sol_types::SolValue;
@@ -27,7 +29,7 @@ pub trait EVMStrategyEncoder: StrategyEncoder {
pub struct SplitSwapStrategyEncoder {} pub struct SplitSwapStrategyEncoder {}
impl EVMStrategyEncoder for SplitSwapStrategyEncoder {} impl EVMStrategyEncoder for SplitSwapStrategyEncoder {}
impl StrategyEncoder for SplitSwapStrategyEncoder { impl StrategyEncoder for SplitSwapStrategyEncoder {
fn encode_strategy(&self, _solution: Solution) -> Result<Vec<u8>, EncodingError> { fn encode_strategy(&self, _solution: Solution) -> Result<(Vec<u8>, Address), EncodingError> {
todo!() todo!()
} }
fn selector(&self, _exact_out: bool) -> &str { 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. /// This strategy encoder is used for solutions that are sent directly to the pool.
/// Only 1 solution with 1 swap is supported. /// Only 1 solution with 1 swap is supported.
pub struct StraightToPoolStrategyEncoder {} pub struct ExecutorEncoder {}
impl EVMStrategyEncoder for StraightToPoolStrategyEncoder {} impl EVMStrategyEncoder for ExecutorEncoder {}
impl StrategyEncoder for StraightToPoolStrategyEncoder { impl StrategyEncoder for ExecutorEncoder {
fn encode_strategy(&self, solution: Solution) -> Result<Vec<u8>, EncodingError> { fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Address), EncodingError> {
if solution.router_address.is_none() { if solution.router_address.is_none() {
return Err(EncodingError::InvalidInput( return Err(EncodingError::InvalidInput(
"Router address is required for straight to pool solutions".to_string(), "Router address is required for straight to pool solutions".to_string(),
@@ -68,10 +70,11 @@ impl StrategyEncoder for StraightToPoolStrategyEncoder {
router_address, router_address,
}; };
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?; 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 let executor_address = Address::from_str(swap_encoder.executor_address())
Ok(protocol_data) .map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?;
Ok((protocol_data, executor_address))
} }
fn selector(&self, _exact_out: bool) -> &str { fn selector(&self, _exact_out: bool) -> &str {
unimplemented!(); "swap(uint256, bytes)"
} }
} }

View File

@@ -1,5 +1,5 @@
use crate::encoding::{ use crate::encoding::{
evm::strategy_encoder::encoder::{SplitSwapStrategyEncoder, StraightToPoolStrategyEncoder}, evm::strategy_encoder::encoder::{ExecutorEncoder, SplitSwapStrategyEncoder},
models::Solution, models::Solution,
strategy_encoder::{StrategyEncoder, StrategySelector}, strategy_encoder::{StrategyEncoder, StrategySelector},
}; };
@@ -9,7 +9,7 @@ pub struct EVMStrategySelector;
impl StrategySelector for EVMStrategySelector { impl StrategySelector for EVMStrategySelector {
fn select_strategy(&self, solution: &Solution) -> Box<dyn StrategyEncoder> { fn select_strategy(&self, solution: &Solution) -> Box<dyn StrategyEncoder> {
if solution.straight_to_pool { if solution.straight_to_pool {
Box::new(StraightToPoolStrategyEncoder {}) Box::new(ExecutorEncoder {})
} else { } else {
Box::new(SplitSwapStrategyEncoder {}) Box::new(SplitSwapStrategyEncoder {})
} }

View File

@@ -1,3 +1,4 @@
use alloy_primitives::Address;
use num_bigint::BigUint; use num_bigint::BigUint;
use tycho_core::{dto::ProtocolComponent, Bytes}; use tycho_core::{dto::ProtocolComponent, Bytes};
@@ -60,6 +61,8 @@ pub struct Transaction {
pub data: Vec<u8>, pub data: Vec<u8>,
// ETH value to be sent with the transaction. // ETH value to be sent with the transaction.
pub value: BigUint, pub value: BigUint,
// Address of the contract to call with the calldata
pub to: Address,
} }
#[allow(dead_code)] #[allow(dead_code)]

View File

@@ -1,8 +1,10 @@
use alloy_primitives::Address;
use crate::encoding::{errors::EncodingError, models::Solution}; use crate::encoding::{errors::EncodingError, models::Solution};
#[allow(dead_code)] #[allow(dead_code)]
pub trait StrategyEncoder { pub trait StrategyEncoder {
fn encode_strategy(&self, to_encode: Solution) -> Result<Vec<u8>, EncodingError>; fn encode_strategy(&self, to_encode: Solution) -> Result<(Vec<u8>, Address), EncodingError>;
fn selector(&self, exact_out: bool) -> &str; fn selector(&self, exact_out: bool) -> &str;
} }