diff --git a/.gas-snapshot b/.gas-snapshot index 3fce107..6e9bf76 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,11 +1,11 @@ DiamondTester:testDiamondDeployed() (gas: 5278) -DiamondTester:testDiamondOwner() (gas: 15392) -DiamondTester:testFacetAddressToSelectorsMappingIsCorrect() (gas: 139943) -DiamondTester:testSelectorToFacetMappingIsCorrect() (gas: 137976) -DiamondTester:testSelectorsAreComplete() (gas: 144581) -DiamondTester:testSelectorsAreUnique() (gas: 191268) +DiamondTester:testDiamondOwner() (gas: 15446) +DiamondTester:testFacetAddressToSelectorsMappingIsCorrect() (gas: 141767) +DiamondTester:testSelectorToFacetMappingIsCorrect() (gas: 139648) +DiamondTester:testSelectorsAreComplete() (gas: 314268) +DiamondTester:testSelectorsAreUnique() (gas: 191322) DiamondTester:testStandardFacetsDeployed() (gas: 13908) -DiamondTester:testSupportsERC165() (gas: 15496) -DiamondTester:testSupportsERC173() (gas: 15475) -DiamondTester:testSupportsIDiamondCut() (gas: 15465) -DiamondTester:testSupportsIDiamondLoupe() (gas: 15496) \ No newline at end of file +DiamondTester:testSupportsERC165() (gas: 15572) +DiamondTester:testSupportsERC173() (gas: 15551) +DiamondTester:testSupportsIDiamondCut() (gas: 15541) +DiamondTester:testSupportsIDiamondLoupe() (gas: 15572) \ No newline at end of file diff --git a/src/Diamond.sol b/src/Diamond.sol index e2aac97..94a214e 100644 --- a/src/Diamond.sol +++ b/src/Diamond.sol @@ -43,10 +43,6 @@ abstract contract Diamond { _diamondCut(_facetCuts, _init, _calldata); } - /// @notice Receive function to accept plain Ether transfers - /// @dev Allows contract to receive Ether without data - receive() external payable virtual {} - /// @notice Fallback function that delegates calls to the appropriate facet based on function selector /// @dev Reads the facet address from diamond storage and performs a delegatecall; reverts if selector is not found fallback() external payable virtual { @@ -54,12 +50,6 @@ abstract contract Diamond { _delegate(_facet()); } - /// @notice Retrieves the implementation address for the current function call - /// @dev A Facet is one of many implementations in a Diamond Proxy - function _facet() internal virtual returns (address) { - return LibDiamond._selectorToFacet(msg.sig); - } - /// @notice Internal function to perform a delegatecall to an implementation /// @param _implementation Address of the implementation to delegate to function _delegate(address _implementation) internal virtual { @@ -88,4 +78,10 @@ abstract contract Diamond { /// @notice Internal hook function to run before a delegatecall to the facet /// @dev This function can be replaced to perform additional logic before the delegatecall function _beforeDelegate() internal virtual {} + + /// @notice Retrieves the implementation address for the current function call + /// @dev A Facet is one of many implementations in a Diamond Proxy + function _facet() internal view virtual returns (address) { + return LibDiamond._selectorToFacet(msg.sig); + } } diff --git a/src/libraries/LibDiamond.sol b/src/libraries/LibDiamond.sol index 178e338..b32624f 100644 --- a/src/libraries/LibDiamond.sol +++ b/src/libraries/LibDiamond.sol @@ -8,8 +8,8 @@ import "@diamond-errors/DiamondErrors.sol"; /// @title LibDiamond /// @notice Internal library providing core functionality for ERC-2535 Diamond proxy management. -/// @author David Dada -/// @author Modified from Nick Mudge (https://github.com/mudgen/diamond-3-hardhat/blob/main/contracts/libraries/LibDiamond.sol) +/// @author Nick Mudge (https://github.com/mudgen/diamond-3-hardhat/blob/main/contracts/libraries/LibDiamond.sol) +/// @author Modified by David Dada (https://github.com/dadadave80) /// /// @dev Defines the diamond storage layout and implements the `_diamondCut` operation and storage accessors library LibDiamond { @@ -103,7 +103,7 @@ library LibDiamond { { uint256 functionSelectorsLength = _functionSelectors.length; if (_facetAddress != address(0)) revert RemoveFacetAddressMustBeZeroAddress(_facetAddress); - if (functionSelectorsLength == 0) revert NoSelectorsProvidedForFacetForCut(_facetAddress); + if (functionSelectorsLength == 0) revert NoSelectorsProvidedForFacetCut(_facetAddress); for (uint256 i; i < functionSelectorsLength; ++i) { bytes4 selector = _functionSelectors[i]; address oldFacetAddress = _selectorToFacet(_ds, selector); @@ -194,9 +194,11 @@ library LibDiamond { address _facetAddress, bytes4[] calldata _functionSelectors ) internal { + if (_facetAddress != address(0)) { + revert RemoveFacetAddressMustBeZeroAddress(_facetAddress); + } uint256 functionSelectorsLength = _functionSelectors.length; - if (_facetAddress != address(0)) revert RemoveFacetAddressMustBeZeroAddress(_facetAddress); - if (functionSelectorsLength == 0) revert NoSelectorsProvidedForFacetForCut(_facetAddress); + if (functionSelectorsLength == 0) revert NoSelectorsProvidedForFacetCut(_facetAddress); for (uint256 i; i < functionSelectorsLength; ++i) { bytes4 selector = _functionSelectors[i]; address oldFacetAddress = _selectorToFacet(_ds, selector); @@ -326,7 +328,7 @@ library LibDiamond { revert(add(32, err), returndata_size) } } else { - revert InitializationFunctionReverted(_init, _calldata); + revert InitializeDiamondCutReverted(_init, _calldata); } } } @@ -347,7 +349,7 @@ library LibDiamond { revert(add(32, err), returndata_size) } } else { - revert InitializationFunctionReverted(_init, _calldata); + revert InitializeDiamondCutReverted(_init, _calldata); } } } diff --git a/src/libraries/errors/DiamondErrors.sol b/src/libraries/errors/DiamondErrors.sol index 11a0571..873bea3 100644 --- a/src/libraries/errors/DiamondErrors.sol +++ b/src/libraries/errors/DiamondErrors.sol @@ -13,7 +13,7 @@ error NoSelectorsGivenToAdd(); /// @notice Thrown when no function selectors are provided for a given facet in a cut /// @param facetAddress The facet contract address for which selectors were expected -error NoSelectorsProvidedForFacetForCut(address facetAddress); +error NoSelectorsProvidedForFacetCut(address facetAddress); /// @notice Thrown when trying to add selectors under the zero address (invalid facet) /// @param selectors The selectors attempted to be added @@ -58,7 +58,7 @@ error CannotRemoveImmutableFunction(bytes4 selector); /// @notice Thrown when the initialization call following a diamond cut reverts /// @param initAddress The address of the init contract that reverted /// @param data The calldata passed to the init contract -error InitializationFunctionReverted(address initAddress, bytes data); +error InitializeDiamondCutReverted(address initAddress, bytes data); //*////////////////////////////////////////////////////////////////////////// // DIAMOND ERRORS