1

I am just trying this using Remix editor. I am able to call functions from another deployed contract(plain as simple) but when that another deployed contract inherits yet another contract, Now the calling functions from the original contract to the deployed contract doesn't work. I just get a transaction revert

Working Code:

pragma solidity >=0.4.22 <0.6.0;
contract Existing {
    Deployed public dc;
    constructor(address _deployedContractAddress) public {
        dc = Deployed(_deployedContractAddress);
    }

    function setA(uint _a) public returns (uint) {
        return dc.setA(_a);
    }
    function getA() public view returns (uint) {
        return dc.a();
    }
}

contract Deployed {
    uint public a = 1;

    function setA(uint _a) public returns (uint) {
        a = _a;
        return a;
    }

}

Not Working Code:

pragma solidity >=0.4.22 <0.6.0;
contract Existing {
    Deployed public dc;
    constructor(address _deployedContractAddress) public {
        dc = Deployed(_deployedContractAddress);
    }

    function setA(uint _a) public returns (uint) {
        return dc.setA(_a);
    }
    function getA() public view returns (uint) {
        return dc.a();
    }
}


contract Ownable {
  address public owner;

  constructor() public {
    owner = msg.sender;
  }

  /**
   * @dev Throws if called by any account other than the owner.
   */
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }

}

contract Deployed is Ownable {
    uint public a = 1;

    function setA(uint _a) public onlyOwner returns (uint) {
        a = _a;
        return a;
    }

}

So here, the contract Deployed is inheriting from contract Ownable and I just get a transaction revert on calling setA

Note: contract Deployed is seperately deployed in a separate file and also included in contract Existing file.

1 Answer 1

0

The problem is not in the inheritance but in the logic of which address owns which contract. In the second (non-working) example if you remove the onlyOwner modifier from the function it works just fine.

In the non-working example this is what happens:

1) You deploy contract Deployed from your wallet, let's say its address is 0x123. That wallet becomes the owner of Deployed (as per the contract Ownable).

2) You deploy contract Existing from the same wallet. No ownership is present here.

3) Contract Existing uses the existing instance of Deployed for making a call to it. Here msg.sender is 0x123.

4) When the call from Existing to Deployed reaches Deployed the msg.sender is the originating contract's address - so the address of Existing. That contract is not the owner of Deployed as the owner is 0x123. Therefore, as the function requires that only the owner can call it, the call fails and the whole transaction is reverted.

2
  • Thanks :) this explains a lot. Could you please suggest me a solution here? Note: I still want to be able to access the deployed version of contract Deployed
    – Fasid Mpm
    Commented Sep 14, 2019 at 7:43
  • Umh, sorry, I have no idea what you really want to accomplish so I can't really suggest anything. But now you know how it works so you should be able to write something that suits your needs better. Or post a new question if you have further problems Commented Sep 14, 2019 at 9:44

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.