TSwapPool::getPriceOfOnePoolTokenInWeth
Won't Work With Tokens That Have Higher or Lower DecimalsDescription:
The getPriceOfOnePoolTokenInWeth
method calculates the price of one pool token in WETH assuming that all tokens use the same decimal system as WETH (18 decimals). However, not all tokens adhere to this standard; some tokens, such as USDC, use fewer decimals (e.g., 6 decimals for USDC). Consequently, the method will return incorrect values for tokens with decimal counts different from 18, leading to inaccurate pricing information.
Impact:
This issue impacts the accuracy of pricing information provided by the contract, potentially leading to incorrect swap rates and misleading users about the actual value of their tokens relative to WETH. Users trading tokens with decimals different from 18 could face significant discrepancies between expected and actual swap amounts, affecting their trading strategies and outcomes.
Proof of Concept:
Consider a scenario where a user wants to swap USDC (which has 6 decimals) for WETH (with 18 decimals). If the contract assumes 1e18 units of USDC for the calculation, it would overestimate the amount of USDC by a factor of 1,000 (since 1e18 / 1e6 = 1,000), leading to incorrect pricing information. For example, if the actual price of 1 WETH is equivalent to 1,000 USDC, the contract would suggest a rate as if 1 WETH were equivalent to 1,000,000 USDC due to the decimal discrepancy.
Recommended Mitigation:
To address this issue, the contract should dynamically adjust the calculation based on the actual decimals of the tokens involved. This can be achieved by adding a decimals
function to the IERC20
interface and modifying the getPriceOfOnePoolTokenInWeth
method to use the correct decimal count for the pool token.
First, extend the IERC20
interface to include the decimals
function:
Then, modify the getPriceOfOnePoolTokenInWeth
method to use the correct decimal count:
By incorporating the actual decimal counts of the tokens, this mitigation ensures accurate pricing calculations regardless of the token's decimal system, enhancing the reliability and fairness of the swap operations.
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.