0
0
Blockchain / Solidityprogramming~10 mins

Diamond pattern (EIP-2535) in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Diamond pattern (EIP-2535)
Start: Deploy Diamond Contract
Add Facets (Modules)
Map Functions to Facets
Call Function
Diamond Delegatecall to Facet
Execute Facet Logic
Return Result
End
The Diamond pattern deploys a main contract (Diamond) that delegates calls to multiple smaller contracts (Facets) based on function selectors.
Execution Sample
Blockchain / Solidity
contract Diamond {
  mapping(bytes4 => address) facets;
  fallback() external {
    address facet = facets[msg.sig];
    (bool success, ) = facet.delegatecall(msg.data);
    require(success, "Delegatecall failed");
  }
}
This code shows how the Diamond contract delegates calls to the correct facet using the function signature.
Execution Table
StepActionInputFacet SelectedDelegatecall ResultOutput
1Receive callfunctionA()Lookup facets['functionA()']facetA address foundProceed
2DelegatecallCall facetA.functionA()facetAExecute facetA logicReturn result from facetA
3Receive callfunctionB()Lookup facets['functionB()']facetB address foundProceed
4DelegatecallCall facetB.functionB()facetBExecute facetB logicReturn result from facetB
5Receive callfunctionX()Lookup facets['functionX()']No facet foundRevert call
6ExitNo valid facetNoneCall revertedError returned
💡 Execution stops when no facet matches the function selector, causing a revert.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
facets{}{'functionA()': facetA}{'functionA()': facetA, 'functionB()': facetB}{'functionA()': facetA, 'functionB()': facetB}{'functionA()': facetA, 'functionB()': facetB}
msg.sigN/AfunctionA()functionB()functionX()functionX()
facetnullfacetAfacetBnullnull
Key Moments - 3 Insights
Why does the Diamond contract use delegatecall instead of a normal call?
Because delegatecall runs the facet's code in the Diamond's storage context, preserving state and allowing modular upgrades. See execution_table steps 2 and 4 where delegatecall executes facet logic.
What happens if a function selector is not found in the facets mapping?
The call reverts because no facet handles that function. This is shown in execution_table step 5 and 6 where no facet is found and the call reverts.
How does the Diamond pattern help with contract upgrades?
It allows adding, replacing, or removing facets without redeploying the whole contract, by updating the facets mapping. This modularity is implied in variable_tracker where facets mapping changes over time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the facet selected at Step 3 when functionB() is called?
AfacetA
Bnull
CfacetB
DNo facet found
💡 Hint
Check the 'Facet Selected' column at Step 3 in the execution_table.
At which step does the call revert due to no facet found?
AStep 4
BStep 5
CStep 2
DStep 1
💡 Hint
Look for 'No facet found' in the 'Delegatecall Result' column in the execution_table.
If a new facet is added for functionX(), how would the variable 'facets' change after Step 5?
AIt would include 'functionX()' mapped to the new facet
BIt would remove 'functionB()'
CIt would stay the same
DIt would become empty
💡 Hint
Refer to variable_tracker showing how facets mapping updates when new functions are added.
Concept Snapshot
Diamond pattern (EIP-2535) allows a single contract to delegate calls to multiple smaller contracts called facets.
Uses a mapping from function selectors to facet addresses.
Calls are forwarded using delegatecall to preserve storage.
Enables modular upgrades by adding/replacing facets.
If no facet matches, the call reverts.
Common in blockchain for flexible contract design.
Full Transcript
The Diamond pattern (EIP-2535) is a way to build smart contracts that can be split into smaller parts called facets. The main contract, called the Diamond, receives calls and looks up which facet should handle the function based on the function signature. It then uses delegatecall to run the facet's code but keeps the Diamond's storage. This allows the contract to be upgraded by adding or changing facets without redeploying everything. If a function is called that no facet handles, the call will revert and return an error. The execution table shows how calls are routed step-by-step, and the variable tracker shows how the facets mapping changes as new facets are added. This pattern helps developers create flexible and upgradeable contracts on the blockchain.