feat: Add clone to EVMTychoEncoder

--- don't change below this line ---
ENG-4088 Took 4 minutes

Took 8 seconds

Took 48 seconds
This commit is contained in:
Diana Carvalho
2025-02-06 11:49:11 +00:00
parent 4680a4be24
commit b333d60d69
6 changed files with 32 additions and 2 deletions

View File

@@ -25,10 +25,11 @@ use crate::encoding::{
/// Struct for managing Permit2 operations, including encoding approvals and fetching allowance
/// data.
#[derive(Clone)]
pub struct Permit2 {
address: Address,
client: Arc<RootProvider<BoxTransport>>,
runtime: Runtime,
runtime: Arc<Runtime>,
signer: PrivateKeySigner,
chain_id: ChainId,
}
@@ -73,7 +74,7 @@ impl Permit2 {
address: Address::from_str("0x000000000022D473030F116dDEE9F6B43aC78BA3")
.map_err(|_| EncodingError::FatalError("Permit2 address not valid".to_string()))?,
client,
runtime,
runtime: Arc::new(runtime),
signer,
chain_id: chain.id,
})

View File

View File

@@ -58,3 +58,15 @@ impl StrategyEncoderRegistry for EVMStrategyEncoderRegistry {
}
}
}
impl Clone for EVMStrategyEncoderRegistry {
fn clone(&self) -> Self {
Self {
strategies: self
.strategies
.iter()
.map(|(k, v)| (k.clone(), v.clone_box()))
.collect(),
}
}
}

View File

@@ -73,6 +73,7 @@ pub trait EVMStrategyEncoder: StrategyEncoder {
/// * `permit2`: Permit2, responsible for managing permit2 operations and providing necessary
/// signatures and permit2 objects for calling the router
/// * `selector`: String, the selector for the swap function in the router contract
#[derive(Clone)]
pub struct SplitSwapStrategyEncoder {
swap_encoder_registry: SwapEncoderRegistry,
permit2: Permit2,
@@ -405,6 +406,10 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
self.swap_encoder_registry
.get_encoder(protocol_system)
}
fn clone_box(&self) -> Box<dyn StrategyEncoder> {
Box::new(self.clone())
}
}
/// This strategy encoder is used for solutions that are sent directly to the executor, bypassing
@@ -412,6 +417,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
///
/// # Fields
/// * `swap_encoder_registry`: SwapEncoderRegistry, containing all possible swap encoders
#[derive(Clone)]
pub struct ExecutorStrategyEncoder {
swap_encoder_registry: SwapEncoderRegistry,
}
@@ -459,10 +465,15 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
.map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?;
Ok((protocol_data, executor_address))
}
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
self.swap_encoder_registry
.get_encoder(protocol_system)
}
fn clone_box(&self) -> Box<dyn StrategyEncoder> {
Box::new(self.clone())
}
}
#[cfg(test)]

View File

@@ -19,6 +19,7 @@ use crate::encoding::{
/// * `router_address`: Bytes, the address of the router to use to execute the swaps.
/// * `native_address`: Address of the chain's native token
/// * `wrapped_address`: Address of the chain's wrapped native token
#[derive(Clone)]
pub struct EVMTychoEncoder<S: StrategyEncoderRegistry> {
strategy_registry: S,
router_address: Bytes,
@@ -183,6 +184,7 @@ mod tests {
}
}
#[derive(Clone)]
struct MockStrategy;
impl StrategyEncoder for MockStrategy {
@@ -202,6 +204,9 @@ mod tests {
fn get_swap_encoder(&self, _protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
None
}
fn clone_box(&self) -> Box<dyn StrategyEncoder> {
Box::new(self.clone())
}
}
fn get_mocked_tycho_encoder() -> EVMTychoEncoder<MockStrategyRegistry> {

View File

@@ -16,6 +16,7 @@ pub trait StrategyEncoder {
#[allow(clippy::borrowed_box)]
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>>;
fn clone_box(&self) -> Box<dyn StrategyEncoder>;
}
/// Contains the supported strategies to encode a solution, and chooses the best strategy to encode