زراعة العائد ومكافآت التخزين
افهم كيفية توزيع عقود زراعة العائد والتخزين في DeFi للرموز بمرور الوقت، بما في ذلك نمط محاسبة reward-per-token المستخدم في البروتوكولات الكبرى.
زراعة العائد ومكافآت التخزين درس مجاني في Blockchain Smart Contracts with Solidity على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.
تعلم Blockchain Smart Contracts with Solidity مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «زراعة العائد ومكافآت التخزين» مجاني؟
نعم — نص درس «زراعة العائد ومكافآت التخزين» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Blockchain Smart Contracts with Solidity، انتقل إلى CoddyKit PRO. تتضمن دورة Blockchain Smart Contracts with Solidity 4 دروس في المجموع.
ماذا ستتعلم في «زراعة العائد ومكافآت التخزين»؟
افهم كيفية توزيع عقود زراعة العائد والتخزين في DeFi للرموز بمرور الوقت، بما في ذلك نمط محاسبة reward-per-token المستخدم في البروتوكولات الكبرى. تتمرن على Blockchain Smart Contracts with Solidity مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- صنّاع السوق الآليون ومجمّعات السيولة
- بروتوكولات الإقراض والاقتراض
- القروض السريعة والمراجحة
- زراعة العائد ومكافآت التخزين