According to the documentation, the protocol charges a 0.3% fee. Therefore, getInputAmountBasedOnOutput
computes the fee that the user needs to pay, using 0.997 * InputAmount
to exchange outputAmount. However, in the calculation involving 0.997
, it erroneously uses 10,000
instead of 1,000
, leading to users paying significantly more than expected.
According to the constant product formula in the documentation, the inputAmount
can be calculated as inputAmount = outputAmount * inputReserves / (outputReserves - outputAmount)
. The protocol charges a 0.3% fee, so the actualInputAmount
should be inputAmount / 0.997
, which is outputAmount * inputReserves * 1,000 / (outputReserves - outputAmount * 0.997)
. However, the implementation incorrectly uses 10_000
instead of 1_000
.
Place the following test into TSwapPool.t.sol
. The following test shows that the TSwapPool::getInputAmountBasedOnOutput
is calculating the price as ten times the expected value:
Due to this error, the actual fee paid by users is inputAmount / 0.0997, which is approximately ten times the correct fee, leading to significant losses for users.
Manual review
It's best to use variables instead of magic numbers to completely avoid making such mistakes again. If must use numbers, it's better to use _
for separation to reduce the likelihood of errors.
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.