Solidity smart contract failing to provide accurate output

Within my ERC20 solidity smart contract, I've implemented the following function:

  function getSummary() public view returns (string, string, address, uint, uint){
    return(
      symbol,
      name,
      creator,
      balances[msg.sender],
      _totalSupply
      );
  }

Upon calling this function in remix, I observe a non-zero balance for the current account in Metamask, which is expected. However, when invoking the same function in a React Next.js application, the balance returned is zero. Here's the relevant code snippet:

const accounts = await web3.eth.getAccounts();
const account = accounts[0];
let tokenContract = new web3.eth.Contract(abi, props.query.address);
let tokenSummary = await tokenContract.methods.getSummary().call();
let balance = await tokenContract.methods.balanceOf(account).call();
console.log(tokenSummary);

Interestingly, while logging the tokenSummary reveals a balance of 0, invoking balanceOf method actually provides the correct, non-zero balance. What could possibly be causing this discrepancy?

Answer №1

Transferring solution from the comments to an answer

Consider using

let tokenSummary = await tokenContract.methods.getSummary().call({from: account})
.

In your code, msg.sender represents the initiator of the transaction/call. If not specified in the transaction object, web3js will default to web3.eth.defaultAccount, which is initially undefined. As a result, msg.sender ended up being 0x0

-- Adam Kipnis; Mar 9, 2018 at 19:55 and Mar 9, 2018 at 20:43

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

Encountered an error with create-react-app and MaterialUI: Invalid hook call issue

I am encountering an issue while trying to set up Create-react-app with Material UI. The error message I receive pertains to Hooks. Could there be something else that I am missing? This is the specific error message being displayed: Error: Invalid hook ...

Alert: The `ref` input is not recognized as a prop. Attempting to access it will only return `undefined`

When attempting to use React, I encountered a warning message that only appeared when clicking the button for the first time. After consulting chatGPT, it assured me that my code was correct and should not be generating these warnings. // App.js import MyR ...

"ReactJS and Express: Building versatile applications for the public and administrative use

Currently, I am in the process of developing a single page application using ReactJS with a separate admin SPA. After going through 4-5 tutorials to establish the basic structure, I find myself at a point where I need guidance on how to create the admin se ...

Error message: 'Encountered issue with react-router-V4 and the new context API in React

Currently, I am experimenting with the integration of React Router V4 alongside the new React context API. Below is the code snippet I am working with: class App extends Component { static propTypes = { router: PropTypes.func.isRequired, ...

Transforming a React Class Component into a React Functional Component

After struggling for a day with multiple failed attempts, I have been unsuccessful in converting a React Class Component. The original class component code is as follows: class NeonCursor extends React.Component { constructor(props) { super(props); ...

What is the best method for displaying plain text using the br tag?

My component looks like this: class News extends Component { state = { isSimple: this.props.isSimple } render() { return ( <div> <div className="extended">extended</div> simple text </div&g ...

implement a listener for scrolling using the specified identifier

Hey, I'm having trouble adding a scroll listener by ID. It works with WINDOW, but it doesn't seem to work in a custom scroll element. Here's the code snippet: componentDidMount() { const scrollPanel = document.getElementById('scrol ...

Understanding how to open a PNG file in the client-side browser and transform it using PNGJS in React

Utilizing React JS for my application development is a current focus of mine. I have a solid understanding of how to import images within the client/browser folder structure. For example: import maze_text from '../../mazes/images/maze_text.png&apos ...

Encountering an NPM error while trying to install particle.js within a React

Encountering this error while attempting to install particle.js on my React App: Error ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfe ...

Workaround for syncing MobX observables when toggling a select/deselect all checkbox

In my application, there is a checkbox list with a 'Select All' checkbox at the top. The list is populated by an observable array of strings. When the user clicks on 'Select All', it should check all checkboxes; subsequent clicks should ...

Showing a collection of objects in a React component

**Recently started learning React and Node, and decided to fetch data into a functional component by following various tutorials. I successfully set up the server, connected it to the database, and fetched the data in React as per the tutorial instruction ...

Align the content evenly on several cards using Material-UI

Trying to work on some frontend coding, but struggling with getting the content justified properly in fixed-height MUI cards. Each card consists of a title, description, divider, and author, but I can't seem to get everything aligned correctly within ...

Tips for selecting content for rendering with GetServerSideProps

Is there a way to display different text before and after a specific time without revealing all the content in the HTML? I've attempted using if-else logic within the component, but it ends up exposing text that should be hidden. Can getServerSideProp ...

Exploring the functionality of Material-UI's TextField component through role-based testing with React

I currently have some components that contain mui TextFields, and there are two specific scenarios for these components: One TextField is meant for LicenseCode and does not require a label. Additionally, there are several TextFields generated using t ...

How to Dynamically Update Sass Styles with Webpack 2?

Currently, I am in the process of setting up a React application that utilizes Webpack2, webpack-dev-middleware, and HMR for the development phase. One peculiar issue that I have encountered is related to updating .scss files. When I make changes to my .sc ...

What steps should I take to ensure my API secret remains secure during my next.js deployment?

Recently, I created a small internal application using React and Next.js. To keep my API key and secret secure, I followed the instructions on how to use an .env file provided by Next.js. In my api/hello.js file, I have a simple request that looks like th ...

Utilize ReactJS to link images with posts

How can we modify the action, reducer, and component to include images for each post in the post list? The current action fetches the post list, but we want to also include the images inside each post. Action export const recipesListFetch = (page = 1) =&g ...

Mui Select fails to update value when defaultValue is specified

I am using a select component from the MUI React library along with react-hook-form and controller. I have set a default value in the controller to use the reset function, but I am unable to change the value when a default value is set. Everything works ...

"data-grid in material-ui allows for columns with flexible widths which can be set with a

Is there a way to make the material-ui data-grid (also x-grid) API allow using "flex" and "width" for column definitions? Currently, setting flex to true omits the width, but ideally I would like to be able to set flex: true and width: 250 so that the co ...

Changing the background color of Material UI MobileStepper

Would appreciate some help. I am currently using the MobileStepper component, specifically: https://mui.com/api/mobile-stepper/ I am trying to set a different background color for this stepper. Upon inspecting the Dev Tools for this component, I discover ...