feat: Add methods to builder to set chain and strategy independently
--- don't change below this line --- ENG-4246 Took 15 minutes
This commit is contained in:
@@ -21,7 +21,9 @@ fn main() {
|
|||||||
.expect("Failed to create user address");
|
.expect("Failed to create user address");
|
||||||
|
|
||||||
// Initialize the encoder
|
// Initialize the encoder
|
||||||
let encoder = EVMEncoderBuilder::tycho_router(TychoCoreChain::Ethereum, swapper_pk, None)
|
let encoder = EVMEncoderBuilder::new()
|
||||||
|
.chain(TychoCoreChain::Ethereum)
|
||||||
|
.tycho_router(swapper_pk, None)
|
||||||
.expect("Failed to create encoder builder")
|
.expect("Failed to create encoder builder")
|
||||||
.build()
|
.build()
|
||||||
.expect("Failed to build encoder");
|
.expect("Failed to build encoder");
|
||||||
|
|||||||
@@ -103,14 +103,16 @@ fn encode_swaps(
|
|||||||
let solution: Solution = serde_json::from_str(input)?;
|
let solution: Solution = serde_json::from_str(input)?;
|
||||||
let chain = Chain::Ethereum;
|
let chain = Chain::Ethereum;
|
||||||
|
|
||||||
let encoder = if use_tycho_router {
|
let mut builder = EVMEncoderBuilder::new().chain(chain);
|
||||||
|
builder = if use_tycho_router {
|
||||||
let private_key = swapper_pk.ok_or(EncodingError::FatalError(
|
let private_key = swapper_pk.ok_or(EncodingError::FatalError(
|
||||||
"Swapper private key is required for tycho_router".to_string(),
|
"Swapper private key is required for tycho_router".to_string(),
|
||||||
))?;
|
))?;
|
||||||
EVMEncoderBuilder::tycho_router(chain, private_key, config_path)?.build()?
|
builder.tycho_router(private_key, config_path)?
|
||||||
} else {
|
} else {
|
||||||
EVMEncoderBuilder::direct_execution(chain, config_path)?.build()?
|
builder.direct_execution(config_path)?
|
||||||
};
|
};
|
||||||
|
let encoder = builder.build()?;
|
||||||
|
|
||||||
let transactions = encoder.encode_router_calldata(vec![solution])?;
|
let transactions = encoder.encode_router_calldata(vec![solution])?;
|
||||||
|
|
||||||
|
|||||||
@@ -11,34 +11,66 @@ use crate::encoding::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub struct EVMEncoderBuilder {
|
pub struct EVMEncoderBuilder {
|
||||||
strategy: Box<dyn StrategyEncoder>,
|
strategy: Option<Box<dyn StrategyEncoder>>,
|
||||||
chain: Chain,
|
chain: Option<Chain>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for EVMEncoderBuilder {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EVMEncoderBuilder {
|
impl EVMEncoderBuilder {
|
||||||
pub fn new(chain: Chain, strategy: Box<dyn StrategyEncoder>) -> Self {
|
pub fn new() -> Self {
|
||||||
EVMEncoderBuilder { chain, strategy }
|
EVMEncoderBuilder { chain: None, strategy: None }
|
||||||
|
}
|
||||||
|
pub fn chain(mut self, chain: Chain) -> Self {
|
||||||
|
self.chain = Some(chain);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub fn strategy_encoder(mut self, strategy: Box<dyn StrategyEncoder>) -> Self {
|
||||||
|
self.strategy = Some(strategy);
|
||||||
|
self
|
||||||
}
|
}
|
||||||
pub fn tycho_router(
|
pub fn tycho_router(
|
||||||
chain: Chain,
|
self,
|
||||||
swapper_pk: String,
|
swapper_pk: String,
|
||||||
executors_file_path: Option<String>,
|
executors_file_path: Option<String>,
|
||||||
) -> Result<Self, EncodingError> {
|
) -> Result<Self, EncodingError> {
|
||||||
|
if let Some(chain) = self.chain {
|
||||||
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?;
|
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?;
|
||||||
let strategy =
|
let strategy =
|
||||||
Box::new(SplitSwapStrategyEncoder::new(swapper_pk, chain, swap_encoder_registry)?);
|
Box::new(SplitSwapStrategyEncoder::new(swapper_pk, chain, swap_encoder_registry)?);
|
||||||
Ok(EVMEncoderBuilder { chain, strategy })
|
Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) })
|
||||||
|
} else {
|
||||||
|
Err(EncodingError::FatalError(
|
||||||
|
"Please set the chain before setting the strategy".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn direct_execution(
|
pub fn direct_execution(
|
||||||
chain: Chain,
|
self,
|
||||||
executors_file_path: Option<String>,
|
executors_file_path: Option<String>,
|
||||||
) -> Result<Self, EncodingError> {
|
) -> Result<Self, EncodingError> {
|
||||||
|
if let Some(chain) = self.chain {
|
||||||
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?;
|
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain)?;
|
||||||
let strategy = Box::new(ExecutorStrategyEncoder::new(swap_encoder_registry));
|
let strategy = Box::new(ExecutorStrategyEncoder::new(swap_encoder_registry));
|
||||||
Ok(EVMEncoderBuilder { chain, strategy })
|
Ok(EVMEncoderBuilder { chain: Some(chain), strategy: Some(strategy) })
|
||||||
|
} else {
|
||||||
|
Err(EncodingError::FatalError(
|
||||||
|
"Please set the chain before setting the strategy".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(self) -> Result<EVMTychoEncoder, EncodingError> {
|
pub fn build(self) -> Result<EVMTychoEncoder, EncodingError> {
|
||||||
EVMTychoEncoder::new(self.chain, self.strategy)
|
if let (Some(chain), Some(strategy)) = (self.chain, self.strategy) {
|
||||||
|
EVMTychoEncoder::new(chain, strategy)
|
||||||
|
} else {
|
||||||
|
Err(EncodingError::FatalError(
|
||||||
|
"Please set the chain and strategy before building the encoder".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user