debts calculation is not accurate
The global debt variable is increased by the borrowed amount excluding the fee, but decreased by the amount including the fee, making the debt tracking inaccurate. This causes all calculations that rely on this variable to be slightly off.
Submitted by gpersoon
Impact
The value of the global variable debts in the contract MochiVault.sol is calculated in an inconsistent way.
In the function borrow() the variable debts is increased with a value excluding the fee. However in repay() and liquidate() it is decreased with the same value as details\[\_id].debt is decreased, which is including the fee.
This would mean that debts will end up in a negative value when all debts are repay-ed. Luckily the function repay() prevents this from happening.
In the meantime the value of debts isn’t accurate. This value is used directly or indirectly in:
utilizationRatio(),stabilityFee()calculateFeeIndex()ofMochiProfileV0.solliveDebtIndex(),accrueDebt(),currentDebt()ofMochiVault.sol
This means the entire debt and claimable calculations are slightly off.
Proof of Concept
function borrow(..)
details\[\_id].debt = totalDebt; // includes the fee
debts += \_amount; // excludes the fee
function repay(..)
debts -= \_amount;\
details\[\_id].debt -= \_amount;
function liquidate(..)
debts -= \_usdm;
details\[\_id].debt -= \_usdm;
see issue page for referenced code.
Recommended Mitigation Steps
In function borrow(): replace debts += \_amount; with debts += totalDebt
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Impact
The entire debt and claimable calculations are slightly off, which could affect interest rates and other dependent functions.
What the attacker needed
None
The fix
In the borrow function, the global debt variable should be increased by the total debt including the fee, not just the borrowed amount.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract keeps a running total of all debts. When someone borrows, the total should go up by the full amount they owe, including any fee. But the code only adds the borrowed amount without the fee. When they repay, it subtracts the full amount including the fee. This mismatch makes the total debt number wrong, which affects other calculations like interest rates.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.