Blockchain Smart Contracts with Solidity · บทเรียน

มาตรฐาน Diamond (พร็อกซีหลายแฟเซต)

สำรวจมาตรฐาน Diamond (EIP-2535) สำหรับสร้างสัญญาที่อัปเกรดได้ มีการแบ่งโมดูลสูง และขยายระบบได้ด้วยหลายแฟเซต

บทเรียน 3 จาก 411 ขั้นตอน

มาตรฐาน Diamond (พร็อกซีหลายแฟเซต) เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Blockchain Smart Contracts with Solidity และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Intro to Diamond Standard

Welcome to the final lesson on upgradeable contracts! We've explored UUPS, but what if your contract grows too big or needs extreme modularity?

The Diamond Standard (EIP-2535) is a powerful pattern that allows a single proxy contract to delegate calls to multiple implementation contracts, called facets.

Single Proxy Limitations

Traditional proxy patterns, like UUPS or Transparent proxies, typically delegate all calls to a single implementation contract.

While effective, this can lead to:

  • Contract Size Limit: Ethereum contracts have a 24KB limit. A single, monolithic implementation can hit this quickly.
  • Complexity: A single contract managing many features becomes hard to maintain and audit.
  • Upgrade Costs: Upgrading means redeploying the entire large implementation contract, even if only a small part changed.

Facets: Modular Contracts

The Diamond Standard solves this by breaking down your contract's logic into smaller, independent pieces called facets.

Think of facets as:

  • Individual smart contracts, each handling specific functionalities.
  • Like 'plugins' or 'modules' for your main proxy.
  • Each facet has its own set of functions.

The Diamond proxy then decides which facet to forward a call to.

How Diamond Proxies Work

At its core, a Diamond proxy works similarly to other proxies:

  1. A user calls a function on the Diamond proxy.
  2. The proxy looks at the function selector (the first 4 bytes of the call data).
  3. It then determines which facet contains that function.
  4. Finally, it delegates the call (using delegatecall) to the correct facet.

The magic is that the proxy maintains a mapping of function selectors to facet addresses.

Diamond Storage: Shared State

A crucial aspect of the Diamond Standard is how state is managed. Even though logic is split across facets, they all operate on the same storage within the Diamond proxy contract.

This is achieved by:

  • Defining a shared storage struct (e.g., AppStorage).
  • Each facet accesses this shared storage using a specific Solidity pattern (often via a library).
  • This ensures consistency and allows facets to interact with the same data.

Managing Facets with `diamondCut`

The central function for managing your Diamond is diamondCut. This function allows you to:

  • Add new facets (new functionality).
  • Replace existing facets (upgrade or fix bugs).
  • Remove facets (deprecate functionality).

diamondCut is typically only callable by the contract's owner, making it the secure upgrade mechanism for your multi-faceted contract.

A Simple Facet Example

Here's what a very basic Solidity facet might look like. Remember, facets are just regular contracts that implement specific logic.

Try compiling this simple example:

pragma solidity ^0.8.0;

contract MySimpleFacet {
    // Facets interact with shared storage
    // located in the Diamond proxy contract.
    // They don't declare state variables directly
    // in the way a standalone contract would.

    function getFacetVersion() external pure returns (string memory) {
        return "MySimpleFacet v1.0";
    }

    function greetUser(string memory _name) external pure returns (string memory) {
        return string(abi.encodePacked("Hello, ", _name, " from MySimpleFacet!"));
    }
}

The Diamond Proxy Itself

The Diamond proxy contract itself is remarkably lean. Its primary responsibilities are:

  • Storing the mapping of function selectors to facet addresses.
  • Implementing the diamondCut function for upgrades.
  • The fallback function, which handles delegating calls to the correct facet.

It acts as the central router for all incoming calls, directing them to the appropriate piece of logic.

Why Choose Diamond Standard?

The Diamond Standard offers significant advantages for complex DApps:

  • Unlimited Contract Size: Easily bypass the 24KB limit by splitting logic into many small facets.
  • Extreme Modularity: Develop and deploy features independently.
  • Gas Efficiency: Only upgrade (deploy) the specific facets that change, not the entire contract.
  • Clear Separation of Concerns: Improves code readability, testing, and security auditing.
  • Incremental Development: Add new features over time without affecting existing ones.

Diamond Standard Check

The Diamond Standard (EIP-2535) provides a robust framework for building highly modular and upgradeable smart contracts. Which of the following statements accurately describe its key benefits or characteristics?

Recap: Multi-facet Proxies

In this lesson, we explored the Diamond Standard (EIP-2535), a powerful pattern for building highly modular and upgradeable smart contracts.

  • We learned about facets, which are individual contracts containing specific logic.
  • The Diamond proxy delegates calls to these multiple facets based on function selectors.
  • All facets share the Diamond proxy's storage.
  • The diamondCut function is used to add, replace, or remove facets.
  • Key benefits include overcoming the 24KB contract size limit, extreme modularity, and efficient upgrades.

You're now equipped with knowledge of advanced upgradeability patterns!

เริ่มต้นได้ฟรี

เรียนรู้ Blockchain Smart Contracts with Solidity ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “มาตรฐาน Diamond (พร็อกซีหลายแฟเซต)” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “มาตรฐาน Diamond (พร็อกซีหลายแฟเซต)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “มาตรฐาน Diamond (พร็อกซีหลายแฟเซต)”

สำรวจมาตรฐาน Diamond (EIP-2535) สำหรับสร้างสัญญาที่อัปเกรดได้ มีการแบ่งโมดูลสูง และขยายระบบได้ด้วยหลายแฟเซต คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “มาตรฐาน Diamond (พร็อกซีหลายแฟเซต)” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม

ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใดสัญญาจึงควรอัปเกรดได้
  2. การนำรูปแบบพร็อกซี UUPS มาใช้
  3. มาตรฐาน Diamond (พร็อกซีหลายแฟเซต)
  4. รูปแบบพร็อกซีโปร่งใสและการจัดวางหน่วยความจำจัดเก็บ
← กลับไปที่ Blockchain Smart Contracts with Solidity