The swap_exact_in
instruction calculates the output token amount and fees incorrectly. Specifically, it does not properly account for token decimals, which can lead to inaccurate swap rates when tokens have different decimal places. Additionally, the fee calculation is applied to the output amount (amount_out
) rather than the input amount (amount_in
), which is inconsistent with common AMM designs where fees are deducted from the input before calculating the output. These issues can cause incorrect token amounts to be swapped, resulting in unfair trades and potentially unexpected losses for users.
Incorrect handling of decimals may cause the swap output to be skewed, especially for tokens with differing decimals.
Calculating fees on the output rather than input can distort the actual value exchanged, potentially disadvantaging liquidity providers or traders.
The combination of these issues can result in loss of funds or unfair exchange rates, degrading user trust and platform reliability.
Handle token decimals properly: Normalize token amounts to a common base (e.g., convert to u128 with decimals factored in) before calculations.
Calculate fees on the input amount: Deduct the fee from amount_in
before computing the output, as is standard in AMM designs.
Use the correct constant product formula with fees:
This approach correctly applies the 0.3% fee on the input amount and then calculates the output amount using the constant product formula. Token decimals should be normalized before these calculations if tokens have different decimal places.
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.