# Solidity: How can a contract B, burn from contract A, generate another contract?

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 {

function Contract_A(){

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

function fetch(){
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?

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

``````pragma solidity ^0.4.4;

contract Contract_A {

function Contract_A(){

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

function fetch(){
cc.change();}
}

contract Contract_B {

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

function create() {

Contract_C cc = new Contract_C();

}
}

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? Oct 17, 2017 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 Oct 17, 2017 at 17:18
• Yes, I did; thanks. To interact with contract B or C, I used the approach explained my comment. Thank you again. Oct 17, 2017 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. Oct 17, 2017 at 18:30
• Great! Good luck :) Oct 17, 2017 at 18:32