feat: Remove batch execute logic from StrategyEncoder

Add action_type and selector to StrategyEncoder
Rename SequentialExactInStrategyEncoder -> SequentialStrategyEncoder
This commit is contained in:
Diana Carvalho
2025-01-14 15:25:29 +00:00
parent 93410b4fe2
commit 68c5a914eb
3 changed files with 74 additions and 38 deletions

View File

@@ -22,8 +22,22 @@ impl<S: StrategySelector, A: UserApprovalsManager> RouterEncoder<S, A> {
let mut calldata_list: Vec<Vec<u8>> = Vec::new();
let encode_for_batch_execute = solution.orders.len() > 1;
for order in solution.orders {
let exact_out = order.exact_out.clone();
let straight_to_pool = order.straight_to_pool.clone();
let strategy = self.strategy_selector.select_strategy(&order);
let contract_interaction = strategy.encode_strategy(order, encode_for_batch_execute)?;
let method_calldata = strategy.encode_strategy(order)?;
let contract_interaction = if encode_for_batch_execute {
let args = (strategy.action_type(exact_out) as u16, method_calldata);
args.abi_encode()
} else {
if straight_to_pool {
method_calldata
} else {
encode_input(strategy.selector(exact_out), method_calldata)
}
};
calldata_list.push(contract_interaction);
}
if encode_for_batch_execute {

View File

@@ -6,14 +6,13 @@ use crate::encoding::models::{
ActionType, EncodingContext, NativeAction, Order, PROPELLER_ROUTER_ADDRESS,
};
use crate::encoding::swap_encoder::{get_swap_encoder, get_swap_executor_address};
use crate::encoding::utils::{biguint_to_u256, encode_input, ple_encode};
use crate::encoding::utils::{biguint_to_u256, ple_encode};
pub trait StrategyEncoder {
fn encode_strategy(
&self,
to_encode: Order,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error>;
fn encode_strategy(&self, to_encode: Order) -> Result<Vec<u8>, Error>;
fn action_type(&self, exact_out: bool) -> ActionType;
fn selector(&self, exact_out: bool) -> &str;
fn encode_protocol_header(
&self,
@@ -33,23 +32,31 @@ pub trait StrategyEncoder {
pub struct SingleSwapStrategyEncoder {}
impl StrategyEncoder for SingleSwapStrategyEncoder {
fn encode_strategy(
&self,
order: Order,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
fn encode_strategy(&self, order: Order) -> Result<Vec<u8>, Error> {
todo!()
}
fn action_type(&self, exact_out: bool) -> ActionType {
if exact_out {
ActionType::SingleExactOut
} else {
ActionType::SingleExactIn
}
}
fn selector(&self, exact_out: bool) -> &str {
if exact_out {
"singleExactOut(uint256, bytes)"
} else {
"singleExactIn(uint256, bytes)"
}
}
}
pub struct SequentialExactInStrategyEncoder {}
pub struct SequentialStrategyEncoder {}
impl StrategyEncoder for SequentialExactInStrategyEncoder {
fn encode_strategy(
&self,
order: Order,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
impl StrategyEncoder for SequentialStrategyEncoder {
fn encode_strategy(&self, order: Order) -> Result<Vec<u8>, Error> {
let mut check_amount = order.check_amount.clone();
if order.slippage.is_some() {
let one_hundred = BigUint::from(100u32);
@@ -83,9 +90,6 @@ impl StrategyEncoder for SequentialExactInStrategyEncoder {
swaps.push(swap_data);
}
let selector = "sequentialExactIn(uint256, uint256, bytes[])";
let action_type = ActionType::SequentialExactIn;
let encoded_swaps = ple_encode(swaps);
let (mut unwrap, mut wrap) = (false, false);
@@ -103,11 +107,22 @@ impl StrategyEncoder for SequentialExactInStrategyEncoder {
encoded_swaps,
)
.abi_encode();
if encode_for_batch_execute {
let args = (action_type as u16, method_calldata);
Ok(args.abi_encode())
Ok(method_calldata)
}
fn action_type(&self, exact_out: bool) -> ActionType {
if exact_out {
ActionType::SequentialExactOut
} else {
Ok(encode_input(selector, method_calldata))
ActionType::SequentialExactIn
}
}
fn selector(&self, exact_out: bool) -> &str {
if exact_out {
"sequentialExactOut(uint256, uint256, bytes[])"
} else {
"sequentialExactIn(uint256, uint256, bytes[])"
}
}
}
@@ -115,13 +130,16 @@ impl StrategyEncoder for SequentialExactInStrategyEncoder {
pub struct SlipSwapStrategyEncoder {}
impl StrategyEncoder for SlipSwapStrategyEncoder {
fn encode_strategy(
&self,
order: Order,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
fn encode_strategy(&self, order: Order) -> Result<Vec<u8>, Error> {
todo!()
}
fn action_type(&self, _exact_out: bool) -> ActionType {
ActionType::SplitIn
}
fn selector(&self, _exact_out: bool) -> &str {
"splitExactIn(uint256, address, uint256, bytes[])"
}
}
/// This strategy encoder is used for orders that are sent directly to the pool.
@@ -129,11 +147,7 @@ impl StrategyEncoder for SlipSwapStrategyEncoder {
pub struct StraightToPoolStrategyEncoder {}
impl StrategyEncoder for StraightToPoolStrategyEncoder {
fn encode_strategy(
&self,
order: Order,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
fn encode_strategy(&self, order: Order) -> Result<Vec<u8>, Error> {
if order.router_address.is_none() {
return Err(anyhow::anyhow!(
"Router address is required for straight to pool orders"
@@ -152,4 +166,12 @@ impl StrategyEncoder for StraightToPoolStrategyEncoder {
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
Ok(protocol_data)
}
fn action_type(&self, _exact_out: bool) -> ActionType {
unimplemented!();
}
fn selector(&self, _exact_out: bool) -> &str {
unimplemented!();
}
}

View File

@@ -1,6 +1,6 @@
use crate::encoding::models::Order;
use crate::encoding::strategy_encoder::{
SequentialExactInStrategyEncoder, SingleSwapStrategyEncoder, SlipSwapStrategyEncoder,
SequentialStrategyEncoder, SingleSwapStrategyEncoder, SlipSwapStrategyEncoder,
StraightToPoolStrategyEncoder, StrategyEncoder,
};
@@ -17,7 +17,7 @@ impl StrategySelector for DefaultStrategySelector {
} else if order.swaps.len() == 1 {
Box::new(SingleSwapStrategyEncoder {})
} else if order.swaps.iter().all(|s| s.split == 0.0) {
Box::new(SequentialExactInStrategyEncoder {})
Box::new(SequentialStrategyEncoder {})
} else {
Box::new(SlipSwapStrategyEncoder {})
}