Section 10 of 18

Build
+20 Lynx

Comptroller: Liquidity Calculation

Key takeaway: Compound V2's accountLiquidity() is the single solvency calculation every state-changing operation routes through, summing collateral value across markets, applying each market's collateralFactor, and comparing to total borrow value. This is also where the majority of audit findings cluster in Compound V2 forks: oracle mismatches, mantissa errors, missing markets in iteration, and overflow in multiplication chains all surface here. Get this function right and the rest of the protocol stays solvent; get it wrong and bad debt accumulates silently.

What You Are Building

You are building the core risk calculation of Compound V2. This is the function that answers the most important question in the entire protocol: "Is this user solvent?" Every safety check in the system flows through this single calculation. It sums up a user's collateral and borrows across every market they have entered and returns either excess collateral (liquidity) or a deficit (shortfall).

This is the function that gets exploited most often in lending protocol audits. Rounding errors, stale prices, missing markets in the iteration, or incorrect collateral factor application here can make the entire protocol insolvent. When auditing Compound forks, this is where the majority of critical findings live.

Your Code

Solution.sol
Solidity
Loading editor...

Requirements

Write your implementation, then click Run Tests. Tests execute on the server.

Sign up free — keep reading + earn 20 Lynx

Zealynx Academy is free. Track your progress, earn Lynx, and climb the leaderboard.

Sign up free to continue

Already have an account? Log in