Last active
February 1, 2023 15:17
-
-
Save Hero-Development/8c720914a5415c6ddafd715eac9732c3 to your computer and use it in GitHub Desktop.
Diamond.sol
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.9; | |
import "@openzeppelin/contracts/proxy/Proxy.sol"; | |
contract Diamond is Proxy{ | |
mapping(bytes4 => address) public implementations; | |
function register( bytes4 selector, address impl ) external { | |
if(impl != address(0)) | |
require(impl.code.length > 0); | |
implementations[ selector ] = impl; | |
} | |
function _implementation() internal view override returns (address impl){ | |
impl = implementations[ msg.sig ]; | |
if( impl == address(0) ) | |
revert( "Function not registered" ); | |
} | |
} | |
// deploy and register these functions later | |
contract ImplementationA{ | |
function add( uint256 value1, uint256 value2 ) external pure returns( uint256 sum ){ | |
sum = value1 + value2; | |
} | |
function sub( uint256, uint256 ) external pure returns( uint256 diff ){ | |
revert( "not implemented" ); | |
} | |
} | |
// deploy and register these functions later | |
contract ImplementationB { | |
function mul( uint256 value1, uint256 value2 ) external pure returns( uint256 product ){ | |
product = value1 * value2; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment