Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Solidity mapping returns object.object

I'm having trouble getting the values from my Smart Contract. The Contract looks like this:

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.3;

import "hardhat/console.sol";

contract Shop {
  mapping(address=>bool) customerKnown;
  mapping(address=>uint) Food;
  uint public foodprice = 5;

  function check_init() private {
      if (customerKnown[msg.sender] != true) {
        customerKnown[msg.sender] = true;
        Food[msg.sender] = 1;
    }  
  }

  function buyFood(uint amount) external {
    check_init();  
    Food[msg.sender] += amount;
  }

  function getFoodAmount() external returns(uint) {
    check_init();
    return Food[msg.sender];
  }
  
   function getFoodPrice()  external view returns(uint){
    return foodprice;
  }
  
}

The Method getFoodPrice() e.g. works and returns 5 . But if I try out getFoodAmount() I'll get [object Object] .

I tried JSON.stringify() on the returned value. That gave me this:

{"type":2,"chainId":5,"nonce":92,"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"},"maxFeePerGas":{"type":"BigNumber","hex":"0x59682f0e"},"gasPrice":null,"gasLimit":{"type":"BigNumber","hex":"0x653f"},"to":"0xd16a37d991C58FD685DBff66D050351b09d58267","value":{"type":"BigNumber","hex":"0x00"},"data":"0x5ad60993","accessList":[],"hash":"0x1361301cef71e2e382c1e1b518da390e7b224b0eb4425f115c876bf9c1bb3d3e","v":0,"r":"0xc11df23ce83b6816719198da0103d4575e79516959619a8f2f93e633c818d2cc","s":"0x186ec7af1bc4e58d36f83c9e368e6e68171ce5b68f79bcd897ef2e28007b1d5c","from":"0xbC3B1AB18C47F0C41d086d44446135332C102056","confirmations":0}

I can't seem to find my value somewhere there. "hex":"0x00" would be wrong, because I'm aspecting 1. Is there any way of getting a clean integer in the frist place?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

  • Make sure you you await the function call

  • Your function might return a BigNumber

A BigNumber is an object which safely allows mathematical operations on numbers of any magnitude.

Most operations which need to return a value will return a BigNumber and parameters which accept values will generally accept them.

Every package has different utility methods to convert a Big Number to number. Convert the big number to number.

about 4 years ago · Juan Pablo Isaza Report

0

The issue here is that you're sending a transaction instead of calling a method.

Indeed getFoodAmount() isn't a view function, and your provider is sending a transaction and returning its receipt. The receipt doesn't contain the return value of function.

The only way to do it is to add a view function to your contract and call that.

See also: How to get return values when a non view function is called?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!