Disclaimer-mark
This is a user generated content for MyStory, a YourStory initiative to enable its community to contribute and have their voices heard. The views and writings here reflect that of the author and not of YourStory.
Disclaimer-mystory

How To Test And Deploy Ethereum Smart Contract

How To Test And Deploy Ethereum Smart Contract

Thursday September 05, 2019,

8 min Read

Test and Deploy Ethereum Smart Contract

Here in this writing, we will look into the step by step guide to test and deploy Ethereum smart contracts. 


We can see this using the MetaMask Chrome Extension and can learn how to test and deploy smart contracts.


Before looking into the topic, will just see the important terms of  blockchain.


Blockchain Terminology


Ethereum: Open software platform that is used to create smart contracts based on blockchain technology and deploy decentralized applications. 


Smart Contracts: It is a digital contract that is based on Ethereum blockchain. Like the usual contracts, the business rules and processes are encoded here. 

Smart contracts are based on the latest blockchain technology. Blockchain applications for the business broke ground to implement the smart contracts to extend the business worldwide. 


Solidity: It is an Ethereum high-level programming language which is contract oriented for developing smart contracts.


Ether (ETH): is a cryptocurrency which functions in Ethereum platform and it is used for the digital transactions like paying the fee for the transaction, or any computational tasks. 


Ethereum Virtual Machine (EVM): EVM refers to Ethereum Virtual Machine. It is a virtual runtime environment to execute the smart contracts which are built on Ethereum


Gas: Gas in Ethereum Virtual Machine is used as a  measuring unit for assigning transaction fee associated with the smart contract.

The computation that occurs in Ethereum Virtual Machine requires some gas. If the computation is complex, it requires more gas to execute smart contracts.


The transaction fee associated with the smart contract is calculated by using the formula


Transaction fee= Gas price * Gas required


Hope now you will understand the basic terms used in this document. Before looking at how smart contract works it is good to know the real time applications of smart contract and blockchain technology


How smart contract works


Using Solidity programming language, the smart contract scripts are developed. Compiled using JSON and deployed on the blockchain where it uses a certain address on the blockchain. 


The deployed smart contracts can be executed at a certain address on Ethereum by entering data and calling the compiled solidity program.


The transaction fee associated with the smart contracts is based on the coding size and complexity and then it is deployed on the decentralized database.


How Does Solidity Work?


As we already discussed it is a programming language to develop smart contracts.

The compiler converts the programming code into the bytecode of EVM. This EVM bytecode is sent as a transaction to the Ethereum network.



Though various sources are explaining how to test and deploy Ethereum smart contracts, here in this post we will  use MetaMask Chrome Extension


It will be beneficial to you in the following conditions:

  • You know the basics of Ethereum blockchain and the smart contracts based on the platform.
  • You know a Solidity programming language to develop smart contracts.


If you are a beginner in the Blockchain and Ethereum, then please go through the following for better understanding.

  • Watch any video related to the complete overview of Blockchain
  • Please go through this blog.
  • Read Ethereum white paper.


Create and Deploy Smart Contracts using Solidity


MetaMask Chrome Extension

This is a google chrome extension which acts as wallet and browser of Ethereum. Without any software installation or downloading blockchain, it allows interaction with smart contracts. For that, you need to add this extension to create a wallet and add ether to it.


Before you initiate with smart contract development, download Metmask extension.  You can either create a new wallet or a wallet can be imported to this chrome extension. Ether should be present in your wallet to deploy the smart contract on Ethereum network.


We will look into the steps involved to develop smart contracts.


Follow the steps to develop smart contracts on Ethereum


1. Creating wallet using Metamask

Enable the MetaMask extension after installing in your chrome. The icon appears on the right top of the browser window. Click on the icon to open the MetaMask in the new tab of the window.


By clicking on "Create Wallet" you will be able to create the wallet and accept the terms and conditions by clicking on the button "I Agree".  It prompts you to create a password. After creating the password, a secret code to backup and restore the account is sent.  


Do not share the secret code with anyone. this will protect your wallet and ethers in the wallet.


Always ensure that you enabled the "Main Ethereum Network"


2. Choose a Test Network

MetaMask wallet contains the following test networks:


  • Rinkeby Test Network
  • Ropsten Test Network
  • Kovan Test Network
  • Goerli Test Network


The above test networks can be used for testing and Ethers in the test network has no value.


3. Load your wallet with dummy Ethers

Load your MetaMask wallet with Ethers to test the smart contracts.


Consider an example of testing the smart contracts using the Ropsten test network.  

Select the Ropsten test network, the initial balance will be zero ether or 0 ETH in your created account.


By clicking on the Deposit button and Get Ether button, you will be able to dummy Ethers to the MetaMask wallet.


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


From test faucet, click on  " Request 1 ether from faucet"  and one ether will be loaded to your wallet.


Once you complete loading the dummy ethers to the wallet, you can create or write the smart contracts in solidity using the IDE Remix browser.


4.Create the smart contract in solidity using the IDE Remix

The solidity code can be written using the IDE Remix browser.  It offers many features. Usually, for small size contracts, you can make use of Remix


The following are the features  included in Remix IDE:


  • It shows warnings like unsafe code, variable names overlapping, the gas cost.
  • Highlights error and syntax
  • Static Analysis
  • Function including objects like Web3
  • Debugger
  • Testing environment
  • Deploying environment
  • Can deploy directly to MetaMask


Start writing the smart contract code using Remix IDE


5. Creating .sol Extension File

To create a .sol extension file,  click on the plus icon available in the Remix browser. The icon will be on the left top of the browser.


6. To Create ECR20 Tokens using Smart Contract Code

ERC20 tokens can be created using the standard template  ERC20.sol

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; } }


To compile the solidity code of smart contract , choose the version of the compiler from IDE Remix.


7. Deploy the Smart Contract

Click on the  " Deploy" button on the right side of IDE Remix to deploy the smart contracts on Ethereum test network. 


When the transaction gets completed successfully, you will be able to see the address of the deployed smart contract on the right side of IDE Remix.       

       

To verify the tokens, Click Add tokens button in the MetaMask window and then enter the address of the smart contract, then you will be able to view the tokens in your wallet.


Testing the Ethereum Smart Contract


  1. On the right hand of Remix window, you will able to run the methods of smart contracts. The methods like balanceOf, totalSupply, transfer can be executed.
  2. Use the transfer method to transfer the tokens to a particular Ethereum address of the wallet and then make use of the "balanceOf" method to check the balance
  3. Call the "totalSupply" method to know the total supply


Follow the Steps to Deploy Smart Contracts


  1. Enable the "Main Ethereum Network" on MetaMask to deploy your smart contract live.
  2. Add the real-valued Ethers to the wallet
  3. Repeat the above steps to deploy the smart contract again using Remix.
  4. Once it is deployed successfully, search the address of your smart contract at http://www.etherscan.io 
  5. Click on "Verify Contract" to verify the smart contract.
  6. Copy the code of smart contract at Etherscan. Choose the compiler version which you used at Remix to compile the code of your smart contract
  7. Enable the Optimization if the same has been used at Remix. If not used, choose No option.
  8. Click on Verify
  9. If no errors in your smart contract, it will be live after a few minutes.

At Etherscan, you can run the smart contracts.


Conclusion:

Hope this article will help you to test and deploy Ethereum smart contract and we have provided the step by step procedure for testing and deploying the smart contracts.


Though smart contracts are not implemented by all the industries, we can definitely say that it is one of the future of Blockchain technology.


Choose the right cryptocurrency development company to avail the best services of latest blockchain technology.