What is the correct method for checking the balances of various tokens in my MetaMask wallet?

I've been trying to figure out how to check the balance of Alpaca tokens in my MetaMask wallet. After doing some research, I came across a code snippet that I tried to use but it ended up throwing an error:

TypeError: contract.balanceOf is not a function

This is the code snippet I found:

let minABI = [
  // balanceOf
  {
    "constant":true,
    "inputs":[{"name":"_owner","type":"address"}],
    "name":"balanceOf",
    "outputs":[{"name":"balance","type":"uint256"}],
    "type":"function"
  },
];

let alpacaAddress = "0x8F0528cE5eF7B51152A59745bEfDD91D97091d2F";

let contract = new web3.eth.Contract(minABI, alpacaAddress);
contract.balanceOf(MyWallet, (error, balance) => {
    console.log(balance.toString());
});

Has anyone encountered this issue before with contract.balanceOf not being recognized as a function?

Answer №1

To interact with contracts using Web3js, you can utilize the methods helper property along with the .call() function for read-only calls.

For example, in your scenario:

contract.methods.balanceOf().call()

Check out the documentation for more details:


It appears that the snippet calling balanceOf() is based on Truffle syntax (docs), which belongs to a different library and may not work properly with a web3 Contract instance created using new web3.eth.Contract().

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

What are the steps to create parallel curves in three.js for road markings?

My goal is to create parallel curved lines that run alongside each other. However, when I try to adjust their positions in one axis, the outcome is not what I expected. The code I am using is fairly simple - it involves a bezier curve as the central path ...

Guide to implementing conditional inline styling within React

I am attempting to incorporate a condition for inline styling, but I keep receiving this error message: Error: The `style` prop requires a mapping from style properties to values, not a string Here is the code in question: <div className="todo&quo ...

Pass an array of links from the parent component to the child component in Vue in order to generate a dynamic

I am currently working on building a menu using vue.js. My setup includes 2 components - Navigation and NavLink. To populate the menu, I have created an array of links in the App.vue file and passed it as props to the Navigation component. Within the Navig ...

The issue at hand is that certain Views within Dot net are unable to reach the CSS and JavaScript files

A issue has arisen where certain views (but not all) are unable to access certain css and js files, resulting in a "404 Not Found" error. For instance, the View of .../Details/Index/1 cannot access one css file and one js file (despite being able to acce ...

Generating dynamic input field values using jQuery in a CodeIgniter PHP framework application

I am facing an issue where I am unable to display the value from a dynamically created input field in the page controller. Below is the jQuery code used to append the dynamic input fields: var tableRow='<tr>'; tableRow+=' ...

Synchronize React Hooks OnchangeORSync React Hooks On

Currently, I am in the process of transitioning my app from using a class model to utilizing hooks. In my code, there is a dropdown list where the onChange method performs the following function: filter(event) { this.setState({ selectedFilter: ...

The img-wrapper is failing to show the PNG image

I'm having an issue with my code where it displays jpg images but not png. How can I make the img-wrapper show png files as well? In my example, the first image in the array is a jpg while the second is a png. However, I only see the title of the imag ...

How do you define a global variable with Javascript and Jquery?

I am attempting to create a global jQuery object variable in order to be able to access it from various functions. Here is my current approach: var object_list = $("#object_list"); var list_length = object_list.find("li").length; $(document).on('ke ...

Tips for coordinating web service requests in JavaScript

My control application utilizes asp.net webservices for functionality. A timer is set to perform an asynchronous webservice call every 5 seconds in order to update the screen. Additionally, there are user buttons that trigger parts of the screen to refres ...

The properties are not appearing on the screen nor in the React Development Tools

Having difficulties grasping React props and mapping data from an array? Struggling to get the props to show up on your Card component, or display in React dev tools? It's unclear where the mistake lies. Seeking assistance to pinpoint the issue. Also ...

Unable to alter state from the onClick event of a dialog button

In the process of developing a Next.js app, I encountered an interesting challenge within one of the components involving a DropdownMenu that triggers a DialogAlert (both powered by Shadcn components). The issue arises when attempting to manage the dialog& ...

Troublesome button appearance

I set up two sections to gather user information using buttons. My goal is for the button styles to change when clicked, making it clear to users which option they have selected, even if they switch between sections. However, I encountered an issue where ...

Exploring JSON Data with the Wikipedia API

My current project involves extracting text from Wikipedia articles using their API, which is not the most user-friendly tool. I am facing challenges with parsing the JSON object that I receive in return. The key containing the desired text is labeled &apo ...

Could there be a scenario where the body onload function runs but there is still some unexec

I am feeling confused by a relatively straightforward question. When it comes to the <body> tag being positioned before content, I am wondering about when the body onload function actually runs - is it at the opening tag or the closing tag? Additio ...

Ensuring distinct characteristics within an array using JSON Schema and AJV

Here is a sample data snippet: "labels": [ {"name" : "sampleId", "value" : "12345"}, {"name" : "SAMPLEID", "value" : "12345"} ] I ...

Effortless code formatting with VS Code for TypeScript and JavaScript

Does anyone know of any extensions or JSON settings that can help me format my code like this: if(true) { } else { } Instead of like this: if(true){ } else { } ...

What is the best way to exchange the chosen selection within 2 select elements?

I have been trying to swap the cities in my select fields using two select elements, but for some reason, it is not working when the button is clicked: https://i.sstatic.net/mHg4Y.jpg <div class="select-wrapper"> <select class="airport-selec ...

Getting rid of a texture in three.js

I am attempting to deallocate a texture once it has been loaded in three.js. The texture is loaded using var tex = THREE.ImageUtils.loadTexture("image.png"); and it is being displayed correctly. However, when I attempt to use: tex.dispose(); I consist ...

Utilizing Angular 11's HostListener to capture click events and retrieve the value of an object

Using the HostListener directive, I am listening for the click event on elements of the DOM. @HostListener('click', ['$event.target']) onClick(e) { console.log("event", e) } Upon clicking a button tag, the "e" object contains the fol ...

Ways to activate script following an ajax request

I'm currently using a form that utilizes an event listener to detect when a button is clicked and then transitions the form to the next fieldset. I need to perform an ajax call and once it's finished, trigger this call. However, I'm having ...