Skip to content
Merged
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
18 changes: 9 additions & 9 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -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)
DiamondTester:testSupportsERC165() (gas: 15572)
DiamondTester:testSupportsERC173() (gas: 15551)
DiamondTester:testSupportsIDiamondCut() (gas: 15541)
DiamondTester:testSupportsIDiamondLoupe() (gas: 15572)
16 changes: 6 additions & 10 deletions src/Diamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,13 @@ 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 {
_beforeDelegate();
_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 {
Expand Down Expand Up @@ -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);
}
}
16 changes: 9 additions & 7 deletions src/libraries/LibDiamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 <daveproxy80@gmail.com> (https://github.com/dadadave80)
///
/// @dev Defines the diamond storage layout and implements the `_diamondCut` operation and storage accessors
library LibDiamond {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -326,7 +328,7 @@ library LibDiamond {
revert(add(32, err), returndata_size)
}
} else {
revert InitializationFunctionReverted(_init, _calldata);
revert InitializeDiamondCutReverted(_init, _calldata);
}
}
}
Expand All @@ -347,7 +349,7 @@ library LibDiamond {
revert(add(32, err), returndata_size)
}
} else {
revert InitializationFunctionReverted(_init, _calldata);
revert InitializeDiamondCutReverted(_init, _calldata);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/errors/DiamondErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down