New keyword in Solidity smart contracts

There are a few different ways to create a smart contract from an existing smart contract. One way is to use the New keyword in Solidity. The New keyword in Solidity is used to create a new instance of a smart contract. In this tutorial we will review what the New keyword does and how to use it in an example Solidity smart contract.

The New keyword in Solidity

New keyword in a Solidity smart contract

Using the New keyword in a Solidity smart contract function will result in:

  • deploying a new contract
  • initialize state variables
  • the new contracts constructor executed
  • nonce value set to one
  • the address of the new contracts instance returned to the caller

Requirements to use the New keyword in Solidity

It should be noted that for the New keyword to successfully execute and create a new contract:

  • The code to create the new contract is known before execution
  • The address of the new contract is computed from the address of the creating contract
  • Creating a new contract requires a gas fee in order to complete the operation

When to use the New keyword in Solidity?

There are many use cases that require a smart contract to create other contracts. The New keyword is useful to keep your application generic. As an example Automated Market Makers (AMM), such as Uniswap and Pancake Swap, use the New keyword to create trading pairs. Read here to learn more about the Uniswap solidity smart contracts on Ethereum.

Contracts that create other contracts are usually referred to as factory contracts. As an example the Uniswap factory contract is used to create trading pairs. Additionally, information about the Uniswap factory contract can be found here.

Example contract

Let’s work through an example to see the New keyword used in practice. The Solidity code below contains an AccountFactory contract and a UserAccount contract. The AccountFactory contact will create new contracts using the code in the UserAccount contract. This sample Solidity code will allow one to create a smart contract for each user account.

In short copy the code below in Remix or your favorite IDE. Next, deploy the factory contract code locally to test its features. Finally, call the function CreateUserAccount. Last but not least the log prints the newly created contract account.

Experiment with the contract below and learn the basics of the New keyword. 

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract UserAccount {
    address public company;
    address public user;
    string public name;

    constructor(address _user, string memory _name) payable {
        company = msg.sender;
        user = _user;
        name = _name;
    } 

}

// The AccountFactory contract below will deploy the code in the UserAccount contract above
contract AccountFactory {
    // state variable array to keep track of the accounts that are created with this contract
    UserAccount[] public useraccounts;


    // this function takes two parameters.  A users account number and a name
    function CreateUserAccount(address _user, string memory _name) external payable{
        // the new keyword is used to create a new contract
        // UserAccount is the name of the contract the NEW keyword will call
        // in the () add the data that needs to be passed to the constructor in the contract above
        // to send ether to the new contract use UserAccount{value: "amount"}(_user, _name);
        // to assign the new contract as a variable call it UserAccount
        UserAccount account = new UserAccount(_user, _name);
        // after the contract is deployed push the new contract account into the array to keep track of newly created contracts
        useraccounts.push(account);
    }


}

Try it in Remix

This code is for learning and entertainment purposes only. Use at your own risk. Remember smart contracts are experimental and can contain bugs.

Resources

Blockchain Networks

Below is a list of EVM compatible Mainnet and Testnet blockchain networks. Each link contains network configuration, links to multiple faucets for test ETH and tokens, bridge details, and technical resources for each blockchain. Basically everything you need to test and deploy smart contracts or decentralized applications on each chain. For a list of popular Ethereum forums and chat applications click here.

Ethereum test network configuration and test ETH faucet information
Optimistic Ethereum Mainnet and Testnet configuration, bridge details, etc.
Polygon network Mainnet and Testnet configuration, faucets for test MATIC tokens, bridge details, etc.
Binance Smart Chain Mainnet and Testnet configuration, faucets for test BNB tokens, bridge details, etc.
Fanton networt Mainnet and Testnet configuration, faucets for test FTM tokens, bridge details, etc.
Kucoin Chain Mainnet and Testnet configuration, faucets for test KCS tokens, bridge details, etc.

Web3 Software Libraries

You can use the following libraries to interact with an EVM compatible blockchain.

Nodes

Learn how to run a Geth node. Read getting started with Geth to run an Ethereum node.

Fix a transaction

How to fix a pending transaction stuck on Ethereum or EVM compatible chain

Next Review – Create a simple Solidity smart contract

-->