Skip to content

Instantly share code, notes, and snippets.

@Hero-Development
Last active February 1, 2023 15:17
Show Gist options
  • Save Hero-Development/8c720914a5415c6ddafd715eac9732c3 to your computer and use it in GitHub Desktop.
Save Hero-Development/8c720914a5415c6ddafd715eac9732c3 to your computer and use it in GitHub Desktop.
Diamond.sol
// 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