-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduces LibDiamondLoupe to avoid external calls for loupe functions
- Loading branch information
Showing
2 changed files
with
62 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.17; | ||
|
||
import { LibDiamond } from "../Libraries/LibDiamond.sol"; | ||
import { IDiamondLoupe } from "../Interfaces/IDiamondLoupe.sol"; | ||
|
||
/// Library for DiamondLoupe functions (to avoid using external calls when using DiamondLoupe) | ||
library LibDiamondLoupe { | ||
function facets() | ||
internal | ||
view | ||
returns (IDiamondLoupe.Facet[] memory facets_) | ||
{ | ||
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); | ||
uint256 numFacets = ds.facetAddresses.length; | ||
facets_ = new IDiamondLoupe.Facet[](numFacets); | ||
for (uint256 i = 0; i < numFacets; ) { | ||
address facetAddress_ = ds.facetAddresses[i]; | ||
facets_[i].facetAddress = facetAddress_; | ||
facets_[i].functionSelectors = ds | ||
.facetFunctionSelectors[facetAddress_] | ||
.functionSelectors; | ||
unchecked { | ||
++i; | ||
} | ||
} | ||
} | ||
|
||
function facetFunctionSelectors( | ||
address _facet | ||
) internal view returns (bytes4[] memory facetFunctionSelectors_) { | ||
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); | ||
facetFunctionSelectors_ = ds | ||
.facetFunctionSelectors[_facet] | ||
.functionSelectors; | ||
} | ||
|
||
function facetAddresses() | ||
internal | ||
view | ||
returns (address[] memory facetAddresses_) | ||
{ | ||
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); | ||
facetAddresses_ = ds.facetAddresses; | ||
} | ||
|
||
function facetAddress( | ||
bytes4 _functionSelector | ||
) internal view returns (address facetAddress_) { | ||
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); | ||
facetAddress_ = ds | ||
.selectorToFacetAndPosition[_functionSelector] | ||
.facetAddress; | ||
} | ||
} |