Modifier dan Pola Checks-Effects-Interactions
Gunakan modifier fungsi untuk menerapkan prasyarat dengan rapi dan terapkan pola checks-effects-interactions untuk menulis kontrak yang lebih aman.
Modifier dan Pola Checks-Effects-Interactions adalah pelajaran Blockchain Smart Contracts with Solidity gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Blockchain Smart Contracts with Solidity, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Blockchain Smart Contracts with Solidity mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Modifier dan Pola Checks-Effects-Interactions” gratis?
Ya — teks lengkap “Modifier dan Pola Checks-Effects-Interactions” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Blockchain Smart Contracts with Solidity, upgrade ke CoddyKit PRO. Kursus Blockchain Smart Contracts with Solidity mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Modifier dan Pola Checks-Effects-Interactions”?
Gunakan modifier fungsi untuk menerapkan prasyarat dengan rapi dan terapkan pola checks-effects-interactions untuk menulis kontrak yang lebih aman. Kamu berlatih Blockchain Smart Contracts with Solidity dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Blockchain Smart Contracts with Solidity?
Tidak diperlukan pengalaman sebelumnya. Blockchain Smart Contracts with Solidity di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Modifier dan Pola Checks-Effects-Interactions” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Blockchain Smart Contracts with Solidity ini?
Ya. Setiap pelajaran Blockchain Smart Contracts with Solidity menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Pewarisan dan Antarmuka
- Pustaka dan Kontrak Abstrak
- Penanganan Error dengan Revert/Require
- Modifier dan Pola Checks-Effects-Interactions