chore: Rename registry -> swap_encoder_registry
Took 14 minutes Took 8 seconds
This commit is contained in:
36
src/encoding/evm/swap_encoder/swap_encoder_registry.rs
Normal file
36
src/encoding/evm/swap_encoder/swap_encoder_registry.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
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(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
|
||||
.get(&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()?;
|
||||
encoders.insert(protocol.to_string(), encoder);
|
||||
}
|
||||
|
||||
Ok(Self { encoders })
|
||||
}
|
||||
|
||||
#[allow(clippy::borrowed_box)]
|
||||
pub fn get_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
|
||||
self.encoders.get(protocol_system)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user