March 31, 2022

A Developer's Guide To Ethereum Blockchain Development using Web3.js

A Developer's Guide To Ethereum Blockchain Development using Web3.js

Talk of Web3 development is everywhere. Many view Web3 as the future of the internet, and it has businesses and developers looking into ways to get started with the technology. 

In short, Web3 apps work over the blockchain instead of being tied to a centralized server. In one way or another, they read or write data to the blockchain. For example, it could be a service that handles different types of cryptocurrency transactions, an NFT marketplace, or a Web3 site that executes smart contracts.

Most Web3 apps run on the Ethereum blockchain. Ethereum allows developers to create tokens that can represent objects or contracts that can then be written to the blockchain. It’s also well known for its associated cryptocurrency Ether, which is the currency used to pay for transactions on the Ethereum blockchain.

Keep reading for a Web3.js tutorial complete with all you need to know  to start developing for the Ethereum blockchain.

What is Web3.js?

One of the first things you’ll need for Ethereum blockchain development is the Web3.js library. It’s a JavaScript library that makes it possible for developers to interact with nodes on the Ethereum blockchain. If you have a basic understanding of JavaScript and blockchain technology, you should be able to use the Web3.js library with few problems.

Getting Started

To start working with Web3.js, you’ll need to install the Node Package Manager (NPM). You can do this by going to nodejs.org and downloading the latest version. Follow the installation instructions and then move on to installing the Web3 library. You can install the Web3.js Library by opening the terminal and typing, “npm install web3”.

If you’re going to build a Web3 app, you’ll need an Ethereum node to which you can connect. One way to do this would be to run your own node; you could do this with software like Geth. The requirements for running a node can seem like a big undertaking, but they’re nothing some research can’t fix.

As an alternative, you could use Infura to connect to the Ethereum blockchain. Infura is a service that offers developers the ability to connect to an Ethereum node for free. You just need to sign up, get an API key and the RPC URL. 

Start With Checking Balances

Start with Ethereum blockchain development by working with something simple. In this case, learn how to check account balances. As long as you have your NPM, the Web3 library, and your RPC URL from Infura, you should have everything you need to get started.

With the node console open, enter “const Web3 = require('web3')”. With that, you can now create a new Web3 connection to the blockchain. Before you do that, you must assign the RPC URL to a variable. It would be something like “const rpcURL = https://mainnet.infura.io/” with your Infura API key at the end.

Now you can generate your connection with “const web3 = new Web3(the RPC URL and your API key here)”.  With that done, you can now communicate with the Ethereum main net, and you can use the connection to check an account balance with web3.eth.getBalance.

Start by assigning the address a variable with “const account = “address of account””. Nowyou can check the account balance for that address with “web3.eth.getBalance(address, (err, wei) => {balance = web3.utils.fromWei(wei, 'ether')})”. This will return the account balance in Wei, which is the smallest unit of Ether. 

Checking the Ether balance in an account is one of the easiest things you can do with Web3.js. That’s what makes it a good starting point for developers. With that said, this is just the beginning. There’s so much more that can be done with Web3 app development when you dig into it.

up next
fili-santillan-1HCb2gPk3ik-unsplash 1
March 9, 2022

Development

React vs Angular: Which One is Right for Your Project?

thisisengineering-raeng-yhCHx8Mc-Kc-unsplash
February 18, 2022

Product Design

Enterprise UX: The Benefits of Usability for Enterprise Software