> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstack.com/llms.txt
> Use this file to discover all available pages before exploring further.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.chainstack.com/feedback

```json
{
  "path": "/recipes/fetch-erc-20-balances-using-ethersjs-and-chainstackprovider",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# Fetch ERC-20 balances using ethers.js and ChainstackProvider

> Fetch ERC-20 token balances using ethers.js ChainstackProvider to call smart contract read functions like balanceOf and decimals through Chainstack nodes.

<AccordionGroup>
  <Accordion title="Overview">
    This code snippet will show you how to use the ethers.js `ChainstackProvider` to call smart contract functions, such as fetching balance and symbol.
  </Accordion>

  <Accordion title="Environment setup">
    1. Install [node.js](https://nodejs.org/en/) in case it is not installed yet.

    Create a new directory for your project, then install the `ethers.js` library.

    `npm install ethers`
  </Accordion>

  <Accordion title="Initialize ChainstackProvider">
    Create a new file, `index.js`, import the ethers.js library, and initialize a new `ChainstackProvider` instance.

    Check the docs to find what chains are supported by `ChainstackProvider`.

    [ChainstackProvider docs](https://docs.chainstack.com/reference/ethersjs-chainstackprovider#supported-networks)

    ```js index.js theme={"system"}
    const ethers = require("ethers");

    // Create a ChainstackProvider instance for BNB mainnet
    const chainstack = new ethers.ChainstackProvider("bnb");
    ```
  </Accordion>

  <Accordion title="Initialize the ABI and smart contract instance">
    We need to create a smart contract instance using the address and ABI.

    In this case, we only use the part of the ABI describing the `symbol()` and `balanceOf()` functions.

    This specific example retrieves the transfer logs for the APE token, but you can use any ERC-20 token smart contract address.

    ```js index.js theme={"system"}
    // ERC20 Token Contract details
    const tokenAddress = "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82"; // ERC20 Token contract address, CAKE in this case
    const abi = [
      // Minimal ABI including balanceOf and symbol functions
      "function balanceOf(address owner) view returns (uint256)",
      "function symbol() view returns (string)",
    ];
    ```
  </Accordion>

  <Accordion title="Function for balance and token symbol">
    Now, we can use the smart contract instance to fetch the data. Ethers will use [`eth_call`](https://docs.chainstack.com/reference/bnb_node_api/eth_call) under the hood.

    This function also converts the response and logs it to the console.

    ```js index.js theme={"system"}
    async function getBalanceAndSymbol(account) {
      // Call balanceOf and symbol functions
      const balance = await tokenContract.balanceOf(account);
      const symbol = await tokenContract.symbol(); // Get the token symbol

      console.log(`Balance: ${ethers.formatEther(balance, "ether")} ${symbol}`);
    }
    ```
  </Accordion>

  <Accordion title="Set up account address and call the function">
    At this point, we can make a constant to hold the address of the account we want to check the balance for, then call the function.

    ```js index.js theme={"system"}
    // Specify the account/address to check balance
    const accountAddress = "0xe26600fF09AcDFB9f4930873806abea9A14f98Ea"; // The wallet address
    getBalanceAndSymbol(accountAddress);
    ```
  </Accordion>

  <Accordion title="Run the script">
    Now you can run the script with `node index`.
  </Accordion>
</AccordionGroup>

<RequestExample>
  ```js index.js theme={"system"}
  const ethers = require("ethers");

  // Create a ChainstackProvider instance for BNB mainnet
  const chainstack = new ethers.ChainstackProvider("bnb");

  // ERC20 Token Contract details
  const tokenAddress = "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82"; // ERC20 Token contract address, CAKE in this case
  const abi = [
    // Minimal ABI including balanceOf and symbol functions
    "function balanceOf(address owner) view returns (uint256)",
    "function symbol() view returns (string)",
  ];

  // Smart contract instance using address, ABI, and Chainstack instance
  const tokenContract = new ethers.Contract(tokenAddress, abi, chainstack);

  async function getBalanceAndSymbol(account) {
    // Call balanceOf and symbol functions
    const balance = await tokenContract.balanceOf(account);
    const symbol = await tokenContract.symbol(); // Get the token symbol

    console.log(`Balance: ${ethers.formatEther(balance, "ether")} ${symbol}`);
  }

  // Specify the account/address to check balance
  const accountAddress = "0xe26600fF09AcDFB9f4930873806abea9A14f98Ea"; // The wallet address
  getBalanceAndSymbol(accountAddress);
  ```
</RequestExample>

<ResponseExample>
  ```bash theme={"system"}
  $ node index
  Balance: 9989.336 Cake
  ```
</ResponseExample>
