-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiamondCoreFacet.sol
65 lines (53 loc) · 2.66 KB
/
DiamondCoreFacet.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import { IERC165 } from "../eip/IERC165.sol";
import { IDiamondCut, IDiamondLoupe } from "../eip/IERC2535.sol";
import { DiamondLibrary } from "./DiamondLibrary.sol";
/// Combines IDiamondLoupe, IDiamondCut and IERC165
contract DiamondCoreFacet is IERC165, IDiamondLoupe, IDiamondCut {
using DiamondLibrary for DiamondLibrary.StoredFacet;
function initialize() external {
DiamondLibrary.setSupportsInterface(type(IERC165).interfaceId, true);
DiamondLibrary.setSupportsInterface(type(IDiamondLoupe).interfaceId, true);
DiamondLibrary.setSupportsInterface(type(IDiamondCut).interfaceId, true);
}
function selectors() external pure returns (bytes4[] memory result) {
result = new bytes4[](6);
result[0] = IERC165.supportsInterface.selector;
result[1] = IDiamondLoupe.facets.selector;
result[2] = IDiamondLoupe.facetFunctionSelectors.selector;
result[3] = IDiamondLoupe.facetAddresses.selector;
result[4] = IDiamondLoupe.facetAddress.selector;
result[5] = IDiamondCut.diamondCut.selector;
}
function facets() external view override returns (IDiamondLoupe.Facet[] memory result) {
DiamondLibrary.DiamondStorage storage ds = DiamondLibrary.getStorage();
uint256 length = ds.facetsArray.length;
result = new IDiamondLoupe.Facet[](length);
for (uint256 i = 0; i < length; i++) {
address facetAddr = ds.facetsArray[i];
result[i] = IDiamondLoupe.Facet({
facetAddress: facetAddr,
functionSelectors: ds.facets[facetAddr].selectors()
});
}
}
function facetFunctionSelectors(address _facet) external view override returns (bytes4[] memory) {
DiamondLibrary.DiamondStorage storage ds = DiamondLibrary.getStorage();
DiamondLibrary.StoredFacet storage facet = ds.facets[_facet];
if (facet.facetAddress != _facet) return new bytes4[](0);
return facet.selectors();
}
function facetAddresses() external view override returns (address[] memory) {
return DiamondLibrary.getStorage().facetsArray;
}
function facetAddress(bytes4 _selector) external view override returns (address) {
return DiamondLibrary.facetAddress(_selector);
}
function diamondCut(FacetCut[] calldata _diamondCut, address _init, bytes calldata _calldata) external override {
DiamondLibrary.diamondCut(_diamondCut, _init, _calldata);
}
function supportsInterface(bytes4 _interfaceId) external view override returns (bool) {
return DiamondLibrary.supportsInterface(_interfaceId);
}
}