What is the best method to check BNB token balance in a React application using the Metamask wallet

I just wanted to share a snippet of code that I've been working on:

    const getBalance = (userAddress: String) => {
    const provider = new Web3((window as any).web3.currentProvider);
    const bnbContract = new provider.eth.Contract(bnbTokenAbi, tokenAddress);
    bnbContract.methods.balanceOf(userAddress).call().then((res: any) => {
      setBalance(res);
    }).catch((err: any) => {
      console.log(err);
    });
  }

Interestingly, only the catch function seems to be working. Can anyone help me figure out why?

Answer №1

The balanceof function is designed to be utilized with tokens other than BNB or ETH, as well as all the primary tokens of various blockchain networks. It's important to use the correct syntax when accessing account balances: address.balance

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm looking for a recommendation for a canvas library that can handle basic 3D rendering

I'm looking to create a basic preview of a Minecraft character with the correct texture applied, without worrying about advanced animations or detailed lighting at this point. I prefer to use canvas rather than relying on WebGL, which is not widely su ...

Can you place more than one Twitter Bootstrap carousel on a single webpage?

Latest Version of Twitter Bootstrap: 2.0.3 Sample HTML Code: <!DOCTYPE html> <html dir="ltr" lang="en-US" xmlns:og="http://opengraphprotocol.org/schema/"> <head> <link rel="stylesheet" type="text/css" media="all" href="reddlec/style. ...

Capturing the final image within a WordPress article - an unspecified quantity

My goal is to identify the final image in a Wordpress post content and place a title over it. Each post may contain a different number of images, which is why I need to target the last one specifically. I currently have code that successfully targets the ...

Tips for creating a dynamic curved SVG path

I'm looking to draw a black border only along the inside of this SVG. I've tried adding stroke and setting the stroke-width, but that applies the border to the entire SVG. Is there a way to limit the stroke to a certain point within the SVG? d ...

Move the textbox on the image by dragging it with the mouse

I need help figuring out how to move text on an image. I am currently designing an online custom shirt design website where users can add text to images. However, I am having trouble allowing users to adjust the position of the text on the image using th ...

Adjust the focus on the Angular JS input element

I have a student database with an input text element. When the page loads, I want a student to be highlighted with the input text focused. I created a dynamic class that will set the focus if it is true. If I apply it to a static class, it works fine. How ...

Issue with React JS: Experiencing difficulties in retrieving user input as it is returning undefined values when

I'm developing an application that allows users to fill out a registration form and then displays the person's name and other details using React JS, all retrieved from a Smart Contract. I've encountered an issue with undefined inputs after ...

The jsonData fails to display on the console without any errors, it simply refuses to print

.controller('StocksCtrl',['$scope','$stateParams','$http', function($scope, $stateParams) { //Retrieving stock data from Yahoo Finance API $http.get("http://finance.yahoo.com/webservice/v1/symbols/YHOO/quo ...

Guide on keeping a footer anchored at the bottom of a webpage

I have gone through numerous tutorials on how to position the footer at the bottom of my webpage, but I'm still struggling to accomplish it for my own site. Some of the resources I've consulted include: Understanding techniques to keep the fo ...

Establish a connection to an already existing database using Mongoose

I've been exploring the inner workings of MongoDB lately. After setting up a local database, I created a collection with the following document: db.user.find().pretty() { "_id" : ObjectId("5a05844833a9b3552ce5cfec"), " ...

The issue of React hooks: Updating state with the updater function nested inside a for loop

In my useEffect hook, I am splitting a string from the state by line break to create an array. The goal is to loop over this array and either add an object to my state/hook (messageContainer) with an id and a message property, like {id: i, message: messag ...

Engage in intricate animations on Blaze

Currently seeking an effective method for implementing animations within my app using Blaze. To provide further clarification, I have included a basic example below: <template name="global"> <h1>Hello everyone!</h1> {{> foo}} ...

Unable to import file: ~bootstrap/scss/functions. The file cannot be found or is unreadable.", "formatted": "Error: File import failed: ~

Attempting to launch a new project, I'm facing difficulties loading CSS files onto the login page. Upon checking the npm build, I encountered an error. Additionally, there seems to be an issue with loading images stored in my folders. As a frontend n ...

Adding custom HTML platform views in the latest version of Flutter for web development is a breeze

I need assistance adding a custom video with an HTML platform view to my Flutter web project using the 1.9 tech preview. I am having trouble locating the previous functions that allowed me to do this in the old flutter_web. I am looking for something simi ...

How to deselect a checkbox using AngularJS

I have a checklist of items <input type="checkbox" ng-model="servicesModel" value={{item.name}} id={{item.name}} ng-click="toggleSelection(item.name)"/> and I need to unselect the currently selected checkbox $scope.toggleSelection = function toggl ...

"Interact with the user by using JavaScript prompts and

How can I dynamically pass a value obtained from a JavaScript prompt to an input field for submission? HTML <form id="createdirForm"><input type="text" name="" id="createdir"/><form> JavaScript function createdir() { var n ...

Easiest method for combining elements made in Angular with D3

Exploring various methods to combine D3 and Angular for learning purposes, seeking advice on the approach: The client application is fed a JSON array of nodes and edges from the server. The main component is a graph visualization using D3 force-directed l ...

Encountering a VueJS error when trying to locate a module while using a JSON file

I've been tasked with finding a solution to dynamically import pages within vuejs projects that are being built with a CLI tool by my team. I initially attempted to use functions as a string ("() => import(...)") and then evaluate that string, but ...

Function in Node.js that accepts an integer without a sign and outputs an incorrect count of '1' bits

When using this NodeJS function with input in signed 2's complement format, it is returning an incorrect number of '1' bits. The intended output should be 31, but the function is currently only returning 1. var hammingWeight = function(n) ...

Is my interpretation of this chunk of code accurate?

I have been working with a cordova sqlite plugin and while I haven't encountered any issues, there are certain aspects of the code that I would like to clarify for my understanding. My main goal is to have a better comprehension of the code structure ...