Comparator mistake in CreditDelegationBranch.settleVaultDebts
will break the functionality and make it does the opposite of its intention.
An overview of CreditDelegationBranch.settleVaultDebts
is like the following:
For given vaultId
, get unsettledRealizedDebtUsd
If unsettledRealizedDebtUsd < 0
, convert vault's collateral asset to usdc
usdcOut
is roughly abs(unsettledRealizedDebtUsd)
vault.marketUnrealizedDebtUsd -= usdcOut
Otherwise, convert market's usdc to vault's collateral asset
usdcIn
is roughly unsettledRealizedDebtUsd
vault.depositedUsdc -= usdcIn
Here, vault.unsettledRealizedDebtUsd = vault.marketUnrealizedDebtUsd - vault.depositedUsdc
Let's check what happens for unsettledRealizedDebtUsd < 0
If unsettledRealizedDebtUsd < 0
it means marketUnrealizedDebtUsd < depositedUsdc
, i.e. deposited USDC is greater than vault's debt.
According to the above flow (No. 2), the protocol swaps vault's collateral asset to usdc. So usdc balance will be increased further. What's more, by 2.ii.,unsettledRealizedDebtUsdAfter = unsettledRealizedDebtUsdBefore - usdcOut
i.e. the credit has been increased, instead of being settled.
Similar thing happens for the case when unsettledRealizedDebtUsd > 0
. i.e. usdc being swapped to collateral asset in spite of usdc shortage. And vault debt being increased by unsettledRealizedDebtUsd
by 3.ii.
So the current functionality is broken, it worsens the situation instead of settling it.
Impossible to settle vaults debts
Incorrect accounting of USD token backing
Manual Review
Vault is in credit when unsettledRealizedDebtUsd < 0
, in debt when unsettledRealizedDebtUsd > 0
.
Current code should be fixed like the following:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.