feat: Refactor Registries

Interface changes:
- Rename StrategySelector to StrategyEncoderRegistry
- Implement clone for SwapEncoder

The StrategyEncoderRegistry needs to be initialised at the highest level and the passed to the TychoEncoder.
The TychoEncoder doesn't hold the chain nor the signer_pk as attributes anymore
The StrategyEncoderRegistry does:
- Initialises the SwapEncoderRegistry
- Initialises all the strategies and saves them in a HashMap
- Later, the TychoEncoder only reads from this hashmap

The StrategyEncoder now each holds a SwapEncoderRegistry as an attribute and they use this to get the correct SwapEncoder instead of reading from the global SWAP_ENCODER_REGISTRY

Simplified the SwapEncoderRegistry to not need a Config (everything is done inside itself)
All SwapEncoders implement clone

Took 2 hours 29 minutes

Took 11 seconds

Took 2 minutes
This commit is contained in:
Diana Carvalho
2025-02-04 13:30:01 +00:00
parent a92ba96d86
commit 23875b8b02
15 changed files with 206 additions and 148 deletions

View File

@@ -1,6 +1,6 @@
use tycho_core::{models::Chain, Bytes};
use crate::encoding::{errors::EncodingError, models::Solution};
use crate::encoding::{errors::EncodingError, models::Solution, swap_encoder::SwapEncoder};
pub trait StrategyEncoder {
fn encode_strategy(
@@ -8,13 +8,19 @@ pub trait StrategyEncoder {
to_encode: Solution,
router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError>;
#[allow(clippy::borrowed_box)]
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>>;
}
pub trait StrategySelector {
fn select_strategy(
&self,
solution: &Solution,
pub trait StrategyEncoderRegistry {
fn new(
chain: Chain,
executors_file_path: &str,
signer_pk: Option<String>,
chain_id: Chain,
) -> Result<Box<dyn StrategyEncoder>, EncodingError>;
) -> Result<Self, EncodingError>
where
Self: Sized;
#[allow(clippy::borrowed_box)]
fn get_encoder(&self, solution: &Solution) -> Result<&Box<dyn StrategyEncoder>, EncodingError>;
}