# Numerous errors when calculating the TVL for the MorphoBlue connector

MorphoBlue connector TVL math ignores token decimals, adds borrows instead of subtracting, and skips loanToken registry update, corrupting share pricing.

- Payout: Audit contest
- Severity: high
- Program: NOYA
- Found by: Zac
- Disclosed: 2025-02-17
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-04-noya-findings/issues/778
- Library page: https://bugbountylibrary.com/r/c4%3A2024-04-noya%3AH-19
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:precision-loss

**Target:** MorphoBlueConnector TVL accounting in NOYA vault
**Root cause:** convertCToL ignores collateral vs loan token decimal differences; _getPositionTVL adds borrowAmount instead of subtracting; withdraw only calls _updateTokenInRegistry for collateralToken, not loanToken.
**Steps:**
1. Deploy MorphoBlue connector with USDC/DAI market (6 vs 18 decimals).
2. Supply 1000 USDC via `connector.supply(amount, usdcDaiMarketId, true)`; TVL = 1e9.
3. Supply 1000 DAI via `connector.supply(amount_dai, usdcDaiMarketId, false)`; TVL jumps to ~1e21 (1000000000001000000000) instead of 2e9 due to missing decimal conversion in `convertCToL`.
4. Call `connector.borrow(amount/2, usdcDaiMarketId)`; TVL increases instead of decreasing because borrowAmount is added in `_getPositionTVL`.
5. Call `connector.repay(amount/2, usdcDaiMarketId)`; TVL decreases.
6. Withdraw USDC then DAI; after DAI withdraw TVL drops to 999945369 because `withdraw` never calls `_updateTokenInRegistry` for loanToken.
**How found:** Manual review of MorphoBlueConnector TVL accounting, noticing decimal mismatch between USDC and DAI, sign error on borrowAmount, and missing registry update on loanToken withdrawal; validated with a Foundry test in MorphoBlue.t.sol.
**Exploitation:** Depositors/withdrawers receive mispriced shares: when TVL is understated, depositors get excess shares and withdrawers get too few base tokens; when overstated, depositors get too few shares and withdrawers extract excess base tokens, enabling value extraction from the vault.
**Insight:** When auditing multi-asset connectors, check that every token conversion normalizes decimals and that debt is subtracted, not added, in position TVL; also verify every asset touched in withdraw/supply is registered so TVL reflects it.

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

**Foundry test showing TVL miscalculation**

```
function testSupplyBorrowRepayWithdraw() public {
        uint256 amount = 1000 * 1e6;
        uint256 amount_dai = 1000 * 1e18;
        console.log("----------- Test Deposit -----------");
        _dealWhale(baseToken, address(connector), USDC_Whale, amount);
        _dealERC20(DAI, address(connector), amount_dai);
        vm.startPrank(owner);

        assertEq(
            address(morphoBlueContract), address(connector.morphoBlue()), "MorphoBlue address is not set correctly"
        );

        connector.supply(amount, usdcDaiMarketId, true);
        uint256 tvl = accountingManager.TVL();
        console.log("TVL: %s", tvl); // tvl = 1000000000

        connector.supply(amount_dai, usdcDaiMarketId, false);
        tvl = accountingManager.TVL();
        console.log("TVL before borrow: %s", tvl); // tvl = 1000000000001000000000
        // NOTE: Due to DAI having a different number of decimals than USDC, the TVL increases by a very large amount
        // correct TVL = 2000000000

        connector.borrow(amount / 2, usdcDaiMarketId);
        tvl = accountingManager.TVL();
        console.log("TVL after borrow: %s", tvl); // 1000000000002000000000
        // NOTE: The TVL increases because the borrow is added instead of subtracted from the TVL

        connector.repay(amount / 2, usdcDaiMarketId);
        tvl = accountingManager.TVL();
        console.log("TVL after repay: %s", tvl); // 1000000000001000000000

        connector.withdraw(amount, usdcDaiMarketId, true);
        tvl = accountingManager.TVL();
        console.log("TVL after withdraw 1: %s", tvl); // 1000000000000000000000

        connector.withdraw(amount_dai, usdcDaiMarketId, false);
        tvl = accountingManager.TVL();
        console.log("TVL after withdraw 2: %s", tvl); // 999945369
        // NOTE: The TVL is half of what it should be because the withdraw function does not call _updateTokenInRegistry for the loanToken
        
        vm.stopPrank();
    }
```

