Getting started with Web3.js and Ethereum Smart Contracts.
Vishal Kumar Verma
Jul 21 2021 - 4 min read
Namaste Developers! 👋
Web3, Ethereum, and Smart Contracts – these words are buzzing everywhere, right? If you’re curious about how to connect your web apps to the Ethereum blockchain, this blog is for you. Don’t worry, I will explain everything step-by-step in simple words.
By the end of this blog, you’ll:
Let’s get started! 🚀
Web3.js is a JavaScript library that allows you to interact with the Ethereum blockchain. You can:
To put it simply, Web3.js is like a bridge between your front-end application and the Ethereum blockchain.
A Smart Contract is like a program that runs on the Ethereum blockchain. You can write these contracts using a language called Solidity.
Smart Contracts are self-executing – once the conditions are met, the code will execute automatically.
For example:
Imagine you want to create a digital agreement. If Person A sends 1 ETH, then Person B receives a digital product. No middleman, no trust issues – just code doing its job.
To write Smart Contracts, you need to use Solidity. Here’s a basic example of a Smart Contract that stores a simple message:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract SimpleContract { string public message; constructor(string memory initMessage) { message = initMessage; } function updateMessage(string memory newMessage) public { message = newMessage; } }
What is happening here?
You can deploy this Smart Contract on the Ethereum blockchain using tools like Remix IDE (a browser-based Solidity editor) or Truffle.
Now that we have a deployed Smart Contract, let’s connect it to our front-end app using Web3.js.
Install the Web3 library using npm:
npm install web3
Here’s a simple script to interact with the Smart Contract:
const Web3 = require('web3'); // Connect to an Ethereum node (Infura or Ganache) const web3 = new Web3("https://ropsten.infura.io/v3/YOUR_INFURA_PROJECT_ID"); // Contract ABI and Address const contractABI = [ /* Your Contract ABI Here */ ]; const contractAddress = "0xYourContractAddress"; // Load the contract const contract = new web3.eth.Contract(contractABI, contractAddress); // Call the contract function async function readMessage() { const message = await contract.methods.message().call(); console.log("Current Message:", message); } // Update the message async function updateMessage(newMessage, senderAddress, privateKey) { const tx = contract.methods.updateMessage(newMessage); const gas = await tx.estimateGas({ from: senderAddress }); const signedTx = await web3.eth.accounts.signTransaction( { to: contractAddress, data: tx.encodeABI(), gas: gas, }, privateKey ); const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction); console.log("Transaction Hash:", receipt.transactionHash); } // Run readMessage();
Example Output
Current Message: Hello, World!
Transaction Hash: 0xabc123...
Web3.js and Solidity are becoming super popular. If you’re a web developer, this is the perfect time to learn how to connect your web apps to the blockchain.
Here’s what you can do next:
Happy coding! 🚀