# PriceFeed does not return to the correct price for quote pairs

Predy's PriceFeed only supports positive decimalsDiff, so quote pairs like BTC/DAI cannot be configured with correct sqrt price, breaking position math.

- Payout: Audit contest
- Severity: medium
- Program: Predy
- Found by: gumgumzum
- Disclosed: 2024-07-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-05-predy-findings/issues/22
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-predy%3AM-08
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:code4rena sev:medium tech:evm tech:solidity vuln:accounting-error vuln:oracle-manipulation

**Target:** PriceFeed oracle contract / PositionCalculator.getSqrtIndexPrice
**Root cause:** PriceFeed stores _decimalsDiff as uint256 and only divides by it, so a negative decimal offset (Pyth oracle with fewer decimals than Chainlink, or quote-direction pairs) cannot be represented, yielding a wrong sqrtPriceX96.
**Steps:**
1. Deploy PriceFeed with quotePrice=WETH Chainlink, pyth=USDC priceId, decimalsDiff=1e0 for the quote pair and a base PriceFeed with decimalsDiff=1e12.
2. Register USDC/WETH pair via registerPair and call predyPool.getSqrtIndexPrice before and after updatePriceOracle.
3. Observe getSqrtIndexPrice returns the same unconverted price for both base and quote pairs (assertApproxEqRel fails with ~100093942% delta).
4. Alternatively use one PriceFeed for both directions: quote pair price is off by ~2.9e10x since PositionCalculator returns PriceFeed.getSqrtPrice() without direction conversion.
**How found:** Warden read PriceFeed.getSqrtPrice and noticed _decimalsDiff is uint256 and only used as a divisor; reasoned that quote pairs / negative decimal offsets cannot be expressed, then wrote a fork test comparing getSqrtIndexPrice for base vs quote pairs before and after oracle update.
**Insight:** When an oracle normalizes two price sources, check whether the decimal-difference parameter is signed and whether the same feed is reused for both pair directions; a uint-only divisor silently forbids inverse/quote configurations.

## Proof of concept (copied exactly from the original report)

**Foundry PoC test showing PriceFeed sqrt price discrepancy**

```
function testSqrtIndexPrice() public {
        vm.startPrank(admin);
        PriceFeed quotePriceFeed = new PriceFeed(
            WETH_CHAINLINK_PRICE_FEED,
            pyth,
            USDC_PYTH_PRICE_ID,
            1e0
        );

        PriceFeed basePriceFeed = new PriceFeed(
            USDC_CHAINLINK_PRICE_FEED,
            pyth,
            WETH_PYTH_PRICE_ID,
            1e12
        );

        address pool = uniswapFactory.getPool(
            address(USDC),
            address(WETH),
            500
        );

        uint256 quotePair = registerPair(pool, address(WETH), address(0), true);
        uint256 basePair = registerPair(pool, address(USDC), address(0), true);

        uint256 quoteIndexPriceBefore = predyPool.getSqrtIndexPrice(quotePair);
        uint256 baseIndexPriceBefore = predyPool.getSqrtIndexPrice(basePair);

        predyPool.updatePriceOracle(quotePair, address(quotePriceFeed));
        predyPool.updatePriceOracle(basePair, address(basePriceFeed));

        assertApproxEqRel(
            baseIndexPriceBefore,
            predyPool.getSqrtIndexPrice(basePair),
            1e16
        );
        assertApproxEqRel(
            quoteIndexPriceBefore,
            predyPool.getSqrtIndexPrice(quotePair),
            1e16
        );
    }
```

