การทำฟาร์มผลตอบแทนและรางวัลจากการนำไปค้ำประกัน
ทำความเข้าใจว่าสัญญาฟาร์มผลตอบแทนและรางวัลจากการนำไปค้ำประกันของ DeFi แจกจ่ายโทเค็นตามเวลาอย่างไร รวมถึงรูปแบบการคำนวณรางวัลต่อโทเค็นที่โปรโตคอลชั้นนำใช้
การทำฟาร์มผลตอบแทนและรางวัลจากการนำไปค้ำประกัน เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Blockchain Smart Contracts with Solidity และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What Is Yield Farming?
Yield farming means depositing tokens into a protocol to earn additional token rewards. Users stake an asset, and the protocol distributes a reward token continuously over time to incentivize participation.
The Core Challenge
The hard part is fairly splitting rewards among many stakers who deposit and withdraw at different times, without looping over every user (which would be too gas-expensive).
The solution is the reward-per-token accumulator pattern.
Reward Per Token Idea
Track a single global accumulator: the total reward earned per staked token since the contract started. Each user stores a snapshot of this accumulator at their last interaction.
Their owed reward = stake amount times (current accumulator minus their snapshot).
State Variables
Set up the bookkeeping variables that drive the accounting.
uint256 public rewardRate; // reward tokens per second
uint256 public lastUpdateTime;
uint256 public rewardPerTokenStored;
uint256 public totalStaked;
mapping(address => uint256) public balances;
mapping(address => uint256) public userRewardPerTokenPaid;
mapping(address => uint256) public rewards;Computing Reward Per Token
The accumulator grows by the reward emitted since the last update, divided by the total staked.
function rewardPerToken() public view returns (uint256) {
if (totalStaked == 0) return rewardPerTokenStored;
uint256 elapsed = block.timestamp - lastUpdateTime;
return rewardPerTokenStored + (elapsed * rewardRate * 1e18) / totalStaked;
}Calculating Earned Rewards
A user's earned amount is their balance times the difference between the current accumulator and their last paid snapshot, plus anything already credited.
function earned(address account) public view returns (uint256) {
uint256 diff = rewardPerToken() - userRewardPerTokenPaid[account];
return (balances[account] * diff) / 1e18 + rewards[account];
}The Update Modifier
Before any state-changing action, settle the user's pending rewards and refresh the global accumulator. A modifier keeps this consistent.
modifier updateReward(address account) {
rewardPerTokenStored = rewardPerToken();
lastUpdateTime = block.timestamp;
if (account != address(0)) {
rewards[account] = earned(account);
userRewardPerTokenPaid[account] = rewardPerTokenStored;
}
_;
}Staking Tokens
Staking pulls tokens from the user and increases their balance and the total. The modifier settles rewards first.
function stake(uint256 amount) external updateReward(msg.sender) {
require(amount > 0, 'zero amount');
totalStaked += amount;
balances[msg.sender] += amount;
stakingToken.transferFrom(msg.sender, address(this), amount);
}Withdrawing Stake
Withdrawal mirrors staking: settle rewards, decrease balances, then return the staked tokens.
function withdraw(uint256 amount) external updateReward(msg.sender) {
require(balances[msg.sender] >= amount, 'too much');
totalStaked -= amount;
balances[msg.sender] -= amount;
stakingToken.transfer(msg.sender, amount);
}Claiming Rewards
Claiming sends accrued reward tokens to the user and resets their pending balance to zero.
function getReward() external updateReward(msg.sender) {
uint256 reward = rewards[msg.sender];
if (reward > 0) {
rewards[msg.sender] = 0;
rewardsToken.transfer(msg.sender, reward);
}
}Risks to Watch
Yield farming carries risks: impermanent loss when staking LP tokens, reward token inflation, and contract exploits. Always ensure the contract holds enough reward tokens to cover emissions and audit the math carefully.
Quick Check
Test your understanding of staking rewards.
Recap
You learned the staking rewards pattern used across DeFi:
- A global reward-per-token accumulator
- Per-user snapshots for O(1) accounting
- An
updateRewardmodifier settling state before stake, withdraw, and claim
This pattern fairly distributes continuous rewards while keeping gas costs low.
คำถามที่พบบ่อย
บทเรียน “การทำฟาร์มผลตอบแทนและรางวัลจากการนำไปค้ำประกัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทำฟาร์มผลตอบแทนและรางวัลจากการนำไปค้ำประกัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทำฟาร์มผลตอบแทนและรางวัลจากการนำไปค้ำประกัน”
ทำความเข้าใจว่าสัญญาฟาร์มผลตอบแทนและรางวัลจากการนำไปค้ำประกันของ DeFi แจกจ่ายโทเค็นตามเวลาอย่างไร รวมถึงรูปแบบการคำนวณรางวัลต่อโทเค็นที่โปรโตคอลชั้นนำใช้ คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การทำฟาร์มผลตอบแทนและรางวัลจากการนำไปค้ำประกัน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม
ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- AMM และกลุ่มสภาพคล่อง
- โพรโทคอลการให้กู้และการกู้ยืม
- สินเชื่อฉับพลันและการทำกำไรจากส่วนต่าง
- การทำฟาร์มผลตอบแทนและรางวัลจากการนำไปค้ำประกัน