fix: Encode sequential swaps with unwrapping WETH correctly

Took 24 minutes


Took 1 minute

Took 24 seconds
This commit is contained in:
Diana Carvalho
2025-08-06 17:58:20 +01:00
parent 93c4b5eb9a
commit c42a5dae6a
5 changed files with 128 additions and 25 deletions

View File

@@ -228,10 +228,11 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder {
let grouped_swaps = group_swaps(&solution.swaps);
let mut wrap = false;
let (mut wrap, mut unwrap) = (false, false);
if let Some(action) = &solution.native_action {
if action == &NativeAction::Wrap {
wrap = true
match *action {
NativeAction::Wrap => wrap = true,
NativeAction::Unwrap => unwrap = true,
}
}
@@ -251,7 +252,7 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder {
let next_swap = grouped_swaps.get(i + 1);
let (swap_receiver, next_swap_optimization) = self
.transfer_optimization
.get_receiver(&solution.receiver, next_swap)?;
.get_receiver(&solution.receiver, next_swap, unwrap)?;
next_in_between_swap_optimization_allowed = next_swap_optimization;
let transfer = self

View File

@@ -82,6 +82,7 @@ impl TransferOptimization {
&self,
solution_receiver: &Bytes,
next_swap: Option<&SwapGroup>,
unwrap: bool,
) -> Result<(Bytes, bool), EncodingError> {
if let Some(next) = next_swap {
// if the protocol of the next swap supports transfer in optimization
@@ -104,7 +105,11 @@ impl TransferOptimization {
}
} else {
// last swap - there is no next swap
Ok((solution_receiver.clone(), false))
if unwrap {
Ok((self.router_address.clone(), false))
} else {
Ok((solution_receiver.clone(), false))
}
}
}
}
@@ -204,16 +209,19 @@ mod tests {
}
#[rstest]
// there is no next swap -> receiver is the solution receiver
#[case(None, receiver(), false)]
// there is no next swap but there is an unwrap -> receiver is the router
#[case(None, true, router_address(), false)]
// there is no next swap and no unwrap -> receiver is the solution receiver
#[case(None, false, receiver(), false)]
// protocol of next swap supports transfer in optimization
#[case(Some("uniswap_v2"), component_id(), true)]
#[case(Some("uniswap_v2"), false, component_id(), true)]
// protocol of next swap supports transfer in optimization but is callback constrained
#[case(Some("uniswap_v3"), router_address(), false)]
#[case(Some("uniswap_v3"), false, router_address(), false)]
// protocol of next swap does not support transfer in optimization
#[case(Some("vm:curve"), router_address(), false)]
#[case(Some("vm:curve"), false, router_address(), false)]
fn test_get_receiver(
#[case] protocol: Option<&str>,
#[case] unwrap: bool,
#[case] expected_receiver: Bytes,
#[case] expected_optimization: bool,
) {
@@ -247,7 +255,7 @@ mod tests {
})
};
let result = optimization.get_receiver(&receiver(), next_swap.as_ref());
let result = optimization.get_receiver(&receiver(), next_swap.as_ref(), unwrap);
assert!(result.is_ok());
let (actual_receiver, optimization_flag) = result.unwrap();