From d447551e20c42cecd47c3005b430924f6d8aff5f Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Wed, 16 Apr 2025 14:57:02 +0100 Subject: [PATCH] fix: Bring back receiver address zero check Add small docs and rename variables --- don't change below this line --- ENG-4314 Took 1 hour 39 minutes --- foundry/src/TychoRouter.sol | 9 +++++++++ .../evm/strategy_encoder/strategy_encoders.rs | 14 ++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/foundry/src/TychoRouter.sol b/foundry/src/TychoRouter.sol index 3c28a11..f9597a1 100644 --- a/foundry/src/TychoRouter.sol +++ b/foundry/src/TychoRouter.sol @@ -411,6 +411,9 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard { address receiver, bytes calldata swaps ) internal returns (uint256 amountOut) { + if (receiver == address(0)) { + revert TychoRouter__AddressZero(); + } if (minAmountOut == 0) { revert TychoRouter__UndefinedMinAmountOut(); } @@ -462,6 +465,9 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard { address receiver, bytes calldata swap_ ) internal returns (uint256 amountOut) { + if (receiver == address(0)) { + revert TychoRouter__AddressZero(); + } if (minAmountOut == 0) { revert TychoRouter__UndefinedMinAmountOut(); } @@ -516,6 +522,9 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard { address receiver, bytes calldata swaps ) internal returns (uint256 amountOut) { + if (receiver == address(0)) { + revert TychoRouter__AddressZero(); + } if (minAmountOut == 0) { revert TychoRouter__UndefinedMinAmountOut(); } diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index be6316b..45a83d8 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -121,7 +121,7 @@ impl StrategyEncoder for SingleSwapStrategyEncoder { )) })?; - let receiver = + let swap_receiver = if !unwrap { solution.receiver.clone() } else { self.router_address.clone() }; let mut grouped_protocol_data: Vec = vec![]; @@ -136,7 +136,7 @@ impl StrategyEncoder for SingleSwapStrategyEncoder { ); let encoding_context = EncodingContext { - receiver: receiver.clone(), + receiver: swap_receiver.clone(), exact_out: solution.exact_out, router_address: Some(self.router_address.clone()), group_token_in: grouped_swap.input_token.clone(), @@ -300,7 +300,9 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder { )) })?; - let receiver = if i == grouped_swaps.len() - 1 && !unwrap { + // if it is the last swap and there isn't an unwrap at the end, we can set the receiver + // to the final user + let swap_receiver = if i == grouped_swaps.len() - 1 && !unwrap { solution.receiver.clone() } else { self.router_address.clone() @@ -318,7 +320,7 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder { ); let encoding_context = EncodingContext { - receiver: receiver.clone(), + receiver: swap_receiver.clone(), exact_out: solution.exact_out, router_address: Some(self.router_address.clone()), group_token_in: grouped_swap.input_token.clone(), @@ -536,7 +538,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { )) })?; - let receiver = if !unwrap && grouped_swap.output_token == solution.checked_token { + let swap_receiver = if !unwrap && grouped_swap.output_token == solution.checked_token { solution.receiver.clone() } else { self.router_address.clone() @@ -554,7 +556,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { ); let encoding_context = EncodingContext { - receiver: receiver.clone(), + receiver: swap_receiver.clone(), exact_out: solution.exact_out, router_address: Some(self.router_address.clone()), group_token_in: grouped_swap.input_token.clone(),