Open In App
Related Articles

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

Improve Article
Improve
Save Article
Save
Like Article
Like

There are various processes involved in deploying a smart contract using Ganache and Truffle Suite:

1. Install Ganache first. Ganache is a personal blockchain for Ethereum development. You must first download and install it. It is available for download from https://www.trufflesuite.com/ganache, the official website.

2. Install Truffle Suite: The next step is to set up Truffle Suite, an Ethereum programming framework. The following command will install it using the npm package manager:

 

3. Make a new undertaking: After installing Truffle Suite, you may create a new project by typing the following line in your terminal:

 

 

4. Create your smart contract using Solidity: The final step is to create your smart contract. In the contracts/ directory that Truffle created when you started the project, you can make a new Solidity file.

5. Build your smart contract: After writing your smart contract, you can use the Truffle command line interface to compile it:

 

6. Set up your smart contract’s deployment parameters. You must identify the network on which you wish to use it. You can do this by making changes to the truffle-config.js file located in your project’s root directory. In order to deploy your smart contract on the Ganache network, you can add a new network configuration. 

 

7. Deploy your smart contract: After setting up your deployment parameters, use the following command to publish your smart contract to the network:

 

Your smart contract will be deployed to the Ganache network as a result.

Smart contracts are programs stored on a blockchain. The term ‘smart contract’ was first coined by Nick Szabo in 1994. It’s a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. One of the main features is that they are immutable once deployed on the blockchain. Solidity and Vyper are the two most active programming languages used to write smart contracts on the Ethereum blockchain with Solidity being the top choice for most developers as it is an object-oriented, statically-typed language and is strongly influenced by more popular OOP languages like JavaScript and C++. 

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 the following ways:

  1. Remix VM (London)
  2. Remix VM (Berlin)
  3. Injected Provider – MetaMask
  4. Hardhat Provider
  5. Ganache Provider
  6. Foundry Provider
  7. Wallet Connect
  8. External Http Provider
  9. L2 – Optimism Provider
  10. L2 – Arbitrum One Provider

This article explains how to deploy your contract by using Ganache 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




// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
 
// Creating a contract
contract shreyansh_05
{
    // Defining a function
    function get_output() public pure returns (string memory){
        return ("Hi, your contract ran successfully");
    }
}



Compile the Smart Contract

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 Ganache Provider in place Remix VM as shown below-

Select Ganache Provider

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

Ganache Provider RPC Server

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

Deploy the Smart 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.

Note: You can use GANACHE-CLI instead of GANACHE-GUI. For that you will have to install ganache cli. The JSON_RPC endpoint will be TTP://127.0.0.1:8545 in remix ganache provider. And you will have to start the server by typing “ganache-cli” in the terminal.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 01 May, 2023
Like Article
Save Article
Previous
Next
Similar Reads