المعدِّلات ونمط Checks-Effects-Interactions
استخدم معدِّلات الدوال لفرض الشروط المسبقة بوضوح، وطبّق نمط checks-effects-interactions لكتابة عقود أكثر أمانًا.
المعدِّلات ونمط Checks-Effects-Interactions درس مجاني في Blockchain Smart Contracts with Solidity على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Blockchain Smart Contracts with Solidity، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Blockchain Smart Contracts with Solidity 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What Are Modifiers?
A modifier is reusable code that runs before (or around) a function body. It is ideal for enforcing preconditions like access control without repeating the same checks everywhere.
Declaring a Modifier
The special _; placeholder marks where the function body executes. Code before it runs first; code after runs when the function returns.
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}Applying a Modifier
Attach a modifier to a function declaration. The check runs automatically on every call, keeping the function body focused on logic.
function withdraw() public onlyOwner {
payable(owner).transfer(address(this).balance);
}Modifiers with Parameters
Modifiers can take arguments, making them flexible for validating dynamic conditions such as minimum values.
modifier costs(uint price) {
require(msg.value >= price, "Not enough");
_;
}Stacking Multiple Modifiers
You can apply several modifiers to one function; they run left to right. Keep them small and single-purpose so combinations stay predictable.
function buy() public payable onlyWhenOpen costs(1 ether) {
// ...
}The Reentrancy Danger
When a contract calls out to an external address, that address can call back into your contract before the first call finishes. This reentrancy can drain funds if state is updated too late.
Checks-Effects-Interactions
The checks-effects-interactions pattern orders your function in three phases: validate inputs (checks), update state (effects), then call external contracts (interactions). External calls come last.
A Vulnerable Withdraw
This sends ETH before zeroing the balance, leaving a reentrancy window.
// VULNERABLE
function withdraw() public {
uint amt = balances[msg.sender];
payable(msg.sender).call{value: amt}("");
balances[msg.sender] = 0; // too late!
}The Safe Version
Update state before the external call so a reentrant call sees a zero balance.
// SAFE
function withdraw() public {
uint amt = balances[msg.sender];
balances[msg.sender] = 0; // effects first
payable(msg.sender).call{value: amt}(""); // interaction last
}Reentrancy Guards
For extra safety, add a reentrancy guard modifier that sets a lock flag during execution, rejecting nested calls. Libraries like OpenZeppelin provide nonReentrant.
Combining the Patterns
Use modifiers for the checks phase (access and validation), structure the body as effects-then-interactions, and add a guard on functions that move value. Together they form a strong defensive baseline.
Quick Check
Check your knowledge of safe contract patterns.
Recap
You learned defensive Solidity patterns:
- Modifiers centralize preconditions like access control
- They can take parameters and be stacked
- Checks-effects-interactions orders functions to prevent reentrancy
- Add a reentrancy guard on value-moving functions
These patterns are foundational to writing secure contracts.
الأسئلة الشائعة
هل درس «المعدِّلات ونمط Checks-Effects-Interactions» مجاني؟
نعم — نص درس «المعدِّلات ونمط Checks-Effects-Interactions» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Blockchain Smart Contracts with Solidity، انتقل إلى CoddyKit PRO. تتضمن دورة Blockchain Smart Contracts with Solidity 4 دروس في المجموع.
ماذا ستتعلم في «المعدِّلات ونمط Checks-Effects-Interactions»؟
استخدم معدِّلات الدوال لفرض الشروط المسبقة بوضوح، وطبّق نمط checks-effects-interactions لكتابة عقود أكثر أمانًا. تتمرن على Blockchain Smart Contracts with Solidity مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Blockchain Smart Contracts with Solidity؟
لا تُشترط خبرة سابقة. Blockchain Smart Contracts with Solidity على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «المعدِّلات ونمط Checks-Effects-Interactions»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Blockchain Smart Contracts with Solidity هذا؟
نعم. كل درس في Blockchain Smart Contracts with Solidity يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الوراثة والواجهات
- المكتبات والعقود المجرّدة
- معالجة الأخطاء باستخدام Revert/Require
- المعدِّلات ونمط Checks-Effects-Interactions