Skip to content

Commit 5b85da8

Browse files
committed
refactor: Replace require statements with revert for better error handling
1 parent ca7817e commit 5b85da8

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/Diamond.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ contract Diamond {
2727
bytes4 selector = msg.sig;
2828
// Lookup facet for function selector
2929
address facet = LibDiamond.diamondStorage().selectorToFacetAndPosition[selector].facetAddress;
30-
require(facet != address(0), FunctionDoesNotExist(selector));
30+
if (facet == address(0)) revert FunctionDoesNotExist(selector);
3131

3232
assembly {
3333
// Copy calldata to memory

src/libraries/LibDiamond.sol

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ library LibDiamond {
8585
/// @param _calldata A function call, including function selector and arguments.
8686
function _diamondCut(FacetCut[] memory _facetCuts, address _init, bytes memory _calldata) internal {
8787
uint256 facetCutsLength = _facetCuts.length;
88-
require(facetCutsLength > 0, NoFacetsInDiamondCut());
88+
if (facetCutsLength == 0) revert NoFacetsInDiamondCut();
8989
for (uint256 facetIndex; facetIndex < facetCutsLength; facetIndex++) {
9090
FacetCutAction action = _facetCuts[facetIndex].action;
9191
if (action == FacetCutAction.Add) {
@@ -107,8 +107,8 @@ library LibDiamond {
107107
/// @param _functionSelectors The function selectors to add to the facet.
108108
function _addFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal {
109109
uint256 functionSelectorsLength = _functionSelectors.length;
110-
require(functionSelectorsLength > 0, NoSelectorsGivenToAdd());
111-
require(_facetAddress != address(0), CannotAddSelectorsToZeroAddress(_functionSelectors));
110+
if (functionSelectorsLength == 0) revert NoSelectorsGivenToAdd();
111+
if (_facetAddress == address(0)) revert CannotAddSelectorsToZeroAddress(_functionSelectors);
112112
DiamondStorage storage ds = diamondStorage();
113113
uint96 selectorPosition = uint96(ds.facetToSelectorsAndPosition[_facetAddress].functionSelectors.length);
114114
// Add new facet address if it does not exist
@@ -118,7 +118,7 @@ library LibDiamond {
118118
for (uint256 selectorIndex; selectorIndex < functionSelectorsLength;) {
119119
bytes4 selector = _functionSelectors[selectorIndex];
120120
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
121-
require(oldFacetAddress == address(0), CannotAddFunctionToDiamondThatAlreadyExists(selector));
121+
if (oldFacetAddress != address(0)) revert CannotAddFunctionToDiamondThatAlreadyExists(selector);
122122
_addFunction(ds, selector, selectorPosition, _facetAddress);
123123
selectorPosition++;
124124
unchecked {
@@ -132,8 +132,8 @@ library LibDiamond {
132132
/// @param _functionSelectors The function selectors to replace in the facet.
133133
function _replaceFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal {
134134
uint256 functionSelectorsLength = _functionSelectors.length;
135-
require(functionSelectorsLength > 0, NoSelectorsGivenToAdd());
136-
require(_facetAddress != address(0), CannotAddSelectorsToZeroAddress(_functionSelectors));
135+
if (functionSelectorsLength == 0) revert NoSelectorsGivenToAdd();
136+
if (_facetAddress == address(0)) revert CannotAddSelectorsToZeroAddress(_functionSelectors);
137137
DiamondStorage storage ds = diamondStorage();
138138
uint96 selectorPosition = uint96(ds.facetToSelectorsAndPosition[_facetAddress].functionSelectors.length);
139139
// add new facet address if it does not exist
@@ -143,9 +143,9 @@ library LibDiamond {
143143
for (uint256 selectorIndex; selectorIndex < functionSelectorsLength;) {
144144
bytes4 selector = _functionSelectors[selectorIndex];
145145
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
146-
require(
147-
oldFacetAddress != _facetAddress, CannotReplaceFunctionWithTheSameFunctionFromTheSameFacet(selector)
148-
);
146+
if (oldFacetAddress == _facetAddress) {
147+
revert CannotReplaceFunctionWithTheSameFunctionFromTheSameFacet(selector);
148+
}
149149
_removeFunction(ds, oldFacetAddress, selector);
150150
_addFunction(ds, selector, selectorPosition, _facetAddress);
151151
selectorPosition++;
@@ -160,8 +160,8 @@ library LibDiamond {
160160
/// @param _functionSelectors The function selectors to remove from the facet.
161161
function _removeFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal {
162162
uint256 functionSelectorsLength = _functionSelectors.length;
163-
require(_facetAddress == address(0), RemoveFacetAddressMustBeZeroAddress(_facetAddress));
164-
require(functionSelectorsLength > 0, NoSelectorsProvidedForFacetForCut(_facetAddress));
163+
if (_facetAddress != address(0)) revert RemoveFacetAddressMustBeZeroAddress(_facetAddress);
164+
if (functionSelectorsLength == 0) revert NoSelectorsProvidedForFacetForCut(_facetAddress);
165165
DiamondStorage storage ds = diamondStorage();
166166
for (uint256 selectorIndex; selectorIndex < functionSelectorsLength;) {
167167
bytes4 selector = _functionSelectors[selectorIndex];
@@ -200,9 +200,9 @@ library LibDiamond {
200200
/// @param _facetAddress The address of the facet to remove the function from.
201201
/// @param _selector The function selector to remove.
202202
function _removeFunction(DiamondStorage storage ds, address _facetAddress, bytes4 _selector) internal {
203-
require(_facetAddress != address(0), CannotRemoveFunctionThatDoesNotExist(_selector));
203+
if (_facetAddress == address(0)) revert CannotRemoveFunctionThatDoesNotExist(_selector);
204204
// an immutable function is a function defined directly in a diamond
205-
require(_facetAddress != address(this), CannotRemoveImmutableFunction(_selector));
205+
if (_facetAddress == address(this)) revert CannotRemoveImmutableFunction(_selector);
206206
// replace selector with last selector, then delete last selector
207207
uint256 selectorPosition = ds.selectorToFacetAndPosition[_selector].functionSelectorPosition;
208208
uint256 lastSelectorPosition = ds.facetToSelectorsAndPosition[_facetAddress].functionSelectors.length - 1;

0 commit comments

Comments
 (0)