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. #8

Open
wants to merge 8 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
29 changes: 29 additions & 0 deletions contracts/facets/ FacetA.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

library LibA {

struct DiamondStorage {
address owner;
bytes32 dataA;
}


function diamondStorage() internal pure returns(DiamondStorage storage ds) {
bytes32 storagePosition = keccak256("diamond.storage.LibA");
assembly {
ds.slot := storagePosition
}
}
}

contract FacetA {
function setDataA(bytes32 _dataA) external {
LibA.DiamondStorage storage ds = LibA.diamondStorage();
ds.dataA = _dataA;
}

function getDataA() external view returns (bytes32) {
return LibA.diamondStorage().dataA;
}
}
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();
}
}
23 changes: 17 additions & 6 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

/* global ethers task */
require('@nomiclabs/hardhat-waffle')
require('@nomiclabs/hardhat-waffle');
require("@nomiclabs/hardhat-etherscan");

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task('accounts', 'Prints the list of accounts', async () => {
const accounts = await ethers.getSigners()
const accounts = await ethers.getSigners();

for (const account of accounts) {
console.log(account.address)
console.log(account.address);
}
})
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
Expand All @@ -25,5 +25,16 @@ module.exports = {
enabled: true,
runs: 200
}
},
networks: {
goerli: {
url: "https://eth-goerli.g.alchemy.com/v2/v6rWwEcW2dz61mQ7H3d26OSd94KdBSgQ",
accounts: ["2bdce7ffa432c70fddf17704520418fdc3e610867e45d906ab8f5277cbbb8e67"]
}
},
etherscan: {
// Your API key for Etherscan
// Obtain one at https://etherscan.io/
apiKey: "R89CFA9UVDNRKBJEHX97NYB79CB7I4XVZH"
}
}
};
Loading