How to find the address of the contract owner?

by Kevvv   Last Updated May 25, 2020 21:28 PM - source

I'm under the impression that the owner of the contract is simply referred to as accounts[0] in Truffle's testing environment, but I'm getting the error by the modifier that the caller of a certain function is not the owner.

I have instantiated contract A and contract B and the following code is in contract B:

    modifier requireContractOwner() {
        require(msg.sender == contractOwner, "Caller is not contract owner");
        _;
    }

    function _authorizeCaller(address addr)
        external
        requireContractOwner
    {
        authorizedCaller[addr] = true;
    }

And I'm calling the functions from contract A:

    function authorizeCaller(address addr) public {
        B._authorizeCaller(addr);
    }

The way I instantiated A is:

    constructor(address dataContract) public {
        contractOwner = msg.sender;
        B = B(dataContract);
    }

and B:

   constructor() public {
        contractOwner = msg.sender;
        authorizedCaller[msg.sender] = true;
    }

Following is how I deploy them:

module.exports = function (deployer, network, accounts) {
  deployer.deploy(B).then(() => {
    return deployer
      .deploy(A, B.address)
      .then(() => {
       // configuration
      });
  });
};

The problem is, when I test calling the function with the owner of the contract:

      let B = await B.new();
      let A = await A.new(B.address);
      await A.authorizeCaller(accounts[1], {
        from: accounts[0],
        gasPrice: 0,
      });

the testing fails saying that accounts[0] is not the owner of the contract by the modifier requireContractOwner().



Related Questions


Truffle — testing enum values

Updated October 27, 2017 06:28 AM

Getting storage array data from contract

Updated August 17, 2018 14:28 PM

What is the most correct way to test modifiers?

Updated October 27, 2017 10:28 AM