First Flight #18: T-Swap

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

Gas saving by caching i_wethToken.balanceOf(address(this))

Summary

There are two external calls to get weth balance in TSwapPool.deposit, we can cache the value to save gas.

Vulnerability Details

In TSwapPool.deposit we get the weth balance https://github.com/Cyfrin/2024-06-t-swap/blob/main/src/TSwapPool.sol#L130
then when calling getPoolTokensToDepositBasedOnWeth, it does the same external call to get weth balance https://github.com/Cyfrin/2024-06-t-swap/blob/main/src/TSwapPool.sol#L428

We can cache the value from first call to save gas.

Impact

Extra gas cost

Tools Used

Manual review

Recommendations

Make another private function that take wethReserves as parameter, this can be called from the deposit and getPoolTokensToDepositBasedOnWeth.

function deposit(
uint256 wethToDeposit,
uint256 minimumLiquidityTokensToMint,
uint256 maximumPoolTokensToDeposit,
uint64 deadline
)
external
revertIfZero(wethToDeposit)
returns (uint256 liquidityTokensToMint)
{
// ....other code
// remove this
// uint256 poolTokensToDeposit = getPoolTokensToDepositBasedOnWeth(
// wethToDeposit
// );
// add this
uint256 poolTokensToDeposit = _getPoolTokensToDepositBasedOnWeth(
wethReserves,
poolTokenReserves,
wethToDeposit
);
}
function getPoolTokensToDepositBasedOnWeth(
uint256 wethToDeposit
) public view returns (uint256) {
uint256 poolTokenReserves = i_poolToken.balanceOf(address(this));
uint256 wethReserves = i_wethToken.balanceOf(address(this));
return _getPoolTokensToDepositBasedOnWeth(wethReserves, poolTokenReserves, wethToDeposit);
}
function _getPoolTokensToDepositBasedOnWeth(
uint256 wethReserves,
uint256 poolTokenReserves,
uint256 wethToDeposit
) public view returns (uint256) {
return (wethToDeposit * poolTokenReserves) / wethReserves;
}
Updates

Appeal created

inallhonesty Lead Judge 11 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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