This project implements a complete ERC-2535 Diamond proxy smart contract using Foundry. The Diamond standard allows for modular smart contracts where functionality can be added, replaced, or removed through facets.
- Diamond Proxy Pattern: Implements the ERC-2535 standard for upgradeable smart contracts
- Modular Architecture: Functionality is separated into different facets
- Two Custom Facets:
StorageFacet: Manages storage operations (values, strings, user balances)MathFacet: Provides mathematical operations (add, subtract, multiply, divide, power, factorial, fibonacci)
- Standard Facets:
DiamondCutFacet: Manages diamond cuts (adding/removing/replacing facets)DiamondLoupeFacet: Provides introspection capabilitiesOwnershipFacet: Manages contract ownership
Diamond (Proxy)
├── DiamondCutFacet (manage upgrades)
├── DiamondLoupeFacet (introspection)
├── OwnershipFacet (ownership management)
├── StorageFacet (storage operations)
└── MathFacet (mathematical operations)
forge buildforge testforge script script/Deploy.s.sol --rpc-url <your_rpc_url> --private-key <your_private_key> --broadcastAfter deployment, you can interact with the diamond through any of the facet interfaces:
// Set a value
StorageFacet(diamondAddress).setValue(42);
// Get a value
uint256 value = StorageFacet(diamondAddress).getValue();
// Set a string
StorageFacet(diamondAddress).setString("Hello Diamond!");
// Set user balance
StorageFacet(diamondAddress).setUserBalance(userAddress, 100);// Perform calculations
uint256 result = MathFacet(diamondAddress).add(5, 3);
uint256 factorial = MathFacet(diamondAddress).factorial(5);
uint256 fibonacci = MathFacet(diamondAddress).fibonacci(10);// Get all facets
IDiamondLoupe.Facet[] memory facets = IDiamondLoupe(diamondAddress).facets();
// Check supported interfaces
bool supports = IERC165(diamondAddress).supportsInterface(interfaceId);
// Transfer ownership
OwnershipFacet(diamondAddress).transferOwnership(newOwner);The project includes comprehensive tests covering:
- Facet functionality
- Diamond introspection
- Ownership management
- Event emissions
- Edge cases and error conditions
- ERC-165 interface support
Run tests with:
forge test -vv- Only the contract owner can perform diamond cuts
- All facets are thoroughly tested
- Proper access control is implemented
- Safe mathematical operations with overflow/underflow protection
This implementation is fully compliant with the ERC-2535 Diamond standard, including:
- Diamond cut functionality
- Diamond loupe (introspection) functionality
- ERC-165 interface support
- Proper storage management
- Event emission for diamond cuts
MIT