Last active
March 4, 2022 21:30
-
-
Save mudgen/fe2ee945446e64c56c9c11c1796c1f3b to your computer and use it in GitHub Desktop.
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
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.11; | |
import { LibDiamond } from "../libraries/LibDiamond.sol"; | |
import { IDiamondCut } from "../interfaces/IDiamondCut.sol"; | |
import { IERC165 } from "../interfaces/IERC165.sol"; | |
import { IERC721 } from "../interfaces/IERC721.sol"; | |
contract Diamond { | |
constructor(address _contractOwner, address _diamondCutFacet, address _diamondLoupeFacet) { | |
LibDiamond.setContractOwner(_contractOwner); | |
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); | |
ds.supportedInterfaces[type(IERC721).interfaceId] = true; | |
// Add the diamondCut external function from the diamondCutFacet | |
IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](2); | |
bytes4[] memory functionSelectors = new bytes4[](1); | |
functionSelectors[0] = IDiamondCut.diamondCut.selector; | |
cut[0] = IDiamondCut.FacetCut({ | |
facetAddress: _diamondCutFacet, | |
action: IDiamondCut.FacetCutAction.Add, | |
functionSelectors: functionSelectors | |
}); | |
// Add the IERC165.supportsInterface function | |
functionSelectors[0] = IERC165.supportsInterface.selector; | |
cut[1] = IDiamondCut.FacetCut({ | |
facetAddress: _diamondLoupeFacet, | |
action: IDiamondCut.FacetCutAction.Add, | |
functionSelectors: functionSelectors | |
}); | |
LibDiamond.diamondCut(cut, address(0), ""); | |
} | |
// Find facet for function that is called and execute the | |
// function if a facet is found and return any value. | |
fallback() external payable { | |
LibDiamond.DiamondStorage storage ds; | |
bytes32 position = LibDiamond.DIAMOND_STORAGE_POSITION; | |
// get diamond storage | |
assembly { | |
ds.slot := position | |
} | |
// get facet from function selector | |
address facet = ds.facetAddressAndSelectorPosition[msg.sig].facetAddress; | |
require(facet != address(0), "Diamond: Function does not exist"); | |
// Execute external function from facet using delegatecall and return any value. | |
assembly { | |
// copy function selector and any arguments | |
calldatacopy(0, 0, calldatasize()) | |
// execute function call using the facet | |
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0) | |
// get any return value | |
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()) | |
} | |
} | |
} | |
receive() external payable {} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment