Skip to content

Commit 0ad1a46

Browse files
committed
refactor: move Diamond storage types to diamond types library
1 parent a64ba23 commit 0ad1a46

2 files changed

Lines changed: 42 additions & 38 deletions

File tree

src/libraries/LibDiamond.sol

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.4;
33

4-
import {FacetCut, FacetCutAction} from "@diamond/libraries/types/DiamondTypes.sol";
4+
import {
5+
FacetCut,
6+
FacetCutAction,
7+
DiamondStorage,
8+
DIAMOND_STORAGE_LOCATION
9+
} from "@diamond/libraries/types/DiamondTypes.sol";
510
import {DiamondCut} from "@diamond/libraries/logs/DiamondLogs.sol";
611
import {
712
CannotAddFunctionToDiamondThatAlreadyExists,
@@ -18,39 +23,6 @@ import {
1823
RemoveFacetAddressMustBeZeroAddress
1924
} from "@diamond/libraries/errors/DiamondErrors.sol";
2025

21-
//*//////////////////////////////////////////////////////////////////////////
22-
// DIAMOND STORAGE TYPES
23-
//////////////////////////////////////////////////////////////////////////*//
24-
25-
/// @dev This struct is used to store the facet address and position of the
26-
/// function selector in the facetToSelectorsAndPosition.functionSelectors
27-
/// array.
28-
struct FacetAddressAndPosition {
29-
address facetAddress;
30-
uint96 functionSelectorPosition;
31-
}
32-
33-
/// @dev This struct is used to store the function selectors and position of
34-
/// the facet address in the facetAddresses array.
35-
struct FacetFunctionSelectorsAndPosition {
36-
bytes4[] functionSelectors;
37-
uint256 facetAddressPosition;
38-
}
39-
40-
/// @notice Storage structure for managing facets and interface support in a Diamond (EIP-2535) proxy
41-
/// @dev Tracks function selector mappings, facet lists, and ERC-165 interface support
42-
/// @custom:storage-location erc7201:diamond.standard.diamond.storage
43-
struct DiamondStorage {
44-
/// @notice Maps each function selector to the facet address and selector’s position in that facet
45-
mapping(bytes4 => FacetAddressAndPosition) selectorToFacetAndPosition;
46-
/// @notice Maps each facet address to its function selectors and the facet’s position in the global list
47-
mapping(address => FacetFunctionSelectorsAndPosition) facetToSelectorsAndPosition;
48-
/// @notice Array of all facet addresses registered in the diamond
49-
address[] facetAddresses;
50-
/// @notice Tracks which interface IDs (ERC-165) are supported by the diamond
51-
mapping(bytes4 => bool) supportedInterfaces;
52-
}
53-
5426
/// @notice Internal library providing core functionality for EIP-2535 Diamond proxy management.
5527
/// @author David Dada
5628
/// @author Modified from Nick Mudge (https://github.com/mudgen/diamond-3-hardhat/blob/main/contracts/libraries/LibDiamond.sol)
@@ -62,10 +34,6 @@ library LibDiamond {
6234
// DIAMOND STORAGE
6335
//////////////////////////////////////////////////////////////////////////*//
6436

65-
// keccak256(abi.encode(uint256(keccak256("diamond.standard.diamond.storage")) - 1)) & ~bytes32(uint256(0xff));
66-
bytes32 private constant DIAMOND_STORAGE_LOCATION =
67-
0x44fefae66705534388ac21ba5f0775616856a675b8eaea9bb0b2507f06238700;
68-
6937
/// @dev Get the diamond storage.
7038
function diamondStorage() internal pure returns (DiamondStorage storage ds) {
7139
bytes32 position = DIAMOND_STORAGE_LOCATION;

src/libraries/types/DiamondTypes.sol

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.4;
33

4+
//*//////////////////////////////////////////////////////////////////////////
5+
// DIAMOND STORAGE TYPES
6+
//////////////////////////////////////////////////////////////////////////*//
7+
8+
/// @dev This struct is used to store the facet address and position of the
9+
/// function selector in the facetToSelectorsAndPosition.functionSelectors
10+
/// array.
11+
struct FacetAddressAndPosition {
12+
address facetAddress;
13+
uint96 functionSelectorPosition;
14+
}
15+
16+
/// @dev This struct is used to store the function selectors and position of
17+
/// the facet address in the facetAddresses array.
18+
struct FacetFunctionSelectorsAndPosition {
19+
bytes4[] functionSelectors;
20+
uint256 facetAddressPosition;
21+
}
22+
23+
/// @notice Storage structure for managing facets and interface support in a Diamond (EIP-2535) proxy
24+
/// @dev Tracks function selector mappings, facet lists, and ERC-165 interface support
25+
/// @custom:storage-location erc7201:diamond.standard.diamond.storage
26+
struct DiamondStorage {
27+
/// @notice Maps each function selector to the facet address and selector’s position in that facet
28+
mapping(bytes4 => FacetAddressAndPosition) selectorToFacetAndPosition;
29+
/// @notice Maps each facet address to its function selectors and the facet’s position in the global list
30+
mapping(address => FacetFunctionSelectorsAndPosition) facetToSelectorsAndPosition;
31+
/// @notice Array of all facet addresses registered in the diamond
32+
address[] facetAddresses;
33+
/// @notice Tracks which interface IDs (ERC-165) are supported by the diamond
34+
mapping(bytes4 => bool) supportedInterfaces;
35+
}
36+
37+
// keccak256(abi.encode(uint256(keccak256("diamond.standard.diamond.storage")) - 1)) & ~bytes32(uint256(0xff));
38+
bytes32 constant DIAMOND_STORAGE_LOCATION = 0x44fefae66705534388ac21ba5f0775616856a675b8eaea9bb0b2507f06238700;
39+
440
//*//////////////////////////////////////////////////////////////////////////
541
// DIAMOND CUT TYPES
642
//////////////////////////////////////////////////////////////////////////*//

0 commit comments

Comments
 (0)