wighawag / hardhat-deploy

hardhat deployment plugin
MIT License
1.19k stars 292 forks source link

Facet Post-Cut Initializer #327

Open hickscorp opened 2 years ago

hickscorp commented 2 years ago

I'm trying to understand something related to initializing a diamond and I can't seem to find a way.

Consider the following code:

  await diamond.deploy('FastDiamond', {
    from: deployer,
    owner: spcOwner,
    facets: ['FastFacet', 'FastAccessFacet', 'FastTokenFacet', 'FastHistoryFacet'],
    execute: { contract: 'FastInitFacet', methodName: 'initialize', args },
    log: true
  });

In the initialize function of one of my facets, I've logged the msg.sender and it's the named account deployer. I'm failing to see:

Also, I don't quite understand how an initializer could add supported interfaces to the diamond. Is there a way?

hickscorp commented 2 years ago

I found an intermediate way - protecting the initialization methods with a modifier such as diamondOwner():

  /// @dev Ensures that a method can only be called by another facet of the same diamond.
  modifier diamondInternal() {
    require(msg.sender == address(this), 'Cannot be called directly');
    _;
  }

  /// @dev Ensures that a method can only be called by the owner of this diamond.
  modifier diamondOwner() {
    require(msg.sender == IERC173(address(this)).owner(), 'Requires ownership');
    _;
  }

I'll report back here once I find a bit more.