First Flight #18: T-Swap

First Flight #18
Beginner FriendlyDeFiFoundry
100 EXP
View results
Submission Details
Severity: low
Valid

`TSwapPool::getPriceOfOnePoolTokenInWeth` Won't Work With Tokens That Have Higher or Lower Decimals

[L-04] TSwapPool::getPriceOfOnePoolTokenInWeth Won't Work With Tokens That Have Higher or Lower Decimals

Description:
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.

function getPriceOfOnePoolTokenInWeth() external view returns (uint256) {
return
getOutputAmountBasedOnInput(
1e18,
i_poolToken.balanceOf(address(this)),
i_wethToken.balanceOf(address(this))
);
}

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:

+ function decimals() external view returns (uint8);

Then, modify the getPriceOfOnePoolTokenInWeth method to use the correct decimal count:

function getPriceOfOnePoolTokenInWeth() external view returns (uint256) {
return
getOutputAmountBasedOnInput(
- 1e18,
+ 10**i_poolToken.decimals(),
i_poolToken.balanceOf(address(this)),
i_wethToken.balanceOf(address(this))
);
}

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.

Updates

Appeal created

inallhonesty Lead Judge 12 months ago
Submission Judgement Published
Validated
Assigned finding tags:

Hardcoded decimal value leads to incorrect conversion when ERC20 does not use 18 decimals.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.