From 6f7cf23b7b3c604fdf7b6a0809e85d3cb7ed583c Mon Sep 17 00:00:00 2001 From: khayss Date: Fri, 18 Oct 2024 15:51:49 +0100 Subject: [PATCH] chore(fmt): format with forge fmt --- src/Diamond.sol | 23 ++- src/facets/DiamondCutFacet.sol | 21 ++- src/facets/DiamondLoupeFacet.sol | 31 ++-- src/facets/ERC721Facet.sol | 132 +++-------------- src/facets/OwnershipFacet.sol | 7 +- src/facets/PresaleFacet.sol | 20 +-- src/interfaces/IDiamondCut.sol | 23 +-- src/interfaces/IDiamondLoupe.sol | 10 +- src/interfaces/IERC721.sol | 48 ++---- src/interfaces/IMerkle.sol | 4 +- src/libraries/LibDiamond.sol | 187 ++++++------------------ src/upgradeInitializers/DiamondInit.sol | 38 +++-- test/DeployDiamond.t.sol | 15 +- test/utils/DiamondUtils.sol | 6 +- 14 files changed, 170 insertions(+), 395 deletions(-) diff --git a/src/Diamond.sol b/src/Diamond.sol index 071b4dc..c356749 100644 --- a/src/Diamond.sol +++ b/src/Diamond.sol @@ -1,13 +1,14 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -/******************************************************************************\ -* Author: Nick Mudge (https://twitter.com/mudgen) -* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 -* -* Implementation of a diamond. -/******************************************************************************/ - +/** + * \ + * Author: Nick Mudge (https://twitter.com/mudgen) + * EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 + * + * Implementation of a diamond. + * /***************************************************************************** + */ import {LibDiamond} from "./libraries/LibDiamond.sol"; import {IDiamondCut} from "./interfaces/IDiamondCut.sol"; @@ -67,12 +68,8 @@ contract Diamond { returndatacopy(0, 0, returndatasize()) // return any return value or error back to the caller switch result - case 0 { - revert(0, returndatasize()) - } - default { - return(0, returndatasize()) - } + case 0 { revert(0, returndatasize()) } + default { return(0, returndatasize()) } } } diff --git a/src/facets/DiamondCutFacet.sol b/src/facets/DiamondCutFacet.sol index bc0a69a..f746e57 100644 --- a/src/facets/DiamondCutFacet.sol +++ b/src/facets/DiamondCutFacet.sol @@ -1,13 +1,14 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -/******************************************************************************\ -* Author: Nick Mudge (https://twitter.com/mudgen) -* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 -/******************************************************************************/ - -import { IDiamondCut } from "../interfaces/IDiamondCut.sol"; -import { LibDiamond } from "../libraries/LibDiamond.sol"; +/** + * \ + * Author: Nick Mudge (https://twitter.com/mudgen) + * EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 + * /***************************************************************************** + */ +import {IDiamondCut} from "../interfaces/IDiamondCut.sol"; +import {LibDiamond} from "../libraries/LibDiamond.sol"; contract DiamondCutFacet is IDiamondCut { /// @notice Add/replace/remove any number of functions and optionally execute @@ -16,11 +17,7 @@ contract DiamondCutFacet is IDiamondCut { /// @param _init The address of the contract or facet to execute _calldata /// @param _calldata A function call, including function selector and arguments /// _calldata is executed with delegatecall on _init - function diamondCut( - FacetCut[] calldata _diamondCut, - address _init, - bytes calldata _calldata - ) external override { + function diamondCut(FacetCut[] calldata _diamondCut, address _init, bytes calldata _calldata) external override { LibDiamond.enforceIsContractOwner(); LibDiamond.diamondCut(_diamondCut, _init, _calldata); } diff --git a/src/facets/DiamondLoupeFacet.sol b/src/facets/DiamondLoupeFacet.sol index 7bd9ce5..9b42804 100644 --- a/src/facets/DiamondLoupeFacet.sol +++ b/src/facets/DiamondLoupeFacet.sol @@ -1,13 +1,15 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -/******************************************************************************\ -* Author: Nick Mudge (https://twitter.com/mudgen) -* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 -/******************************************************************************/ +/** + * \ + * Author: Nick Mudge (https://twitter.com/mudgen) + * EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 + * /***************************************************************************** + */ -import { LibDiamond } from "../libraries/LibDiamond.sol"; -import { IDiamondLoupe } from "../interfaces/IDiamondLoupe.sol"; -import { IERC165 } from "../interfaces/IERC165.sol"; +import {LibDiamond} from "../libraries/LibDiamond.sol"; +import {IDiamondLoupe} from "../interfaces/IDiamondLoupe.sol"; +import {IERC165} from "../interfaces/IERC165.sol"; contract DiamondLoupeFacet is IDiamondLoupe, IERC165 { // Diamond Loupe Functions @@ -21,7 +23,7 @@ contract DiamondLoupeFacet is IDiamondLoupe, IERC165 { /// @notice Gets all facets and their selectors. /// @return facets_ Facet - function facets() external override view returns (Facet[] memory facets_) { + function facets() external view override returns (Facet[] memory facets_) { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); uint256 numFacets = ds.facetAddresses.length; facets_ = new Facet[](numFacets); @@ -35,14 +37,19 @@ contract DiamondLoupeFacet is IDiamondLoupe, IERC165 { /// @notice Gets all the function selectors provided by a facet. /// @param _facet The facet address. /// @return facetFunctionSelectors_ - function facetFunctionSelectors(address _facet) external override view returns (bytes4[] memory facetFunctionSelectors_) { + function facetFunctionSelectors(address _facet) + external + view + override + returns (bytes4[] memory facetFunctionSelectors_) + { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); facetFunctionSelectors_ = ds.facetFunctionSelectors[_facet].functionSelectors; } /// @notice Get all the facet addresses used by a diamond. /// @return facetAddresses_ - function facetAddresses() external override view returns (address[] memory facetAddresses_) { + function facetAddresses() external view override returns (address[] memory facetAddresses_) { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); facetAddresses_ = ds.facetAddresses; } @@ -51,13 +58,13 @@ contract DiamondLoupeFacet is IDiamondLoupe, IERC165 { /// @dev If facet is not found return address(0). /// @param _functionSelector The function selector. /// @return facetAddress_ The facet address. - function facetAddress(bytes4 _functionSelector) external override view returns (address facetAddress_) { + function facetAddress(bytes4 _functionSelector) external view override returns (address facetAddress_) { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); facetAddress_ = ds.selectorToFacetAndPosition[_functionSelector].facetAddress; } // This implements ERC-165. - function supportsInterface(bytes4 _interfaceId) external override view returns (bool) { + function supportsInterface(bytes4 _interfaceId) external view override returns (bool) { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); return ds.supportedInterfaces[_interfaceId]; } diff --git a/src/facets/ERC721Facet.sol b/src/facets/ERC721Facet.sol index df4ee73..d0acd88 100644 --- a/src/facets/ERC721Facet.sol +++ b/src/facets/ERC721Facet.sol @@ -13,14 +13,10 @@ contract ERC721Facet is IERC721 { /** * @dev See {IERC165-supportsInterface}. */ - function supportsInterface( - bytes4 interfaceId - ) public view virtual override returns (bool) { + function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); - return - interfaceId == type(IERC721).interfaceId || - interfaceId == type(IERC721Metadata).interfaceId || - ds.supportedInterfaces[interfaceId]; + return interfaceId == type(IERC721).interfaceId || interfaceId == type(IERC721Metadata).interfaceId + || ds.supportedInterfaces[interfaceId]; } /** @@ -64,16 +60,11 @@ contract ERC721Facet is IERC721 { /** * @dev See {IERC721Metadata-tokenURI}. */ - function tokenURI( - uint256 tokenId - ) public view virtual returns (string memory) { + function tokenURI(uint256 tokenId) public view virtual returns (string memory) { _requireOwned(tokenId); string memory baseURI = _baseURI(); - return - bytes(baseURI).length > 0 - ? string.concat(baseURI, tokenId.toString()) - : ""; + return bytes(baseURI).length > 0 ? string.concat(baseURI, tokenId.toString()) : ""; } /** @@ -95,9 +86,7 @@ contract ERC721Facet is IERC721 { /** * @dev See {IERC721-getApproved}. */ - function getApproved( - uint256 tokenId - ) public view virtual returns (address) { + function getApproved(uint256 tokenId) public view virtual returns (address) { _requireOwned(tokenId); return _getApproved(tokenId); @@ -113,10 +102,7 @@ contract ERC721Facet is IERC721 { /** * @dev See {IERC721-isApprovedForAll}. */ - function isApprovedForAll( - address owner, - address operator - ) public view virtual returns (bool) { + function isApprovedForAll(address owner, address operator) public view virtual returns (bool) { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); return ds._operatorApprovals[owner][operator]; } @@ -124,11 +110,7 @@ contract ERC721Facet is IERC721 { /** * @dev See {IERC721-transferFrom}. */ - function transferFrom( - address from, - address to, - uint256 tokenId - ) public virtual { + function transferFrom(address from, address to, uint256 tokenId) public virtual { if (to == address(0)) { revert ERC721InvalidReceiver(address(0)); } @@ -143,31 +125,16 @@ contract ERC721Facet is IERC721 { /** * @dev See {IERC721-safeTransferFrom}. */ - function safeTransferFrom( - address from, - address to, - uint256 tokenId - ) public { + function safeTransferFrom(address from, address to, uint256 tokenId) public { safeTransferFrom(from, to, tokenId, ""); } /** * @dev See {IERC721-safeTransferFrom}. */ - function safeTransferFrom( - address from, - address to, - uint256 tokenId, - bytes memory data - ) public virtual { + function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public virtual { transferFrom(from, to, tokenId); - ERC721Utils.checkOnERC721Received( - _msgSender(), - from, - to, - tokenId, - data - ); + ERC721Utils.checkOnERC721Received(_msgSender(), from, to, tokenId, data); } /** @@ -187,9 +154,7 @@ contract ERC721Facet is IERC721 { /** * @dev Returns the approved address for `tokenId`. Returns 0 if `tokenId` is not minted. */ - function _getApproved( - uint256 tokenId - ) internal view virtual returns (address) { + function _getApproved(uint256 tokenId) internal view virtual returns (address) { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); return ds._tokenApprovals[tokenId]; @@ -202,16 +167,9 @@ contract ERC721Facet is IERC721 { * WARNING: This function assumes that `owner` is the actual owner of `tokenId` and does not verify this * assumption. */ - function _isAuthorized( - address owner, - address spender, - uint256 tokenId - ) internal view virtual returns (bool) { - return - spender != address(0) && - (owner == spender || - isApprovedForAll(owner, spender) || - _getApproved(tokenId) == spender); + function _isAuthorized(address owner, address spender, uint256 tokenId) internal view virtual returns (bool) { + return spender != address(0) + && (owner == spender || isApprovedForAll(owner, spender) || _getApproved(tokenId) == spender); } /** @@ -223,11 +181,7 @@ contract ERC721Facet is IERC721 { * WARNING: This function assumes that `owner` is the actual owner of `tokenId` and does not verify this * assumption. */ - function _checkAuthorized( - address owner, - address spender, - uint256 tokenId - ) internal view virtual { + function _checkAuthorized(address owner, address spender, uint256 tokenId) internal view virtual { if (!_isAuthorized(owner, spender, tokenId)) { if (owner == address(0)) { revert ERC721NonexistentToken(tokenId); @@ -265,11 +219,7 @@ contract ERC721Facet is IERC721 { * * NOTE: If overriding this function in a way that tracks balances, see also {_increaseBalance}. */ - function _update( - address to, - uint256 tokenId, - address auth - ) internal virtual returns (address) { + function _update(address to, uint256 tokenId, address auth) internal virtual returns (address) { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); address from = _ownerOf(tokenId); @@ -342,19 +292,9 @@ contract ERC721Facet is IERC721 { * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is * forwarded in {IERC721Receiver-onERC721Received} to contract recipients. */ - function _safeMint( - address to, - uint256 tokenId, - bytes memory data - ) internal virtual { + function _safeMint(address to, uint256 tokenId, bytes memory data) internal virtual { _mint(to, tokenId); - ERC721Utils.checkOnERC721Received( - _msgSender(), - address(0), - to, - tokenId, - data - ); + ERC721Utils.checkOnERC721Received(_msgSender(), address(0), to, tokenId, data); } /** @@ -425,20 +365,9 @@ contract ERC721Facet is IERC721 { * @dev Same as {xref-ERC721-_safeTransfer-address-address-uint256-}[`_safeTransfer`], with an additional `data` parameter which is * forwarded in {IERC721Receiver-onERC721Received} to contract recipients. */ - function _safeTransfer( - address from, - address to, - uint256 tokenId, - bytes memory data - ) internal virtual { + function _safeTransfer(address from, address to, uint256 tokenId, bytes memory data) internal virtual { _transfer(from, to, tokenId); - ERC721Utils.checkOnERC721Received( - _msgSender(), - from, - to, - tokenId, - data - ); + ERC721Utils.checkOnERC721Received(_msgSender(), from, to, tokenId, data); } /** @@ -459,23 +388,14 @@ contract ERC721Facet is IERC721 { * @dev Variant of `_approve` with an optional flag to enable or disable the {Approval} event. The event is not * emitted in the context of transfers. */ - function _approve( - address to, - uint256 tokenId, - address auth, - bool emitEvent - ) internal virtual { + function _approve(address to, uint256 tokenId, address auth, bool emitEvent) internal virtual { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); // Avoid reading the owner unless necessary if (emitEvent || auth != address(0)) { address owner = _requireOwned(tokenId); // We do not use _isAuthorized because single-token approvals should not be able to call approve - if ( - auth != address(0) && - owner != auth && - !isApprovedForAll(owner, auth) - ) { + if (auth != address(0) && owner != auth && !isApprovedForAll(owner, auth)) { revert ERC721InvalidApprover(auth); } @@ -495,11 +415,7 @@ contract ERC721Facet is IERC721 { * * Emits an {ApprovalForAll} event. */ - function _setApprovalForAll( - address owner, - address operator, - bool approved - ) internal virtual { + function _setApprovalForAll(address owner, address operator, bool approved) internal virtual { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); if (operator == address(0)) { diff --git a/src/facets/OwnershipFacet.sol b/src/facets/OwnershipFacet.sol index 7018dda..4427236 100644 --- a/src/facets/OwnershipFacet.sol +++ b/src/facets/OwnershipFacet.sol @@ -1,8 +1,8 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -import { LibDiamond } from "../libraries/LibDiamond.sol"; -import { IERC173 } from "../interfaces/IERC173.sol"; +import {LibDiamond} from "../libraries/LibDiamond.sol"; +import {IERC173} from "../interfaces/IERC173.sol"; contract OwnershipFacet is IERC173 { function transferOwnership(address _newOwner) external override { @@ -10,8 +10,7 @@ contract OwnershipFacet is IERC173 { LibDiamond.setContractOwner(_newOwner); } - function owner() external override view returns (address owner_) { + function owner() external view override returns (address owner_) { owner_ = LibDiamond.contractOwner(); } - } diff --git a/src/facets/PresaleFacet.sol b/src/facets/PresaleFacet.sol index 1246ee7..5980d14 100644 --- a/src/facets/PresaleFacet.sol +++ b/src/facets/PresaleFacet.sol @@ -6,17 +6,14 @@ import {IERC721} from "../interfaces/IERC721.sol"; import {LibDiamond} from "../libraries/LibDiamond.sol"; contract PresaleFacet is IPresale { - function enforceIsActive( - LibDiamond.DiamondStorage storage ds - ) internal view { + function enforceIsActive(LibDiamond.DiamondStorage storage ds) internal view { if (!ds.presaleActive) revert PresaleNotActive(); } - function enforcehasNotExceededMaxPurchase( - LibDiamond.DiamondStorage storage ds - ) internal view { - if (ds.purchases[msgSender()] >= ds.maxPurchase) + function enforcehasNotExceededMaxPurchase(LibDiamond.DiamondStorage storage ds) internal view { + if (ds.purchases[msgSender()] >= ds.maxPurchase) { revert MaxPurchaseExceeded(); + } } function msgSender() internal view returns (address) { @@ -34,15 +31,12 @@ contract PresaleFacet is IPresale { enforcehasNotExceededMaxPurchase(ds); if (msgValue() != ds.presalePrice) revert IncorrectEtherAmount(); - if (IERC721(ds.nftContract).ownerOf(tokenId) != address(this)) + if (IERC721(ds.nftContract).ownerOf(tokenId) != address(this)) { revert NFTNotAvailable(); + } ds.purchases[msg.sender]++; - IERC721(ds.nftContract).transferFrom( - address(this), - msg.sender, - tokenId - ); + IERC721(ds.nftContract).transferFrom(address(this), msg.sender, tokenId); emit NFTPurchased(msg.sender, tokenId); } diff --git a/src/interfaces/IDiamondCut.sol b/src/interfaces/IDiamondCut.sol index 2972f69..4f4272c 100644 --- a/src/interfaces/IDiamondCut.sol +++ b/src/interfaces/IDiamondCut.sol @@ -1,13 +1,18 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -/******************************************************************************\ -* Author: Nick Mudge (https://twitter.com/mudgen) -* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 -/******************************************************************************/ - +/** + * \ + * Author: Nick Mudge (https://twitter.com/mudgen) + * EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 + * /***************************************************************************** + */ interface IDiamondCut { - enum FacetCutAction {Add, Replace, Remove} + enum FacetCutAction { + Add, + Replace, + Remove + } // Add=0, Replace=1, Remove=2 struct FacetCut { @@ -22,11 +27,7 @@ interface IDiamondCut { /// @param _init The address of the contract or facet to execute _calldata /// @param _calldata A function call, including function selector and arguments /// _calldata is executed with delegatecall on _init - function diamondCut( - FacetCut[] calldata _diamondCut, - address _init, - bytes calldata _calldata - ) external; + function diamondCut(FacetCut[] calldata _diamondCut, address _init, bytes calldata _calldata) external; event DiamondCut(FacetCut[] _diamondCut, address _init, bytes _calldata); } diff --git a/src/interfaces/IDiamondLoupe.sol b/src/interfaces/IDiamondLoupe.sol index c3b2570..baca8ed 100644 --- a/src/interfaces/IDiamondLoupe.sol +++ b/src/interfaces/IDiamondLoupe.sol @@ -1,10 +1,12 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -/******************************************************************************\ -* Author: Nick Mudge (https://twitter.com/mudgen) -* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 -/******************************************************************************/ +/** + * \ + * Author: Nick Mudge (https://twitter.com/mudgen) + * EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 + * /***************************************************************************** + */ // A loupe is a small magnifying glass used to look at diamonds. // These functions look at diamonds diff --git a/src/interfaces/IERC721.sol b/src/interfaces/IERC721.sol index b21f6d2..3326491 100644 --- a/src/interfaces/IERC721.sol +++ b/src/interfaces/IERC721.sol @@ -12,40 +12,24 @@ interface IERC721 is IERC165 { error ERC721InsufficientApproval(address spender, uint256 tokenId); error ERC721InvalidReceiver(address receiver); error ERC721InvalidSender(address sender); - error ERC721IncorrectOwner( - address from, - uint256 tokenId, - address previousOwner - ); + error ERC721IncorrectOwner(address from, uint256 tokenId, address previousOwner); error ERC721InvalidApprover(address auth); error ERC721InvalidOperator(address operator); - + /** * @dev Emitted when `tokenId` token is transferred from `from` to `to`. */ - event Transfer( - address indexed from, - address indexed to, - uint256 indexed tokenId - ); + event Transfer(address indexed from, address indexed to, uint256 indexed tokenId); /** * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token. */ - event Approval( - address indexed owner, - address indexed approved, - uint256 indexed tokenId - ); + event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId); /** * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets. */ - event ApprovalForAll( - address indexed owner, - address indexed operator, - bool approved - ); + event ApprovalForAll(address indexed owner, address indexed operator, bool approved); /** * @dev Returns the number of tokens in ``owner``'s account. @@ -75,12 +59,7 @@ interface IERC721 is IERC165 { * * Emits a {Transfer} event. */ - function safeTransferFrom( - address from, - address to, - uint256 tokenId, - bytes calldata data - ) external; + function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external; /** * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients @@ -98,11 +77,7 @@ interface IERC721 is IERC165 { * * Emits a {Transfer} event. */ - function safeTransferFrom( - address from, - address to, - uint256 tokenId - ) external; + function safeTransferFrom(address from, address to, uint256 tokenId) external; /** * @dev Transfers `tokenId` token from `from` to `to`. @@ -156,17 +131,12 @@ interface IERC721 is IERC165 { * * - `tokenId` must exist. */ - function getApproved( - uint256 tokenId - ) external view returns (address operator); + function getApproved(uint256 tokenId) external view returns (address operator); /** * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`. * * See {setApprovalForAll} */ - function isApprovedForAll( - address owner, - address operator - ) external view returns (bool); + function isApprovedForAll(address owner, address operator) external view returns (bool); } diff --git a/src/interfaces/IMerkle.sol b/src/interfaces/IMerkle.sol index 76bfacd..607e0f7 100644 --- a/src/interfaces/IMerkle.sol +++ b/src/interfaces/IMerkle.sol @@ -1,6 +1,4 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -interface IMerkle { - -} \ No newline at end of file +interface IMerkle {} diff --git a/src/libraries/LibDiamond.sol b/src/libraries/LibDiamond.sol index bebbd24..6409a09 100644 --- a/src/libraries/LibDiamond.sol +++ b/src/libraries/LibDiamond.sol @@ -1,10 +1,12 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -/******************************************************************************\ -* Author: Nick Mudge (https://twitter.com/mudgen) -* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 -/******************************************************************************/ +/** + * \ + * Author: Nick Mudge (https://twitter.com/mudgen) + * EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 + * /***************************************************************************** + */ import {IDiamondCut} from "../interfaces/IDiamondCut.sol"; library LibDiamond { @@ -21,8 +23,8 @@ library LibDiamond { error NonEmptyCalldata(); error EmptyCalldata(); error InitCallFailed(); - bytes32 constant DIAMOND_STORAGE_POSITION = - keccak256("diamond.standard.diamond.storage"); + + bytes32 constant DIAMOND_STORAGE_POSITION = keccak256("diamond.standard.diamond.storage"); struct FacetAddressAndPosition { address facetAddress; @@ -68,21 +70,14 @@ library LibDiamond { mapping(address => uint256) purchases; } - function diamondStorage() - internal - pure - returns (DiamondStorage storage ds) - { + function diamondStorage() internal pure returns (DiamondStorage storage ds) { bytes32 position = DIAMOND_STORAGE_POSITION; assembly { ds.slot := position } } - event OwnershipTransferred( - address indexed previousOwner, - address indexed newOwner - ); + event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); function setContractOwner(address _newOwner) internal { DiamondStorage storage ds = diamondStorage(); @@ -96,43 +91,23 @@ library LibDiamond { } function enforceIsContractOwner() internal view { - if (msg.sender != diamondStorage().contractOwner) + if (msg.sender != diamondStorage().contractOwner) { revert NotDiamondOwner(); + } } - event DiamondCut( - IDiamondCut.FacetCut[] _diamondCut, - address _init, - bytes _calldata - ); + event DiamondCut(IDiamondCut.FacetCut[] _diamondCut, address _init, bytes _calldata); // Internal function version of diamondCut - function diamondCut( - IDiamondCut.FacetCut[] memory _diamondCut, - address _init, - bytes memory _calldata - ) internal { - for ( - uint256 facetIndex; - facetIndex < _diamondCut.length; - facetIndex++ - ) { + function diamondCut(IDiamondCut.FacetCut[] memory _diamondCut, address _init, bytes memory _calldata) internal { + for (uint256 facetIndex; facetIndex < _diamondCut.length; facetIndex++) { IDiamondCut.FacetCutAction action = _diamondCut[facetIndex].action; if (action == IDiamondCut.FacetCutAction.Add) { - addFunctions( - _diamondCut[facetIndex].facetAddress, - _diamondCut[facetIndex].functionSelectors - ); + addFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors); } else if (action == IDiamondCut.FacetCutAction.Replace) { - replaceFunctions( - _diamondCut[facetIndex].facetAddress, - _diamondCut[facetIndex].functionSelectors - ); + replaceFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors); } else if (action == IDiamondCut.FacetCutAction.Remove) { - removeFunctions( - _diamondCut[facetIndex].facetAddress, - _diamondCut[facetIndex].functionSelectors - ); + removeFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors); } else { revert InValidFacetCutAction(); } @@ -141,140 +116,83 @@ library LibDiamond { initializeDiamondCut(_init, _calldata); } - function addFunctions( - address _facetAddress, - bytes4[] memory _functionSelectors - ) internal { + function addFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal { if (_functionSelectors.length <= 0) revert NoSelectorsInFacet(); DiamondStorage storage ds = diamondStorage(); if (_facetAddress == address(0)) revert NoZeroAddress(); - uint96 selectorPosition = uint96( - ds.facetFunctionSelectors[_facetAddress].functionSelectors.length - ); + uint96 selectorPosition = uint96(ds.facetFunctionSelectors[_facetAddress].functionSelectors.length); // add new facet address if it does not exist if (selectorPosition == 0) { addFacet(ds, _facetAddress); } - for ( - uint256 selectorIndex; - selectorIndex < _functionSelectors.length; - selectorIndex++ - ) { + for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) { bytes4 selector = _functionSelectors[selectorIndex]; - address oldFacetAddress = ds - .selectorToFacetAndPosition[selector] - .facetAddress; + address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress; if (oldFacetAddress != address(0)) revert SelectorExists(selector); addFunction(ds, selector, selectorPosition, _facetAddress); selectorPosition++; } } - function replaceFunctions( - address _facetAddress, - bytes4[] memory _functionSelectors - ) internal { + function replaceFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal { if (_functionSelectors.length <= 0) revert NoSelectorsInFacet(); DiamondStorage storage ds = diamondStorage(); if (_facetAddress == address(0)) revert NoZeroAddress(); - uint96 selectorPosition = uint96( - ds.facetFunctionSelectors[_facetAddress].functionSelectors.length - ); + uint96 selectorPosition = uint96(ds.facetFunctionSelectors[_facetAddress].functionSelectors.length); // add new facet address if it does not exist if (selectorPosition == 0) { addFacet(ds, _facetAddress); } - for ( - uint256 selectorIndex; - selectorIndex < _functionSelectors.length; - selectorIndex++ - ) { + for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) { bytes4 selector = _functionSelectors[selectorIndex]; - address oldFacetAddress = ds - .selectorToFacetAndPosition[selector] - .facetAddress; - if (oldFacetAddress == _facetAddress) + address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress; + if (oldFacetAddress == _facetAddress) { revert SameSelectorReplacement(selector); + } removeFunction(ds, oldFacetAddress, selector); addFunction(ds, selector, selectorPosition, _facetAddress); selectorPosition++; } } - function removeFunctions( - address _facetAddress, - bytes4[] memory _functionSelectors - ) internal { + function removeFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal { if (_functionSelectors.length <= 0) revert NoSelectorsInFacet(); DiamondStorage storage ds = diamondStorage(); // if function does not exist then do nothing and return if (_facetAddress != address(0)) revert MustBeZeroAddress(); - for ( - uint256 selectorIndex; - selectorIndex < _functionSelectors.length; - selectorIndex++ - ) { + for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) { bytes4 selector = _functionSelectors[selectorIndex]; - address oldFacetAddress = ds - .selectorToFacetAndPosition[selector] - .facetAddress; + address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress; removeFunction(ds, oldFacetAddress, selector); } } - function addFacet( - DiamondStorage storage ds, - address _facetAddress - ) internal { + function addFacet(DiamondStorage storage ds, address _facetAddress) internal { enforceHasContractCode(_facetAddress); - ds.facetFunctionSelectors[_facetAddress].facetAddressPosition = ds - .facetAddresses - .length; + ds.facetFunctionSelectors[_facetAddress].facetAddressPosition = ds.facetAddresses.length; ds.facetAddresses.push(_facetAddress); } - function addFunction( - DiamondStorage storage ds, - bytes4 _selector, - uint96 _selectorPosition, - address _facetAddress - ) internal { - ds - .selectorToFacetAndPosition[_selector] - .functionSelectorPosition = _selectorPosition; - ds.facetFunctionSelectors[_facetAddress].functionSelectors.push( - _selector - ); + function addFunction(DiamondStorage storage ds, bytes4 _selector, uint96 _selectorPosition, address _facetAddress) + internal + { + ds.selectorToFacetAndPosition[_selector].functionSelectorPosition = _selectorPosition; + ds.facetFunctionSelectors[_facetAddress].functionSelectors.push(_selector); ds.selectorToFacetAndPosition[_selector].facetAddress = _facetAddress; } - function removeFunction( - DiamondStorage storage ds, - address _facetAddress, - bytes4 _selector - ) internal { + function removeFunction(DiamondStorage storage ds, address _facetAddress, bytes4 _selector) internal { if (_facetAddress == address(0)) revert NonExistentSelector(_selector); // an immutable function is a function defined directly in a diamond if (_facetAddress == address(this)) revert ImmutableFunction(_selector); // replace selector with last selector, then delete last selector - uint256 selectorPosition = ds - .selectorToFacetAndPosition[_selector] - .functionSelectorPosition; - uint256 lastSelectorPosition = ds - .facetFunctionSelectors[_facetAddress] - .functionSelectors - .length - 1; + uint256 selectorPosition = ds.selectorToFacetAndPosition[_selector].functionSelectorPosition; + uint256 lastSelectorPosition = ds.facetFunctionSelectors[_facetAddress].functionSelectors.length - 1; // if not the same then replace _selector with lastSelector if (selectorPosition != lastSelectorPosition) { - bytes4 lastSelector = ds - .facetFunctionSelectors[_facetAddress] - .functionSelectors[lastSelectorPosition]; - ds.facetFunctionSelectors[_facetAddress].functionSelectors[ - selectorPosition - ] = lastSelector; - ds - .selectorToFacetAndPosition[lastSelector] - .functionSelectorPosition = uint96(selectorPosition); + bytes4 lastSelector = ds.facetFunctionSelectors[_facetAddress].functionSelectors[lastSelectorPosition]; + ds.facetFunctionSelectors[_facetAddress].functionSelectors[selectorPosition] = lastSelector; + ds.selectorToFacetAndPosition[lastSelector].functionSelectorPosition = uint96(selectorPosition); } // delete the last selector ds.facetFunctionSelectors[_facetAddress].functionSelectors.pop(); @@ -284,17 +202,11 @@ library LibDiamond { if (lastSelectorPosition == 0) { // replace facet address with last facet address and delete last facet address uint256 lastFacetAddressPosition = ds.facetAddresses.length - 1; - uint256 facetAddressPosition = ds - .facetFunctionSelectors[_facetAddress] - .facetAddressPosition; + uint256 facetAddressPosition = ds.facetFunctionSelectors[_facetAddress].facetAddressPosition; if (facetAddressPosition != lastFacetAddressPosition) { - address lastFacetAddress = ds.facetAddresses[ - lastFacetAddressPosition - ]; + address lastFacetAddress = ds.facetAddresses[lastFacetAddressPosition]; ds.facetAddresses[facetAddressPosition] = lastFacetAddress; - ds - .facetFunctionSelectors[lastFacetAddress] - .facetAddressPosition = facetAddressPosition; + ds.facetFunctionSelectors[lastFacetAddress].facetAddressPosition = facetAddressPosition; } ds.facetAddresses.pop(); delete ds @@ -303,10 +215,7 @@ library LibDiamond { } } - function initializeDiamondCut( - address _init, - bytes memory _calldata - ) internal { + function initializeDiamondCut(address _init, bytes memory _calldata) internal { if (_init == address(0)) { if (_calldata.length > 0) revert NonEmptyCalldata(); } else { diff --git a/src/upgradeInitializers/DiamondInit.sol b/src/upgradeInitializers/DiamondInit.sol index 1a9ed48..3ce6e6a 100644 --- a/src/upgradeInitializers/DiamondInit.sol +++ b/src/upgradeInitializers/DiamondInit.sol @@ -1,26 +1,26 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; -/******************************************************************************\ -* Author: Nick Mudge (https://twitter.com/mudgen) -* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 -* -* Implementation of a diamond. -/******************************************************************************/ - +/** + * \ + * Author: Nick Mudge (https://twitter.com/mudgen) + * EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 + * + * Implementation of a diamond. + * /***************************************************************************** + */ import {LibDiamond} from "../libraries/LibDiamond.sol"; -import { IDiamondLoupe } from "../interfaces/IDiamondLoupe.sol"; -import { IDiamondCut } from "../interfaces/IDiamondCut.sol"; -import { IERC173 } from "../interfaces/IERC173.sol"; -import { IERC165 } from "../interfaces/IERC165.sol"; +import {IDiamondLoupe} from "../interfaces/IDiamondLoupe.sol"; +import {IDiamondCut} from "../interfaces/IDiamondCut.sol"; +import {IERC173} from "../interfaces/IERC173.sol"; +import {IERC165} from "../interfaces/IERC165.sol"; // It is exapected that this contract is customized if you want to deploy your diamond // with data from a deployment script. Use the init function to initialize state variables // of your diamond. Add parameters to the init funciton if you need to. -contract DiamondInit { - - // You can add parameters to this function in order to pass in +contract DiamondInit { + // You can add parameters to this function in order to pass in // data to set your own state variables function init() external { // adding ERC165 data @@ -30,13 +30,11 @@ contract DiamondInit { ds.supportedInterfaces[type(IDiamondLoupe).interfaceId] = true; ds.supportedInterfaces[type(IERC173).interfaceId] = true; - // add your own state variables - // EIP-2535 specifies that the `diamondCut` function takes two optional + // add your own state variables + // EIP-2535 specifies that the `diamondCut` function takes two optional // arguments: address _init and bytes calldata _calldata // These arguments are used to execute an arbitrary function using delegatecall // in order to set state variables in the diamond during deployment or an upgrade - // More info here: https://eips.ethereum.org/EIPS/eip-2535#diamond-interface + // More info here: https://eips.ethereum.org/EIPS/eip-2535#diamond-interface } - - -} \ No newline at end of file +} diff --git a/test/DeployDiamond.t.sol b/test/DeployDiamond.t.sol index 3bd4e9b..abbf6ba 100644 --- a/test/DeployDiamond.t.sol +++ b/test/DeployDiamond.t.sol @@ -33,14 +33,7 @@ contract DiamondDeployer is DiamondUtils, IDiamondCut { dCutFacet = new DiamondCutFacet(); erc721Facet = new ERC721Facet(); diamond = new Diamond( - address(this), - address(dCutFacet), - address(erc721Facet), - "Web3Bridge", - "W3CXI", - 0.1 ether, - 10, - true + address(this), address(dCutFacet), address(erc721Facet), "Web3Bridge", "W3CXI", 0.1 ether, 10, true ); dLoupe = new DiamondLoupeFacet(); ownerF = new OwnershipFacet(); @@ -81,9 +74,5 @@ contract DiamondDeployer is DiamondUtils, IDiamondCut { DiamondLoupeFacet(address(diamond)).facetAddresses(); } - function diamondCut( - FacetCut[] calldata _diamondCut, - address _init, - bytes calldata _calldata - ) external override {} + function diamondCut(FacetCut[] calldata _diamondCut, address _init, bytes calldata _calldata) external override {} } diff --git a/test/utils/DiamondUtils.sol b/test/utils/DiamondUtils.sol index ee0f893..990524c 100644 --- a/test/utils/DiamondUtils.sol +++ b/test/utils/DiamondUtils.sol @@ -7,9 +7,7 @@ import "solidity-stringutils/strings.sol"; abstract contract DiamondUtils is Test { using strings for *; - function generateSelectors( - string memory _facetName - ) internal returns (bytes4[] memory selectors) { + function generateSelectors(string memory _facetName) internal returns (bytes4[] memory selectors) { //get string of contract methods string[] memory cmd = new string[](4); cmd[0] = "forge"; @@ -34,7 +32,7 @@ abstract contract DiamondUtils is Test { strings.slice memory dbquote = '"'.toSlice(); selectors = new bytes4[]((s.count(colon))); - for (uint i = 0; i < selectors.length; i++) { + for (uint256 i = 0; i < selectors.length; i++) { s.split(dbquote); // advance to next doublequote // split at colon, extract string up to next doublequote for methodname strings.slice memory method = s.split(colon).until(dbquote);