Understanding Web3 Providers

ยท

3 min read

In recent years, we have experienced rapid adoption of web3 and blockchain at large. According to a statistic from state of the dapps, there are 128.73k active users in the DApp ecosystem. Since DApps are applications that interact with the blockchain through the help of smart contracts, there has to be a mechanism to connect applications with blockchain nodes. Hence, the need for providers.

Introduction

Providers are communication layers between a client and a blockchain node. In other words, they provide a way for clients to talk to the blockchain. Providers take in JSON-RPC requests and return a response.

The are several ways to connect to blockchain node but the most common are:

  • IPC

  • Web Sockets

  • HTTP

IPC

IPC stands for Interprocess communication. It is a method of communication that is used to exchange and synchronize data between two or more separate programs or processes. It uses the filesystem thus it is mostly used when running a local node. It is also said to be the fastest and most secure. As a rule of thumb, If you must connect on the same machine as the node, choose IPC.

Web Sockets

Web Sockets are two-way communication channels between a client and a server. Unlike IPCs, these servers can be remote and have some cool features like persistent connection, multiplexing requests, and pub-sub events. As a rule of thumb, If you must connect to a node on a different computer, use Websockets.

HTTP

HTTP stands for HyperText Transfer Protocol. It is a protocol for transferring information between networked devices. HTTP is the foundation of data communication for the World Wide Web. Typically, HTTP involves a client machine making a request to a server, which then sends a response message. Although more nodes support HTTP, it is relatively slower than Web Sockets. This is because Web Sockets use Bi-directional communication and users can both send and receive messages in real-time.

Getting started with Ethers.js

Ethers.js is a library for interacting with the Ethereum Blockchain and its ecosystem. Ethers.js has many methods for connecting to the Ethereum blockchain. JsonRpcProvider, which allows us to connect with Ethereum using JSON-RPC, is one such abstraction. Another example is the Web3Provider, which encapsulates an existing Web3-compatible application and exposes it as an ethers.js Provider to be used with the rest of the library.

For a complete list of providers available, visit the ethers.js documentation

Code Sample: Connecting to MetaMask

First, we'll install Ethers.js by running the following command:

npm install ethers

Next, copy the following code:

const { ethers } = require("ethers");

const provider = new ethers.providers.Web3Provider(window.ethereum);

const signer = provider.getSigner()

In the code above, we start by Importing ethers from the ethers library. Next, we create a constant called provider and assign it to a new instance of Web3Providerand pass in window.ethereum which MetaMask injects into each page. Finally, we create a constant called signer and assign it to the getSigner method provided by the provider we just created.

A Signer is a class with access to your account's private key that may sign messages and transactions to allow the network to charge your ether account.

Caveats

For convenience, many developers use libraries such as ethers.js, web3.js, thirdweb, etc. The following are important caveats to be aware of regardless of the library:

  • Turn off all connection options that are not in use. This can help to reduce gas consumption and free up resources in general.

  • Use asynchronous code to make sure a transaction has been confirmed.

Summary

Providers in Web3 are an essential part of blockchain-powered applications. This is because they provide a way for a client to connect to the blockchain. There are several ways to connect to blockchain nodes but the most common are:

  • IPC ( which is ideal for connecting on the same machine as the node )

  • Web Sockets ( which is ideal for connecting to a node on a different computer )

  • HTTP