Categories
Cryptocurrency Ethereum Mastering Development

Smart contract function not returning expected output

I’m quite new at this and I’m learning both Solidity, Javascript and React to play around with smart contracts. My contract ran well in Remix and in a local test net. I ran a bunch of tests using Mocha and everything went well.

My problem lies when I’m trying to fetch data using a .call() function. This is the code in the React/Javascript part (callback function from a button):

checkAccount = async () => {
   const data = await tacosavings.methods.getAccount().call();
   const { 0: money, 1: timeLeft } = data;

   this.setState({
      message: `Your account data -> 
      Tacos: ${web3.utils.fromWei(money, 'ether')} ether - 
      Timelock left: ${timeLeft} seconds`,
   });
};

In my smart contract the function goes like this (accountExists is a modifier but not relevant to this I think):

    function getAccount() external view accountExists returns (uint256, uint256) {
        if (accountData[msg.sender].timeLock < now) {
            uint256 timeRemaining = 0;
        } else {
            timeRemaining = accountData[msg.sender].timeLock - now;
        }
        return (accountData[msg.sender].savings, timeRemaining);
    }

The problem lies in that in Remix my return value makes sense, but in React either as a console log or in the “website” its a non-sense value:

Your account data -> Tacos: 3963877391197344453575983046348115674221700746820753546331.534351508065746944 ether – Timelock left: 862718293348820473429344482784628181556388621521298319395315527974912 seconds

Any ideas?

Many thanks in advance =)!

Leave a Reply

Your email address will not be published. Required fields are marked *