Log in Register
Home Search Categories About
Bilel T
read article

How to Connect to Ganache with Web3.js and Get User Balance

Ganache, a popular blockchain emulator, is an excellent tool for testing and developing Ethereum smart contracts. To interact with your smart contracts and query account balances, you'll need to use Web3.js, the de facto JavaScript library for Ethereum. In this article, we'll walk you through the process of connecting to Ganache using Web3.js and fetching a user's account balance.

Prerequisites

Before we get started, make sure you have the following in place:

  1. Ganache: Download and install Ganache to set up a local Ethereum blockchain. You can find it here.

  2. Node.js and npm: Make sure you have Node.js and npm (Node Package Manager) installed on your machine. You can download them here.

  3. Web3.js: Create a new Node.js project and install Web3.js using the following command:

    npm install web3

Connecting to Ganache

To connect to your local Ganache blockchain, follow these steps:

1. Import Web3.js

Start by importing the Web3.js library into your Node.js project:

const Web3 = require('web3');

2. Initialize Web3

Next, initialize Web3 by providing the URL to your Ganache instance:

const web3 = new Web3('http://localhost:7545'); // Replace with your Ganache URL

3. Check Connection

To ensure that you've successfully connected to Ganache, you can check the current block number:

web3.eth.getBlockNumber().then(blockNumber => {
  console.log(`Connected to Ganache. Current block number: ${blockNumber}`);
});

Getting User Balance

Now that you're connected to Ganache, you can retrieve the balance of a user's Ethereum account. We'll use the eth.getBalance method:

const userAddress = '0xYourUserAddress'; // Replace with the user's Ethereum address

web3.eth.getBalance(userAddress, (error, weiBalance) => {
  if (!error) {
    const balance = web3.utils.fromWei(weiBalance, 'ether');
    console.log(`User balance: ${balance} ETH`);
  } else {
    console.error(`Error fetching balance: ${error}`);
  }
});

Replace '0xYourUserAddress' with the Ethereum address for which you want to check the balance.

Putting It All Together

Here's the complete code to connect to Ganache and get a user's balance:

const Web3 = require('web3');
const web3 = new Web3('http://localhost:7545'); // Replace with your Ganache URL

web3.eth.getBlockNumber().then(blockNumber => {
  console.log(`Connected to Ganache. Current block number: ${blockNumber}`);
});

const userAddress = '0xYourUserAddress'; // Replace with the user's Ethereum address

web3.eth.getBalance(userAddress, (error, weiBalance) => {
  if (!error) {
    const balance = web3.utils.fromWei(weiBalance, 'ether');
    console.log(`User balance: ${balance} ETH`);
  } else {
    console.error(`Error fetching balance: ${error}`);
  }
});

Conclution

Connecting to Ganache using Web3.js is a crucial step in developing and testing Ethereum applications and smart contracts. With the ability to fetch user balances, you can build more robust and user-friendly decentralized applications. So, go ahead, experiment, and create amazing blockchain applications with confidence!