From 68c5a914ebf7eef086c7fbeb8464e83f31670048 Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Tue, 14 Jan 2025 15:25:29 +0000 Subject: [PATCH] feat: Remove batch execute logic from StrategyEncoder Add action_type and selector to StrategyEncoder Rename SequentialExactInStrategyEncoder -> SequentialStrategyEncoder --- src/encoding/router_encoder.rs | 16 +++++- src/encoding/strategy_encoder.rs | 92 +++++++++++++++++++------------ src/encoding/strategy_selector.rs | 4 +- 3 files changed, 74 insertions(+), 38 deletions(-) diff --git a/src/encoding/router_encoder.rs b/src/encoding/router_encoder.rs index f741304..9a8491f 100644 --- a/src/encoding/router_encoder.rs +++ b/src/encoding/router_encoder.rs @@ -22,8 +22,22 @@ impl RouterEncoder { let mut calldata_list: Vec> = 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 { diff --git a/src/encoding/strategy_encoder.rs b/src/encoding/strategy_encoder.rs index ac280fb..610aa37 100644 --- a/src/encoding/strategy_encoder.rs +++ b/src/encoding/strategy_encoder.rs @@ -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, Error>; + fn encode_strategy(&self, to_encode: Order) -> Result, 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, Error> { + fn encode_strategy(&self, order: Order) -> Result, 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, Error> { +impl StrategyEncoder for SequentialStrategyEncoder { + fn encode_strategy(&self, order: Order) -> Result, 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, Error> { + fn encode_strategy(&self, order: Order) -> Result, 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, Error> { + fn encode_strategy(&self, order: Order) -> Result, 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!(); + } } diff --git a/src/encoding/strategy_selector.rs b/src/encoding/strategy_selector.rs index 602c9d6..4c01a9c 100644 --- a/src/encoding/strategy_selector.rs +++ b/src/encoding/strategy_selector.rs @@ -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 {}) }