sunny0714 / EIP-2535-implementation

EIP-2535 Diamond standard implementation
MIT License
2 stars 0 forks source link

How to handle constructor and access control system using OZ library in Diamond Implementation #13

Open sunny0714 opened 2 years ago

sunny0714 commented 2 years ago

Simple MyContract.sol is here

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract MyContract is AccessControl {

    bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
    address public tokenAddress;

    modifier onlyAdmin() {
        require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "Caller is not admin");
        _;
    }

    constructor(address _admin) {
        _setupRole(DEFAULT_ADMIN_ROLE, _admin);
        _setupRole(PAUSER_ROLE, _admin);
    }

    function setTokenAddress(address _tokenAddress)
        external
        onlyAdmin
    {
        tokenAddress = _tokenAddress;
    }
}
sunny0714 commented 2 years ago

And tried MyContractFacet.sol is as follows

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

bytes32 constant PAUSER_ROLE = keccak256("PAUSER_ROLE");

struct AppStorage {
    address tokenAddress;
}

contract MyContractFacet is AccessControl {

    AppStorage internal s;

    modifier onlyAdmin() {
        require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "Caller is not admin");
        _;
    }

    constructor(address _admin) {
        _setupRole(DEFAULT_ADMIN_ROLE, _admin);
        _setupRole(PAUSER_ROLE, _admin);
    }

    function setTokenAddress(address _tokenAddress)
        external
        onlyAdmin
    {
        s.tokenAddress = _tokenAddress;
    }
}

Please educate me with your expertize in Diamond

sunny0714 commented 2 years ago

Follow this Q&A in Stackoverflow And feel free to add your opinions