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
This commit is contained in:
@@ -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<u8> = 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<u8> = 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<Bytes> = 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<Bytes> = 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<u8> = 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())
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user