Web3.js : Utiliser la console Node avec Ganache et Web3.js
3 min read
3 hours ago
Published on Feb 03, 2025
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
Introduction
In this tutorial, we will learn how to use the Node console with Ganache and Web3.js to retrieve the Ether balance of an account and execute a transaction of 5 Ethers between two Ganache accounts. This guide is perfect for beginners looking to understand blockchain interactions using JavaScript and Web3.js.
Step 1: Set Up Your Environment
To get started, ensure you have the following installed on your machine:
- Node.js: Download and install from Node.js official website.
- Ganache: Install Ganache, which is a personal Ethereum blockchain for development. Download it from Truffle Suite's Ganache page.
- Web3.js: Install the library to interact with the Ethereum blockchain.
Installing Web3.js
- Open your terminal or command prompt.
- Create a new directory for your project and navigate into it:
mkdir my-eth-project cd my-eth-project
- Initialize a new Node.js project:
npm init -y
- Install Web3.js:
npm install web3
Step 2: Launch Ganache
- Open the Ganache application.
- Create a new workspace or use the default one.
- Note the RPC server URL (usually
http://127.0.0.1:7545
) and the accounts generated by Ganache.
Step 3: Access the Node Console
- Open your terminal in the project directory.
- Start the Node console:
node
Step 4: Connect to Ganache Using Web3.js
- Inside the Node console, require the Web3 library:
const Web3 = require('web3');
- Create a new instance of Web3 connected to Ganache:
const web3 = new Web3('http://127.0.0.1:7545');
Step 5: Retrieve Ether Balance of an Account
- Use the following code to get the balance of a specific account (replace the address with one from your Ganache accounts):
const address = '0xYourGanacheAccountAddress'; web3.eth.getBalance(address).then(balance => { console.log('Ether Balance:', web3.utils.fromWei(balance, 'ether')); });
Step 6: Execute a Transaction of 5 Ethers
- To send 5 Ethers from one account to another, you need to define the sender and receiver addresses, as well as the transaction details:
const senderAddress = '0xSenderAccountAddress'; const receiverAddress = '0xReceiverAccountAddress'; const value = web3.utils.toWei('5', 'ether'); web3.eth.sendTransaction({ from: senderAddress, to: receiverAddress, value: value }).then(receipt => { console.log('Transaction Receipt:', receipt); }).catch(error => { console.error('Transaction Error:', error); });
Conclusion
In this tutorial, we covered how to set up your environment to work with Web3.js and Ganache. We retrieved the Ether balance of an account and executed a transaction of 5 Ethers between two accounts. This foundational knowledge is essential for developing decentralized applications on the Ethereum blockchain.
Next Steps
- Explore more Web3.js functions to interact with smart contracts.
- Experiment with deploying your own smart contracts on Ganache.
- Join the community on Discord for support and further learning resources.