Summary
The TSwapPool::swapExactInput
function is expected to return the actual amount of tokens bought by the caller. However, while it declares the named return output
, it is never assigned a value, nor uses an explicit return statement.
Impact
The return value will always be 0, giving incorrect information to the caller.
Proof of Code
Include this test in TSwapPool.t.sol
:
function testWrongOutputInSwapExactInput() public {
testDeposit();
uint256 expectedOutput =
pool.getOutputAmountBasedOnInput(1e18, poolToken.balanceOf(address(pool)), weth.balanceOf(address(pool)));
vm.startPrank(user);
poolToken.approve(address(pool), 1e18);
uint256 actualOutput = pool.swapExactInput(poolToken, 1e18, weth, 0, uint64(block.timestamp));
assert(actualOutput != expectedOutput);
assertEq(actualOutput, 0);
}
Tools Used
Manual review
Recommendations
Make the following changes to swapExactInput
:
returns (
uint256 output
)
{
uint256 inputReserves = inputToken.balanceOf(address(this));
uint256 outputReserves = outputToken.balanceOf(address(this));
- uint256 outputAmount = getOutputAmountBasedOnInput(inputAmount, inputReserves, outputReserves);
+ output = getOutputAmountBasedOnInput(inputAmount, inputReserves, outputReserves);
- if (outputAmount < minOutputAmount) {
- revert TSwapPool__OutputTooLow(outputAmount, minOutputAmount);
+ if (output < minOutputAmount) {
+ revert TSwapPool__OutputTooLow(output, minOutputAmount);
}
- _swap(inputToken, inputAmount, outputToken, outputAmount);
+ _swap(inputToken, inputAmount, outputToken, output);
}