1

As we know if we want contract A, in the future, creates another contract: contract B, when we're deploying contract A, we also put the code of contract B in the transaction. So we send the following code to the blockchain to deploy contract A:

pragma solidity ^0.4.4;

contract Contract_A {

address public b_addr;
function Contract_A(){

 }
  function create() {
    Contract_B bb = new Contract_B();
    b_addr = address(bb); }

function fetch(){
    Contract_B cc = Contract_B(b_addr);
    cc.change();}    
}

contract Contract_B {

  uint public val;
  function Contract_B(){
   val=7;}
   function change(){
   val+=90;}
  }

Assume, contract B wants to create another contract: contract C, in the future.

The question is: where should we put the code of contract C?

1

I tried something as follow on Remix IDE and it worked.

pragma solidity ^0.4.4;

contract Contract_A {

address public b_addr;
function Contract_A(){

 }
  function create() {
    Contract_B bb = new Contract_B();
    b_addr = address(bb); }

function fetch(){
    Contract_B cc = Contract_B(b_addr);
    cc.change();}    
}

contract Contract_B {

    address public c_addr;
  uint public val;
  function Contract_B(){
   val=7;}
   function change(){
   val+=90;}

   function create() {

    Contract_C cc = new Contract_C();
    c_addr = address(cc); 

   }
}


contract Contract_C {

    //contract C code
}

AFAIK, you can either use a function defined in contract A to create a contract of B and call it (every time you call the function a contract B instance will be created ), or may be you can assign the address value of created contract B at the very first time and and check if the address is set whenever the function is called.

  • thanks for the answer! question: How can we interact with contract B (even in remix)? Do we need to mention both contractB and contractC? In general, when we are interacting with contractA, we need ABI of all three contracts; when we interacting with B, we need ABI of contract B and C, and when we interact with contract C, we need ABI of contract C? – Ay. Oct 17 '17 at 15:43
  • AFAIK, you can either use a function defined in contract A to create a contract of B and call it (every time you call the function a contract B instance will be created ), or may be you can assign the address value of created contract B at the very first time and and check if the address is set whenever the function is called, this may look like singleton pattern and will not create B contracts every time you call. And if you return the value of B's address once it's created you can directly call B from may be web3 in that case you will need the ABI – Achala Dissanayake Oct 17 '17 at 17:18
  • Yes, I did; thanks. To interact with contract B or C, I used the approach explained my comment. Thank you again. – Ay. Oct 17 '17 at 18:15
  • Yes, that's right. The one mentioned in my first comment: ... When I'm interacting with contract B deployed, I have to include ABI of contract B and C. – Ay. Oct 17 '17 at 18:30
  • Great! Good luck :) – Achala Dissanayake Oct 17 '17 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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