Section 13 of 18

Build
+15 Lynx

CToken: Repay

Key takeaway: Compound V2's repayBorrow() and repayBorrowBehalf() functions burn debt by transferring underlying back to the cToken contract, with the latter allowing third-party repayments (essential for liquidator flows). Both call accrueInterest first so the repayment applies against current debt. The function caps the repay amount at the current debt to prevent users from accidentally over-paying, a small but important defensive choice that some forks remove during refactors.

What You Are Building

You are building the repayment mechanism that reverses a borrow. When a borrower wants to reduce their debt, they send underlying tokens back to the protocol. The protocol reduces their BorrowSnapshot and decreases totalBorrows. This is the mirror image of borrowFresh from the previous section.

Repayment introduces two patterns that do not exist in Uniswap V2 because Uniswap has no debt. First, the "repay on behalf" pattern: anyone can repay another user's debt. No approval is needed because repaying someone's debt can only help them. This is essential for liquidation, where the liquidator repays the borrower's debt as part of the seizure process. Second, the type(uint256).max convenience pattern: instead of forcing the user to calculate their exact debt (which changes every block due to interest accrual), they can pass max uint to mean "repay everything."

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 15 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