Create a Contract with New

Published by Mario Oettler on

Creating a contract by another contract with new is pretty simple.

pragma solidity 0.8.20;

contract parentContract{
    
    childContract public child;
    
    constructor() payable{
        
    }
    
    function createContract(string memory _childName, uint256 _childAge) public{
        child = new childContract{value: 100}(_childName, _childAge);
    }
}


contract childContract{
    
    string  public name;
    uint256 public age;
    
    constructor(string memory _name, uint256 _age) payable {
        name = _name;
        age  = _age;
    }
}
Categories: