freeradiantbunny.org

freeradiantbunny.org/blog

provider function calls

This page lists ethers.js provider methods.

The provider object in the ethers.js library is used to interact with an Ethereum blockchain. Below is a list of common function calls that work with the provider object:

General Blockchain Queries

Account and Balance Queries

Event and Log Queries

Contract-related Methods

Utility Methods

These methods enable developers to interact programmatically with the Ethereum blockchain using the ethers.js library's provider object. Each method is essential for blockchain development tasks like querying state, executing transactions, and managing accounts.


To fetch the balances associated with an address on the Flare Network or any EVM-compatible network, you would typically call the getBalance(address) method to get the native token balance (usually in wei), but for other tokens (such as ERC-20 tokens or gas tokens), you need to interact with the respective token contracts using the ERC-20 standard's balanceOf(address) method.

The key steps for retrieving balances for multiple tokens are:

Steps:

Get the native token balance.

Get the ERC-20 token balances by calling the balanceOf(address) on each token contract.

(Optional) Get the gas token balances, which might involve querying custom contracts specific to the network (Flare might have specific gas tokens).

Here is the code:

                                                                const { ethers } = require("ethers");
                                                                // Example address (replace with a valid Ethereum address)
                                                                const address = "0xYourEthereumAddress";
                                                                // Set up a provider (Flare network's RPC URL or any EVM-compatible network)
                                                                const provider = new ethers.JsonRpcProvider("https://flare-network-rpc-url");
                                                                // ERC-20 token contract ABI (minimal to call `balanceOf`)
                                                                const erc20Abi = [
                                                                "function balanceOf(address owner) view returns (uint256)"
                                                                ];
                                                                // List of ERC-20 token addresses (replace with actual addresses of tokens on the Flare Network)
                                                                const erc20Addresses = [
                                                                "0xTokenAddress1",
                                                                "0xTokenAddress2",
                                                                "0xTokenAddress3"
                                                                // Add more token addresses here
                                                                ];
                                                                async function getBalances() {
                                                                // Get native token balance (e.g., FLR or equivalent)
                                                                const balanceWei = await provider.getBalance(address);
                                                                const balanceEther = ethers.utils.formatUnits(balanceWei, 18); // Convert wei to ether (or FLR equivalent)
                                                                console.log(`Native Token Balance: ${balanceEther} FLR`);
                                                                // Loop through ERC-20 token addresses and get their balances
                                                                for (const tokenAddress of erc20Addresses) {
                                                                const tokenContract = new ethers.Contract(tokenAddress, erc20Abi, provider);
                                                                const tokenBalance = await tokenContract.balanceOf(address);
                                                                const tokenBalanceFormatted = ethers.utils.formatUnits(tokenBalance, 18); // Convert token balance (assumed 18 decimals)
                                                                console.log(`ERC-20 Token (${tokenAddress}) Balance: ${tokenBalanceFormatted}`);
                                                                }
                                                                }
                                                                getBalances();