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

Viewed 1215

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?

7 Answers

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

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 });

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.

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.

i have try to node version 12.18.0 then working successfully

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

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 }) }) }) }

Related