Section 7 of 18
CToken: Withdraw (redeem)
Key takeaway: Compound V2's
redeem()andredeemUnderlying()functions burn cTokens to withdraw the underlying token. They route through the Comptroller'sredeemAllowedhook to verify that withdrawing won't push the user underwater on any outstanding borrows. The exchange-rate-driven math is symmetric withmint(), so the same accrueInterest-first pattern keeps the rate fresh. If the user has no borrows the check passes trivially; otherwise it depends entirely onaccountLiquidity.
What You Are Building
You are adding the withdrawal mechanism to the CToken. Users burn cTokens to get their underlying tokens back. This is the reverse of mintFresh() from the previous section, but with a critical difference: the Comptroller can block the withdrawal if those cTokens are backing a borrow.
Your Code
Requirements
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 continueAlready have an account? Log in