How to use GANACHE Truffle Suite to Deploy a Smart Contract in Solidity (Blockchain)?

Smart contracts are self-executing contracts. They were first proposed by Nick Szabo in the ’90s. They are set of rules and protocols which two parties agree upon and have to follow. One of the main features is that they are immutable once deployed on the blockchain. It is widely used in the Ethereum blockchain. The main language used for writing smart contracts is Solidity.

IDE

To write and execute solidity codes, the most common IDE used is an online IDE known as REMIX. You can either open it online on https://remix.ethereum.org/ or install it in your system from https://github.com/ethereum/remix-ide. You can also use Mist (the Ethereum Dapp browser).  

After you write the code and compile it you can deploy it in 3 ways –

  1. JavaScriptVM (https://www.geeksforgeeks.org/hello-world-smart-contract-in-remix-ide/)
  2. Injected Web3
  3. Web3 Provider

This article explains how to deploy your contract by using Ganache as a web3 provider.

Ganache

Ganache is a personalized blockchain for Ethereum development. It can be used to run tests, execute commands, and inspect states while controlling how the chain operates. It is provided by Truffle Suite and can be downloaded from https://www.trufflesuite.com/ganache



Step 1: Open Remix IDE in your browser. After opening click on “ + ” and write the filename as follows –

Open Remix IDE

Step 2: Write the following sample code for testing and compile by clicking on the Compile button as shown –   

Solidity

filter_none

edit
close

play_arrow

link
brightness_4
code

// Solidity program to demonstrate 
pragma solidity ^0.4.26;
  
// Creating a contract
contract shreyansh_05  
{
    // Defining a function
    function get_output() public pure returns (string) 
    {
       return ("Hi, your contract ran successfully");
   }
  
}

chevron_right


Solidity program

Step 3: After compilation open GANACHE on your desktop which you have downloaded from the link given above. The screen will look like below. Click on QUICKSTART Ethereum

Ganache Window

Step 4: Now you will get 10 default accounts for your blockchain at a local RPC server HTTP://127.0.0.1:7545 as below-



Default Accounts

Step 5: Go back to the browser and move to Deploy section just below the compilation and select Web3 Provider in place JavascriptVM as shown below-

Deploy section

Step 6: Enter the server HTTP://127.0.0.1:7545 as Web3 Provider. The screen will look like below –

Enter Server

Step 7: Now your contract is ready to be deployed. Click on the Deploy button and the deployed contract will look as follows- 

Deployed contract

Step 8: Expand the deployed contract as below and get the output using the get_output() function-

Output

Step 9: Now, to verify whether your transaction (process) was reflected on the server or not, open GANACHE and move to TRANSACTIONS. Here you will get the details as below –

View Transaction in Ganache

Now your contract is completely ready to function. Make sure the compiler version matches the version of your solidity code. This is the basic implementation of GANACHE with solidity. 




My Personal Notes arrow_drop_up

Never give up

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.