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
- getBlockNumber() - Fetches the latest block number.
- getGasPrice() - Retrieves the current gas price.
- getNetwork() - Returns details about the connected network (e.g., chain ID and name).
- getBlock(blockHashOrBlockTag) - Fetches information about a specific block by hash or block number.
- getBlockWithTransactions(blockHashOrBlockTag) - Similar to getBlock, but also includes all transactions in the block.
- getTransaction(transactionHash) - Retrieves details about a specific transaction by its hash.
- getTransactionReceipt(transactionHash) - Fetches the receipt of a completed transaction.
- getTransactionCount(address[, blockTag]) - Gets the number of transactions sent from a specific address.
Account and Balance Queries
- getBalance(address[, blockTag]) - Retrieves the Ether balance of an address at a specific block.
- getCode(address[, blockTag]) - Gets the deployed bytecode at an address.
- getStorageAt(address, position[, blockTag]) - Fetches a storage slot value from a contract's storage.
- resolveName(name) - Resolves an ENS (Ethereum Name Service) name to an address.
- lookupAddress(address) - Resolves an address to its ENS name, if available.
Event and Log Queries
- getLogs(filter) - Retrieves logs that match a given filter object.
Contract-related Methods
- call(transaction[, blockTag]) - Executes a "call" to a contract without submitting a transaction.
- estimateGas(transaction) - Estimates the amount of gas required to execute a transaction.
- sendTransaction(signedTransaction) - Submits a signed transaction to the network.
Utility Methods
- waitForTransaction(transactionHash[, confirmations, timeout]) - Waits for a transaction to be mined with a specified number of confirmations.
- getFeeData() - Fetches current fee data (base fee, max fee per gas, and priority fee).
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:
- Native token balance: Use getBalance to get the native cryptocurrency balance (in wei).
- ERC-20 token balances: Interact with the ERC-20 token contracts using their ABI and address to call the balanceOf function.
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();