Merge branch 'main' into router/hr/ENG-4239-usv4-swap-encoder
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
mod group_swaps;
|
||||
pub mod strategy_encoder_registry;
|
||||
mod strategy_encoders;
|
||||
pub mod strategy_encoders;
|
||||
mod strategy_validators;
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
evm::{
|
||||
strategy_encoder::strategy_encoders::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder},
|
||||
swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
|
||||
},
|
||||
models::{Chain, Solution},
|
||||
strategy_encoder::{StrategyEncoder, StrategyEncoderRegistry},
|
||||
};
|
||||
|
||||
/// Contains all supported strategies to encode a solution.
|
||||
///
|
||||
/// # Fields
|
||||
/// * `strategies` - A hashmap containing the name of the strategy as a key and the strategy encoder
|
||||
/// as a value.
|
||||
pub struct EVMStrategyEncoderRegistry {
|
||||
strategies: HashMap<String, Box<dyn StrategyEncoder>>,
|
||||
}
|
||||
|
||||
impl StrategyEncoderRegistry for EVMStrategyEncoderRegistry {
|
||||
fn new(
|
||||
chain: tycho_core::dto::Chain,
|
||||
executors_file_path: Option<String>,
|
||||
signer_pk: Option<String>,
|
||||
) -> Result<Self, EncodingError> {
|
||||
let chain = Chain::from(chain);
|
||||
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain.clone())?;
|
||||
|
||||
let mut strategies: HashMap<String, Box<dyn StrategyEncoder>> = HashMap::new();
|
||||
strategies.insert(
|
||||
"executor".to_string(),
|
||||
Box::new(ExecutorStrategyEncoder::new(swap_encoder_registry.clone())),
|
||||
);
|
||||
if let Some(signer) = signer_pk {
|
||||
strategies.insert(
|
||||
"split_swap".to_string(),
|
||||
Box::new(
|
||||
SplitSwapStrategyEncoder::new(signer, chain, swap_encoder_registry).unwrap(),
|
||||
),
|
||||
);
|
||||
}
|
||||
Ok(Self { strategies })
|
||||
}
|
||||
fn get_encoder(&self, solution: &Solution) -> Result<&Box<dyn StrategyEncoder>, EncodingError> {
|
||||
if solution.direct_execution {
|
||||
self.strategies
|
||||
.get("executor")
|
||||
.ok_or(EncodingError::FatalError("Executor strategy not found".to_string()))
|
||||
} else {
|
||||
self.strategies
|
||||
.get("split_swap")
|
||||
.ok_or(EncodingError::FatalError("Split swap strategy not found. Please pass the signer private key to the StrategySelector constructor".to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for EVMStrategyEncoderRegistry {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
strategies: self
|
||||
.strategies
|
||||
.iter()
|
||||
.map(|(k, v)| (k.clone(), v.clone_box()))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,13 +89,14 @@ pub struct SplitSwapStrategyEncoder {
|
||||
|
||||
impl SplitSwapStrategyEncoder {
|
||||
pub fn new(
|
||||
signer_pk: String,
|
||||
chain: Chain,
|
||||
swapper_pk: String,
|
||||
blockchain: tycho_core::dto::Chain,
|
||||
swap_encoder_registry: SwapEncoderRegistry,
|
||||
) -> Result<Self, EncodingError> {
|
||||
let chain = Chain::from(blockchain);
|
||||
let selector = "swap(uint256,address,address,uint256,bool,bool,uint256,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string();
|
||||
Ok(Self {
|
||||
permit2: Permit2::new(signer_pk, chain.clone())?,
|
||||
permit2: Permit2::new(swapper_pk, chain.clone())?,
|
||||
selector,
|
||||
swap_encoder_registry,
|
||||
native_address: chain.native_token()?,
|
||||
@@ -340,8 +341,8 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::encoding::models::Swap;
|
||||
|
||||
fn eth_chain() -> Chain {
|
||||
TychoCoreChain::Ethereum.into()
|
||||
fn eth_chain() -> TychoCoreChain {
|
||||
TychoCoreChain::Ethereum
|
||||
}
|
||||
|
||||
fn eth() -> Bytes {
|
||||
@@ -387,7 +388,6 @@ mod tests {
|
||||
// The receiver was generated with `makeAddr("bob") using forge`
|
||||
receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
|
||||
swaps: vec![swap],
|
||||
direct_execution: true,
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
slippage: None,
|
||||
native_action: None,
|
||||
@@ -445,7 +445,6 @@ mod tests {
|
||||
sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(),
|
||||
receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
|
||||
swaps: vec![swap.clone(), swap],
|
||||
direct_execution: true,
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
slippage: None,
|
||||
native_action: None,
|
||||
@@ -496,7 +495,6 @@ mod tests {
|
||||
// The receiver was generated with `makeAddr("bob") using forge`
|
||||
receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
|
||||
swaps: vec![swap_a, swap_b],
|
||||
direct_execution: true,
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
slippage: None,
|
||||
native_action: None,
|
||||
|
||||
Reference in New Issue
Block a user