0Pricing
Blockchain Smart Contracts with Solidity · Lektion

Modifier und das Checks-Effects-Interactions-Pattern

Verwenden Sie Funktions-Modifier, um Vorbedingungen sauber durchzusetzen, und wenden Sie das Checks-Effects-Interactions-Pattern an, um sicherere Contracts zu schreiben

Modifier und das Checks-Effects-Interactions-Pattern ist eine kostenlose Blockchain Smart Contracts with Solidity-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Blockchain Smart Contracts with Solidity-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Blockchain Smart Contracts with Solidity-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Modifier und das Checks-Effects-Interactions-Pattern“ kostenlos?

Ja — der vollständige Text von „Modifier und das Checks-Effects-Interactions-Pattern“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Blockchain Smart Contracts with Solidity-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Blockchain Smart Contracts with Solidity-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Modifier und das Checks-Effects-Interactions-Pattern“?

Verwenden Sie Funktions-Modifier, um Vorbedingungen sauber durchzusetzen, und wenden Sie das Checks-Effects-Interactions-Pattern an, um sicherere Contracts zu schreiben Du übst Blockchain Smart Contracts with Solidity mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Blockchain Smart Contracts with Solidity zu starten?

Keine Vorkenntnisse erforderlich. Blockchain Smart Contracts with Solidity auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Modifier und das Checks-Effects-Interactions-Pattern“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Blockchain Smart Contracts with Solidity-Lektion Code schreiben und ausführen?

Ja. Jede Blockchain Smart Contracts with Solidity-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Vererbung und Interfaces
  2. Libraries und abstrakte Contracts
  3. Fehlerbehandlung mit Revert/Require
  4. Modifier und das Checks-Effects-Interactions-Pattern
← Zurück zu Blockchain Smart Contracts with Solidity