2

I have a contract that "is Initializable" and it is the base contract so I am using the onlyInitializing modifier. However, after I deploy my contract and call the initialize function I receive "Error: VM Exception while processing transaction: reverted with reason string 'Initializable: contract is not initializing'" I am using hardhat deploy. My initialize function

   function initialize(
        IERC20 _token,
        uint256 _maxDuration,
        uint256 _minDuration
    ) public onlyInitializing {
        token = _token;
        maxDuration = _maxDuration;
        minDuration = _minDuration;
    }

and here is some of my deploy script where I hit the error

   await deploy('Crowdfund', {
      from: deployer,
      log: true,
      waitConfirmations: waitBlockConfirmations
    });

    log('Initializing Crowdfund');

    crowdfund = await ethers.getContract('Crowdfund');
    const initTx = await crowdfund.initialize(
      initArgs.tokenAddress,
      initArgs.min_duration,
      initArgs.max_duration
    );
    initTx.wait(1);

    log('Crowdfund initialized');

FULL REPO

1 Answer 1

1

You should use the initializer() modifier instead of onlyInitializing(). initializer() is used in the top-level initialize call, onlyInitializing() only in functions called inside it.

    function initialize(
        IERC20 _token,
        uint256 _maxDuration,
        uint256 _minDuration
    ) public initializer {
        token = _token;
        maxDuration = _maxDuration;
        minDuration = _minDuration;
    }
3
  • Thank you! Why is it that when I use the initializer modifier I am able to call the initialize function on my contract more than once? Shouldn't I only be able to call the function with at modifier once? Commented Feb 18, 2023 at 17:22
  • @KateJohnson Yeah, the second time should revert. It's possible that there's a bug in your deploy/test scripts. Try making all transactions in the same function, one after the other, calling initialize two times and it should behave as expected.
    – 0xSanson
    Commented Feb 18, 2023 at 17:53
  • I tried that and it didn't revert but when I used both modifiers it did revert as expected Commented Feb 18, 2023 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.