Tutorial: Using Lava Gateway for Polygon RPC (with web3.js)

KagemniKarimu
Lava Network
Published in
4 min readMar 9, 2023

Polygon (formerly Matic Network) is a platform for developing decentralized applications running on Ethereum Virtual Machine (EVM).

With RPC, you can access data from the blockchain enabling the development of dapps that interact with Polygon’s smart contracts yet benefit from Ethereum’s security. Web3.js is a powerful tool also used to access data and build applications across Ethereum’s ecosystem broadly. Luckily, anything that works on Ethereum tends to work on Polygon! That means we’ll be able to use Web3.js with a Lava RPC Endpoint.

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 Polygon via RPC below!

Using Web3.js with Lava to build on Polygon

In this tutorial, I will use Web3.js together with Lava gateway in order to connect to Polygon and query data. In case you’re unaware, Web3.js is a JavaScript library for interacting with the Ethereum blockchain, providing developers with an easy-to-use interfaces for calling RPC nodes. It is highly configurable and works with a broad spectrum of interfaces and providers — so the information shared in this guide may follow the same steps taken to access other blockchains not mentioned here.

✔️ To complete this tutorial you will need the following

1. Access your Lava gateway account and pull the API Access for Polygon.

Access your Lava gateway account and pull the API Access for Polygon. You can select create new project — or add it access to an existing project. After doing so, locate Polygon or Polygon Testnet for selection. Whichever you choose, you can use multiple interfaces on Polygon, so don’t sweat!

2.

Select the API access for Polygon! You can click on the panel containing it that is pictured below.

From there, copy the URL for the RPC Endpoint to your clipboard or keep in a place where you can easily retrieve it. You’ll need this in order to configure your Web3.js!

3.

Install Web3.js

npm install web3

ℹ️ Note that there are two ways to use Web3.js with the Polygon network. Web3.js is available via plugin for Matic.js, Polygon’s native javascript library. If you’re using Matic.js, I recommend you check out the guide here: 📜Matic.js / Web3.js Setup. Alternatively you can use Web3.js standalone for your all your dapp needs.

In this tutorial, we’ll be focused on the latter approach; using Web3.js is fairly ubiquitous and, as mentioned above, these same steps may be amply applied to accessing other EVM-based blockchains.

4.

Create a .js file, namelyindex.js , or open the node console by typing node into the terminal. Input the following content without // comment lines or line breaks:

//Bring in Web3.js
const Web3 = require('web3');

//Bring in Lava's Gateway RPC Endpoint
const rpcURL = 'YOUR-RPC-ENDPOINT-URL-GOES-HERE';

//Create a new Web3 object using Lava's Gateway RPC endpoint
const web3 = new Web3(rpcURL);

5.

Now, we can check the balance of a specific address using Web3.js functions

//Bring in an address to check the balance of!
//be sure you've chosen the correct RPC (for mainnet) or testnet balance
const address = 'YOUR-WALLET-ADDRESS-GOES-HERE';

//using the address we input, get the Balance of that address in Wei
// then convert that from wei into Gwei!
web3.eth.getBalance(address, (err, wei) => {
balance = web3.utils.fromWei(wei, 'Gwei');
console.log('Current Balance:',balance,'Gwei');
});

// for good measure, let's print the blocknumber!
let blockNum = web3.eth.getBlockNumber()
blockNum.then(console.log);

If you’ve created a file named ‘index.js’ — you can execute your code by running node index.js ! Your print out should look as so!

That’s it! you’re using Polygon to check the balance and print the current block number. While Web3.js is a great start, I really recommend you take a look at Matic.js!

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

About Lava 🌋

Lava is a decentralized network of top-tier API providers, where developers make one subscription to access any blockchain. Providers are rewarded for their quality of service, so your users can fetch data and send transactions with maximum speed, data integrity and uptime. Pairings are made simultaneously to multiple providers, meaning your users can make query or transact in private.

We help developers build web3-native apps on any chain, while giving users the best possible experience.

You can follow all the Lava related news on our Twitter and Discord. You can also read Lava’s Litepaper on our website.

--

--