0

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 =)!

1

I forgot to specify in the call method the account from I was sending the transaction from.

   checkAccount = async () => {
   const accounts = await web3.eth.getAccounts();
   const data = await tacosavings.methods.getAccount().call({
       from: accounts[0]
   });
   const { 0: money, 1: timeLeft } = data;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.