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,
- Ropsten
- Kovan
- 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,
After a few minutes, you’ll see ethers reflected in your account.
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,
- pragma solidity >=0.4.0 <=0.6.0;
-
- contract news{
-
- struct newsfeed{
- address publisher;
- string newsdesc;
- }
- mapping(uint => newsfeed) public newsfeeds;
- uint public newsCount;
-
- function addnews(string memory newsdesc) public {
- newsCount++;
- newsfeeds[newsCount].publisher = msg.sender;
- newsfeeds[newsCount].newsdesc = newsdesc;
-
- }
- }
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.
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.
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.
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.
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!