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

Ethereum
 
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?

Ethereum
 
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.
 
Ganache
 
Download and install Ganache from http://truffleframework.com/ganache/
 
Ethereum
 
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.
 
Ethereum
 
After installing, open Ganache and you will see this window.
 
Ethereum
 
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 https://remix.ethereum.org/. We will use it to write and compile our smart contract code.
 
Ethereum
 
MyEtherWallet
 
MyEtherWallet is an online wallet & client-side interface, which can interact with Ethereum blockchain & perform operations on the blockchain.
 
Ethereum
 
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 https://github.com/kvhnuke/etherwallet/releases and run it locally.
 
Ethereum
 
You can download the latest release, unzip it, and open index.html.
 
Ethereum
 
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 http://solidity.readthedocs.io 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.
 
Ethereum
 
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.
 
Ethereum
 
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
 
Ethereum
 
Give some name to Node and enter URL & post what you copied from Ganache, and hit Save & Use Custom Node.
 
Ethereum
 
You will see the success message on the bottom.
 
Ethereum
 
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.
 
Ethereum
 
Go back to Remix IDE, click on details & copy BYTECODE.
 
Ethereum
 
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",  
  4.     "opcodes""PUSstrong 0x80 PUSstrong 0x40 MSTORE PUSstrong 0x0 DUP1 SSTORE CALLVALUE DUP1 ISZERO PUSstrong 0x14 JUMPI PUSstrong 0x0 DUP1 REVERT JUMPDEST POP PUSstrong 0x17E DUP1 PUSstrong 0x24 PUSstrong 0x0 CODECOPY PUSstrong 0x0 RETURN STOP PUSstrong 0x80 PUSstrong 0x40 MSTORE PUSstrong 0x4 CALLDATASIZE LT PUSstrong 0x57 JUMPI PUSstrong 0x0 CALLDATALOAD PUSstrong9 0x100000000000000000000000000000000000000000000000000000000 SWAP1 DIV PUSH4 0xFFFFFFFF AND DUP1 PUSH4 0x3E21F396 EQ PUSstrong 0x5C JUMPI DUP1 PUSH4 0x830FB67C EQ PUSstrong 0xA7 JUMPI DUP1 PUSH4 0xD3507D71 EQ PUSstrong 0xF2 JUMPI JUMPDEST PUSstrong 0x0 DUP1 REVERT JUMPDEST CALLVALUE DUP1 ISZERO PUSstrong 0x68 JUMPI PUSstrong 0x0 DUP1 REVERT JUMPDEST POP PUSstrong 0x91 PUSstrong 0x4 DUP1 CALLDATASIZE SUB DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSstrong 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 DUP1 CALLDATALOAD SWAP1 PUSstrong 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSstrong 0x11D JUMP JUMPDEST PUSstrong 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSstrong 0x20 ADD SWAP2 POP POP PUSstrong 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST CALLVALUE DUP1 ISZERO PUSstrong 0xB3 JUMPI PUSstrong 0x0 DUP1 REVERT JUMPDEST POP PUSstrong 0xDC PUSstrong 0x4 DUP1 CALLDATASIZE SUB DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSstrong 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 DUP1 CALLDATALOAD SWAP1 PUSstrong 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSstrong 0x133 JUMP JUMPDEST PUSstrong 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSstrong 0x20 ADD SWAP2 POP POP PUSstrong 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST CALLVALUE DUP1 ISZERO PUSstrong 0xFE JUMPI PUSstrong 0x0 DUP1 REVERT JUMPDEST POP PUSstrong 0x107 PUSstrong 0x149 JUMP JUMPDEST PUSstrong 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSstrong 0x20 ADD SWAP2 POP POP PUSstrong 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSstrong 0x0 DUP2 DUP4 SUB PUSstrong 0x0 DUP2 SWAP1 SSTORE POP PUSstrong 0x0 SLOAD SWAP1 POP SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSstrong 0x0 DUP2 DUP4 ADD PUSstrong 0x0 DUP2 SWAP1 SSTORE POP PUSstrong 0x0 SLOAD SWAP1 POP SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSstrong 0x0 DUP1 SLOAD SWAP1 POP SWAP1 JUMP STOP LOG1 PUSH6 0x627A7A723058 KECCAK256 SELFDESTRUCT LOG4 EXTCODESIZE 0xcf DUP14 0xb9 0x1f PUSstrong1 0x6657D0616346EAF4DCFFE1E83BE6D2EE91F6390F14 SHL DELEGATECALL PUSstrong0 0x2900000000000000000000000000000000000000000000000000000000 ",  
  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.
 
Ethereum
 
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.
 

Ethereum

Open Ganache and copy a key from one of the wallets and use this in the above step, and click Unblock.
 
Ethereum
 
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.
 
Ethereum
 
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.
 
Ethereum
 
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.
 
Ethereum
 
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.
 
Ethereum
  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.
 
Ethereum
 
Let’s call them and test.
 
Ethereum
 
While calling Add or Subtract, it will show a warning before making a transaction, along with the gas limit.
 
Ethereum
 
But in LastOperation no warning will appear because it adds no data to the blockchain.
 
Ethereum
 
You can see all those transactions in Ganache, along with the gas used.
 
Ethereum
 
I hope it helps.


Similar Articles