0Pricing
Web3 & DApp Development Fundamentals · Lektion

Upgradebare Contracts

Proxy-Patterns

Upgradebare Contracts ist eine kostenlose Web3 & DApp Development Fundamentals-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 Web3 & DApp Development Fundamentals-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.

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

The Immutability Problem

Deployed contract code cannot be changed. If you find a bug or need a new feature, the code is frozen at its address.

Upgradeable contracts work around this using a proxy pattern that lets you swap the logic while keeping the same address and data.

The Proxy Pattern

Upgradeability splits a contract into two parts:

  • Proxy — holds the state and a pointer to the logic; users interact with it.
  • Implementation — holds the code, no permanent state.

The proxy delegatecalls into the implementation, so logic runs against the proxy's storage.

How delegatecall Works

delegatecall executes another contract's code in the caller's storage context. So when the proxy delegatecalls the implementation:

  • Code comes from the implementation.
  • Storage read/written is the proxy's.

Upgrading just changes which implementation the proxy points to.

Initializers, Not Constructors

Constructors run at deploy time and do not affect proxy storage. Upgradeable contracts use an initialize function instead:

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; contract Box is Initializable { uint256 public value; function initialize(uint256 v) public initializer { value = v; } }
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

contract Box is Initializable {
    uint256 public value;
    function initialize(uint256 v) public initializer {
        value = v;
    }
}

The Upgradeable Library

OpenZeppelin ships a separate package for this. Its modules use initializers instead of constructors:

npm install @openzeppelin/contracts-upgradeable

Pair it with the Hardhat Upgrades plugin to deploy and manage proxies safely.

npm install @openzeppelin/contracts-upgradeable @openzeppelin/hardhat-upgrades

Deploying a Proxy

The Hardhat Upgrades plugin deploys the implementation and proxy together, calling your initializer:

const { ethers, upgrades } = require("hardhat"); const Box = await ethers.getContractFactory("Box"); const box = await upgrades.deployProxy(Box, [42]); await box.waitForDeployment();

The array holds the initializer arguments.

const { ethers, upgrades } = require("hardhat");

const Box = await ethers.getContractFactory("Box");
const box = await upgrades.deployProxy(Box, [42]);
await box.waitForDeployment();

Performing an Upgrade

To upgrade, deploy a new implementation and point the existing proxy at it:

const BoxV2 = await ethers.getContractFactory("BoxV2"); const upgraded = await upgrades.upgradeProxy( await box.getAddress(), BoxV2 );

The address and stored state are preserved; only the logic changes.

const BoxV2 = await ethers.getContractFactory("BoxV2");
const upgraded = await upgrades.upgradeProxy(
  await box.getAddress(),
  BoxV2
);

Storage Layout Rules

Because the proxy keeps its storage, new versions must preserve the storage layout:

  • Never reorder or remove existing state variables.
  • Only append new variables at the end.
  • Reserve gaps in base contracts for future fields.

The plugin checks for unsafe changes and warns you.

Transparent vs UUPS

Two common proxy styles:

  • Transparent — upgrade logic lives in the proxy; simple but slightly more gas.
  • UUPS — upgrade logic lives in the implementation (via UUPSUpgradeable); cheaper, but you must not forget to include it.

UUPS is now the generally recommended default.

Upgradeability Trade-offs

Upgradeability is powerful but adds risk:

  • Whoever controls upgrades can change the rules — a centralization concern.
  • Storage and initializer mistakes can brick the contract.

Guard the upgrade authority with a multisig or timelock, and test upgrades thoroughly.

Disabling Initializers in the Implementation

An implementation contract should never be initialized directly. Lock it in its constructor:

constructor() { _disableInitializers(); }

This prevents an attacker from taking over the standalone implementation while leaving the proxy's initializer usable.

constructor() {
    _disableInitializers();
}

Quick Check

Test your understanding of upgradeable contracts.

Recap

You learned how upgradeable contracts work.

  • A proxy holds state and delegatecalls a swappable implementation.
  • Use initialize (guarded by initializer) instead of a constructor.
  • The Hardhat Upgrades plugin deploys and upgrades proxies safely.
  • Preserve storage layout: only append variables; use gaps.
  • Choose Transparent or UUPS, and protect upgrade authority with a multisig or timelock.

Häufig gestellte Fragen

Ist die Lektion „Upgradebare Contracts“ kostenlos?

Ja — der vollständige Text von „Upgradebare Contracts“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Web3 & DApp Development Fundamentals-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Upgradebare Contracts“?

Proxy-Patterns Du übst Web3 & DApp Development Fundamentals 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 Web3 & DApp Development Fundamentals zu starten?

Keine Vorkenntnisse erforderlich. Web3 & DApp Development Fundamentals 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 „Upgradebare Contracts“?

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 Web3 & DApp Development Fundamentals-Lektion Code schreiben und ausführen?

Ja. Jede Web3 & DApp Development Fundamentals-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. Warum OpenZeppelin?
  2. Zugriffskontrolle
  3. Token-Erweiterungen
  4. Upgradebare Contracts
← Zurück zu Web3 & DApp Development Fundamentals