Using External Libraries
Sending raw RPC requests can be error-prone and sometimes overwhelming. This guide will list some common external libraries for the Ethereum ecosystem that are compatible with the Stargazer ChainProvider.
Ethers.js
The ethers.js package is a general purpose library for interacting with the ethereum ecosystem. It offers different features from contract interaction to EIP-712 message signing for wallets.
In ethers.js there are different types of providers, the Stargazer ChainProvider
is compatible with ethers.js Web3Provider
.
import * as ethers from "ethers";
const ethProvider = window.stargazer.getProvider("ethereum");
const ethersProvider = new ethers.providers.Web3Provider(ethProvider);
Once the ethers.js Web3Provider
is created you can start interacting with the network using this library.
// get balance from address
await ethersProvider.getBalance("0xEb14c9bb6C2DEc2eCb9B278C9fa1EC763B04d545");
// { BigNumber: "36428926959297445147" }
// get current block number
await ethersProvider.getBlockNumber();
// 14983198
// get current gas price
await ethersProvider.getGasPrice();
// { BigNumber: "23610503242" }
Web3.js
The web3.js library offers a simple but powerful API to interact with the ethereum ecosystem using EIP-1193, HTTP, IPC or WebSocket providers.
The web3.js library reveals the Web3
class which is compatible with the Stargazer ChainProvider
.
import Web3 from "web3";
const ethProvider = window.stargazer.getProvider("ethereum");
const web3Provider = new Web3(ethProvider);
Once the web3.js Web3
object is created you can start interacting with the network using this library.
// get balance from address
await web3Provider.eth.getBalance("0xEb14c9bb6C2DEc2eCb9B278C9fa1EC763B04d545");
// "36428926959297445147"
// get current block number
await web3Provider.eth.getBlockNumber();
// 14983198
// get current gas price
await web3Provider.eth.getGasPrice();
// "23610503242"