Create Your First Smart Contract In Ethereum With Ganache And Remix IDE

Ethereum is the most popular blockchain platform today. Let's see how to create, deploy, and test smart contracts. In this, we will be using Ganache, Remix IDE & MyEtherWallet. We will be using the use of a local instance of Ethereum as a private blockchain for the sake of simplicity. Before that, let’s have a quick look at Smart Contracts.

What is Smart Contract?

The above image explains pretty much everything about Smart Contracts and its origin. Nick Szabo, who is a cryptography genius and legal scholar, coined the term Smart Contract back in 1994. He was trying to store and execute legal contracts with the help of cryptography. Since then there have been many implementations of Smart Contract, but nothing was widely accepted and popular like Ethereum Smart Contract.
Tools Used
Let’s understand what the above-mentioned tools do exactly, and how to use them.
Download and install Ganache from
Ganache runs a local instance of Ethereum, so we don’t need to struggle with Geth command line, along with this we get a few accounts created by default, which can be used in development and testing. Ganache has a built-in block explorer also, so we can easily track all the blocks and transactions.
Apart from this Ganache has many other useful features which you can see below, according to their website.
After installing, open Ganache and you will see this window.
You can see here a list of accounts along with their address, balance, and other information. In the header, you can see blocks, transaction information, etc. Observe RPC SERVER Address here, with this address you will be interacting with this blockchain.
Remix IDE
Remix is an online IDE for solidity language, you can access it online at We will use it to write and compile our smart contract code.
MyEtherWallet is an online wallet & client-side interface, which can interact with Ethereum blockchain & perform operations on the blockchain.
But here we are using private blockchain running in our local, which can’t be accessed by an online version, so we will download it from and run it locally.
You can download the latest release, unzip it, and open index.html.
Write Smart Contract in Solidity
A new language! No, don’t get scared, it’s very similar to the code you write in your day to day life, which a few keywords here and there. Here, I am not using any complex code but you can always go and check for a complete reference.
Solidity is a high level, contract oriented language which runs on Ethereum Virtual Machine (EVM). Let’s understand the code, We are going to create a calculator here, which is capable of performing addition and subtraction operations.
The first line in any solidity program, tells the compiler the version of language it is targeting. In the second line, as you know this is a contract oriented language, contract keyword is used to define a contract. You may relate it with class in OOPS to understand.
Contracts will contain fields & function along with access modifier applied to them like any other language. In this code, I have used int in all the places but solidity does have a rich type of system which has many contract specific types as well like address and balance etc.
  1. pragma solidity ^0.4.24;  
  2. contract Calculator {  
  3.     int private lastValue = 0;  
  4.     function Add(int a, int b) public returns (int) {  
  5.         lastValue = a + b;  
  6.         return lastValue;  
  7.     }  
  8.     function Subtract(int a, int b) public returns (int) {  
  9.         lastValue = a - b;  
  10.         return lastValue;  
  11.     }  
  12.     function LastOperation() public constant returns (int) {  
  13.         return lastValue;  
  14.     }  
  15. }   
In the above code you can see there is one field lastValue, which stores the last operation performed. Here LastOperation has a constant keyword, but add and subtract do not. because LastOperation doesn’t change the state whether or not add and subtract are mutating it.
Open Remix IDE and replace the existing code with the above one. Then click Start to compile and if it compiles successfully, you can see your contract name (Calculator here) in the green box, below the details button. On click of the details button, you can see all the data about the compiled contract, including generated byte code, which we will be using further.
Your code is compiled successfully, now let’s deploy and test it.
Deploy Smart Contract to Private Ethereum Blockchain & Test It
We will deploy it to blockchain running in Ganache with the help of MyEtherWallet. Open Ganache and grab RPC SERVER URL.
Now, open index.html of MyEhterWallet, and connect to this blockchain. To do this click on dropdown as shown in the image and select Add Custom Network/Node
Give some name to Node and enter URL & post what you copied from Ganache, and hit Save & Use Custom Node.
You will see the success message on the bottom.
Now go to contacts in the menu and click deploy the contract, here you need Byte Code of your smart contract to deploy, which we will get from details of the contract in Remix IDE.
Go back to Remix IDE, click on details & copy BYTECODE.
We need to pick the contents of an object, copy it, and paste in Byte Code in Deploy Contract in MyEtherWallet.
  1. {  
  2.     "linkReferences": {},  
  3.     "object""60806040526000805534801561001457600080fd5b5061017e806100246000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633e21f3961461005c578063830fb67c146100a7578063d3507d71146100f2575b600080fd5b34801561006857600080fd5b50610091600480360381019080803590602001909291908035906020019092919050505061011d565b6040518082815260200191505060405180910390f35b3480156100b357600080fd5b506100dc6004803603810190808035906020019092919080359060200190929190505050610133565b6040518082815260200191505060405180910390f35b3480156100fe57600080fd5b50610107610149565b6040518082815260200191505060405180910390f35b6000818303600081905550600054905092915050565b6000818301600081905550600054905092915050565b600080549050905600a165627a7a72305820ffa43bcf8db91f746657d0616346eaf4dcffe1e83be6d2ee91f6390f141bf47d0029",  
  5.     "sourceMap""26:391:0:-;;;77:1;53:25;;26:391;8:9:-1;5:2;;;30:1;27;20:12;5:2;26:391:0;;;;;;;"  
  6. }  
Right after pasting Byte Code, Gas Limit should appear. To access your wallet, MyEtherWallet needs a private key. Wallet? Yes, let me explain wallet to you in the next step.
As explained earlier Ganache will have a few accounts created by default. The place where the information about the account is stored is referred to as a wallet. We will use one of those accounts.


Open Ganache and copy a key from one of the wallets and use this in the above step, and click Unblock.
After adding byte code and giving wallet access, hit Sign Transaction button, this makes transaction verifiable. Now the Deploy Contract button will appear, hit it, and confirm the transaction to deploy your smart contract to the blockchain.
Let’s verify in Ganache whether it’s deployed successfully. Go to transactions in Ganache, here you can see one Contract Creation transaction is created in a block. Congrats! Your smart contract is deployed successfully.
Let’s test it now, click to open the above-shown transaction, and copy CREATED CONTRACT ADDRESS. Goto MyEtherWallet & under Contracts, click Interact with Contract, paste the Contract Address here.
For the ABI interface, you need to go to Remix IDE then details, get ABI form here and paste. ABI interface contains information about functions available in a smart contract.
  1. [  
  2.     {  
  3.         "constant"false,  
  4.         "inputs": [  
  5.             {  
  6.                 "name""a",  
  7.                 "type""int256"  
  8.             },  
  9.             {  
  10.                 "name""b",  
  11.                 "type""int256"  
  12.             }  
  13.         ],  
  14.         "name""Subtract",  
  15.         "outputs": [  
  16.             {  
  17.                 "name""",  
  18.                 "type""int256"  
  19.             }  
  20.         ],  
  21.         "payable"false,  
  22.         "stateMutability""nonpayable",  
  23.         "type""function"  
  24.     },  
  25.     {  
  26.         "constant"false,  
  27.         "inputs": [  
  28.             {  
  29.                 "name""a",  
  30.                 "type""int256"  
  31.             },  
  32.             {  
  33.                 "name""b",  
  34.                 "type""int256"  
  35.             }  
  36.         ],  
  37.         "name""Add",  
  38.         "outputs": [  
  39.             {  
  40.                 "name""",  
  41.                 "type""int256"  
  42.             }  
  43.         ],  
  44.         "payable"false,  
  45.         "stateMutability""nonpayable",  
  46.         "type""function"  
  47.     },  
  48.     {  
  49.         "constant"true,  
  50.         "inputs": [],  
  51.         "name""LastOperation",  
  52.         "outputs": [  
  53.             {  
  54.                 "name""",  
  55.                 "type""int256"  
  56.             }  
  57.         ],  
  58.         "payable"false,  
  59.         "stateMutability""view",  
  60.         "type""function"  
  61.     }  
  62. ]   
After clicking Access, you can see all available functions in your smart contract.
Let’s call them and test.
While calling Add or Subtract, it will show a warning before making a transaction, along with the gas limit.
But in LastOperation no warning will appear because it adds no data to the blockchain.
You can see all those transactions in Ganache, along with the gas used.
I hope it helps.

Similar Articles