0
0
Blockchain / Solidityprogramming~30 mins

Diamond pattern (EIP-2535) in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Diamond Pattern (EIP-2535) Basic Setup
📖 Scenario: You are building a modular smart contract system using the Diamond pattern (EIP-2535). This pattern allows you to split your contract logic into multiple smaller contracts called facets, which can be added, replaced, or removed dynamically.This project guides you through creating a simple diamond contract with one facet and then adding a function selector to it.
🎯 Goal: Build a basic diamond contract setup with one facet and add a function selector to the diamond's selector set.
📋 What You'll Learn
Create a diamond storage struct to hold facet addresses and selectors
Define a facet contract with one function
Add a function selector to the diamond storage
Print the selector to verify it was added
💡 Why This Matters
🌍 Real World
The Diamond pattern is used in blockchain to build upgradeable and modular smart contracts that can grow and change without losing state.
💼 Career
Understanding EIP-2535 is valuable for blockchain developers working on complex decentralized applications requiring modularity and upgradeability.
Progress0 / 4 steps
1
Create Diamond Storage Struct
Create a struct called DiamondStorage with a mapping(bytes4 => address) facets to store function selectors and their facet addresses. Then create a function diamondStorage() that returns a storage pointer to DiamondStorage at a fixed slot.
Blockchain / Solidity
Need a hint?

Use mapping(bytes4 => address) inside the struct to map selectors to facet addresses. Use keccak256 to get a unique storage slot and inline assembly to assign it.

2
Create a Facet Contract with One Function
Create a contract called SimpleFacet with a public function hello() that returns the string "Hello, Diamond!".
Blockchain / Solidity
Need a hint?

Define a contract with a simple public function that returns a fixed string.

3
Add Function Selector to Diamond Storage
Inside a new contract called Diamond, create a function addFacet() that stores the address of SimpleFacet for the selector of hello() in the diamond storage mapping facets. Use diamondStorage() to access storage. Use SimpleFacet.hello.selector to get the selector.
Blockchain / Solidity
Need a hint?

Use the diamond storage function to get storage, then assign the facet address to the selector key.

4
Print the Stored Selector Address
In the Diamond contract, add a public view function getFacetAddress() that takes a bytes4 selector and returns the facet address from diamond storage. Then write a script to deploy SimpleFacet and Diamond, call addFacet() with the SimpleFacet address, and print the address returned by getFacetAddress(SimpleFacet.hello.selector).
Blockchain / Solidity
Need a hint?

Write a getter function to read from diamond storage. Then simulate deployment and calls to add and retrieve the facet address.