fix: Add new to SwapEncoder trait
Add executor_address in SwapEncoderBuilder::new and not in the builder pattern
This commit is contained in:
@@ -6,33 +6,22 @@ use std::str::FromStr;
|
|||||||
|
|
||||||
pub struct SwapEncoderBuilder {
|
pub struct SwapEncoderBuilder {
|
||||||
protocol_system: String,
|
protocol_system: String,
|
||||||
executor_address: Option<Address>,
|
executor_address: Address,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SwapEncoderBuilder {
|
impl SwapEncoderBuilder {
|
||||||
pub fn new(protocol_system: &str) -> Self {
|
pub fn new(protocol_system: &str, executor_address: &str) -> Self {
|
||||||
SwapEncoderBuilder {
|
SwapEncoderBuilder {
|
||||||
protocol_system: protocol_system.to_string(),
|
protocol_system: protocol_system.to_string(),
|
||||||
executor_address: None,
|
executor_address: Address::from_str(executor_address)
|
||||||
|
.expect(&format!("Invalid address: {}", executor_address)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn executor_address(mut self, address: &str) -> Self {
|
|
||||||
self.executor_address =
|
|
||||||
Some(Address::from_str(address).expect(&format!("Invalid address: {}", address)));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn build(self) -> Result<Box<dyn SwapEncoder>, String> {
|
pub fn build(self) -> Result<Box<dyn SwapEncoder>, String> {
|
||||||
let executor_address = self.executor_address.ok_or_else(|| {
|
|
||||||
format!(
|
|
||||||
"Executor address must be provided for protocol: {}",
|
|
||||||
self.protocol_system
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
match self.protocol_system.as_str() {
|
match self.protocol_system.as_str() {
|
||||||
"uniswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(executor_address))),
|
"uniswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(self.executor_address))),
|
||||||
"vm:balancer_v2" => Ok(Box::new(BalancerV2SwapEncoder::new(executor_address))),
|
"vm:balancer_v2" => Ok(Box::new(BalancerV2SwapEncoder::new(self.executor_address))),
|
||||||
_ => Err(format!("Unknown protocol system: {}", self.protocol_system)),
|
_ => Err(format!("Unknown protocol system: {}", self.protocol_system)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ impl SwapEncoderRegistry {
|
|||||||
let mut encoders = HashMap::new();
|
let mut encoders = HashMap::new();
|
||||||
|
|
||||||
for (protocol, executor_address) in config.executors {
|
for (protocol, executor_address) in config.executors {
|
||||||
let builder = SwapEncoderBuilder::new(&protocol).executor_address(&executor_address);
|
let builder = SwapEncoderBuilder::new(&protocol, &executor_address);
|
||||||
let encoder = builder.build().expect(&format!(
|
let encoder = builder.build().expect(&format!(
|
||||||
"Failed to build swap encoder for protocol: {}",
|
"Failed to build swap encoder for protocol: {}",
|
||||||
protocol
|
protocol
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use anyhow::Error;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
pub trait SwapEncoder: Sync + Send {
|
pub trait SwapEncoder: Sync + Send {
|
||||||
|
fn new(executor_address: Address) -> Self;
|
||||||
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error>;
|
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error>;
|
||||||
fn executor_address(&self) -> Address;
|
fn executor_address(&self) -> Address;
|
||||||
}
|
}
|
||||||
@@ -15,12 +16,11 @@ pub struct UniswapV2SwapEncoder {
|
|||||||
executor_address: Address,
|
executor_address: Address,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UniswapV2SwapEncoder {
|
impl UniswapV2SwapEncoder {}
|
||||||
pub fn new(executor_address: Address) -> Self {
|
impl SwapEncoder for UniswapV2SwapEncoder {
|
||||||
|
fn new(executor_address: Address) -> Self {
|
||||||
Self { executor_address }
|
Self { executor_address }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
impl SwapEncoder for UniswapV2SwapEncoder {
|
|
||||||
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error> {
|
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
@@ -35,17 +35,14 @@ pub struct BalancerV2SwapEncoder {
|
|||||||
vault_address: Address,
|
vault_address: Address,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BalancerV2SwapEncoder {
|
impl SwapEncoder for BalancerV2SwapEncoder {
|
||||||
pub fn new(executor_address: Address) -> Self {
|
fn new(executor_address: Address) -> Self {
|
||||||
Self {
|
Self {
|
||||||
executor_address,
|
executor_address,
|
||||||
vault_address: Address::from_str("0xba12222222228d8ba445958a75a0704d566bf2c8")
|
vault_address: Address::from_str("0xba12222222228d8ba445958a75a0704d566bf2c8")
|
||||||
.expect("Invalid string for balancer vault address"),
|
.expect("Invalid string for balancer vault address"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl SwapEncoder for BalancerV2SwapEncoder {
|
|
||||||
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error> {
|
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error> {
|
||||||
let token_approvals_manager = ProtocolApprovalsManager::new();
|
let token_approvals_manager = ProtocolApprovalsManager::new();
|
||||||
let runtime = tokio::runtime::Handle::try_current()
|
let runtime = tokio::runtime::Handle::try_current()
|
||||||
|
|||||||
Reference in New Issue
Block a user