How To Deploy And Test Your Smart Contracts On Ropsten TestNet Using Metamask And Remix

 
 
 
Imagine going to an ice-cream shop and tasting a few flavors of ice-cream before choosing the flavor you actually want to pay for. Ethereum Testnets are just like that. Before deploying your smart contracts to a real Ethereum network that actually costs you real ethers, you perform your tests by deploying it on testnets that are just a simulation of the real Ethereum network.
 
There are three testnets currently in use,
  1. Ropsten
  2. Kovan
  3. Rinkeby
You can choose any of the testnets for deployment based on your requirements. In this tutorial, I am going to use Ropsten Testnet because it closely resembles Ethereum and you can easily get fake ethers. If you want me to write for other Testnets, let me know in comments.
 
So, let’s get started!
 

Dependencies

 
You must have Metamask Extension installed. If you need help setting this extension up, refer to my article.
 
Step 1 - Get Fake Ethers for your Account
 
First things first. Navigate to Ropsten Ethereum Faucet and enter your metamask account address there to have ethers transferred in your account. You can copy your account address from metamask like this,
 
How To Deploy And Test Your Smart Contracts On Ropsten TestNet Using Metamask And Remix
 
After a few minutes, you’ll see ethers reflected in your account.
 
How To Deploy And Test Your Smart Contracts On Ropsten TestNet Using Metamask And Remix
 
Step 2 - Deploy the Contract
 
Go to remix.ethereum.org and click on “use previous version”. Remix is a browser-based IDE for creating and deploying Ethereum smart contracts. No, don’t judge me for using the previous version. I just find it more stable and handy.
 
Paste the following smart contract code in solidity file in Remix,
  1. pragma solidity >=0.4.0 <=0.6.0;  
  2.   
  3. contract news{  
  4.       
  5.     struct newsfeed{  
  6.         address publisher;  
  7.         string newsdesc;  
  8.     }  
  9.     mapping(uint => newsfeed) public newsfeeds;  
  10.     uint public newsCount;  
  11.   
  12.     function addnews(string memory newsdesc) public {  
  13.         newsCount++;  
  14.         newsfeeds[newsCount].publisher = msg.sender;  
  15.         newsfeeds[newsCount].newsdesc = newsdesc;  
  16.   
  17.     }  
  18. }  
This contract allows the publisher to write news data on the blockchain and store the publisher address against his data.
 
Now, navigate to the Compile tab and compile your contract.
 
How To Deploy And Test Your Smart Contracts On Ropsten TestNet Using Metamask And Remix 
 
Now, your contract is error-free and ready to be deployed. Click on the "Run" tab beside the "Compile" tab and click on the "Deploy" button. You’ll see metamask popping up for confirming the transaction. Confirm it and Proceed.
 
 After a few seconds, you’ll receive a notification that your contract is successfully deployed and all the contract data will be shown here for testing.
 
How To Deploy And Test Your Smart Contracts On Ropsten TestNet Using Metamask And Remix 
 
Step 3 - Testing Your Smart Contract
 
Ok, so let’s test our contract to see if it is adding the news successfully. In newsdesc string under addnews function, pass any random string and transact.
 
How To Deploy And Test Your Smart Contracts On Ropsten TestNet Using Metamask And Remix 
 
This will pop up metamask for transaction confirmation as you are writing data to blockchain and it will cost gas cost in term of Weis.
 
Now, when you access the mapping “newsfeeds” with the key 1, you’ll see the information of news stored there, i.e., the account from which the news is published and the news string.
 
How To Deploy And Test Your Smart Contracts On Ropsten TestNet Using Metamask And Remix 
 
That’s all guys. If you enjoy the article and want to build a fully decentralized DApp from this same Smart Contract with Interface, head to this detailed article. Happy Chaining!


Similar Articles