ethers.js
is a popular, lightweight JavaScript library designed for interacting with the Ethereum Blockchain and its ecosystem. It provides a comprehensive tool suite for connecting Ethereum nodes via JSON-RPC, managing wallets, and deploying and interacting with smart contracts.
Recently, ethers.js
has introduced the ChainstackProvider
, a new addition to its Community Providers.
Chainstack provider on the ethers.js documentation .
ChainstackProvider
integrates directly withethers.js
, enabling developers to connect seamlessly to multiple blockchain networks supported by Chainstack via JSON-RPC endpoints. The ChainstackProvider
enhances the development of decentralized applications by offering robust and scalable connections to blockchain networks such as Ethereum Mainnet, Arbitrum, BNB Smart Chain Mainnet, and Polygon.
Supported Networks
ChainstackProvider
supports connections to:
- Ethereum Mainnet (mainnet)
- Arbitrum (arbitrum)
- BNB Smart Chain Mainnet (bnb)
- Polygon (matic)
Class Overview
ChainstackProvider
The ChainstackProvider
extends JsonRpcProvider
and implements the CommunityResourcable
interface. It allows users to connect to Chainstack’s JSON-RPC endpoints.
This provider is particularly useful for developers needing reliable network access to test and prototype blockchain applications.
A default, highly-throttled node is used by default. Deploy a high performance node and use your authorization key to increase performance.
Constructor
new ChainstackProvider(network?: Networkish, apiKey?: null | string)
Parameters:
network
: Optional. The network identifier.apiKey
: Optional. The authorization key from your deployed Chainstack node.
Getting Started
To start using the ChainstackProvider
you must first install ethers.js
:
npm install ethers
Example Usage
Here's an example of creating a ChainstackProvider
instance and call the eth_chainId
method:
const ethers = require("ethers");
// Create a ChainstackProvider instance for Ethereum mainnet
const chainstack = new ethers.ChainstackProvider("mainnet");
const chainId = async () => {
// This will return the value in Hex
const chainId = await chainstack.send("eth_chainId");
console.log(`Hex Chain ID: ${chainId}`);
};
chainId();
API Key Configuration
For better performance and higher rate limits, deploy a node from Chainstack and use its authorization key to instantiate theChainstackProvider
.
Follow these steps to sign up on Chainstack, deploy a node, and find your endpoint credentials:
You must deploy a Global Elastic Node to use the authorization key in
ChainstackProvider
.
Once deployed, your node RPC ULR will look like this:
Now you can add the AUTH_KEY
to the ChainstackProvider
instance:
const ethers = require("ethers");
// Create a ChainstackProvider instance for Ethereum mainnet
const chainstack = new ethers.ChainstackProvider("mainnet", "AUTH_KEY");