# Incorrect accounting bug of the yDUSD vault leads to total loss of depositors' DUSD assets

yDUSD vault mints DUSD discounts directly to itself without updating tracked shares, so depositors get 0 shares and lose all principal.

- Payout: Audit contest
- Severity: high
- Program: DittoETH
- Found by: serial-coder
- Disclosed: 2024-07-29
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-07-dittoeth-findings/issues/7
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-dittoeth%3AH-03
- 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:share-inflation

**Target:** ERC4626 yDUSD vault / OrdersFacet discount minting
**Root cause:** Discount DUSD is minted directly to the vault, raising totalAssets (spot balance) without minting shares, so totalSupply stays 0 while totalAssets > 0; previewDeposit rounds shares down to 0.
**Steps:**
1. Trigger a discounted match so OrdersFacet._matchIsDiscounted mints newDebt DUSD to the yDUSD vault (totalAssets=newDebt, totalSupply=0).
2. User calls yDUSD.deposit(assets, receiver).
3. previewDeposit -> _convertToShares computes assets*(0+1)/(newDebt+1) = 0 when assets < newDebt.
4. _deposit mints 0 shares; slippage check `newBalance < 0.01e18*0 + oldBalance` passes.
5. User's DUSD is taken but balanceOf(receiver)=0; maxWithdraw(receiver)=0, withdraw reverts ERC4626WithdrawMoreThanMax.
**How found:** Code review of the discount path: noticed IERC20.mint to yieldVault bypasses share accounting, then traced ERC4626 previewDeposit/_convertToShares rounding against totalSupply=0 vs totalAssets=newDebt.
**Exploitation:** Any depositor who deposits after a discount mint to an empty/low-supply vault receives zero or fewer shares; their principal is absorbed into the vault and unrecoverable (withdraw reverts).
**Insight:** When a vault receives tokens via direct mint/transfer rather than deposit, check whether totalAssets (spot balance) can diverge from totalSupply; ERC4626 rounding-down then zeroes out shares for small deposits.

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

**Foundry PoC test showing depositor loses all DUSD**

```
function test_PoC_yDUSD_Yault_totalSupply_IncorrectInternalAccounting() public {
    // Create discounts to generate newDebt
    uint88 newDebt = uint88(discountSetUp());
    assertEq(rebasingToken.totalAssets(), newDebt);
    assertEq(rebasingToken.totalSupply(), 0);
    assertEq(rebasingToken.balanceOf(receiver), 0);
    assertEq(rebasingToken.balanceOf(_diamond), 0);
    assertEq(rebasingToken.balanceOf(_yDUSD), 0);
    assertEq(token.balanceOf(_yDUSD), newDebt);

    // Receiver deposits DUSD into the vault to get yDUSD
    vm.prank(receiver);
    rebasingToken.deposit(DEFAULT_AMOUNT, receiver);

    assertEq(rebasingToken.totalAssets(), DEFAULT_AMOUNT + newDebt);
    assertEq(rebasingToken.totalSupply(), 0);       // 0 amount due to the invalid accounting bug of totalSupply
    assertEq(rebasingToken.balanceOf(receiver), 0); // 0 amount due to the invalid accounting bug of totalSupply
    assertEq(rebasingToken.balanceOf(_diamond), 0);
    assertEq(rebasingToken.balanceOf(_yDUSD), 0);
    assertEq(token.balanceOf(_yDUSD), DEFAULT_AMOUNT + newDebt);

    // Match at oracle
    fundLimitBidOpt(DEFAULT_PRICE, DEFAULT_AMOUNT, extra);
    fundLimitAskOpt(DEFAULT_PRICE, DEFAULT_AMOUNT, extra);

    skip(C.DISCOUNT_WAIT_TIME);

    // Receiver expects to withdraw yDUSD and get back more DUSD than the original amount (Expect Revert!!!)
    // @dev Roughly DEFAULT_AMOUNT + newDebt, but rounded down
    uint88 withdrawAmountReceiver = 54999999999999999999990;
    vm.prank(receiver);
    vm.expectRevert(Errors.ERC4626WithdrawMoreThanMax.selector); // Expect Revert!!!
    rebasingToken.proposeWithdraw(withdrawAmountReceiver);

    // 0 amount due to the invalid accounting bug of totalSupply
    assertEq(rebasingToken.maxWithdraw(receiver), 0);
}
```

