Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git add. #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Diamond-3-Hardhat Implementation

![image](https://github.com/JohnDeveloperJ/diamond-3-hardhat/assets/143453887/2927e7e0-cfaf-45e9-8bff-dd64757bace7)

This is an implementation for [EIP-2535 Diamond Standard](https://github.com/ethereum/EIPs/issues/2535). To learn about other implementations go here: https://github.com/mudgen/diamond

The standard loupe functions have been gas-optimized in this implementation and can be called in on-chain transactions. However keep in mind that a diamond can have any number of functions and facets so it is still possible to get out-of-gas errors when calling loupe functions. Except for the `facetAddress` loupe function which has a fixed gas cost.
Expand Down
52 changes: 52 additions & 0 deletions contracts/facets/ManagerFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../libraries/LibDiamond.sol";
// Import the necessary contracts and libraries

/// @title FacetManager - A contract for managing facets in an EIP-2535 Diamond
/// @author John Johnson
/// @dev This contract provides functions for adding, replacing, and removing facets in an EIP-2535 compliant Diamond proxy.
contract FacetManager {
// Address of the DiamondCutFacet
address internal diamondCutFacetAddress;

/// @dev Initializes the FacetManager with the address of the DiamondCutFacet.
/// @param _diamondCutFacetAddress The address of the DiamondCutFacet in the Diamond proxy.
constructor(address _diamondCutFacetAddress) {
diamondCutFacetAddress = _diamondCutFacetAddress;
}

/// @notice Adds a new facet to the Diamond proxy, associating it with the given function selectors.
/// @param facet The address of the new facet to add.
/// @param functionSelectors An array of function selectors to be associated with the facet.
/// @dev Only the owner is allowed to add facets.
function addFacet(address facet, bytes4[] memory functionSelectors) external {
// Ensure only authorized entities can call this function
require(msg.sender == owner(), "Only the owner can add facets");

IDiamondCut.FacetCutAction action = IDiamondCut.FacetCutAction.Add;

IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](1);
cut[0] = IDiamondCut.FacetCut({
facetAddress: facet,
action: action,
functionSelectors: functionSelectors
});

bytes memory initData = abi.encodeWithSignature("diamondCut((address,uint8,bytes4[])[])", cut);

// Call the diamondCut function of DiamondCutFacet to add the new facet
(bool success, ) = diamondCutFacetAddress.call(initData);
require(success, "Facet addition failed");
}

// Add more functions to replace or remove facets as needed
// ...

/// @notice Returns the owner of the Diamond proxy.
/// @return The address of the Diamond proxy owner.
function owner() public view returns (address) {
return LibDiamond.contractOwner();
}
}
Loading