# The `MixOracle.getThePrice` function calculates the price incorrectly using the `TarotOracle.getResult` function as the TWAP price

MixOracle misuses TarotOracle.getResult (a reserve0+price cumulative, not a TWAP) as the TWAP price, producing wildly wrong prices that disrupt lend/borrow order matching.

- Payout: Audit contest
- Severity: medium
- Program: Debita
- Found by: KupiaSec
- Disclosed: 2024-10-31
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-10-debita-judging/issues/501
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-10-debita%3AM-14
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:oracle-manipulation

**Target:** MixOracle.getThePrice price oracle used by DebitaV3Aggregator.matchOffersV3
**Root cause:** getThePrice treats TarotOracle.getResult's output (time-weighted reserve0+price cumulative, not synced with the Uniswap pair) as a UQ112x112 TWAP price, so the derived USD price is wrong.
**Steps:**
1. Read MixOracle.getThePrice: `(uint224 twapPrice112x112, ) = priceFeed.getResult(uniswapPair)` at L50.
2. Inspect TarotPriceOracle.getPriceCumulativeCurrent: it returns `reserve0CumulativeLast + priceLatest*timeElapsed`, not a TWAP.
3. Note it does not call uniswapV2Pair.sync(), so cumulative is stale.
4. Compute amountOfAttached = (2**112 * 10**dec1)/twapPrice112x112 and price = amountOfAttached*pythPrice/10**dec1.
5. Compare against real pair price via `IUniswapV2Pair(pair).current(token,1e18)`; observed mix price 147639521176897807 vs actual 926069876 (~159M x off).
**How found:** Code review of the oracle path: noticed getResult returns a cumulative sum (reserve0 + price) rather than a TWAP and lacks pair sync, then wrote a fork test comparing computed price to the pair's current price.
**Insight:** When an oracle adapter consumes a third-party 'TWAP' helper, verify the helper actually returns a UQ112x112 average price and syncs the pair; cumulative-sum functions (reserve0CumulativeLast + price*elapsed) look TWAP-like but are not, and the mismatch can be orders of magnitude.

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

**Test function comparing MixOracle price to real Uniswap price**

```
function testTotalPrice() public{
        IUniswapV2Pair(EQUALPAIR).sync();
        DebitaMixOracle.setAttachedTarotPriceOracle(EQUALPAIR);
        vm.warp(block.timestamp + 1201);
        IUniswapV2Pair(EQUALPAIR).sync();
        (int originPrice, int realPrice) = DebitaMixOracle.getTotalPrice(EQUAL, EQUALPAIR);
        console.logString(" price:");
        console.logUint(uint(originPrice));
        console.logString("actual price:");
        console.logUint(uint(realPrice));
        console.logString("price diff ratio:");
        console.logUint(uint(originPrice / realPrice));
    }
```

**Diff showing getTotalPrice returning real Uniswap price**

```
function getTotalPrice(address tokenAddress, address uniswapV2Pair) public returns (int, int) {
        // get tarotOracle address
        address _priceFeed = AttachedTarotOracle[tokenAddress];
        require(_priceFeed != address(0), "Price feed not set");
        require(!isPaused, "Contract is paused");
        ITarotOracle priceFeed = ITarotOracle(_priceFeed);

        address uniswapPair = AttachedUniswapPair[tokenAddress];
        require(isFeedAvailable[uniswapPair], "Price feed not available");
        // get twap price from token1 in token0
        (uint224 twapPrice112x112, ) = priceFeed.getResult(uniswapPair);
        address attached = AttachedPricedToken[tokenAddress];

        // Get the price from the pyth contract, no older than 20 minutes
        // get usd price of token0
        int attachedTokenPrice = IPyth(debitaPythOracle).getThePrice(attached);
        uint decimalsToken1 = ERC20(attached).decimals();
        uint decimalsToken0 = ERC20(tokenAddress).decimals();

        // calculate the amount of attached token that is needed to get 1 token1
        int amountOfAttached = int(
            (((2 ** 112)) * (10 ** decimalsToken1)) / twapPrice112x112
        );

        // calculate the price of 1 token1 in usd based on the attached token
        uint price = (uint(amountOfAttached) * uint(attachedTokenPrice)) /
            (10 ** decimalsToken1);

        require(price > 0, "Invalid price");
-       return int(uint(price));
+       uint wftmPrice = IUniswapV2Pair(uniswapV2Pair).current(tokenAddress, 1e18);
+       // uint realPrice = (uint(attachedTokenPrice)) * wftmPrice;
+       uint realPrice = (uint(attachedTokenPrice)) * wftmPrice / (10 ** decimalsToken1);
+       return (int(uint(price)), int(uint(realPrice)));
    }
```

