修飾子とChecks-Effects-Interactionsパターン
関数修飾子で事前条件を明確に適用し、Checks-Effects-Interactionsパターンを使ってより安全なコントラクトを作成します。
「修飾子とChecks-Effects-Interactionsパターン」はCoddyKit上の無料Blockchain Smart Contracts with Solidityレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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時間対応のAIチューター)、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時間対応のAIチューターがレッスンを進める中での質問に答えます。
Blockchain Smart Contracts with Solidityを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのBlockchain Smart Contracts with Solidityは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「修飾子とChecks-Effects-Interactionsパターン」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このBlockchain Smart Contracts with Solidityレッスンでコードを書いて実行できますか?
はい。すべてのBlockchain Smart Contracts with Solidityレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 継承とインターフェース
- ライブラリと抽象コントラクト
- Revert/Requireによるエラー処理
- 修飾子とChecks-Effects-Interactionsパターン