Suppose I have 3 contracts, namely Owner, B and C:
contract Owned {
address public owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
}
Contract B
contract B is Onwed{
constructor() public{
uint256 totalSupply = 10000000000; // any amount
}
}
Contract C
I want to create an instance of contract B
, in contract C. There are 2 ways to do it
contract C{
constructor() public{
B instance = B(address_of_deployed_B); // Method 1
B instance = new B(); // Method 2
B instance2 = new B(); // 2 instances of B
}
}
What I actually want here is, the owner
of the contract is set to contract C's address
.
But the following is happening.
Method 1 works fine when I try to instantiate this way, but since it's already deployed, I can't set owner properly. Method 2 is not able to deploy 2 instances of the contract in the constructor(shows revert).
[Edit 1] Method 2 is working fine. But it isn't working if you initialise 2 instances of the same contract.
txn mined but the execution failed
in remix IDE. – Aman Pandey Jun 15 '19 at 6:08