0Pricing
Web3 & DApp Development Fundamentals · レッスン

アップグレード可能なコントラクト

プロキシパターン

「アップグレード可能なコントラクト」はCoddyKit上の無料Web3 & DApp Development Fundamentalsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWeb3 & DApp Development Fundamentals学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Web3 & DApp Development Fundamentalsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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.

よくある質問

「アップグレード可能なコントラクト」レッスンは無料ですか?

はい。「アップグレード可能なコントラクト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web3 & DApp Development Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web3 & DApp Development Fundamentalsコースには全4レッスンが含まれています。

「アップグレード可能なコントラクト」で何を学びますか?

プロキシパターン ブラウザで直接実行するハンズオンコードでWeb3 & DApp Development Fundamentalsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Web3 & DApp Development Fundamentalsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWeb3 & DApp Development Fundamentalsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「アップグレード可能なコントラクト」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWeb3 & DApp Development Fundamentalsレッスンでコードを書いて実行できますか?

はい。すべてのWeb3 & DApp Development Fundamentalsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. OpenZeppelinを使う理由
  2. アクセス制御
  3. トークン拡張
  4. アップグレード可能なコントラクト
← Web3 & DApp Development Fundamentalsに戻る