Submitted by cmichel
The total debt in MochiVault.accrueDebt increases by the current debt times the debt index growth.
This is correct but the total debt is then reduced again by the calling users discounted debt, meaning, the total debt depends on which specific user performs the debt accrual.
This should not be the case.
Assume we have a total debt of 2000, two users A and B, where A has a debt of 1000, and B has a debt of 100.
The (previous) debtIndex = 1.0 and accruing it now would increase it to 1.1.
Theres a difference if user A or B first does the accrual.
User A calls accrueDebt: increased = 2000 * 1.1/1.0 - 2000 = 200. Thus debts is first set to 2200. The users increasedDebt = 1000 * 1.1 / 1.0 - 1000 = 100 and assume a discount of 10%, thus discountedDebt = 100 * 10% = 10.
Then debts = 2200 - 10 = 2190.
The next accrual will work with a total debt of 2190.
User B calls accrueDebt: increased = 2000 * 1.1/1.0 - 2000 = 200. Thus debts is first set to 2200. The users increasedDebt = 100 * 1.1 / 1.0 - 100 = 10 and assume a discount of 10%, thus discountedDebt = 10 * 10% = 1.
Then debts = 2200 - 1 = 2199.
The next accrual will work with a total debt of 2199, leading to more debt overall.
The total debt of a system depends on who performs the accruals which should ideally not be the case.
The discrepancy compounds and can grow quite large if a whale always does the accrual compared to someone with almost no debt or no discount.
Dont use the discounts or track the weighted average discount across all users that is subtracted from the increased total debt each time, i.e., reduce it by the discount of all users (instead of current caller only) when accruing to correctly track the debt.
ryuheimat (Mochi) confirmed
