Merge branch 'refs/heads/main' into audit/dc/one-transfer-from-only

# Conflicts:
#	foundry/src/executors/TokenTransfer.sol

Took 38 seconds
This commit is contained in:
Diana Carvalho
2025-05-15 14:53:18 +01:00

View File

@@ -486,16 +486,15 @@ contract TychoRouter is
Address.sendValue(payable(receiver), amountOut); Address.sendValue(payable(receiver), amountOut);
} }
if (tokenIn != tokenOut) { _verifyAmountOutWasReceived(
uint256 currentBalanceTokenOut = _balanceOf(tokenOut, receiver); tokenIn,
uint256 userAmount = currentBalanceTokenOut - initialBalanceTokenOut; tokenOut,
if (userAmount != amountOut) { initialBalanceTokenOut,
revert TychoRouter__AmountOutNotFullyReceived( amountOut,
userAmount, amountOut receiver,
amountIn
); );
} }
}
}
/** /**
* @notice Internal implementation of the core swap logic shared between singleSwap() and singleSwapPermit2(). * @notice Internal implementation of the core swap logic shared between singleSwap() and singleSwapPermit2().
@@ -543,16 +542,15 @@ contract TychoRouter is
Address.sendValue(payable(receiver), amountOut); Address.sendValue(payable(receiver), amountOut);
} }
if (tokenIn != tokenOut) { _verifyAmountOutWasReceived(
uint256 currentBalanceTokenOut = _balanceOf(tokenOut, receiver); tokenIn,
uint256 userAmount = currentBalanceTokenOut - initialBalanceTokenOut; tokenOut,
if (userAmount != amountOut) { initialBalanceTokenOut,
revert TychoRouter__AmountOutNotFullyReceived( amountOut,
userAmount, amountOut receiver,
amountIn
); );
} }
}
}
/** /**
* @notice Internal implementation of the core swap logic shared between sequentialSwap() and sequentialSwapPermit2(). * @notice Internal implementation of the core swap logic shared between sequentialSwap() and sequentialSwapPermit2().
@@ -596,17 +594,15 @@ contract TychoRouter is
_unwrapETH(amountOut); _unwrapETH(amountOut);
Address.sendValue(payable(receiver), amountOut); Address.sendValue(payable(receiver), amountOut);
} }
_verifyAmountOutWasReceived(
if (tokenIn != tokenOut) { tokenIn,
uint256 currentBalanceTokenOut = _balanceOf(tokenOut, receiver); tokenOut,
uint256 userAmount = currentBalanceTokenOut - initialBalanceTokenOut; initialBalanceTokenOut,
if (userAmount != amountOut) { amountOut,
revert TychoRouter__AmountOutNotFullyReceived( receiver,
userAmount, amountOut amountIn
); );
} }
}
}
/** /**
* @dev Executes sequential swaps as defined by the provided swap graph. * @dev Executes sequential swaps as defined by the provided swap graph.
@@ -834,4 +830,27 @@ contract TychoRouter is
return return
token == address(0) ? owner.balance : IERC20(token).balanceOf(owner); token == address(0) ? owner.balance : IERC20(token).balanceOf(owner);
} }
/**
* @dev Verifies that the expected amount of output tokens was received by the receiver.
* It also handles the case of arbitrage swaps where the input and output tokens are the same.
*/
function _verifyAmountOutWasReceived(
address tokenIn,
address tokenOut,
uint256 initialBalanceTokenOut,
uint256 amountOut,
address receiver,
uint256 amountIn
) internal view {
uint256 currentBalanceTokenOut = _balanceOf(tokenOut, receiver);
if (tokenIn == tokenOut) {
// If it is an arbitrage, we need to remove the amountIn from the initial balance to get a correct userAmount
initialBalanceTokenOut -= amountIn;
}
uint256 userAmount = currentBalanceTokenOut - initialBalanceTokenOut;
if (userAmount != amountOut) {
revert TychoRouter__AmountOutNotFullyReceived(userAmount, amountOut);
}
}
} }