Last active
December 19, 2025 15:18
-
-
Save mudgen/92a2e53b9a59817012dba2c72c8d84aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity >=0.8.31; | |
| contract DiamondInspectFacet { | |
| bytes32 constant DIAMOND_STORAGE_POSITION = keccak256("ercXXXX.diamond"); | |
| /** | |
| * @notice Data stored for each function selector. | |
| * @dev Facet address of function selector. | |
| * Position of selector in the 'bytes4[] selectors' array. | |
| */ | |
| struct FacetAndPosition { | |
| address facet; | |
| uint32 position; | |
| } | |
| /** | |
| * @custom:storage-location erc8042:ercXXXX.diamond | |
| */ | |
| struct DiamondStorage { | |
| mapping(bytes4 functionSelector => FacetAndPosition) facetAndPosition; | |
| /** | |
| * Array of all function selectors that can be called in the diamond. | |
| */ | |
| bytes4[] selectors; | |
| } | |
| /** | |
| * @notice Retrieves the diamond's storage struct from its fixed position. | |
| * @dev Uses inline assembly to access the storage slot directly. | |
| * @return s The `DiamondStorage` struct stored at `DIAMOND_STORAGE_POSITION`. | |
| */ | |
| function getStorage() internal pure returns (DiamondStorage storage s) { | |
| bytes32 position = DIAMOND_STORAGE_POSITION; | |
| assembly { | |
| s.slot := position | |
| } | |
| } | |
| struct FunctionFacetPair { | |
| bytes4 selector; | |
| address facet; | |
| } | |
| /** | |
| * @notice Returns an array of all function selectors and their corresponding facet addresses. | |
| * @dev Iterates through the diamond's stored selectors and pairs each with its facet. | |
| * @return pairs An array of `FunctionFacetPair` structs, each containing a selector and its facet address. | |
| */ | |
| function functionFacetPairs() external view returns(FunctionFacetPair[] memory pairs) { | |
| DiamondStorage storage s = getStorage(); | |
| uint256 length = s.selectors.length; | |
| pairs = new FunctionFacetPair[](length); | |
| for(uint i; i < length; i++){ | |
| bytes4 selector = s.selectors[i]; | |
| pairs[i] = FunctionFacetPair(selector, s.facetAndPosition[selector].facet); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment