-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiamondLoupeFacet.sol
142 lines (137 loc) · 6.33 KB
/
DiamondLoupeFacet.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {LibDiamond} from "../libraries/LibDiamond.sol";
import {iDiamondLoupe} from "../interfaces/iDiamondLoupe.sol";
/**
* @dev EIP-2535 Diamond Loupe Facet and Loupe Functions
*/
contract DiamondLoupeFacet is iDiamondLoupe {
/// @dev gets all facets and their selectors.
/// @return facets_ Facet
function facets() external view override returns (Facet[] memory facets_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
facets_ = new Facet[](ds.selectorCount);
uint16[] memory numFacetSelectors = new uint16[](ds.selectorCount);
uint256 numFacets;
uint256 selectorIndex;
// loop through function selectors
for (uint256 slotIndex; selectorIndex < ds.selectorCount; slotIndex++) {
bytes32 slot = ds.selectorSlots[slotIndex];
for (uint256 selectorSlotIndex; selectorSlotIndex < 8; selectorSlotIndex++) {
selectorIndex++;
if (selectorIndex > ds.selectorCount) {
break;
}
// " << 5 is the same as multiplying by 32 ( * 32)
bytes4 selector = bytes4(slot << (selectorSlotIndex << 5));
address facetAddress_ = address(bytes20(ds.facets[selector]));
bool continueLoop;
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
if (facets_[facetIndex].facetAddress == facetAddress_) {
facets_[facetIndex].functionSelectors[numFacetSelectors[facetIndex]] = selector;
// probably will never have more than 256 functions from one facet contract
require(numFacetSelectors[facetIndex] < 255);
numFacetSelectors[facetIndex]++;
continueLoop = true;
break;
}
}
if (continueLoop) {
continue;
}
facets_[numFacets].facetAddress = facetAddress_;
facets_[numFacets].functionSelectors = new bytes4[](ds.selectorCount);
facets_[numFacets].functionSelectors[0] = selector;
numFacetSelectors[numFacets] = 1;
numFacets++;
}
}
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
uint256 numSelectors = numFacetSelectors[facetIndex];
bytes4[] memory selectors = facets_[facetIndex].functionSelectors;
// setting the number of selectors
assembly {
mstore(selectors, numSelectors)
}
}
// setting the number of facets
assembly {
mstore(facets_, numFacets)
}
}
/// @notice Gets all the function selectors supported by a specific facet.
/// @param _facet The facet address.
/// @return _facetFunctionSelectors The selectors associated with a facet address.
function facetFunctionSelectors(address _facet) external view override returns (bytes4[] memory _facetFunctionSelectors) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 numSelectors;
_facetFunctionSelectors = new bytes4[](ds.selectorCount);
uint256 selectorIndex;
// loop through function selectors
for (uint256 slotIndex; selectorIndex < ds.selectorCount; slotIndex++) {
bytes32 slot = ds.selectorSlots[slotIndex];
for (uint256 selectorSlotIndex; selectorSlotIndex < 8; selectorSlotIndex++) {
selectorIndex++;
if (selectorIndex > ds.selectorCount) {
break;
}
// " << 5 is the same as multiplying by 32 ( * 32)
bytes4 selector = bytes4(slot << (selectorSlotIndex << 5));
address facet = address(bytes20(ds.facets[selector]));
if (_facet == facet) {
_facetFunctionSelectors[numSelectors] = selector;
numSelectors++;
}
}
}
// Set the number of selectors in the array
assembly {
mstore(_facetFunctionSelectors, numSelectors)
}
}
/// @notice Get all the facet addresses used by a diamond.
/// @return facetAddresses_
function facetAddresses() external view override returns (address[] memory facetAddresses_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
facetAddresses_ = new address[](ds.selectorCount);
uint256 numFacets;
uint256 selectorIndex;
// loop through function selectors
for (uint256 slotIndex; selectorIndex < ds.selectorCount; slotIndex++) {
bytes32 slot = ds.selectorSlots[slotIndex];
for (uint256 selectorSlotIndex; selectorSlotIndex < 8; selectorSlotIndex++) {
selectorIndex++;
if (selectorIndex > ds.selectorCount) {
break;
}
// " << 5 is the same as multiplying by 32 ( * 32)
bytes4 selector = bytes4(slot << (selectorSlotIndex << 5));
address facetAddress_ = address(bytes20(ds.facets[selector]));
bool continueLoop;
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
if (facetAddress_ == facetAddresses_[facetIndex]) {
continueLoop = true;
break;
}
}
if (continueLoop) {
continue;
}
facetAddresses_[numFacets] = facetAddress_;
numFacets++;
}
}
// Set the number of facet addresses in the array
assembly {
mstore(facetAddresses_, numFacets)
}
}
/// @notice Gets the facet that supports the given selector.
/// @dev If facet is not found return address(0).
/// @param _functionSelector The function selector.
/// @return facetAddress_ The facet address.
function facetAddress(bytes4 _functionSelector) external view override returns (address facetAddress_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
facetAddress_ = address(bytes20(ds.facets[_functionSelector]));
}
}