Ask Your Question
0

What is the process to incorporate multiple inheritance using OpenZeppelin upgradeable contracts?

asked 2021-07-21 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-01-16 08:00:00 +0000

lalupa gravatar image

To incorporate multiple inheritance using OpenZeppelin upgradeable contracts, you can follow the below steps:

  1. Create a new contract that inherits from multiple contracts that you want to combine.
  2. Use the "is" keyword to indicate that your new contract is an extension of the other contracts.
  3. Use the "WithAdminRole" or other role-related contracts you want to use as a base.
  4. Use the "Initializable" contract from OpenZeppelin to ensure that the constructor is only called once during contract deployment, even if the contract is upgraded.
  5. Use the OpenZeppelin "Initializable" modifier for each constructor that your new contract extends.
  6. Use the "super" keyword to call the parent constructors.

Here's an example:

import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

contract MyNewContract is Initializable, OwnableUpgradeable, AccessControlUpgradeable {
    bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");

    function initialize() initializer public {
        __Ownable_init();
        __AccessControl_init();
        _setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
        _setupRole(ADMIN_ROLE, msg.sender);
    }
}

In the example above, "MyNewContract" is inheriting from "OwnableUpgradeable", "AccessControlUpgradeable", and "Initializable". The constant "ADMIN_ROLE" is also declared using the keccak256 hashing algorithm. Finally, the constructor "initialize()" is created using the "initializer" modifier from OpenZeppelin.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-21 11:00:00 +0000

Seen: 8 times

Last updated: Jan 16 '22