0

I would like to have a single owner across the parent contract and child contracts that this parent creates. However, when the child contract is created, it redeploys the library and uses the parent contract as the msg.sender. This makes sense, but I was wondering if there is a way to use the parents library instead of deploying a new instance of it.

contract Owned {
  address public owner;
  function Owned() public {
    owner = msg.sender;
  }
}


contract A is Owned{ 
  B b;
  function A() { 
    b = new B(); 
  }
}

// This contract deploys another Owned library with contract A as the owner
contract B is Owned  {
  address parent;
  function B() {
    parent = msg.sender;
  }  
}

1 Answer 1

0

You should be aware that in your example you are not dealing with libraries, but with contracts inheriting from each other.

So, when you write contract A and B is Owned it would be the same as having your code look like this:

contract A{ 
  B b;
  function A() { 
    owner = msg.sender; // This would be called in Owner constructor 
    b = new B(); 
  }
}

// This contract deploys another Owned library with contract A as the owner
contract B{
  address parent;
  function B() {
    owner = msg.sender; // This would be called in Owner constructor
    parent = msg.sender;
  }  
}

I'm not sure what's your ultimate intention, if having a third contract "owner" that is the owner of both A and B, or if you would like to deploy A, which in turn deploys B, but have B's owner be the account that deployed A instead of A itself.

UPDATE: Here's how you could have it so both A and B's owner is the account that deployed A:

pragma solidity ^0.4.18;

contract Owned {
  address public owner;
  function Owned(address _owner) public {
    owner = _owner;
  }
}


contract A is Owned{ 
  B public b;
  function A()
  Owned(msg.sender)
  { 
    b = new B(msg.sender); 
  }
}

// This contract deploys another Owned library with contract A as the owner
contract B is Owned  {
  address parent;
  function B(address _owner)
  Owned(_owner)
  {
    parent = msg.sender;
  }  
}
4
  • Accidentally wrote "contract" instead of "library". In any case, what I'm trying to ask is how the parent and child contract share the same library? When the child contract is deployed, it doesn't seem to inherit from the same library. What my intention is to have a single owner across all contracts.
    – arete
    Nov 22, 2017 at 15:56
  • If you change the Owned contract to be a library the code won't even compile... Nov 22, 2017 at 16:13
  • Check my updated answer for a solution to your question Nov 22, 2017 at 16:22
  • Ok thanks, so there is no way to inherit from an already existing contract?
    – arete
    Nov 25, 2017 at 12:24

Your Answer

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

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