Tutorial - Create a contract from another contract with solidity
Tags: solidity, programming, tutorial
Published: 2022-05-24 04:23:33

> _This tutorial is meant for those with a basic knowledge of Ethereum and smart contracts, who have some knowledge of Solidity._
> The purpose of building this blog is to write down the detailed operation history and my memo for learning the dApps.
> If you are also interested and want to get hands dirty, just follow these steps below and have fun!~

### Prerequisites
- [Remix](https://remix.ethereum.org/)

## Getting started

First we create a contract named Account:
```solidity
// SPDX-License-Identifier:MIT
// Adjust your own solc
pragma solidity ^0.8.14;

contract Account {
    address public bank;
    address public owner;

    constructor (address _owner) payable {
        bank = msg.sender;
        owner = _owner;
    }
}
```
> This contract `Account` has two state variables `bank` and `owner` and uses a `payable` constructor to initialize state variable `owner`. 

We will be using another contract called AccountFactory to deploy contract Account.
```solidity
...
contract AccountFactory {
    Account[] public accounts;
    function createAccount(address _owner) external payable {
        Account account = new Account{value: 111}(_owner);
        accounts.push(account);
    }
}
```
The whole contract New.Sol:

```solidity
// SPDX-License-Identifier:MIT
// Adjust your own solc
pragma solidity ^0.8.14;

contract Account {
    address public bank;
    address public owner;

    constructor (address _owner) payable {
        bank = msg.sender;
        owner = _owner;
    }
}
contract AccountFactory {
    Account[] public accounts;
    function createAccount(address _owner) external payable {
        Account account = new Account{value: 111}(_owner);
        accounts.push(account);
    }
}
```

> We created an Account array `accounts`. When we deploy a new contract (using contract Account), we name it account and send 111 wei to this contract and push it into `accounts`. We make function `createAccount` payable since we want the newly created contract receive ETH.

Now time to use Remix to deploy and test our smart contract.
Select contract **AccountFactory** and click **Deploy** to deploy it. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80bsjk4y4xejhb1zdo32.png)

Paste the current account address (address(0)) along with 200 Wei and click `createAccount`. 
Then if we input **0** to fetch accounts[0], we will get the contract address that we have just deployed:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8gt91k9lrc7fub573vq.png)

Let's copy this contract address(accounts[0]) and paste it into **At Address** and switch the contract to **Account**, and then click **At Address**:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr4re4bn8py8m4q3j8w5.png)
We will get values transferred to state variable `bank` and `owner` via **constructor** `Account()`:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8528nwltllrqky0843lp.png)
We can see the owner is the address who has created the contract accounts[0], and the bank is referred to the **msg.sender**, which is the address of contract **AccountFactory**.

### Reference
https://www.youtube.com/watch?v=J2Wp2SHq1Qo&list=PLO5VPQH6OWdVQwpQfw9rZ67O6Pjfo6q-p&index=45