The Vault.sol's recalculateVaultsCreditCapacity's _updateCreditDelegationsuses wrong values to calculate the Vault's new updated credit delegation.
The recalculateVaultsCreditCapacity function is called or used in mostly all of the protocol's function. Hence this is the core function of the codebase. What this function does is perform many operations like updating states, etc in regards to vaults and the connected markets. And one of these operations is that it updates the vault's credit delegation to its connected markets and this is done via _updateCreditDelegations function.
Now the ISSUE originates from this function. What is does that caches the connectedMarketIds connected to a vault and iterates through them and then takes previousCreditDelegationUsdX18 of vault to the market id and the totalCreditDelegationWeightCache i.e "The total amount of credit delegation weight in the vault." along with these creditDelegationShareX18 is taken which is computes as:- UD60x18 creditDelegationShareX18 = ud60x18(creditDelegation.weight).div(ud60x18(totalCreditDelegationWeightCache)); , vaultCreditCapacityUsdX18 i.e totalAssets - totalDebt . And after that by taking vaultCreditCapacity and multipllying it by creditDelegationShareX18 newCreditDelegationUsdX18 is computed. Lastly creditDeltaUsdX18 variable is computed and updateTotalDelegatedCredit function is called passing in this creditDelta param and in this function the the totalDelgatedCreditUsd is updated by adding the creditDelta to totalDelegatedCreditUsd. Now the issue here is in the calculation of the creditDeltaUsdX18 variable, this is of type UD60x18 i.e unsigned. And this is computed like this - UD60x18 creditDeltaUsdX18 = newCreditDelegationUsdX18.sub(previousCreditDelegationUsdX18); Now this will return a negative value if the previous delegation > new delegation. I'll explain this by giving the example calculation using some values.
Lets assume that for a market id and a vault:
previousCreditDelegationUsdX18 = 500000e18
totalCreditDelegationWeightCache = 1 million assets in 18 dec i.e zaros internal precision
creditDelegationShareX18 = creditDelegation.weight / totalCreditDelegationWeightCache = 1e18 as weight set previously in the updateVaultAndCreditDelegationWeight function.
vaultCreditCapacityUsdX18- this is totalAssets - totalDebt, so lets say total debt is 80ke18 that means 1 million - 100k = 900ke18
newCreditDelegationUsdX18 = vaultCreditCapacityUsdX18 * creditDelegationShareX18 i.e 50ke18 * 1e18 = 900ke18
Now the creditDelta is computed as- newCreditDelegationUsdX18.sub(previousCreditDelegationUsdX18); i.e 900ke18 - 1 million This will oviously return a negative value but this will revert as the creditDeltaUsdX18 is set as UD60x18 type. this will result in revert of the recalculateVaultsCreditCapacity function.
Due to the wrong accounting of the creditDelta variable the recalculateVaultsCreditCapacity will revert which is one of most important and core functionality of the system
Either change the creditDeltaUsdX18 from type SD60x18 to UD60x18 or change the computation of it to the inverse of its current. for eg:
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.