feat: Add StrategySelector
This way the user could be able to extend this easily
This commit is contained in:
@@ -3,5 +3,6 @@ mod models;
|
|||||||
mod permit2;
|
mod permit2;
|
||||||
mod router_encoder;
|
mod router_encoder;
|
||||||
mod strategy_encoder;
|
mod strategy_encoder;
|
||||||
|
mod strategy_selector;
|
||||||
mod swap_encoder;
|
mod swap_encoder;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use alloy_primitives::{Address, U256};
|
use alloy_primitives::U256;
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tycho_core::Bytes;
|
use tycho_core::Bytes;
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
use crate::encoding::models::{Order, Solution, PROPELLER_ROUTER_ADDRESS};
|
use crate::encoding::models::{Solution, PROPELLER_ROUTER_ADDRESS};
|
||||||
use crate::encoding::permit2::{Permit2, PermitRequest};
|
use crate::encoding::permit2::{Permit2, PermitRequest};
|
||||||
use crate::encoding::strategy_encoder::{
|
use crate::encoding::strategy_encoder::StrategyEncoder;
|
||||||
SequentialExactInStrategyEncoder, SingleSwapStrategyEncoder, SlipSwapStrategyEncoder,
|
use crate::encoding::strategy_selector::StrategySelector;
|
||||||
StrategyEncoder,
|
|
||||||
};
|
|
||||||
use crate::encoding::utils::{encode_input, ple_encode};
|
use crate::encoding::utils::{encode_input, ple_encode};
|
||||||
use alloy_sol_types::SolValue;
|
use alloy_sol_types::SolValue;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
struct RouterEncoder {}
|
struct RouterEncoder<S: StrategySelector> {
|
||||||
|
strategy_selector: S,
|
||||||
impl RouterEncoder {
|
}
|
||||||
|
impl<S: StrategySelector> RouterEncoder<S> {
|
||||||
|
pub fn new(strategy_selector: S) -> Self {
|
||||||
|
RouterEncoder { strategy_selector }
|
||||||
|
}
|
||||||
pub fn encode_router_calldata(&self, solution: Solution) -> Result<Vec<u8>, Error> {
|
pub fn encode_router_calldata(&self, solution: Solution) -> Result<Vec<u8>, Error> {
|
||||||
let permit_calldata = self.handle_approvals(&solution)?; // TODO: where should we append this?
|
let permit_calldata = self.handle_approvals(&solution)?; // TODO: where should we append this?
|
||||||
let mut calldata_list: Vec<Vec<u8>> = Vec::new();
|
let mut calldata_list: Vec<Vec<u8>> = Vec::new();
|
||||||
let encode_for_batch_execute = solution.orders.len() > 1;
|
let encode_for_batch_execute = solution.orders.len() > 1;
|
||||||
for order in solution.orders {
|
for order in solution.orders {
|
||||||
let strategy = self.get_strategy(&order);
|
let strategy = self.strategy_selector.select_strategy(&order);
|
||||||
let contract_interaction = strategy.encode_strategy(order, encode_for_batch_execute)?;
|
let contract_interaction = strategy.encode_strategy(order, encode_for_batch_execute)?;
|
||||||
calldata_list.push(contract_interaction);
|
calldata_list.push(contract_interaction);
|
||||||
}
|
}
|
||||||
@@ -44,14 +45,4 @@ impl RouterEncoder {
|
|||||||
}
|
}
|
||||||
Ok(Permit2::new().encode_permit(permits))
|
Ok(Permit2::new().encode_permit(permits))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_strategy(&self, order: &Order) -> &dyn StrategyEncoder {
|
|
||||||
if order.swaps.len() == 1 {
|
|
||||||
&SingleSwapStrategyEncoder {}
|
|
||||||
} else if order.swaps.iter().all(|s| s.split == 0.0) {
|
|
||||||
&SequentialExactInStrategyEncoder {}
|
|
||||||
} else {
|
|
||||||
&SlipSwapStrategyEncoder {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
use alloy_primitives::Address;
|
|
||||||
use alloy_sol_types::SolValue;
|
use alloy_sol_types::SolValue;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
use std::cmp::min;
|
|
||||||
|
|
||||||
use crate::encoding::models::{
|
use crate::encoding::models::{
|
||||||
ActionType, EncodingContext, NativeAction, Order, PROPELLER_ROUTER_ADDRESS,
|
ActionType, EncodingContext, NativeAction, Order, PROPELLER_ROUTER_ADDRESS,
|
||||||
|
|||||||
23
src/encoding/strategy_selector.rs
Normal file
23
src/encoding/strategy_selector.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use crate::encoding::models::Order;
|
||||||
|
use crate::encoding::strategy_encoder::{
|
||||||
|
SequentialExactInStrategyEncoder, SingleSwapStrategyEncoder, SlipSwapStrategyEncoder,
|
||||||
|
StrategyEncoder,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub trait StrategySelector {
|
||||||
|
fn select_strategy(&self, order: &Order) -> Box<dyn StrategyEncoder>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DefaultStrategySelector;
|
||||||
|
|
||||||
|
impl StrategySelector for DefaultStrategySelector {
|
||||||
|
fn select_strategy(&self, order: &Order) -> Box<dyn StrategyEncoder> {
|
||||||
|
if order.swaps.len() == 1 {
|
||||||
|
Box::new(SingleSwapStrategyEncoder {})
|
||||||
|
} else if order.swaps.iter().all(|s| s.split == 0.0) {
|
||||||
|
Box::new(SequentialExactInStrategyEncoder {})
|
||||||
|
} else {
|
||||||
|
Box::new(SlipSwapStrategyEncoder {})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user