VotingPool.sol Uniswap V2 pricing formula for spTKN is incorrect
The conversion factor used to price spTKN LP tokens in VotingPool applies the Uniswap V2 fair LP pricing formula incorrectly: the PEAS/stablecoin price is inverted, stablecoins with fewer than 18 decimals are not normalised, and a multiplication can overflow. As a result users are minted the wrong number of VotingPool shares for their staked tokens.
Source: https://github.com/sherlock-audit/2025-01-peapods-finance-judging/issues/498
Found by
pkqs90
Summary
VotingPool.sol Uniswap V2 pricing formula for spTKN is incorrect, leading to minting a wrong amount of shares.
Root Cause
VotingPool is used for user staking their pTKN, spTKN tokens. User are granted the amount of shares equal to the price of the staked tokens. For spTKN, since it is a Uniswap V2 LP of the pTKN and pairedLpTKN, it is priced using the fair pricing formula: https://blog.alphaventuredao.io/fair-lp-token-pricing/.
The formula is 2 * sqrt(k * p0 * p1) / totalSupply. Here, we assume the pricing use Q96 for handling float numbers, so price p0 and p1 are both under Q96 denomination. Since it is assumed pairedLpTKN is a stablecoin, we can assume p1 = Q96.
Bug 1: _sqrtPriceX96 the price of PEAS/stablecoin pool. However, _pricePeasNumX96 should be PEAS/stablecoin price, and it is currently incorrectly reversed to stablecoin/PEAS. This is because Uniswap pool price is token0/token1, and the check uint256 _pricePeasNumX96 = _token1 == PEAS ? _priceX96 : FixedPoint96.Q96 ** 2 / _priceX96; here should be _token0 == PEAS instead.
Bug 2: If the stablecoin does not have 18 decimals, it should be normalized to 18 decimals first (e.g. USDC).
Bug 3: The _pricePPeasNumX96 * _k multiplication may overflow. Assume both reserve amount has 18 decimals. If both reseve has (1e6)e18 tokens, and price is 2, this would be Q96 * 1e24 * 1e24 * 2, which is over uint256. Having a PEAS/stablecoin Uniswap pool with 1e6 USDC and PEAS is not very hard (considering PEAS is ~5 USD).
function getConversionFactor(address _spTKN)
external
view
override
returns (uint256 _factor, uint256 _denomenator)
{
(uint256 _pFactor, uint256 _pDenomenator) = _calculateCbrWithDen(IStakingPoolToken(_spTKN).INDEX_FUND());
address _lpTkn = IStakingPoolToken(_spTKN).stakingToken();
address _token1 = IUniswapV3Pool(PEAS_STABLE_CL_POOL).token1();
uint160 _sqrtPriceX96 = TWAP_UTILS.sqrtPriceX96FromPoolAndInterval(PEAS_STABLE_CL_POOL);
uint256 _priceX96 = TWAP_UTILS.priceX96FromSqrtPriceX96(_sqrtPriceX96);
uint256 _pricePeasNumX96 = _token1 == PEAS ? _priceX96 : FixedPoint96.Q96 ** 2 / _priceX96;
uint256 _pricePPeasNumX96 = (_pricePeasNumX96 * _pFactor) / _pDenomenator;
(uint112 _reserve0, uint112 _reserve1) = V2_RESERVES.getReserves(_lpTkn);
uint256 _k = uint256(_reserve0) * _reserve1;
uint256 _avgTotalPeasInLpX96 = _sqrt(_pricePPeasNumX96 * _k) * 2 ** (96 / 2);
_factor = (_avgTotalPeasInLpX96 * 2) / IERC20(_lpTkn).totalSupply();
_denomenator = FixedPoint96.Q96;
}
Internal pre-conditions
N/A
External pre-conditions
N/A
Attack Path
N/A
Impact
Pricing of spTKN would be incorrect, leading to incorrect amount of VotingPool shares minted to users.
PoC
N/A
Mitigation
Fix the formula.
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
Likely the conversion-factor code inverts the PEAS/stablecoin price by checking token1 instead of token0, skips decimal normalisation for non-18-decimal stablecoins, and multiplies price by the reserve product without overflow protection.
Impact
Pricing of spTKN would be incorrect, leading to incorrect amount of VotingPool shares minted to users.
What the attacker needed
SpTKN LP tokens staked in VotingPool; PEAS/stablecoin pool used for pricing
The fix
The formula should be corrected so the price orientation, decimal normalisation and overflow handling are right.
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.
A staking pool gives you shares based on how much your tokens are worth. To value a special token that represents a share of a trading pool, the code uses a standard maths formula, but it plugs the numbers in the wrong way round and forgets that some stablecoins use fewer decimal places. So the pool can hand out too many or too few shares.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.