0

Got below error when migrating contracts.

Error:  *** Deployment Failed ***

"Reader" hit a require or revert statement somewhere in its constructor. Try:
   * Verifying that your constructor params satisfy all require conditions.
   * Adding reason strings to your require statements.

the contract code is given below:

// SPDX-License-Identifier: NOLICENSE
pragma solidity ^0.7.0;

contract Base{
    address managerAddress;
    
    function setManagerAddress(address _managerAddress) public {
        managerAddress = _managerAddress;
    }
}
// SPDX-License-Identifier: NOLICENSE
pragma solidity ^0.7.0;

contract ContractManager {
    mapping(string => address) addressOf;
    
    constructor() {
        setAddress("ContractManager", address(this));
    }
    
    function setAddress(string memory _name, address _address) public {
        addressOf[_name] = _address; 
    }

    function getAddress(string memory _name) public view returns (address) {
        return addressOf[_name];
    }

    function deleteAddress(string memory _name) public {
        addressOf[_name] = address(0);
    }
}   
// SPDX-License-Identifier: NOLICENSE
pragma solidity ^0.7.0;

contract Store {
    uint value;

    function getValue() external view returns(uint){
        return value;
    }

    function setValue(uint _value) public {
        value = _value;
    }
}
// SPDX-License-Identifier: NOLICENSE
pragma solidity ^0.7.0;

import "./Store.sol";
import "./Base.sol";
import "./ContractManager.sol";

contract Reader is Base {
    Store public s;
    address storeAddress;
    ContractManager public c;
    constructor() {
        c = ContractManager(managerAddress);
        storeAddress = c.getAddress("Store");
        s = Store(storeAddress);
    }
    
    function setVal(uint _val) public {
        s.setValue(_val);
    }
}

EDIT: This is the migration script for deploying reader which also sets managerAddress

const ContractManager = artifacts.require("ContractManager")
const Reader = artifacts.require("Reader")

module.exports = (deployer) => {
    deployer.deploy(Reader)
    .then(()=>{
        return Reader.deployed()
    })
    .then((rd) => {
        rd.setManagerAddress(ContractManager.address);
    })
};

1 Answer 1

2

The problem is that manager address is never initialized before Reader's constructor is executed.

constructor() {
    c = ContractManager(managerAddress);
    storeAddress = c.getAddress("Store");
    s = Store(storeAddress);
}

So c is pointing to the zero address, and solidity fail at c.getAddress("") because the zero address doesn't have bytecode.

2
  • I have initialised managerAddress while migrating the contract Jul 6, 2021 at 5:26
  • @DeepakBhavsar It's setting the address after the contract is deployed, so it won't work because it's needed in the constructor. So either managerAddress is set in the constructor, or Store configuration is delayed after managerAddress is configured.
    – Ismael
    Jul 6, 2021 at 14:07

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.