'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 1:[1]

changed the function to async await syntax:

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

Solution 2:[2]

Having the same issue while working on the same course as you, maybe try using node 10 and redeploy everything.

Let me know if that works.

Solution 3:[3]

Try the following, it worked for me:

  1. Go to your Test account in Metamask, Settings > Advanced Settings and then click on "Reset account".
  2. Erase the cache of your browser.

Warning: Make sure you are under your Test account in Metamask.

Solution 4:[4]

i have try to node version 12.18.0 then working successfully

Solution 5:[5]

removing .on('transactionHash', (hash) => { and using async await syntax works for me

Solution 6:[6]

Changing the parameter value of 'on' function from 'transactionHash' to 'confirmation' fixed the issue. Had to change this while doing approve operation.

sellTokens = (tokenAmount) => { this.setState({ loading: true }) this.state.token.methods.approve(this.state.cryptoExchange.address, tokenAmount).send({ from: this.state.account }).on('confirmation', (confirmation ) => { this.state.cryptoExchange.methods.sellTokens(tokenAmount).send({ from: this.state.account }).on('transactionHash', (hash) => { this.setState({ loading: false }) }) }) }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AmodhGS
Solution 2 Nagasaki
Solution 3 Julien
Solution 4 Navneet Parmar
Solution 5 Slope
Solution 6 subin