CREATE CONTRACT FROM ANOTHER CONTRACT

30 September 2022, Abdulhakim Altunkaya

How to create a contract from another Contract and save it inside a Contract variable?

Normally, when we develop contracts, we deploy them by ourselves by using any platforms of our choice such as Remix, Hardhat, Truffle etc. But we can also deploy a contract by using another contract

To do this, we will first create a very simple "Child" contract. And then we will create a "Parent" contract. Our target will be deploying Child contract by using Parent contract. Create a child contract like below on Remix.

this is Child contract image

Then create a Parent contract like image below. Do not forget to import "Child contract" on top. Also, create a function that will access to the Child contract and return a value from it. Here we create "getName" function to access "name" variable of Child contract.

this is Child contract image

Then deploy the Parent contract by entering any values you want. Here I will enter "Sukria" for mother name, and "Azad" for child name.

this is Child contract image

Then all we need to do is to interact with our contract. You can see here that, when you click on Contract variable which is called "child", it is returning the contract address to you. This means, Child contract has successfully been deployed by Parent contract. Also, when you click on getName view function, you can access the "name" variable of Child contract from Parent contract.

this is Child contract image

Click on arrows for the next For loop & Array exercise: ⇨ ⇨