Files
tycho-execution/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs
TAMARA LIPOWSKI e83b8d9aef feat: Take Chain object containing native/wrapped addresses
- This way this chain object contains everything we need, we don't need to worry about doing any transformation or calling any supplementary functions inside any of the encoders
- Needed to move our new Chain object to a higher level since this is used in the higher-level encoder traits. This required some weird default values in the constants in order to avoid using alloy's hex literal. I could have instead opted to make Bytes parse a string I think, though this would mean possibly returning an error at the constants level, which is not nice either.

Question:
- Do we want the user to be in charge of passing the native and wrapped token every single time? This may be a bit annoying for the user. For now, I have defaulted to those in constants.rs, this would take 5 mins to remove though if you don't like it, and it would get rid of this complicated bytes initialization.
2025-02-05 17:14:56 -05:00

52 lines
1.9 KiB
Rust

use std::collections::HashMap;
use crate::encoding::{
errors::EncodingError,
evm::{
strategy_encoder::strategy_encoders::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder},
swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
},
models::{Chain, Solution},
strategy_encoder::{StrategyEncoder, StrategyEncoderRegistry},
};
pub struct EVMStrategyEncoderRegistry {
strategies: HashMap<String, Box<dyn StrategyEncoder>>,
}
impl StrategyEncoderRegistry for EVMStrategyEncoderRegistry {
fn new(
chain: Chain,
executors_file_path: &str,
signer_pk: Option<String>,
) -> Result<Self, EncodingError> {
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain.clone())?;
let mut strategies: HashMap<String, Box<dyn StrategyEncoder>> = HashMap::new();
strategies.insert(
"executor".to_string(),
Box::new(ExecutorStrategyEncoder::new(swap_encoder_registry.clone())),
);
if let Some(signer) = signer_pk {
strategies.insert(
"split_swap".to_string(),
Box::new(
SplitSwapStrategyEncoder::new(signer, chain, swap_encoder_registry).unwrap(),
),
);
}
Ok(Self { strategies })
}
fn get_encoder(&self, solution: &Solution) -> Result<&Box<dyn StrategyEncoder>, EncodingError> {
if solution.direct_execution {
self.strategies
.get("executor")
.ok_or(EncodingError::FatalError("Executor strategy not found".to_string()))
} else {
self.strategies
.get("split_swap")
.ok_or(EncodingError::FatalError("Split swap strategy not found. Please pass the signer private key to the StrategySelector constructor".to_string()))
}
}
}