From 304740574b31b9af6ce84971e2fe912ee1627cd8 Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Tue, 22 Apr 2025 12:41:59 +0100 Subject: [PATCH] chore: get_transfer_type should receive a SwapGroup and not a Swap Only the first swap of a SwapGroup would actually get a transfer in, so we don't need to do this inside of the swaps loop Rename in_token and out_token to token_in and token_out in SwapGroup --- don't change below this line --- ENG-4446 Took 22 minutes --- src/encoding/evm/group_swaps.rs | 42 ++++----- .../evm/strategy_encoder/strategy_encoders.rs | 89 +++++++++---------- .../transfer_optimizations.rs | 66 ++++++-------- 3 files changed, 91 insertions(+), 106 deletions(-) diff --git a/src/encoding/evm/group_swaps.rs b/src/encoding/evm/group_swaps.rs index 1409b9f..5ec5fe6 100644 --- a/src/encoding/evm/group_swaps.rs +++ b/src/encoding/evm/group_swaps.rs @@ -6,15 +6,15 @@ use crate::encoding::{evm::constants::GROUPABLE_PROTOCOLS, models::Swap}; /// optimization. /// /// # Fields -/// * `input_token`: Bytes, the input token of the first swap -/// * `output_token`: Bytes, the output token of the final swap +/// * `token_in`: Bytes, the input token of the first swap +/// * `token_out`: Bytes, the output token of the final swap /// * `protocol_system`: String, the protocol system of the swaps /// * `swaps`: Vec, the sequence of swaps to be executed as a group /// * `split`: f64, the split percentage of the first swap in the group #[derive(Clone, PartialEq, Debug)] pub struct SwapGroup { - pub input_token: Bytes, - pub output_token: Bytes, + pub token_in: Bytes, + pub token_out: Bytes, pub protocol_system: String, pub swaps: Vec, pub split: f64, @@ -44,7 +44,7 @@ pub fn group_swaps(swaps: Vec) -> Vec { if let Some(group) = current_group.as_mut() { group.swaps.push(swap.clone()); // Update the output token of the current group. - group.output_token = swap.token_out.clone(); + group.token_out = swap.token_out.clone(); } } else { // Not second or later USV4 pool. Push the current group (if it exists) and then @@ -53,8 +53,8 @@ pub fn group_swaps(swaps: Vec) -> Vec { grouped_swaps.push(group.clone()); } current_group = Some(SwapGroup { - input_token: swap.token_in.clone(), - output_token: swap.token_out.clone(), + token_in: swap.token_in.clone(), + token_out: swap.token_out.clone(), protocol_system: current_swap_protocol.clone(), swaps: vec![swap.clone()], split: swap.split, @@ -135,15 +135,15 @@ mod tests { vec![ SwapGroup { swaps: vec![swap_weth_wbtc, swap_wbtc_usdc], - input_token: weth, - output_token: usdc.clone(), + token_in: weth, + token_out: usdc.clone(), protocol_system: "uniswap_v4".to_string(), split: 0f64, }, SwapGroup { swaps: vec![swap_usdc_dai], - input_token: usdc, - output_token: dai, + token_in: usdc, + token_out: dai, protocol_system: "uniswap_v2".to_string(), split: 0f64, } @@ -216,22 +216,22 @@ mod tests { vec![ SwapGroup { swaps: vec![swap_wbtc_weth], - input_token: wbtc.clone(), - output_token: weth.clone(), + token_in: wbtc.clone(), + token_out: weth.clone(), protocol_system: "uniswap_v4".to_string(), split: 0f64, }, SwapGroup { swaps: vec![swap_weth_usdc], - input_token: weth.clone(), - output_token: usdc.clone(), + token_in: weth.clone(), + token_out: usdc.clone(), protocol_system: "uniswap_v4".to_string(), split: 0.5f64, }, SwapGroup { swaps: vec![swap_weth_dai, swap_dai_usdc], - input_token: weth, - output_token: usdc, + token_in: weth, + token_out: usdc, protocol_system: "uniswap_v4".to_string(), split: 0f64, } @@ -304,15 +304,15 @@ mod tests { vec![ SwapGroup { swaps: vec![swap_weth_wbtc, swap_wbtc_usdc], - input_token: weth.clone(), - output_token: usdc.clone(), + token_in: weth.clone(), + token_out: usdc.clone(), protocol_system: "vm:balancer_v3".to_string(), split: 0.5f64, }, SwapGroup { swaps: vec![swap_weth_dai, swap_dai_usdc], - input_token: weth, - output_token: usdc, + token_in: weth, + token_out: usdc, protocol_system: "uniswap_v4".to_string(), split: 0f64, } diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index bad91bf..1d473c1 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -125,20 +125,20 @@ impl StrategyEncoder for SingleSwapStrategyEncoder { let swap_receiver = if !unwrap { solution.receiver.clone() } else { self.router_address.clone() }; + let transfer_type = self + .transfer_optimization + .get_transfer_type(grouped_swap.clone(), solution.given_token.clone(), wrap, false); + let encoding_context = EncodingContext { + receiver: swap_receiver.clone(), + exact_out: solution.exact_out, + router_address: Some(self.router_address.clone()), + group_token_in: grouped_swap.token_in.clone(), + group_token_out: grouped_swap.token_out.clone(), + transfer_type: transfer_type.clone(), + }; + let mut grouped_protocol_data: Vec = vec![]; for swap in grouped_swap.swaps.iter() { - let transfer_type = self - .transfer_optimization - .get_transfer_type(swap.clone(), solution.given_token.clone(), wrap, false); - - let encoding_context = EncodingContext { - receiver: swap_receiver.clone(), - exact_out: solution.exact_out, - router_address: Some(self.router_address.clone()), - group_token_in: grouped_swap.input_token.clone(), - group_token_out: grouped_swap.output_token.clone(), - transfer_type: transfer_type.clone(), - }; let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?; grouped_protocol_data.extend(protocol_data); } @@ -327,25 +327,25 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder { solution.receiver.clone() // last swap - there is not next swap }; + let transfer_type = self + .transfer_optimization + .get_transfer_type( + grouped_swap.clone(), + solution.given_token.clone(), + wrap, + in_between_swap_optimization, + ); + let encoding_context = EncodingContext { + receiver: swap_receiver.clone(), + exact_out: solution.exact_out, + router_address: Some(self.router_address.clone()), + group_token_in: grouped_swap.token_in.clone(), + group_token_out: grouped_swap.token_out.clone(), + transfer_type: transfer_type.clone(), + }; + let mut grouped_protocol_data: Vec = vec![]; for swap in grouped_swap.swaps.iter() { - let transfer_type = self - .transfer_optimization - .get_transfer_type( - swap.clone(), - solution.given_token.clone(), - wrap, - in_between_swap_optimization, - ); - - let encoding_context = EncodingContext { - receiver: swap_receiver.clone(), - exact_out: solution.exact_out, - router_address: Some(self.router_address.clone()), - group_token_in: grouped_swap.input_token.clone(), - group_token_out: grouped_swap.output_token.clone(), - transfer_type: transfer_type.clone(), - }; let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?; grouped_protocol_data.extend(protocol_data); @@ -517,7 +517,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { let intermediary_tokens: HashSet = grouped_swaps .iter() .flat_map(|grouped_swap| { - vec![grouped_swap.input_token.clone(), grouped_swap.output_token.clone()] + vec![grouped_swap.token_in.clone(), grouped_swap.token_out.clone()] }) .collect(); let mut intermediary_tokens: Vec = intermediary_tokens @@ -562,34 +562,33 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { )) })?; - let swap_receiver = if !unwrap && grouped_swap.output_token == solution.checked_token { + let swap_receiver = if !unwrap && grouped_swap.token_out == solution.checked_token { solution.receiver.clone() } else { self.router_address.clone() }; + let transfer_type = self + .transfer_optimization + .get_transfer_type(grouped_swap.clone(), solution.given_token.clone(), wrap, false); + let encoding_context = EncodingContext { + receiver: swap_receiver.clone(), + exact_out: solution.exact_out, + router_address: Some(self.router_address.clone()), + group_token_in: grouped_swap.token_in.clone(), + group_token_out: grouped_swap.token_out.clone(), + transfer_type: transfer_type.clone(), + }; let mut grouped_protocol_data: Vec = vec![]; for swap in grouped_swap.swaps.iter() { - let transfer_type = self - .transfer_optimization - .get_transfer_type(swap.clone(), solution.given_token.clone(), wrap, false); - - let encoding_context = EncodingContext { - receiver: swap_receiver.clone(), - exact_out: solution.exact_out, - router_address: Some(self.router_address.clone()), - group_token_in: grouped_swap.input_token.clone(), - group_token_out: grouped_swap.output_token.clone(), - transfer_type: transfer_type.clone(), - }; let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?; grouped_protocol_data.extend(protocol_data); } let swap_data = self.encode_swap_header( - get_token_position(tokens.clone(), grouped_swap.input_token.clone())?, - get_token_position(tokens.clone(), grouped_swap.output_token.clone())?, + get_token_position(tokens.clone(), grouped_swap.token_in.clone())?, + get_token_position(tokens.clone(), grouped_swap.token_out.clone())?, percentage_to_uint24(grouped_swap.split), Bytes::from_str(swap_encoder.executor_address()).map_err(|_| { EncodingError::FatalError("Invalid executor address".to_string()) diff --git a/src/encoding/evm/strategy_encoder/transfer_optimizations.rs b/src/encoding/evm/strategy_encoder/transfer_optimizations.rs index 2c19fec..3d0b8a1 100644 --- a/src/encoding/evm/strategy_encoder/transfer_optimizations.rs +++ b/src/encoding/evm/strategy_encoder/transfer_optimizations.rs @@ -1,8 +1,8 @@ use tycho_common::Bytes; use crate::encoding::{ - evm::constants::IN_TRANSFER_REQUIRED_PROTOCOLS, - models::{Swap, TransferType}, + evm::{constants::IN_TRANSFER_REQUIRED_PROTOCOLS, group_swaps::SwapGroup}, + models::TransferType, }; /// A struct that defines how the tokens will be transferred into the given pool given the solution. @@ -20,13 +20,13 @@ impl TransferOptimization { /// Returns the transfer method that should be used for the given swap and solution. pub fn get_transfer_type( &self, - swap: Swap, + swap: SwapGroup, given_token: Bytes, wrap: bool, in_between_swap_optimization: bool, ) -> TransferType { let in_transfer_required: bool = - IN_TRANSFER_REQUIRED_PROTOCOLS.contains(&swap.component.protocol_system.as_str()); + IN_TRANSFER_REQUIRED_PROTOCOLS.contains(&swap.protocol_system.as_str()); let is_first_swap = swap.token_in == given_token; @@ -65,7 +65,7 @@ impl TransferOptimization { #[cfg(test)] mod tests { use alloy_primitives::hex; - use tycho_common::{models::protocol::ProtocolComponent, Bytes}; + use tycho_common::Bytes; use super::*; @@ -88,14 +88,12 @@ mod tests { #[test] fn test_first_swap_transfer_from_permit2() { // The swap token is the same as the given token, which is not the native token - let swap = Swap { - component: ProtocolComponent { - protocol_system: "uniswap_v2".to_string(), - ..Default::default() - }, + let swap = SwapGroup { + protocol_system: "uniswap_v2".to_string(), token_in: weth(), token_out: dai(), split: 0f64, + swaps: vec![], }; let optimization = TransferOptimization::new(eth(), weth(), true); let transfer_method = optimization.get_transfer_type(swap.clone(), weth(), false, false); @@ -105,14 +103,12 @@ mod tests { #[test] fn test_first_swap_transfer_from() { // The swap token is the same as the given token, which is not the native token - let swap = Swap { - component: ProtocolComponent { - protocol_system: "uniswap_v2".to_string(), - ..Default::default() - }, + let swap = SwapGroup { + protocol_system: "uniswap_v2".to_string(), token_in: weth(), token_out: dai(), split: 0f64, + swaps: vec![], }; let optimization = TransferOptimization::new(eth(), weth(), false); let transfer_method = optimization.get_transfer_type(swap.clone(), weth(), false, false); @@ -123,14 +119,12 @@ mod tests { fn test_first_swap_native() { // The swap token is the same as the given token, and it's the native token. // No transfer action is needed. - let swap = Swap { - component: ProtocolComponent { - protocol_system: "uniswap_v2".to_string(), - ..Default::default() - }, + let swap = SwapGroup { + protocol_system: "uniswap_v2".to_string(), token_in: eth(), token_out: dai(), split: 0f64, + swaps: vec![], }; let optimization = TransferOptimization::new(eth(), weth(), false); let transfer_method = optimization.get_transfer_type(swap.clone(), eth(), false, false); @@ -141,14 +135,12 @@ mod tests { fn test_first_swap_wrapped() { // The swap token is NOT the same as the given token, but we are wrapping. // Since the swap's token in is the wrapped token - this is the first swap. - let swap = Swap { - component: ProtocolComponent { - protocol_system: "uniswap_v2".to_string(), - ..Default::default() - }, + let swap = SwapGroup { + protocol_system: "uniswap_v2".to_string(), token_in: weth(), token_out: dai(), split: 0f64, + swaps: vec![], }; let optimization = TransferOptimization::new(eth(), weth(), false); let transfer_method = optimization.get_transfer_type(swap.clone(), eth(), true, false); @@ -159,14 +151,12 @@ mod tests { fn test_not_first_swap() { // The swap token is NOT the same as the given token, and we are NOT wrapping. // Thus, this is not the first swap. - let swap = Swap { - component: ProtocolComponent { - protocol_system: "uniswap_v2".to_string(), - ..Default::default() - }, + let swap = SwapGroup { + protocol_system: "uniswap_v2".to_string(), token_in: usdc(), token_out: dai(), split: 0f64, + swaps: vec![], }; let optimization = TransferOptimization::new(eth(), weth(), false); let transfer_method = optimization.get_transfer_type(swap.clone(), weth(), false, false); @@ -177,14 +167,12 @@ mod tests { fn test_not_first_swap_funds_in_router() { // Not the first swap and the protocol requires the funds to be in the router (which they // already are, so the transfer type is None) - let swap = Swap { - component: ProtocolComponent { - protocol_system: "vm:curve".to_string(), - ..Default::default() - }, + let swap = SwapGroup { + protocol_system: "vm:curve".to_string(), token_in: usdc(), token_out: dai(), split: 0f64, + swaps: vec![], }; let optimization = TransferOptimization::new(eth(), weth(), false); let transfer_method = optimization.get_transfer_type(swap.clone(), weth(), false, false); @@ -195,14 +183,12 @@ mod tests { fn test_not_first_swap_in_between_swap_optimization() { // Not the first swap and the in between swaps are optimized. The funds should already be in // the next pool or in the router - let swap = Swap { - component: ProtocolComponent { - protocol_system: "uniswap_v2".to_string(), - ..Default::default() - }, + let swap = SwapGroup { + protocol_system: "uniswap_v2".to_string(), token_in: usdc(), token_out: dai(), split: 0f64, + swaps: vec![], }; let optimization = TransferOptimization::new(eth(), weth(), false); let transfer_method = optimization.get_transfer_type(swap.clone(), weth(), false, true);