โพรโทคอลการให้กู้และการกู้ยืม
ศึกษาโครงสร้างสัญญาอัจฉริยะเบื้องหลังแพลตฟอร์มการให้กู้และกู้ยืมแบบกระจายศูนย์ เช่น Aave และ Compound
โพรโทคอลการให้กู้และการกู้ยืม เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Blockchain Smart Contracts with Solidity และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to DeFi Lending!
Decentralized Finance (DeFi) lending platforms allow users to lend out their crypto assets to earn interest, or borrow assets by providing collateral.
These platforms operate without traditional banks, using smart contracts to automate the entire process, making it transparent and accessible to anyone with an internet connection.
Who are the Players?
In a DeFi lending protocol, there are two main participants:
- Lenders: Users who deposit their cryptocurrencies into a smart contract to earn interest. They are providing liquidity.
- Borrowers: Users who want to take out a loan. They must provide collateral, usually more than the value of the loan (over-collateralization).
Lending Pools: The Core
Instead of peer-to-peer matching, DeFi lending platforms use liquidity pools. Lenders deposit their assets into these pools.
Borrowers then draw funds from these same pools. This model ensures continuous availability of assets and efficient interest rate determination.
Dynamic Interest Rates
Interest rates in DeFi lending are typically algorithmic, meaning they adjust automatically based on supply and demand within the liquidity pool.
- When a pool has high utilization (lots of borrowing), interest rates for borrowers increase, incentivizing lenders.
- When utilization is low, rates decrease, encouraging borrowing.
Securing Your Loan with Collateral
To borrow assets, you must provide collateral. This collateral is locked in the smart contract and serves as security for the loan.
DeFi loans are almost always over-collateralized, meaning the value of your collateral is greater than the value of your loan. The Loan-to-Value (LTV) ratio indicates how much you can borrow relative to your collateral.
When Collateral Isn't Enough
If the value of your collateral drops significantly, pushing your LTV ratio above a certain threshold, your loan becomes eligible for liquidation.
This means your collateral is automatically sold to repay your loan, preventing the protocol from incurring bad debt. This process is often carried out by external "liquidators" who earn a fee.
Inside the Lending Protocol
A DeFi lending protocol typically involves several smart contract components:
- LendingPool Contract: Manages deposits, borrows, interest, and liquidations.
- ERC-20 Token Contracts: For the actual assets being lent/borrowed (e.g., DAI, USDC, WETH).
- Oracle Contracts: Provide real-time price feeds for assets to calculate LTV.
Code Example: Deposit Logic
Let's look at a simplified deposit function. Users approve the contract to spend their tokens, then call this function to add liquidity. The contract records the deposit.
Try running the code to see the structure!
/* SPDX-License-Identifier: MIT */
pragma solidity ^0.8.0;
interface IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function transfer(address recipient, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
contract SimpleLendingPool {
// Mapping to track deposits: asset address => user address => amount
mapping(address => mapping(address => uint256)) public deposits;
// Function to deposit collateral
function deposit(address asset, uint256 amount) public {
require(amount > 0, "Deposit amount must be > 0");
// Transfer tokens from sender to this contract
bool success = IERC20(asset).transferFrom(msg.sender, address(this), amount);
require(success, "Token transfer failed");
deposits[asset][msg.sender] += amount;
// In a real protocol, you'd mint interest-bearing tokens here
}
// Placeholder for other functions like borrow, repay, withdraw
// ...
}Code Example: Borrow Logic
Building on the deposit, here's a simplified borrow function. It checks if the user has enough collateral and then transfers the requested asset from the pool to the borrower.
Note the simplified getAssetPrice for demonstration; real systems use robust oracles.
/* SPDX-License-Identifier: MIT */
pragma solidity ^0.8.0;
interface IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function transfer(address recipient, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
contract SimpleLendingPool {
mapping(address => mapping(address => uint256)) public deposits;
mapping(address => mapping(address => uint256)) public borrows;
// Simplified price oracle for demonstration
function getAssetPrice(address asset) internal pure returns (uint256) {
// Assume all assets have a price of 1 ether for simplicity (1e18 wei)
// In a real system, this would fetch actual prices from a robust oracle.
return 1 ether;
}
function deposit(address asset, uint256 amount) public {
require(amount > 0, "Deposit amount must be > 0");
IERC20(asset).transferFrom(msg.sender, address(this), amount);
deposits[asset][msg.sender] += amount;
}
// Function to borrow assets against collateral
function borrow(address assetToBorrow, uint256 amount, address collateralAsset) public {
require(amount > 0, "Borrow amount must be > 0");
require(deposits[collateralAsset][msg.sender] > 0, "No collateral deposited");
// Calculate collateral value and borrow value (simplified LTV of 50% for demo)
uint256 userCollateralValue = deposits[collateralAsset][msg.sender] * getAssetPrice(collateralAsset);
uint256 borrowValue = amount * getAssetPrice(assetToBorrow);
require(borrowValue <= userCollateralValue / 2, "Insufficient collateral for loan");
// Transfer tokens from this contract to the borrower
bool success = IERC20(assetToBorrow).transfer(msg.sender, amount);
require(success, "Token transfer failed");
borrows[assetToBorrow][msg.sender] += amount;
// A real contract would also track interest and repayment schedules
}
// ... (repay, withdraw functions would also be here in a full contract)
}Quick Check: Lending Concepts
You've learned about the core mechanics of DeFi lending. Now, let's see how well you understand the role of collateral.
Key Takeaways
In this lesson, we explored how decentralized lending and borrowing platforms work. You learned about:
- The roles of lenders and borrowers.
- The use of liquidity pools for efficient asset management.
- Dynamic interest rates based on supply and demand.
- The critical role of over-collateralization and LTV.
- How liquidation mechanisms protect the protocol.
- A simplified look at the
depositandborrowsmart contract functions.
Next, we'll dive into another advanced DeFi concept: Flash Loans!
เรียนรู้ Blockchain Smart Contracts with Solidity ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “โพรโทคอลการให้กู้และการกู้ยืม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “โพรโทคอลการให้กู้และการกู้ยืม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “โพรโทคอลการให้กู้และการกู้ยืม”
ศึกษาโครงสร้างสัญญาอัจฉริยะเบื้องหลังแพลตฟอร์มการให้กู้และกู้ยืมแบบกระจายศูนย์ เช่น Aave และ Compound คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “โพรโทคอลการให้กู้และการกู้ยืม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม
ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- AMM และกลุ่มสภาพคล่อง
- โพรโทคอลการให้กู้และการกู้ยืม
- สินเชื่อฉับพลันและการทำกำไรจากส่วนต่าง
- การทำฟาร์มผลตอบแทนและรางวัลจากการนำไปค้ำประกัน