🎓Smart Contract Creation

This is purely educational purposes. However we will create ( multi-chained factory contract ) Which will create smart contract with a click of a button. And will be deployed in the mainnet.

What is a smart contract? What is a hybrid smart contract?

When deployed to a blockchain, a smart contract is a set of instructions that can be executed without intervention from third parties. The smart contract code defines how it responds to input, just like the code of any other computer program.

A valuable feature of smart contracts is that they can store and manage on-chain assets (like ETH or ERC20 tokens), just like you can with an Ethereum wallet. Because they have an on-chain address like a wallet, they can do everything any other address can. This enables you to program automated actions when receiving and transferring assets.

Smart contracts can connect to real-world market prices of assets to produce powerful applications. Securely connecting smart contracts with off-chain data and services is what makes them hybrid smart contracts. This is done using oracles.

What language is a smart contract written in?

The most popular language for writing smart contracts on Ethereum and EVM Chains is Solidity. It was created by the Ethereum Foundation specifically for smart contract development and is constantly being updated. Other languages exist for writing smart contracts on Ethereum and EVM Chains, but Solidity is the language used commonly for creation of the smart contracts.

If you've ever written Javascript, Java, or other object-oriented scripting languages, Solidity should be easy to understand. Similar to object-oriented languages, Solidity is considered to be a contract-oriented language.

What does a smart contract look like?

The structure of a smart contract is similar to that of a class in Javascript, with a few differences. For example, the following HelloWorld contract is a simple smart contract that stores a single variable and includes a function to update the value of that variable.

// SPDX-License-Identifier: MIT

pragma solidity 0.8.7;

contract HelloWorld {
  string public message;

  constructor(string memory initialMessage) {
    message = initialMessage;
  }

  function updateMessage(string memory newMessage) public {
    message = newMessage;
  }
}

For this example, override is necessary in the Test contract function because it overrides the base function contained in the numberComparison interface. The contract uses pure instead of view because the isSameNum function in the Test contract does not return a storage variable.

What is Remix ?

Remix is a web IDE (integrated development environment) for creating, running, and debugging smart contracts in the browser. It is developed and maintained by the Ethereum foundation. Remix allows Solidity developers to write smart contracts without a development machine since everything required is included in the web interface. It allows for a simplified method of interacting with deployed contracts, without the need for a command line interface. Remix also has support for samples. This means that Remix can load code from Github.

What does "deploying" mean?

Deploying a smart contract is the process of pushing the code to the blockchain, at which point it resides with an on-chain address. Once it's deployed, the code cannot be changed and is said to be immutable.

As long as the address is known, its functions can be called through an interface, on Etherscan, or through a library like web3js, web3py, ethers, and more. Contracts can also be written to interact with other contracts on the blockchain.

What is Metamask?

Contracts are deployed by other addresses on the network. To deploy a smart contract, you need an address. Not only that, but you need an address which you can easily use with Remix. Fortunately, MetaMask is just what is needed. Metamask allows anyone to create an address, store funds, and interact with Ethereum compatible blockchains from a browser extension.

Deploy Your First Smart Contract

You can write your first smart contract and run it in your browser without any knowledge about Ethereum or blockchains. This guide shows you how easy it is to develop smart contracts using the Solidity language, a MetaMask wallet and the Remix Development Environment. You can use all of these tools in your browser for free with no signup required.

Overview

In general, you create and deploy your smart contracts operate using the following process:

  1. Write: Write a contract to define how the contract functions, what data it can store, what other contracts it interacts with, and what external APIs it might call.

  2. Compile: Pass your smart contract code through a compiler to translate the contract into byte code that the blockchain can understand. For example, Solidity code must be compiled before it can run in the Ethereum Virtual Machine.

  3. Deploy: Send the compiled smart contract to the blockchain. From that point forward, the contract cannot be altered. However, you can still interact with the contract in several ways.

  4. Run functions: When you run the functions that you defined for the contract, the network processes those functions and modifies the state of your contract. For some functions, the network charges a small fee to complete the work. Your contract can also have functions that transfer funds to other contracts or wallets.

This guide walks you through each step, but you must install and fund your MetaMask wallet first.

Install and fund your MetaMask wallet

  1. After you install the extension, open your browser extension list and click MetaMask to open MetaMask.

  2. Follow the instructions in MetaMask to create a new MetaMask wallet. The new wallet includes a 12-word mnemonic phrase. This phrase is the key to your wallet. Copy that phrase down in a very secure location that only you can access. You can use this phrase to retrieve your wallet later or add it to another browser.

  3. Set MetaMask to use the Kovan test network.

  4. Go to the Kovan testnet faucet to send 0.1 test ETH to your MetaMask wallet address. You can copy your wallet address by clicking your account name in MetaMask. After the faucet completes the transaction, you should have 0.1 ETH in your MetaMask wallet on the Kovan testnet. Now that you configured your wallet and funded it with testnet ETH, you can write, compile, and deploy your contract.

Deploying smart contracts on-chain requires a wallet and ETH. The ETH pays for the work required by the Ethereum network to add the contract to the blockchain and store the variables. The wallet holds the ETH that you need to pay for the transaction. Install MetaMask, configure it to use the Kovan test network, and fund your wallet with free testnet ETH.

Write, compile, and deploy your first smart contract

Your first contract is a simple HelloWorld.sol example. This example shows you how to set and retrieve variables in a smart contract on-chain.

// SPDX-License-Identifier: MIT

pragma solidity 0.8.7;

contract HelloWorld {
  string public message;

  constructor(string memory initialMessage) {
    message = initialMessage;
  }

  function updateMessage(string memory newMessage) public {
    message = newMessage;
  }
}

Open the example contract in the Remix IDE. Remix opens and shows the contents of the smart contract. You can modify the code in this editor when you write your own contract.

Because the code is already written, you can start the compile step. On the left side of Remix, click the Solidity Compiler tab to view the compiler settings.

For this contract, use the default compiler settings. Click the Compile HelloWorld.sol button to compile the contract. This converts the contract from Solidity into bytecode that the Ethereum Virtual Machine can understand. Remix automatically detects the correct compiler version depending on the pragma that you specify in the contract.

After Remix compiles the contract, deploy it. On the left side of Remix, click the Deploy and Run tab to view the deployment settings.

In the deployment settings, select the Injected Web3 environment. This tells Remix that you want to deploy your contract to the blockchain that you configured in MetaMask. You could optionally use one of the Javascript VM options, but they run in a virtual environment with no connection to an actual blockchain or Chainlink oracles.

Next to the Deploy button, enter a message that you want to send with the smart contract when you deploy it. This contract has a constructor that sets an initial message when you deploy the contract.

  1. Click the Deploy button to deploy the contract and its initial message to the blockchain network. MetaMask opens and asks you to confirm payment to deploy the contract. Make sure MetaMask is set to the Kovan network before you accept the transaction. Because these transactions are on the blockchain, they are not reversible.

In the MetaMask prompt, click Confirm to approve the transaction and spend your testnet ETH required to deploy the contract.

After a few seconds, the transaction completes and your contract appears under the Deployed Contracts list in Remix. Click the contract dropdown to view its variables and functions.

Click the message variable. Remix retrieves and prints the initial message that you set.

The contract has an address just like your wallet address. If you save this address, you can return to your deployed contract at any time to retrieve variables or execute functions. To see details about your deployed contract, copy the contract address from the list in Remix and search for it in the Etherscan Kovan Testnet Explorer.

Run functions in your contract

Because you deployed the contract to an actual blockchain, several nodes on the test network confirmed your payment for the smart contract. The contract, its variables, and its functions remain in the blockchain permanently. To change the message variable that is stored with your contract, run the updateMessage function.

In your deployed contract, enter a new message next to the updateMessage function.

  1. Click the updateMessage button to set the new message in the contract data. MetaMask opens and asks you to confirm payment to update the state of your contract.

  2. In the new MetaMask prompt, click Confirm to approve the transaction.

  3. Click the message variable again to see the updated value. It might take a few seconds before the transaction updates the variable.

Now you know how to deploy example contracts to a test network and run the functions in those contracts. You can write your own contracts and test them using this same process.

Last updated