Open In App

How to Set up Ganche with Metamask?

Last Updated : 05 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Ganache is a development tool in the Truffle Suite and is used for setting up a personal Ethereum Blockchain to deploy contracts, develop your applications, and run tests.

MetaMask is a software cryptocurrency wallet used to interact with the Ethereum blockchain.

Installing MetaMask:

1. Click on url and install the MetaMask extension. As of now, Metamask supports Chrome, Firefox, and other popular chromium-based browsers(Brave, Edge).

Install MetaMask

2. Add the MetaMask extension to Google Chrome.

Add MetaMask to chrome

3. Then proceed to create a new wallet, accept terms and conditions and finish the installation process by setting up a password for your account.

Get StartedCreate walletAccept terms of MetamaskCreate password

4. Verify the installation by opening MetaMask from the toolbar.

Verify MetaMask installationOpen MetaMask Interface

Installing Ganache: Follow the steps below to install ganache.

1. Download the Executable Package File by visiting this link.

Download exe

2. Alternatively, you can also install ganache-cli by executing the following command in the terminal. Omit sudo, if you are on a Windows platform and open the cmd in administrator mode.

sudo npm install -g ganache-cli

Import Ganache Account to Your Metamask Wallet:

Follow the steps below to import the ganache account to your MetaMask wallet.

Start the ganache blockchain and copy the private key of the desired account.

Ganache GUI

Ganache GUI

Ganache CLI

Ganache CLIGanache CLIGanache CLI

Add the local blockchain network in MetaMask by entering the RPC URL and Chain ID (Default Value: HTTP://127.0.0.1:8545 and 1337 respectively).

Add local blockchain networkAdd network details

Create a new account by entering the copied private key.

Create a new accountImport accountAccount created

MetaMask is now connected to the required node on the ganache blockchain successfully. Now, we will deploy a smart contract written in solidity and will use truffle to deploy it. This will consume some gas from our account and check whether the changes are reflected in our MetaMask wallet or not.

Steps to Deploy the Smart Contract

Installing truffle.

sudo npm install -g truffle

Verify the installation by executing.

verify the installation

Creating our smart contract.

mkdir folder_name
cd folder_name
truffle init
truffle create contract TestContract

Creating smart contract

Now, open the TestContract.sol in any code editor of your choice and update its content to-

Solidity




// Solidity program to implement
// the above approach
pragma solidity ^0.5.0;
 
contract TestContract
{
  uint public counter = 0;
 
  constructor() public
  {
    IncrementCounter();
  }
 
  function IncrementCounter() public
  {
    counter ++;
  }
}


In the above code, we have created a smart contract TestContract that has a variable counter and a method IncrementCounter that increments the value of our variable by one.

Now, create a new file 2_deploy_contracts.js in the migration subfolder and import our smart contact.

Javascript




// Javascript program to implement
// the above approach
var TestContract = artifacts.require("./TestContract.sol");
 
module.exports = function(deployer)
{
  deployer.deploy(TestContract);
};


At last, compile the smart contract by executing :

truffle compile

truffle compile

Note: If experiencing any difficulties/errors in following the above steps, please have a look at the source code of the compiled contract.

Deploying the smart contract

Deploying smart contractDeploying smart contract

After transaction.

Result after transactionResult after transaction

Check the ethers left after the transaction.

Check ethers left after transaction



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads