Skip to content
Audit finding Advanced

spTKNMinimalOracle `_calculateSpTknPerBase()` does not calculate correct price for podded or fraxlend pair pairedLpTKNs

The oracle function for calculating the price of spTKN in terms of baseTKN incorrectly assumes a 1:1 ratio between pairedLpTKN and baseTKN when the paired token is a podded token or a Fraxlend pair token. This leads to incorrect pricing, which could cause overestimation or underestimation of borrow asset value in Fraxlend.

bretzel Follow with pkqs90

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-01-peapods-finance-judging/issues/445

Found by

bretzel, pkqs90

Summary

spTKNMinimalOracle _calculateSpTknPerBase() does not calculate correct price for podded or fraxlend pair pairedLpTKNs.

Root Cause

First, let’s clarify the denomination: tokenA/tokenB represents how much tokenB is worth per tokenA. For example, ETH/USDC = 3000 means 1 ETH is equivalent to 3000 USDC.

The _calculateSpTknPerBase() function is used to calculate baseTKN/spTKN. It starts with the _priceBasePerPTkn18 variable, which is pTKN/baseTKN.

Now, we need to convert pTKN to spTKN. Because spTKN is the Uniswap V2 LP of pTKN and pairedLpTKN, the idea is to use the Uniswap V2 LP fair pricing formula. In order to do that, we need the price of pairedLpTKN/baseTKN.

For normal pods, baseTKN is equal to pairedLpTKN (e.g USDC as pairedLpTKN). However, pods (e.g. pOHM) and fraxlend pair (self-lending pods e.g. fUSDC) tokens are also supported. The bug here is, for both podded tokens and fraxlend pair tokens, the formula is wrong.

From this doc, https://docs.google.com/document/d/1Z-T_07QpJlqXlbBSiC_YverKFfu-gcSkOBzU1icMRkM/edit?tab=t.0, the spTKN is first priced against pairedLpTKN (i.e. spTKN/pairedLpTKN), then converted to spTKN/baseTKN.

The two bugs here are:

  1. The current code calculates _basePerSpTkn18 as if pairedLpTKN/baseTKN is 1:1. However, this is incorrect. We should convert pTKN/baseTKN (which is _priceBasePerPTkn18) to pTKN/pairedLpTKN by dividing a sqrt(ratio) to the formula, assuming ratio to be the asset/share ratio of either podded token or fraxlend pair (Recall that fair LP pricing formula is fairPrice = 2 * sqrt(k * priceToken1 * priceToken2) / lpSuppply).

  2. After calculating _basePerSpTkn18 (spTKN/pairedLpTKN), the code reverses it to pairedLpTKN/spTKN, then multiply the asset/share ratio. However, the correct order is to first multiply the asset/share ratio to get spTKN/baseTKN, and then reverse it, and finally we can get baseTKN/spTKN.

https://github.com/sherlock-audit/2025-01-peapods-finance/blob/main/contracts/contracts/lvf/LeverageManager.sol#L60

Solidity
    function _calculateSpTknPerBase(uint256 _price18) internal view returns (uint256 _spTknBasePrice18) {
        uint256 _priceBasePerPTkn18 = _calculateBasePerPTkn(_price18);
        address _pair = _getPair();

        (uint112 _reserve0, uint112 _reserve1) = V2_RESERVES.getReserves(_pair);
        uint256 _k = uint256(_reserve0) * _reserve1;
        uint256 _kDec = 10 ** IERC20Metadata(IUniswapV2Pair(_pair).token0()).decimals()
            * 10 ** IERC20Metadata(IUniswapV2Pair(_pair).token1()).decimals();
        uint256 _avgBaseAssetInLp18 = _sqrt((_priceBasePerPTkn18 * _k) / _kDec) * 10 ** (18 / 2);
@>      uint256 _basePerSpTkn18 =
            (2 * _avgBaseAssetInLp18 * 10 ** IERC20Metadata(_pair).decimals()) / IERC20(_pair).totalSupply();
        require(_basePerSpTkn18 > 0, "V2R");
@>      _spTknBasePrice18 = 10 ** (18 * 2) / _basePerSpTkn18;

        // if the base asset is a pod, we will assume that the CL/chainlink pool(s) are
        // pricing the underlying asset of the base asset pod, and therefore we will
        // adjust the output price by CBR and unwrap fee for this pod for more accuracy and
        // better handling accounting for liquidation path
        if (BASE_IS_POD) {
            _spTknBasePrice18 = _checkAndHandleBaseTokenPodConfig(_spTknBasePrice18);
        } else if (BASE_IS_FRAX_PAIR) {
            _spTknBasePrice18 = IFraxlendPair(BASE_TOKEN).convertToAssets(_spTknBasePrice18);
        }
    }

Internal pre-conditions

N/A

External pre-conditions

N/A

Attack Path

N/A

Impact

Pods with podded token or fraxlend pair token as pairedLpTKN would have incorrect oracle result, leading to overestimating or underestimating borrow asset value in Fraxlend.

PoC

N/A

Mitigation

Fix the formula accordingly. The correct code should be:

Solidity
    function _calculateSpTknPerBase(uint256 _price18) internal view returns (uint256 _spTknBasePrice18) {
        uint256 _priceBasePerPTkn18 = _calculateBasePerPTkn(_price18);
        address _pair = _getPair();

        uint256 cbr;
        if (BASE_IS_POD) {
            cbr = _checkAndHandleBaseTokenPodConfig(1e18);
        } else if (BASE_IS_FRAX_PAIR) {
            cbr = IFraxlendPair(BASE_TOKEN).convertToAssets(1e18);
        }

        (uint112 _reserve0, uint112 _reserve1) = V2_RESERVES.getReserves(_pair);
        uint256 _k = uint256(_reserve0) * _reserve1;
        uint256 _kDec = 10 ** IERC20Metadata(IUniswapV2Pair(_pair).token0()).decimals()
            * 10 ** IERC20Metadata(IUniswapV2Pair(_pair).token1()).decimals();
        uint256 _avgBaseAssetInLp18 = _sqrt((_priceBasePerPTkn18 * _k * 1e18) / _kDec / cbr) * 10 ** (18 / 2);
        uint256 _basePerSpTkn18 =
            (2 * _avgBaseAssetInLp18 * 10 ** IERC20Metadata(_pair).decimals()) / IERC20(_pair).totalSupply();
        _basePerSpTkn18 = _basePerSpTkn18 * cbr / 1e18;
        require(_basePerSpTkn18 > 0, "V2R");
        _spTknBasePrice18 = 10 ** (18 * 2) / _basePerSpTkn18;
    }

The original report as published on Sherlock, by bretzel. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 code assumes a 1:1 ratio between pairedLpTKN and baseTKN when calculating the LP fair price, and applies the asset/share ratio conversion in the wrong order.

What the attacker needed

Podded token or Fraxlend pair as pairedLpTKN

The fix

The formula should be corrected to account for the asset/share ratio of the podded token or Fraxlend pair token, as shown in the mitigation code.

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 report is about a price calculation bug in a smart contract. The contract tries to work out the price of a special token (spTKN) using a formula, but it forgets to adjust for the fact that the paired token might not be worth the same as the base token. As a result, the price it calculates can be too high or too low, which could cause problems when people borrow against it.

More by bretzel

View all

1 more disclosed report, best first.

More from Peapods

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.