0Pricing
Blockchain Smart Contracts with Solidity · 강의

UUPS 프록시 패턴 구현

안전한 계약 업그레이드를 위해 OpenZeppelin을 사용하여 범용 업그레이드 가능 프록시 표준(UUPS)을 구현하는 방법을 학습합니다.

UUPS 프록시 패턴 구현은(는) CoddyKit의 무료 Blockchain Smart Contracts with Solidity 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Blockchain Smart Contracts with Solidity 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Blockchain Smart Contracts with Solidity 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Understanding UUPS

The Universal Upgradeable Proxy Standard (UUPS) is a popular design pattern for creating upgradeable smart contracts. It allows you to change a contract's logic without changing its address on the blockchain.

This is crucial for fixing bugs, adding new features, or adapting to changing requirements without losing user data or contract state.

How UUPS Works

UUPS uses a proxy contract that holds the contract's state (data) and a separate implementation contract that holds the contract's logic (functions).

  • Proxy Contract: A simple, unchanging contract that forwards calls to the current implementation.
  • Implementation Contract: Contains the actual business logic. This is the contract that gets upgraded.

The key difference in UUPS is that the upgrade logic resides within the implementation contract, not the proxy.

Trusting OpenZeppelin

Implementing upgradeable contracts correctly is complex and prone to errors. That's why we rely on battle-tested libraries like OpenZeppelin Contracts.

OpenZeppelin provides secure and audited implementations of proxy patterns, including UUPS, making it much safer and easier to build upgradeable contracts.

Building UUPS Contracts

To make your contract UUPS-compatible, it needs to inherit from OpenZeppelin's UUPSUpgradeable contract. This base contract provides the necessary functions and modifiers for upgradeability.

Remember, upgradeable contracts use an initializer function instead of a constructor, as the proxy never calls the implementation's constructor directly.

Our First Upgradeable Contract

Here's a basic contract, MyContractV1, ready for UUPS upgrades. Notice it inherits from OpenZeppelin's UUPSUpgradeable. You'll typically install OpenZeppelin contracts via npm and import them.

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

import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";

contract MyContractV1 is UUPSUpgradeable {
    // This contract will contain our initial logic
    // We'll add an initializer in the next scene
}

Initializing UUPS Contracts

In upgradeable contracts, you cannot use a regular constructor. The proxy contract is deployed once, and its constructor is called. The implementation contract's constructor is never called when the proxy is deployed or upgraded.

Instead, you use an initializer function. This function is called once, typically right after the proxy is deployed, to set up the initial state of your logic contract.

Initializing State

Let's add an initializer to our MyContractV1. We use __UUPSUpgradeable_init() and __Ownable_init() to properly initialize base contracts, and Reinitializer(1) to prevent multiple initializations.

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

import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

contract MyContractV1 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
    string public message;

    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() {
        _disableInitializers();
    }

    function initialize(string memory initialMessage) public initializer {
        __UUPSUpgradeable_init();
        __Ownable_init(); // Initialize Ownable for access control
        message = initialMessage;
    }

    function setMessage(string memory newMessage) public onlyOwner {
        message = newMessage;
    }

    // Required for UUPS, defines who can upgrade
    function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}
}

Deploying Your UUPS Contract

Deploying UUPS contracts involves using an upgrades plugin (e.g., for Hardhat or Truffle). This plugin handles creating the proxy and linking it to your initial implementation contract.

  • It deploys your implementation contract.
  • It deploys the proxy contract, pointing to your implementation.
  • It calls the initialize function on the proxy.

This process ensures your contract is correctly set up for future upgrades.

Performing an Upgrade

When you want to upgrade your contract, you deploy a new version of your implementation contract (e.g., MyContractV2). Then, you call an upgrade function on the existing proxy contract, telling it to point to the new implementation.

The _authorizeUpgrade function in your implementation contract controls who has permission to perform this upgrade, often restricted to the contract owner.

Upgrading Your Logic

Let's imagine we want to add a new feature to MyContractV1. We create MyContractV2, inheriting from MyContractV1 to carry over its state and existing logic. We then add our new function.

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

import "./MyContractV1.sol"; // Assuming MyContractV1 is in the same directory

contract MyContractV2 is MyContractV1 {
    uint public counter; // New state variable

    // We don't need a new initializer unless we have new base contracts
    // to initialize, or specific V2 setup.

    function incrementCounter() public onlyOwner {
        counter++;
    }

    // Existing functions like setMessage and message getter are still available
    // via the proxy, using the logic from MyContractV2.
}

UUPS Key Concepts

Which of the following statements are TRUE regarding the Universal Upgradeable Proxy Standard (UUPS) implemented with OpenZeppelin?

UUPS: Secure Upgrades

In this lesson, you learned about the Universal Upgradeable Proxy Standard (UUPS) and how to implement it using OpenZeppelin Contracts.

  • UUPS separates state (proxy) from logic (implementation).
  • You use UUPSUpgradeable as a base contract.
  • initialize functions replace constructors for setup.
  • OpenZeppelin's upgrade plugins simplify deployment and upgrades.

UUPS is a powerful pattern for building future-proof smart contracts!

자주 묻는 질문

“UUPS 프록시 패턴 구현” 강의는 무료인가요?

네 — “UUPS 프록시 패턴 구현” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Blockchain Smart Contracts with Solidity 강의 전체를 잠금 해제할 수 있습니다. Blockchain Smart Contracts with Solidity 강의에는 총 4개의 강의가 포함되어 있습니다.

“UUPS 프록시 패턴 구현”에서 뭘 배우나요?

안전한 계약 업그레이드를 위해 OpenZeppelin을 사용하여 범용 업그레이드 가능 프록시 표준(UUPS)을 구현하는 방법을 학습합니다. 브라우저에서 직접 실행하는 실습 코드로 Blockchain Smart Contracts with Solidity을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Blockchain Smart Contracts with Solidity을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Blockchain Smart Contracts with Solidity은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“UUPS 프록시 패턴 구현” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Blockchain Smart Contracts with Solidity 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Blockchain Smart Contracts with Solidity 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 업그레이드 가능한 계약이 필요한 이유
  2. UUPS 프록시 패턴 구현
  3. 다이아몬드 표준(다중 패싯 프록시)
  4. 투명 프록시 패턴과 스토리지 레이아웃
← Blockchain Smart Contracts with Solidity(으)로 돌아가기