Search code examples
javascriptethereumweb3jsmetamask

What could be a reason why Metamask won't display confirmation prompt even though transaction goes through?


I am building a dapp using Metamask and Web3. Everything seems to work fine in the contract, but in my dapp .send() functions like the one below to interact with the contract seem to work and I get the receipt, but before that they don't display that confirmation prompt for the user to accept the transaction. It gets accepted automatically. At some point I did get the prompt working, but it stopped and I'm not not sure if it's a contract or front-end mistake.

Can anyone tell me potential reasons this could be happening?

async function addImage() {
    await contract.methods
      .addImage(this.props.token, this.props.image)
      .send({
        from: accounts[0],
        gas: 6721975,
        gasPrice: "30000000",
      })
      .once("receipt", (receipt) => {
        console.log(receipt);
      })
      .catch((err) => {
        console.log(err);
      });
  }

Solution

  • Ok, I just realised that it could be due to Metamask not injecting web3 anymore. If I replace this line in my code

    const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    

    for

    const web3 = new Web3(window.ethereum);
    

    Then it works. Confirmation prompt opens. Still a bit confused on these two libraries interfere with each other and why web3 won't open Metamask, but that's a good start.