Section 12 of 18
CToken: Borrow
Key takeaway: Compound V2's
borrow()function transfers underlying tokens to the user and increments their debt, but only afteraccrueInterestand a ComptrollerborrowAllowedcheck that runs throughaccountLiquidityto verify the borrow keeps the user solvent. The borrower'sborrowIndexsnapshot is updated to the current global index so future debt calculations are correct. Get the snapshot ordering wrong and the borrower over- or under-pays interest on every subsequent call.
What You Are Building
You are adding the borrow mechanism to the CToken. This is what makes Compound a lending protocol, not just a savings account. Users who have supplied collateral and entered markets can borrow underlying tokens from any market. The borrowed amount accrues interest over time through the BorrowSnapshot pattern you saw in section 5.
Uniswap V2 has no equivalent. There is nothing in Uniswap that lets you take tokens out of a pool and owe them back later. Borrowing is an entirely new concept relative to what you built in the DEX module.
Your Code
Requirements
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 continueAlready have an account? Log in