- 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.
35 lines
1.2 KiB
Rust
35 lines
1.2 KiB
Rust
use std::{collections::HashMap, fs};
|
|
|
|
use crate::encoding::{
|
|
errors::EncodingError, evm::swap_encoder::builder::SwapEncoderBuilder, models::Chain,
|
|
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<String, HashMap<String, String>> = serde_json::from_str(&config_str)?;
|
|
let mut encoders = HashMap::new();
|
|
let executors = config
|
|
.get(&blockchain.name)
|
|
.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)
|
|
}
|
|
}
|