Skip to content

gfournierPro/test-diamond-proxy

Repository files navigation

ERC-2535 Diamond Proxy Smart Contract

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.

Features

  • 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 capabilities
    • OwnershipFacet: Manages contract ownership

Contract Architecture

Diamond (Proxy)
├── DiamondCutFacet (manage upgrades)
├── DiamondLoupeFacet (introspection)
├── OwnershipFacet (ownership management)
├── StorageFacet (storage operations)
└── MathFacet (mathematical operations)

Usage

Build

forge build

Test

forge test

Deploy

forge script script/Deploy.s.sol --rpc-url <your_rpc_url> --private-key <your_private_key> --broadcast

Contract Interaction

After deployment, you can interact with the diamond through any of the facet interfaces:

Storage Operations

// 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);

Mathematical Operations

// Perform calculations
uint256 result = MathFacet(diamondAddress).add(5, 3);
uint256 factorial = MathFacet(diamondAddress).factorial(5);
uint256 fibonacci = MathFacet(diamondAddress).fibonacci(10);

Diamond Management

// 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);

Testing

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

Security Considerations

  • 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

ERC-2535 Compliance

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

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published