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:
@@ -1,4 +1,5 @@
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
evm::swap_encoder::swap_encoders::{BalancerV2SwapEncoder, UniswapV2SwapEncoder},
|
||||
swap_encoder::SwapEncoder,
|
||||
};
|
||||
@@ -16,11 +17,14 @@ impl SwapEncoderBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(self) -> Result<Box<dyn SwapEncoder>, String> {
|
||||
pub fn build(self) -> Result<Box<dyn SwapEncoder>, EncodingError> {
|
||||
match self.protocol_system.as_str() {
|
||||
"uniswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(self.executor_address))),
|
||||
"vm:balancer_v2" => Ok(Box::new(BalancerV2SwapEncoder::new(self.executor_address))),
|
||||
_ => Err(format!("Unknown protocol system: {}", self.protocol_system)),
|
||||
_ => Err(EncodingError::FatalError(format!(
|
||||
"Unknown protocol system: {}",
|
||||
self.protocol_system
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,3 @@
|
||||
mod builder;
|
||||
mod registry;
|
||||
pub mod registry;
|
||||
mod swap_encoders;
|
||||
|
||||
use std::sync::RwLock;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use tycho_core::dto::Chain;
|
||||
|
||||
use crate::encoding::evm::swap_encoder::registry::{Config, SwapEncoderRegistry};
|
||||
|
||||
// TODO: init this at the higher level at some point
|
||||
lazy_static! {
|
||||
pub static ref SWAP_ENCODER_REGISTRY: RwLock<SwapEncoderRegistry> = {
|
||||
let config = Config::from_file("src/encoding/config/executor_addresses.json")
|
||||
.expect("Failed to load configuration file");
|
||||
RwLock::new(SwapEncoderRegistry::new(config, Chain::Ethereum))
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,33 +1,32 @@
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
use serde::Deserialize;
|
||||
use tycho_core::dto::Chain;
|
||||
use tycho_core::models::Chain;
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError, evm::swap_encoder::builder::SwapEncoderBuilder,
|
||||
swap_encoder::SwapEncoder,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SwapEncoderRegistry {
|
||||
encoders: HashMap<String, Box<dyn SwapEncoder>>,
|
||||
}
|
||||
|
||||
impl SwapEncoderRegistry {
|
||||
pub fn new(config: Config, blockchain: Chain) -> Self {
|
||||
pub fn new(executors_file_path: &str, blockchain: Chain) -> Result<Self, EncodingError> {
|
||||
let config_str = fs::read_to_string(executors_file_path)?;
|
||||
let config: HashMap<Chain, HashMap<String, String>> = serde_json::from_str(&config_str)?;
|
||||
let mut encoders = HashMap::new();
|
||||
let executors = config
|
||||
.executors
|
||||
.get(&blockchain)
|
||||
.unwrap_or_else(|| panic!("No executors found for blockchain: {}", blockchain));
|
||||
.ok_or(EncodingError::FatalError("No executors found for blockchain".to_string()))?;
|
||||
for (protocol, executor_address) in executors {
|
||||
let builder = SwapEncoderBuilder::new(protocol, executor_address);
|
||||
let encoder = builder.build().unwrap_or_else(|_| {
|
||||
panic!("Failed to build swap encoder for protocol: {}", protocol)
|
||||
});
|
||||
let encoder = builder.build()?;
|
||||
encoders.insert(protocol.to_string(), encoder);
|
||||
}
|
||||
|
||||
Self { encoders }
|
||||
Ok(Self { encoders })
|
||||
}
|
||||
|
||||
#[allow(clippy::borrowed_box)]
|
||||
@@ -35,17 +34,3 @@ impl SwapEncoderRegistry {
|
||||
self.encoders.get(protocol_system)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
pub executors: HashMap<Chain, HashMap<String, String>>, /* Blockchain -> {Protocol ->
|
||||
* Executor address} mapping */
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from_file(path: &str) -> Result<Self, EncodingError> {
|
||||
let config_str = fs::read_to_string(path)?;
|
||||
let config: Config = serde_json::from_str(&config_str)?;
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ use crate::encoding::{
|
||||
swap_encoder::SwapEncoder,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct UniswapV2SwapEncoder {
|
||||
executor_address: String,
|
||||
executor_selector: String,
|
||||
@@ -59,8 +60,13 @@ impl SwapEncoder for UniswapV2SwapEncoder {
|
||||
fn executor_selector(&self) -> &str {
|
||||
&self.executor_selector
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn SwapEncoder> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct UniswapV3SwapEncoder {
|
||||
executor_address: String,
|
||||
executor_selector: String,
|
||||
@@ -129,8 +135,12 @@ impl SwapEncoder for UniswapV3SwapEncoder {
|
||||
fn executor_selector(&self) -> &str {
|
||||
&self.executor_selector
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn SwapEncoder> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BalancerV2SwapEncoder {
|
||||
executor_address: String,
|
||||
executor_selector: String,
|
||||
@@ -179,6 +189,9 @@ impl SwapEncoder for BalancerV2SwapEncoder {
|
||||
fn executor_selector(&self) -> &str {
|
||||
&self.executor_selector
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn SwapEncoder> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user