0Pricing
Web3 & DApp Development Fundamentals · Leçon

Pourquoi OpenZeppelin

Code éprouvé sur le terrain

Pourquoi OpenZeppelin est une leçon Web3 & DApp Development Fundamentals gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Web3 & DApp Development Fundamentals, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Web3 & DApp Development Fundamentals comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

What Is OpenZeppelin

OpenZeppelin Contracts is a library of secure, reusable smart contract building blocks for Solidity.

  • Implements common standards (ERC-20, ERC-721, ERC-1155).
  • Provides access control, security, and utility modules.
  • Is audited and widely used across production protocols.

Why Not Write It Yourself

Reimplementing token standards by hand is risky:

  • Subtle bugs in arithmetic or approvals can drain funds.
  • Standards have edge cases easy to miss.
  • Custom code is unaudited and untested by the community.

Reusing battle-tested code lets you focus on your unique logic.

Installing the Library

Add OpenZeppelin Contracts as a dependency:

npm install @openzeppelin/contracts

It installs into node_modules, and Hardhat resolves imports from there automatically.

npm install @openzeppelin/contracts

Importing Contracts

Import the modules you need by their package path:

// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

The path mirrors the library's folder structure (token, access, security, utils).

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

Inheriting a Standard

You build your contract by inheriting from a base. An ERC-20 token in just a few lines:

contract MyToken is ERC20 { constructor() ERC20("MyToken", "MTK") { _mint(msg.sender, 1000000 * 10 ** decimals()); } }

All transfer, approval, and balance logic is inherited.

contract MyToken is ERC20 {
    constructor() ERC20("MyToken", "MTK") {
        _mint(msg.sender, 1000000 * 10 ** decimals());
    }
}

Overriding Behavior

You can customize inherited functions by overriding them, while still calling the parent with super:

function transfer(address to, uint256 amount) public override returns (bool) { require(to != address(0), "No zero address"); return super.transfer(to, amount); }
function transfer(address to, uint256 amount)
    public override returns (bool) {
    require(to != address(0), "No zero address");
    return super.transfer(to, amount);
}

The Module Categories

OpenZeppelin is organized into areas:

  • token/ — ERC-20, ERC-721, ERC-1155 implementations.
  • access/ — Ownable, AccessControl.
  • security/ — ReentrancyGuard, Pausable.
  • utils/ — math, strings, cryptography helpers.

The Wizard

OpenZeppelin offers a web-based Contracts Wizard that generates a starter contract from checkboxes — pick the standard, toggle mintable, pausable, or access control, and copy the code.

It is a great way to scaffold a correct base before customizing.

Audited and Versioned

OpenZeppelin releases are audited and follow semantic versioning. Pin a version in package.json so a major upgrade does not silently change behavior:

"@openzeppelin/contracts": "^5.0.0"

Read the migration guide before bumping major versions.

"@openzeppelin/contracts": "^5.0.0"

Not a Silver Bullet

OpenZeppelin secures the building blocks, but your logic still needs review:

  • Inheriting securely written code does not make custom functions safe.
  • Misconfiguration (wrong roles, missing checks) can still create holes.

Always test and, for high-value contracts, get an audit.

Contracts vs Contracts-Upgradeable

OpenZeppelin ships two packages:

  • @openzeppelin/contracts — standard contracts with normal constructors.
  • @openzeppelin/contracts-upgradeable — the same modules adapted for proxies, using initializers.

Pick the upgradeable variant only if you actually deploy behind a proxy.

Quick Check

Test your understanding of why teams use OpenZeppelin.

Recap

You learned why OpenZeppelin is the standard library for Solidity.

  • It offers audited, reusable implementations of token standards and security modules.
  • Install with npm and inherit base contracts like ERC20.
  • Override functions with super to customize behavior.
  • Modules cover token, access, security, and utils; the Wizard scaffolds code.
  • Pin versions and still test your own logic — it is not a silver bullet.

Questions Fréquemment Posées

La leçon « Pourquoi OpenZeppelin » est-elle gratuite ?

Oui — le texte complet de « Pourquoi OpenZeppelin » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Web3 & DApp Development Fundamentals, passe à CoddyKit PRO. Le cours Web3 & DApp Development Fundamentals comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Pourquoi OpenZeppelin » ?

Code éprouvé sur le terrain Tu pratiques Web3 & DApp Development Fundamentals avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Web3 & DApp Development Fundamentals ?

Aucune expérience préalable n'est requise. Web3 & DApp Development Fundamentals sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.

Combien de temps prend la leçon « Pourquoi OpenZeppelin » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Web3 & DApp Development Fundamentals ?

Oui. Chaque leçon Web3 & DApp Development Fundamentals inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Pourquoi OpenZeppelin
  2. Contrôle des accès
  3. Extensions de jetons
  4. Contrats évolutifs
← Retour à Web3 & DApp Development Fundamentals