Search code examples
blockchainsoliditysmartcontractstrufflemetamask

MetaMask - RPC Error: Cannot set properties of undefined (setting 'loadingDefaults') error


I'm building a staking function and hitting the following error after giving permission to access my token:

"MetaMask - RPC Error: Cannot set properties of undefined (setting 'loadingDefaults')"

Staking function Solidity contract:

    // Staking function
    function depositTokens(uint _amount) public {
        require(_amount > 0, 'Amount has to be > 0');
    // Transfer tether tokens to this contract
    tether.transferFrom(msg.sender, address(this), _amount);

    // Update Staking balance
    stakingBalance[msg.sender] = stakingBalance[msg.sender] + _amount;

    if(!hasStaked[msg.sender]) {
        stakers.push(msg.sender);
    }

    // Update Staking balance
    isStaking[msg.sender] = true;
    hasStaked[msg.sender] = true;
    
    }

Staking Frontend

stakeTokens = (amount) => {
this.setState({loading: true })
this.state.tether.methods.approve(this.state.deBank._address, amount).send({from: this.state.account}).on('transactionHash', (hash) => {
  this.state.deBank.methods.depositTokens(amount).send({from: this.state.account}).on('transactionHash', (hash) => {
    this.setState({loading:false})
  })
}) 

}

enter image description here

What is weird is that in 25-30% of the case, I get to the second approval step and the transaction goes through.

Anyone has an idea what's causing this?


Solution

  • Reinstalling modules and recompiling didn't do anything, but it worked out after I changed the function to async await syntax:

    stakeTokens = async (amount) => {
      this.setState({ loading: true });
    
      await this.state.tether.methods
        .approve(this.state.decentralBank._address, amount)
        .send({ from: this.state.account });
    
      await this.state.decentralBank.methods
        .depositTokens(amount)
        .send({ from: this.state.account });
    
      this.setState({ loading: false });
    
    };
    

    It should prompt Metamask twice now. 1st for Approve and 2nd for Deposit Tokens.

    This error has an open issue on Metamask`s Github: https://github.com/MetaMask/metamask-extension/issues/13197