Creating and Deploying an Ethereum Smart Contract

Create, test, and deploy smart contracts with MetaMask and Solidity.

Ever since Ethereum was introduced in 2015 by Canadian-Russian programmer, Vitalik Buterin, it has brought forth new decentralized applications (dApps). However, Ethereum’s success is significantly attributed to the implementation of smart contracts.

Many people believe that smart contracts are a new concept and have been invented with the Ethereum Blockchain Platform. However, they date back to 1996 when computer scientist, Nick Szabo, coined the term and defined them as:

I call these new contracts “smart”, because they are far more functional than their inanimate paper-based ancestors. No use of artificial intelligence is implied. A smart contract is a set of promises, specified in digital form, including protocols within which the parties perform on these promises.

His work later inspired other scientists and researchers, including Vitalik. Before we go deeper into the creation and deployment of an Ethereum smart contract, it is essential to understand the Ethereum Virtual Machine and Gas.

Ethereum Virtual Machine (EVM)

The purpose of EVM is to serve as a runtime environment for smart contracts built on Ethereum. Consider it as a global supercomputer that executes all the smart contracts.

As the name indicates, Ethereum Virtual Machine is not physical, but a virtual machine.

Gas

In the Ethereum Virtual Machine, Gas is a measurement unit used for assigning fees to each transaction with a smart contract. Each computation happening in the EVM needs some amount of gas. The more complex the computation is, the more the gas is required to run the smart contracts.

Transaction fee = Total gas used*gas price

Let’s understand what is a smart contract and how it works.

What Is a Smart Contract?

Smart Contracts are the business logic or a protocol according to which all the transactions on a Blockchain happen. The general objective of the smart contract is to satisfy common contractual conditions. For example, if we want to create our own token on Ethereum, we need to develop smart contracts according to which all the calculations on our token would happen.

It is a stand-alone script written in Solidity, compiled into JSON, and deployed to a particular address on the blockchain. Just like we can call a URL endpoint of a RESTful API to run some logic through an HttpRequest, we can execute a deployed smart contract similarly at a particular address by entering accurate data along with Ethereum to call the compiled and deployed Solidity function.

Smart contracts can be deployed to the decentralized database for a fee proportional to the storage size of the containing code. It can be also be defined as a collection of code stored in the blockchain network, defining conditions to which all parties within the contract should agree upon.

We will be sharing an example of an Ethereum smart contract using the Solidity programming language. So, it is first essential to understand what is Solidity.

What Is Solidity?

Solidity is a JavaScript-like language developed specifically for creating smart contracts. It is typed statically and supports libraries, inheritance, and complex user-defined types.

Solidity compiler converts code into EVM bytecode, which is sent to the Ethereum network as a deployment transaction. Such transactions have more fees compared to smart contract interactions, and the owner of the contract must pay them.

Creating and Deploying Ethereum Smart Contracts With Solidity

MetaMask Chrome Extension

MetaMask acts both as an Ethereum browser and a wallet. It allows you to interact with smart contracts and dApps on the web without downloading the blockchain or installing any software. You only need to add MetaMask as a Chrome Extension, create a wallet, and submit Ether.

Though MetaMask is currently available for Google Chrome browser, it is expected to launch for Firefox too in the coming years.

Download the MetaMask Chrome extension before you start writing smart contracts.

Once it is downloaded and added as a Chrome extension, you can either import an already created wallet or create a new wallet. You must have some amount of Ethers in your Ethereum wallet to deploy an Ethereum smart contract on the network.

Creating and Deploying an Ethereum Smart Contract 1

Steps to Develop an Ethereum Smart Contract

1. Create a Wallet at MetaMask

Install MetaMask in your Chrome browser and enable it. Once it is installed, click on its icon on the top right of the browser page. Clicking on it will open it in a new tab of the browser.

Click on Create Wallet and agree to the terms and conditions by clicking I agree to proceed further. It will ask you to create a password.

After you create a password, it will send you a secret backup phrase that can be used for backing up and restoring the account. Do not disclose it or share it with someone, as this phrase can take away your Ethers.

Creating and Deploying an Ethereum Smart Contract 2

Next, ensure that you are in the Main Ethereum Network. If you find a checkmark next to “Main Ethereum Network, you are in the right place.

2. Select a Test Network

You might also find the following test networks in your MetaMask wallet:

  • Robsten Test Network
  • Kovan Test Network
  • Rinkeby Test Network
  • Goerli Test Network

Creating and Deploying an Ethereum Smart Contract 3

The above networks are for testing purposes only; note that the Ethers of these networks have no real value.

Step 3: Add Some Dummy Ethers in Your Wallet

In case you want to test the smart contract, you must have some dummy Ethers in your MetaMask wallet.

For example, if you want to test a contract using the Robsten test network, select it, and you will find 0 ETH as the initial balance in your account.

To add dummy Ethers, click on the Deposit and Get Ether button under Test Faucet.

Creating and Deploying an Ethereum Smart Contract 4

To proceed, you need to click request 1 ether from faucet and one ETH will be added to your wallet. You can add as many Ethers as you want in the test network.

Creating and Deploying an Ethereum Smart Contract 5

For example, I have added one ETH in this scenario.

Creating and Deploying an Ethereum Smart Contract 6

Once the dummy Ethers are added to the wallet, you can start writing smart contracts on the Remix Browser IDE in Solidity.

4. Use Editor Remix to Write the Smart Contract in Solidity

We will use Remix Browser IDE to write our Solidity code. Remix is the best option for writing smart contracts, as it comes with a handful of features and offers comprehensive development experience.

It is usually used for writing smaller sized contracts. Remix’s features include:

  • Warnings like Gas cost, unsafe code, checks for overlapping variable names, and whether functions can be constant or not.
  • Syntax and error highlighting.
  • Functions with injected Web3 objects.
  • Static analysis.
  • Integrated debugger.
  • Integrated testing and deployment environment.
  • Deploy directly to Mist or MetaMask.

Let’s start writing smart contract code by going here.

5. Create a .sol Extension File

Open Remix Browser, and click on the plus icon on the top left side next to the browser to create a .sol extension file.

6. Smart Contract Code to Create ECR20 Tokens

ERC20.sol is a standard template for ERC20 tokens.

pragma solidity ^0.4.0;
import “./ERC20.sol”;
contract myToken is ERC20{
mapping(address =>uint256) public amount;
uint256 totalAmount;
string tokenName;
string tokenSymbol;
uint256 decimal;
constructor() public{
totalAmount = 10000 * 10**18;
amount[msg.sender]=totalAmount;
tokenName=”Mytoken”;
tokenSymbol=”Mytoken”;
decimal=18;
}
function totalSupply() public view returns(uint256){
return totalAmount;
}
function balanceOf(address to_who) public view
returns(uint256){
return amount[to_who];
}
function transfer(address to_a,uint256 _value) public
returns(bool){
require(_value<=amount[msg.sender]);
amount[msg.sender]=amount[msg.sender]-_value;
amount[to_a]=amount[to_a]+_value;
return true;
}
}

Select a version of the compiler from Remix to compile the solidity Ethereum smart contract code.

7. Deploy Your Contract

Deploy the smart contract at the Ethereum test network by pressing the deploy button at the right-hand side of the Remix window. Wait until the transaction is complete.

After the transaction commits successfully, the address of the smart contract would be visible at the right-hand side of the Remix window. At first, all the ERC20 token will be stored in the wallet of the user who is deploying the smart contract.

To check the tokens in your wallet, go to the MetaMask window, click add tokens, enter the smart contract address, and click ok. You should be able to see the number of tokens there.

Test an Ethereum Smart Contract

  1. Try to run all the methods of your smart contract like transfer, totalSuppy, and balanceOf (in above smart contract example). These methods are present at the right-hand side of the Remix window, and you can run all the methods from there itself.
  2. Try to transfer some tokens to other Ethereum wallet addresses, and then check the balance of that address by calling the balanceOf method.
  3. Try to get total supply by calling the totalSupply method.

Steps to Deploy Ethereum Smart Contracts

  1. To make your smart contract live, switch to the main Ethereum network at MetaMask.
  2. Add some real Ethers.
  3. Now, again deploy your smart contract using remix, as mentioned in the above steps.
  4. When a smart contract is deployed successfully, visit http://www.etherscan.io and search your smart contract address there. Select your smart contract.
  5. Now you need to verify your smart contract here, click verify contract.
  6. Copy your smart contract code and paste it at Etherscan. Select the same compiler version that you selected at remix to compile your code.
  7. Check optimization to Yes if you had selected optimization at remix; otherwise, select No.
  8. Click Verify.
  9. It will take a few minutes, and your smart contract will be live if no issues occurs.

You can now run your smart contract methods at Etherscan.

This article has been published from the source link without modifications to the text. Only the headline has been changed.

Source link