From 6430c99d7665e855d950dde7f199e9f69cf1f642 Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Fri, 4 Apr 2025 15:47:30 +0100 Subject: [PATCH] feat: Delete EVMStrategyEncoder (this is now unnecessary) Moved ple_encode into utils.rs --- don't change below this line --- ENG-4306 Took 5 minutes --- .../evm/strategy_encoder/strategy_encoders.rs | 28 ++----------------- src/encoding/evm/utils.rs | 17 +++++++++++ 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index 4498c3b..f2192bb 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -16,7 +16,7 @@ use crate::encoding::{ swap_encoder::swap_encoder_registry::SwapEncoderRegistry, utils::{ biguint_to_u256, bytes_to_address, encode_input, get_min_amount_for_solution, - get_token_position, percentage_to_uint24, + get_token_position, percentage_to_uint24, ple_encode, }, }, models::{Chain, EncodingContext, NativeAction, Solution}, @@ -24,24 +24,6 @@ use crate::encoding::{ swap_encoder::SwapEncoder, }; -/// Encodes a solution using a specific strategy for execution on the EVM-compatible network. -pub trait EVMStrategyEncoder: StrategyEncoder { - /// Uses prefix-length encoding to efficient encode action data. - /// - /// Prefix-length encoding is a data encoding method where the beginning of a data segment - /// (the "prefix") contains information about the length of the following data. - fn ple_encode(&self, action_data_array: Vec>) -> Vec { - let mut encoded_action_data: Vec = Vec::new(); - - for action_data in action_data_array { - let args = (encoded_action_data, action_data.len() as u16, action_data); - encoded_action_data = args.abi_encode_packed(); - } - - encoded_action_data - } -} - /// Represents the encoder for a swap strategy which supports single swaps. /// /// # Fields @@ -90,8 +72,6 @@ impl SingleSwapStrategyEncoder { } } -impl EVMStrategyEncoder for SingleSwapStrategyEncoder {} - impl StrategyEncoder for SingleSwapStrategyEncoder { fn encode_strategy(&self, solution: Solution) -> Result<(Vec, Bytes), EncodingError> { let grouped_swaps = group_swaps(solution.clone().swaps); @@ -285,8 +265,6 @@ impl SplitSwapStrategyEncoder { } } -impl EVMStrategyEncoder for SplitSwapStrategyEncoder {} - impl StrategyEncoder for SplitSwapStrategyEncoder { fn encode_strategy(&self, solution: Solution) -> Result<(Vec, Bytes), EncodingError> { self.split_swap_validator @@ -387,7 +365,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { swaps.push(swap_data); } - let encoded_swaps = self.ple_encode(swaps); + let encoded_swaps = ple_encode(swaps); let tokens_len = if solution.given_token == solution.checked_token { tokens.len() - 1 } else { @@ -458,7 +436,7 @@ impl ExecutorStrategyEncoder { Self { swap_encoder_registry } } } -impl EVMStrategyEncoder for ExecutorStrategyEncoder {} + impl StrategyEncoder for ExecutorStrategyEncoder { fn encode_strategy(&self, solution: Solution) -> Result<(Vec, Bytes), EncodingError> { let grouped_swaps = group_swaps(solution.clone().swaps); diff --git a/src/encoding/evm/utils.rs b/src/encoding/evm/utils.rs index b8af4f5..0ac8f01 100644 --- a/src/encoding/evm/utils.rs +++ b/src/encoding/evm/utils.rs @@ -1,6 +1,7 @@ use std::{cmp::max, sync::Arc}; use alloy_primitives::{aliases::U24, keccak256, Address, FixedBytes, Keccak256, U256, U8}; +use alloy_sol_types::SolValue; use num_bigint::BigUint; use tokio::runtime::{Handle, Runtime}; use tycho_common::Bytes; @@ -133,6 +134,22 @@ pub fn get_runtime() -> Result<(Handle, Option>), EncodingError> { } } } + +/// Uses prefix-length encoding to efficient encode action data. +/// +/// Prefix-length encoding is a data encoding method where the beginning of a data segment +/// (the "prefix") contains information about the length of the following data. +pub fn ple_encode(action_data_array: Vec>) -> Vec { + let mut encoded_action_data: Vec = Vec::new(); + + for action_data in action_data_array { + let args = (encoded_action_data, action_data.len() as u16, action_data); + encoded_action_data = args.abi_encode_packed(); + } + + encoded_action_data +} + #[cfg(test)] mod tests { use num_bigint::BigUint;