Merge pull request #88 from propeller-heads/router/hr/ENG-4279-check-leftover
refactor: check if amountIn was fully consumed based on balance changes
This commit is contained in:
@@ -150,8 +150,23 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
|||||||
tokenIn = address(_weth);
|
tokenIn = address(_weth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint256 initialBalance = tokenIn == address(0)
|
||||||
|
? address(this).balance
|
||||||
|
: IERC20(tokenIn).balanceOf(address(this));
|
||||||
|
|
||||||
amountOut = _swap(amountIn, nTokens, swaps);
|
amountOut = _swap(amountIn, nTokens, swaps);
|
||||||
|
|
||||||
|
uint256 currentBalance = tokenIn == address(0)
|
||||||
|
? address(this).balance
|
||||||
|
: IERC20(tokenIn).balanceOf(address(this));
|
||||||
|
|
||||||
|
uint256 amountConsumed = initialBalance - currentBalance;
|
||||||
|
|
||||||
|
if (amountConsumed < amountIn) {
|
||||||
|
uint256 leftoverAmount = amountIn - amountConsumed;
|
||||||
|
revert TychoRouter__AmountInNotFullySpent(leftoverAmount);
|
||||||
|
}
|
||||||
|
|
||||||
if (fee > 0) {
|
if (fee > 0) {
|
||||||
uint256 feeAmount = (amountOut * fee) / 10000;
|
uint256 feeAmount = (amountOut * fee) / 10000;
|
||||||
amountOut -= feeAmount;
|
amountOut -= feeAmount;
|
||||||
@@ -162,17 +177,6 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
|
|||||||
revert TychoRouter__NegativeSlippage(amountOut, minAmountOut);
|
revert TychoRouter__NegativeSlippage(amountOut, minAmountOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint256 leftoverAmountIn;
|
|
||||||
if (tokenIn == address(0)) {
|
|
||||||
leftoverAmountIn = address(this).balance;
|
|
||||||
} else {
|
|
||||||
leftoverAmountIn = IERC20(tokenIn).balanceOf(address(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (leftoverAmountIn > 0) {
|
|
||||||
revert TychoRouter__AmountInNotFullySpent(leftoverAmountIn);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (unwrapEth) {
|
if (unwrapEth) {
|
||||||
_unwrapETH(amountOut);
|
_unwrapETH(amountOut);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user