wrong condition checking in price calculation
The getTokenPriceInLPShares function checks whether total shares minted is zero instead of whether the total reserve is zero, so it can return an incorrect price or cause a division by zero. An attacker could potentially trigger a runtime failure if the function is called when total shares minted is zero.
Originally disclosed on Code4rena
Submitted by CertoraInc
LiquidityProviders.sol#L180-L186
The getTokenPriceInLPShares function calculates the token price in LP shares, but it checks a wrong condition - if supposed to return BASE_DIVISOR if the total reserve is zero, not if the total shares minted is zero. This might leads to a case where the price is calculated incorrectly, or a division by zero is happening.
Proof of Concept
This is the wrong function implementation:
function getTokenPriceInLPShares(address _baseToken) public view returns (uint256) {
uint256 supply = totalSharesMinted[_baseToken];
if (supply > 0) {
return totalSharesMinted[_baseToken] / totalReserve[_baseToken];
}
return BASE_DIVISOR;
}
This function is used in this contract only in the removeLiquidity and claimFee function, so it’s called only if funds were already deposited and totalReserve is not zero, but it can be problematic when other contracts will use this function (it’s a public view function so it might get called from outside of the contract).
Recommended Mitigation Steps
The correct code should be:
function getTokenPriceInLPShares(address _baseToken) public view returns (uint256) {
uint256 reserve = totalReserve[_baseToken];
if (reserve > 0) {
return totalSharesMinted[_baseToken] / totalReserve[_baseToken];
}
return BASE_DIVISOR;
}
ankurdubey521 (Biconomy) confirmed and commented:
HP-25: C4 Audit Fixes, Dynamic Fee Changes bcnmy/hyphen-contract#42
Great catch, even though the real impact is not that clear and severe, I will favor a warden and leave it as Medium severity.
The warden didn’t show any attack path that could leak value. This is a view function that is incorrect as to spec so I think this should be a low.
Yes, it is a view function but nevertheless, I think it possesses a hypothetical risk path that this function can fail at runtime if the totalSharesMinted is 0. It is somewhere between low and medium categories, I am curious what other certified wardens think about where should this belong.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The condition checks totalSharesMinted instead of totalReserve, so the zero-check is on the wrong variable.
Impact
The function may return an incorrect price or fail at runtime with a division by zero if total shares minted is zero, though the real impact is limited because it is a view function and currently only called when funds are already deposited.
What the attacker needed
TotalSharesMinted is zero; function called externally
The fix
The condition should check totalReserve instead of totalSharesMinted, and the fix was confirmed by the project.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
This is a smart contract function that calculates a price by dividing two numbers. The code checks the wrong number to decide if it should avoid dividing by zero, so it might divide by zero and crash, or give a wrong price. A crash could stop the contract from working properly.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.