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 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 exact_out = order.exact_out.clone();
let straight_to_pool = order.straight_to_pool.clone();
let strategy = self.strategy_selector.select_strategy(&order); 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); calldata_list.push(contract_interaction);
} }
if encode_for_batch_execute { if encode_for_batch_execute {

View File

@@ -6,14 +6,13 @@ use crate::encoding::models::{
ActionType, EncodingContext, NativeAction, Order, PROPELLER_ROUTER_ADDRESS, ActionType, EncodingContext, NativeAction, Order, PROPELLER_ROUTER_ADDRESS,
}; };
use crate::encoding::swap_encoder::{get_swap_encoder, get_swap_executor_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 { pub trait StrategyEncoder {
fn encode_strategy( fn encode_strategy(&self, to_encode: Order) -> Result<Vec<u8>, Error>;
&self,
to_encode: Order, fn action_type(&self, exact_out: bool) -> ActionType;
encode_for_batch_execute: bool, fn selector(&self, exact_out: bool) -> &str;
) -> Result<Vec<u8>, Error>;
fn encode_protocol_header( fn encode_protocol_header(
&self, &self,
@@ -33,23 +32,31 @@ pub trait StrategyEncoder {
pub struct SingleSwapStrategyEncoder {} pub struct SingleSwapStrategyEncoder {}
impl StrategyEncoder for SingleSwapStrategyEncoder { impl StrategyEncoder for SingleSwapStrategyEncoder {
fn encode_strategy( fn encode_strategy(&self, order: Order) -> Result<Vec<u8>, Error> {
&self,
order: Order,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
todo!() todo!()
} }
fn action_type(&self, exact_out: bool) -> ActionType {
if exact_out {
ActionType::SingleExactOut
} else {
ActionType::SingleExactIn
}
} }
pub struct SequentialExactInStrategyEncoder {} fn selector(&self, exact_out: bool) -> &str {
if exact_out {
"singleExactOut(uint256, bytes)"
} else {
"singleExactIn(uint256, bytes)"
}
}
}
impl StrategyEncoder for SequentialExactInStrategyEncoder { pub struct SequentialStrategyEncoder {}
fn encode_strategy(
&self, impl StrategyEncoder for SequentialStrategyEncoder {
order: Order, fn encode_strategy(&self, order: Order) -> Result<Vec<u8>, Error> {
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
let mut check_amount = order.check_amount.clone(); let mut check_amount = order.check_amount.clone();
if order.slippage.is_some() { if order.slippage.is_some() {
let one_hundred = BigUint::from(100u32); let one_hundred = BigUint::from(100u32);
@@ -83,9 +90,6 @@ impl StrategyEncoder for SequentialExactInStrategyEncoder {
swaps.push(swap_data); swaps.push(swap_data);
} }
let selector = "sequentialExactIn(uint256, uint256, bytes[])";
let action_type = ActionType::SequentialExactIn;
let encoded_swaps = ple_encode(swaps); let encoded_swaps = ple_encode(swaps);
let (mut unwrap, mut wrap) = (false, false); let (mut unwrap, mut wrap) = (false, false);
@@ -103,11 +107,22 @@ impl StrategyEncoder for SequentialExactInStrategyEncoder {
encoded_swaps, encoded_swaps,
) )
.abi_encode(); .abi_encode();
if encode_for_batch_execute { Ok(method_calldata)
let args = (action_type as u16, method_calldata); }
Ok(args.abi_encode())
fn action_type(&self, exact_out: bool) -> ActionType {
if exact_out {
ActionType::SequentialExactOut
} else { } 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 {} pub struct SlipSwapStrategyEncoder {}
impl StrategyEncoder for SlipSwapStrategyEncoder { impl StrategyEncoder for SlipSwapStrategyEncoder {
fn encode_strategy( fn encode_strategy(&self, order: Order) -> Result<Vec<u8>, Error> {
&self,
order: Order,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
todo!() 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. /// 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 {} pub struct StraightToPoolStrategyEncoder {}
impl StrategyEncoder for StraightToPoolStrategyEncoder { impl StrategyEncoder for StraightToPoolStrategyEncoder {
fn encode_strategy( fn encode_strategy(&self, order: Order) -> Result<Vec<u8>, Error> {
&self,
order: Order,
encode_for_batch_execute: bool,
) -> Result<Vec<u8>, Error> {
if order.router_address.is_none() { if order.router_address.is_none() {
return Err(anyhow::anyhow!( return Err(anyhow::anyhow!(
"Router address is required for straight to pool orders" "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)?; let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
Ok(protocol_data) 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::models::Order;
use crate::encoding::strategy_encoder::{ use crate::encoding::strategy_encoder::{
SequentialExactInStrategyEncoder, SingleSwapStrategyEncoder, SlipSwapStrategyEncoder, SequentialStrategyEncoder, SingleSwapStrategyEncoder, SlipSwapStrategyEncoder,
StraightToPoolStrategyEncoder, StrategyEncoder, StraightToPoolStrategyEncoder, StrategyEncoder,
}; };
@@ -17,7 +17,7 @@ impl StrategySelector for DefaultStrategySelector {
} else if order.swaps.len() == 1 { } else if order.swaps.len() == 1 {
Box::new(SingleSwapStrategyEncoder {}) Box::new(SingleSwapStrategyEncoder {})
} else if order.swaps.iter().all(|s| s.split == 0.0) { } else if order.swaps.iter().all(|s| s.split == 0.0) {
Box::new(SequentialExactInStrategyEncoder {}) Box::new(SequentialStrategyEncoder {})
} else { } else {
Box::new(SlipSwapStrategyEncoder {}) Box::new(SlipSwapStrategyEncoder {})
} }