The calculations in calculateMinimumAmountOut()
will put the user utilizing a swap function in danger of a sandwich attack. If he gets sandwiched for a big amount and thus lose a big part of his liquidity his SmartVault
can even get liquidated. As there is no manual way to set the minimumAmountOut a user doesn't have an option to protect himself from being sandwiched if he want to utilize the swap function. If a user tries to make a swap where the collateralValueMinusSwapValue >= requiredCollateralValue
in the calculateMinimumAmountOut()
function is true, this makes things even worse as this will set the minAmountOut to 0
. The parameters supplied in the below POC are not some magic values which showcase an edge case, whatever the amountIn put in the swap()
function the minAmountOut will always be too low, and thus unsafe for the user to execute a swap. The bigger the amount, the bigger the danger of a user getting liquidated after he gets sandwiched.
After executing the steps provided in the above gist in order to set up the tests, add the following functions to the AuditorTests.t.sol
For this test I have added the following console.logs to the SmartVaultV3.sol
contract, I have modified the calculateMinimumAmountOut()
function to this
I have also imported this in the SmartVaultV3.sol
contract
import {Test, console2} from "forge-std/Test.sol";
I have used the same output and input tokens to better illustrate the difference between the minAmoutOut and the amount of input token. As we can se from the above logs if a user want to swap 23e18ETH
the minAmountOut outputted by calculateMinimumAmountOut()
will be 9e18ETH
which is a difference of 23-9 = 14e18ETH
which the user can lose due to sandwich attack.
To run the test use: forge test -vvv --mt test_SwapMinAmountOutWorksIncorectly
If a user decides to swap some of the assets he has as collateral, the calculations in calculateMinimumAmountOut()
will set too low minAmountOut which will make him a victim of a sandwich attack, which can even lead to the liquidation of his SmartVault
Manual review & Foundry
Remove the calculateMinimumAmountOut()
function, and let the user set the desired minimumAmountOut manually
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.