ChainstackProvider
in ethers.js lets you connect to multiple blockchains (Ethereum, Polygon, Arbitrum, etc.) with minimal setup.ChainstackProvider
. This allows you to leverage Chainstack nodes in ethers out of the box. This tutorial will guide you through setting up a Next.js project, integrating ethers.js with ChainstackProvider
building a fully functional DApp that cretrieves wallet balances from multiple blockchain networks.
ChainstackProvider
and the supported chains on the ethers ChainstackProvider Documentation.ChainstackProvider
is so simple that you only need an IDE and Node.js installed. Install Node.js from the official website if you don’t have it yet.
ChainstackProvider
you need to at least ethers V6.12.0
.
ChainstackProvider
?ChainstackProvider
. It provides a very convenient way to spin up DApps and prototypes but keep in mind that the endpoints used are heavily throttled and will not be suitable for a production environment. The good news is that ChainstackProvider
supports your own Chainstack nodes as well; you just need to deploy a Global Node from the Chainstack console and initialize the provider with the node authorization key.
Follow these steps to sign up on Chainstack, deploy a node, and find your endpoint credentials:
ChainstackProvider
.https://ethereum-mainnet.core.chainstack.com/AUTH_KEYNow you can add the
AUTH_KEY
to the ChainstackProvider
instance:
ChainstackProvider
in a Next.js project.
...src/app/page.js
and paste the following code:
page.js
page.js
file is responsible for the user interface of your DApp. It allows users to input their Ethereum wallet address and fetches the wallet balances from multiple blockchain networks by interacting with your backend API. Here’s a simple breakdown of the important parts:
useState
from React for managing state within the component and ethers
from the ethers.js library to validate Ethereum addresses.
walletAddress
to store the user-inputted wallet address.balances
to store the fetched balances.loading
to indicate the loading state while fetching data.error
to display any errors that occur during the fetching process.ChainstackProvider
, you take care of that in the following section:
POST
request to the /api/balances
endpoint with the wallet address.balances
state or sets an error message if the request fails.route.js
to understand how the balances are retrieved from multiple blockchain networks using ChainstackProvider.
ChainstackProvider
to fetch data from the chains.
In src/app
create a new directory for the API route. The final path will be ...src/app/api/balances/route.ts
.
Create each directory, api
, balances
, and then the route.js
file in it. Once you have route.js
paste this code into it:
route.ts
route.ts
file handles the backend logic for your DApp. It interacts with multiple blockchain networks using ChainstackProvider
to fetch the wallet balances. Here’s a detailed breakdown of the important parts of the code:
providers
that holds instances of ChainstackProvider
for each supported blockchain network. This allows you to connect to Ethereum, Polygon, BNB Smart Chain, and Arbitrum networks.
getBalance
function takes a network name and a wallet address as parameters. It uses the corresponding provider to fetch the balance of the given wallet address and converts the balance from wei
to ether
using ethers.formatEther
.
getAllBalances
function takes a wallet address as a parameter. It iterates over all the networks defined in the providers
object, fetches the balance for each network using the getBalance
function, and stores the results in the balances
object.
POST
function handles incoming POST requests to your API endpoint:
walletAddress
from the request body.walletAddress
is missing; it returns a 400 status with an error message.getAllBalances
function to fetch the balances for all networks.route.ts
file handles the backend logic for your DApp. It connects to multiple blockchain networks using theChainstackProvider
to fetch the wallet balances and returns the results to the front end. This is a good way to keep your endpoints secure, as the front end does not call the RPC node directly but calls something similar to a proxy server instead.
ChainstackProvider
. Now, to run it, simply start the development server:
http://localhost:3000
. You can use this address to test it: 0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE
.
ChainstackProvider
. By integrating these powerful tools into a Next.js project, you’ve seen for yourself how easy it is to fetch and display wallet balances from multiple blockchain networks. This project showcases the simplicity and efficiency of using ChainstackProvider to interact with various blockchain networks, making it an excellent choice for developers looking to build decentralized applications.