Open code423n4 opened 2 years ago
WatchPug
https://github.com/code-423n4/2022-01-timeswap/blob/bf50d2a8bb93a5571f35f96bd74af54d9c92a210/Timeswap/Timeswap-V1-Core/contracts/TimeswapPair.sol#L157-L163
if (pool.state.totalLiquidity == 0) { uint256 liquidityTotal = MintMath.getLiquidityTotal(xIncrease); liquidityOut = MintMath.getLiquidity(maturity, liquidityTotal, protocolFee); pool.state.totalLiquidity += liquidityTotal; pool.liquidities[factory.owner()] += liquidityTotal - liquidityOut; }
At L157, we know that pool.state.totalLiquidity == 0, therefore at L161 += can be replaced with =.
pool.state.totalLiquidity == 0
+=
=
Using = directly can avoid unnecessary storage read of pool.state.totalLiquidity and save some gas.
pool.state.totalLiquidity
Change to:
if (pool.state.totalLiquidity == 0) { uint256 liquidityTotal = MintMath.getLiquidityTotal(xIncrease); liquidityOut = MintMath.getLiquidity(maturity, liquidityTotal, protocolFee); pool.state.totalLiquidity = liquidityTotal; pool.liquidities[factory.owner()] += liquidityTotal - liquidityOut; }
The refactor makes this unnecessary.
Handle
WatchPug
Vulnerability details
https://github.com/code-423n4/2022-01-timeswap/blob/bf50d2a8bb93a5571f35f96bd74af54d9c92a210/Timeswap/Timeswap-V1-Core/contracts/TimeswapPair.sol#L157-L163
At L157, we know that
pool.state.totalLiquidity == 0
, therefore at L161+=
can be replaced with=
.Using
=
directly can avoid unnecessary storage read ofpool.state.totalLiquidity
and save some gas.Recommendation
Change to: