0Pricing
Blockchain Smart Contracts with Solidity · 课时

Diamond 标准(多 facet 代理)

探索 Diamond 标准(EIP-2535),使用多个 facet 构建高度模块化且可扩展的可升级合约。

Diamond 标准(多 facet 代理) 是 CoddyKit 上的免费 Blockchain Smart Contracts with Solidity 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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!

常见问题解答

「Diamond 标准(多 facet 代理)」课时是免费的吗?

是的 — 「Diamond 标准(多 facet 代理)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Blockchain Smart Contracts with Solidity 课程的其余内容,请升级到 CoddyKit PRO。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。

「Diamond 标准(多 facet 代理)」这节课中我会学到什么?

探索 Diamond 标准(EIP-2535),使用多个 facet 构建高度模块化且可扩展的可升级合约。 你通过在浏览器中直接运行的动手代码来练习 Blockchain Smart Contracts with Solidity,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Blockchain Smart Contracts with Solidity 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Blockchain Smart Contracts with Solidity 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「Diamond 标准(多 facet 代理)」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Blockchain Smart Contracts with Solidity 课中编写并运行代码吗?

能。每节 Blockchain Smart Contracts with Solidity 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 为什么需要可升级合约
  2. UUPS 代理模式实现
  3. Diamond 标准(多 facet 代理)
  4. 透明代理模式与存储布局
← 返回 Blockchain Smart Contracts with Solidity