Tutorial: Using Lava gateway for Ethereum RPC Access (ethers.js)

Ethan
Lava Network
Published in
3 min readFeb 23, 2023

Ethereum RPC (Remote Procedure Call) service is an essential part of the technology stack for decentralized applications built on Ethereum. RPC allows developers to communicate with the Ethereum blockchain and interact with smart contracts, send transactions, and retrieve information from within their own applications. Chances are if you’ve used any dapp or smart contract, you’ve touched RPC in some ways. Ordinarily it’s a behind-the-scenes operation that’s happening, but here we’ll be working explicitly to access blockchain data.

In this tutorial, I will demonstrate how to use the Lava gateway with ethers.js to do easy interaction with RPC. For those who don’t already know, ethers.js is a JavaScript library designed to work with Ethereum blockchain networks, including Ethereum mainnet and testnets like Sepolia, Goerli, and Holesovice. Ethers.js provides an easy-to-use interface to interact with smart contracts on the blockchain. It also allows developers to create decentralized apps (dapps) in a JavaScript environment, making it accessible for web developers and other users less familiar with blockchain technology. As it is a common framework for web3 development, I’ll show how it can be used with Lava’s gateway here.

Before going forward, it would be best if you familiarize yourself with the Lava gateway. If you’re not already familiar with the gateway and how to set up RPC endpoints, take a look at our Getting Started guide. Otherwise, take your first steps in using Lava to access Ethereum blockchain information below!

✔️ To complete this tutorial you will need the following

That’s it! Let’s get started. There are a few steps we need to take together 🚀

1.

Install ethers.js with the following command:

npm install --save ethers

2.

Next, access the Lava gateway and create ETH1 API access. This will provision a JSONRPC endpoint which you can access. JSONRPC is the default interface for Ethereum RPC nodes, but it is not the only one Lava provides! Locate your JSONRPC endpoint within your project and copy it into the clipboard. It should look something like the address pictured below below:

3.

Open your index.js file or create a new one. We need to require “ethers” in order to use it in our project. This requires node.js. It is possible to import { ethers } from "ethers"; instead, but it is not recommended for security reasons.

const ethers = require("ethers");

4.

Now, that we have required ethers.js, we can set our Web3 Provider like so:

//using StaticJsonRpcProvider confers certain performance benefits
//when the network being connected to is not changing (as in Lava gateway!)
const rpcEndpoint = 'https://endpoint-from-lava-gateway-goes-here';
const provider = new ethers.providers.StaticJsonRpcProvider(rpcEndpoint);

Note that it is perfectly possible and even desirable to use websockets if you need to maintain a persistent connection and get more immediate responses. The Lava gateway supports both JSONRPC (HTTP) and JSONRPC (WSS) connections.

5.

To get the proper endpoint, you will have to return to the gateway and retrieve the websocket endpoint URL from the Ethereum API Access panel. Once you have done so, you can put it to work as illustrated here:

//make sure the rpcEndpoint URL that is used corresponds to JSONRPC/WEBSOCKET
//on the Lava gateway - your address should start with wss://
const rpcEndpoint = 'wss://endpoint-from-lava-gateway-goes-here';
const provider = new ethers.providers.WebSocketProvider(rpcEndpoint)

That’s it! Now we can do something interesting like query for balances.

let block = await provider.getBlockNumber();
// returns the block number
let exampleBalance = await provider.getBalance("example.eth");
//returns the balance
console.log(block,exampleBalance);

To get a full list of possible queries that a provider can perform in ethers.js take a look at the attached list: https://docs.ethers.org/v5/api/providers/provider/

Lava gateway can be used in powerful ways with ethers.js to build dApps with beautiful frontends. I encourage you to explore ethers.js and try building your Ethereum dapp using Lava RPC !

Did you enjoy this tutorial? Share & tag me on twitter @kagemnikarimu 🌋 for more Web3 educational content!

--

--

Lava Network
Lava Network

Published in Lava Network

Lava is the open standard for blockchain RPC and APIs. We make it easy for developers to build web3 applications using blockchain data, while ensuring data freshness and high up-time.

No responses yet