Submitted by serial-coder, also found by d3e4
The current implementation of the yDUSD vault does not properly support the auto-compounding token rewards mechanism by directly minting the DUSD assets to the vault.
Due to an incorrect accounting bug of the vaults total supply (shares), depositors can lose some deposited DUSD assets (principal) or even all the assets when the Ditto protocol mints DUSD debts to the vault to account for any discounts.
When the match price is below the oracle price, the OrdersFacet::_matchIsDiscounted() will be invoked to account for a discount by minting the discount (newDebt) (@1 in the snippet below) to the yDUSD vault.
Assuming the yDUSD vault is empty (i.e., both totalAssets and totalSupply are 0), for simplicitys sake. This step will increase the vaults total DUSD assets (totalAssets  spot balance) without updating the vaults total supply (totalSupply  tracked shares).
After @1, when a user deposits their DUSD assets to the yDUSD vault, the ERC4626::previewDeposit() (@2 in the snippet below) will be executed to calculate the shares based on the deposited assets.
An incorrect accounting bug of the vaults total supply (tracked shares) occurs in @1 above. In this example, the calculated shares will be 0 since the totalSupply is 0 (if the totalSupply != 0, the calculated shares can be less than expected). For more details, refer to @2.1 below.
Since the calculated shares == 0, the user will receive 0 shares and lose all deposited DUSD assets (@3). Even the slippage protection check (@4) cannot detect the invalid calculation due to the slippage.mul(shares) == 0, and the users tracked shares remain unchanged. (Actually, I discovered another issue regarding the slippage protection check, which will be reported separately.)
To calculate the users shares, the ERC4626::previewDeposit() calls the ERC4626::_convertToShares() (@2.1 in the snippet below).
The calculated shares will be 0 (due to the rounding down) (@2.2) because the ERC20::totalSupply() will return 0 (the vaults tracked total shares) while the ERC4626::totalAssets() will return the previously minted discount amount (i.e., the newDebt from @1). Refer to the @2.2 for a detailed explanation of the calculation.
As you can see, the ERC20::totalSupply() returns the vaults tracked total shares (0) (@2.2.1 in the snippet below), and ERC4626::totalAssets() returns the previously minted discount amount (i.e., the newDebt from @1) (@2.2.2).
Depositors can lose some deposited DUSD assets (principal) or whole assets when the Ditto protocol mints DUSD debts to the yDUSD vault to account for discounts.
This section provides a coded PoC.
Place the test_PoC_yDUSD_Yault_totalSupply_IncorrectInternalAccounting() in the .test/YDUSD.t.sol file and run the test using the command: forge test -vv --mt test_PoC_yDUSD_Yault_totalSupply_IncorrectInternalAccounting.
The PoC shows that a user loses all DUSD assets (both principal and yield) when he deposits the assets into the yDUSD vault right after the protocol has minted debts to the vault.
Rework the yDUSD vault by applying the concept of a single-sided auto-compounding token rewards mechanism of the xERC4626, which is fully compatible with the ERC4626 and ultimately maintains balances using internal accounting to prevent instantaneous changes in the exchange rate.
@ditto-eth (sponsor) acknowledged
Note: see original submission for full discussion.
