1

I am trying to call mint() on a ERC721 contract which accepts two parameters: _address & tokenURI. I have the following code which I am calling from frontend:

const mintNFT = async (tokenURI)=>{
  state.isMinting=true;
  const contractAddress = import.meta.env.VITE_SMART_CONTRACT_ADDRESS;
  const contract = new ethers.Contract(contractAddress,contractABI.abi,snap.signer);
  debugger;
  try {
    const mint = await contract.mint(snap.walletAddress,tokenURI);
    await mint.wait();
    console.log("minted successfully!! ");
    state.isMinting=false;
    state.isMinted=true;
  } catch (error) {
    console.log("Error while minting! ",error);
    state.isMinting=false;
  }
}

But I am getting error when contract.mint() is called. I am getting this error, Cannot assign to read only property '_events' of object '#<Web3Provider>

Currently I am using ethers.js v^5.7.2 I am also getting this warning in metamask:

We noticed that the current website tried to use the removed window.web3 API. If the site appears to be broken, please click here for more information.

I researched a lot on the internet but was not able to find the solution of this problem. May anyone else also encountered the same problem?

Deployed Smart contract (verified): https://sepolia.etherscan.io/address/0x1604Fef32d056bB14035056A12d78EBd9706680E

Thanks

1 Answer 1

1

Your error is with the Web3Provider object of ethers.js.

FIrst we check if Web3Provider is initialized properly

const provider = new ethers.providers.Web3Provider(window.ethereum);

Then we get the signer and use it for our contract

const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, contractABI.abi, signer);

then in our mint function:

const mintNFT = async (tokenURI) => {
  state.isMinting = true;
  
  const contractAddress = import.meta.env.VITE_SMART_CONTRACT_ADDRESS;
  const provider = new ethers.providers.Web3Provider(window.ethereum);
  const signer = provider.getSigner();
  const contract = new ethers.Contract(contractAddress, contractABI.abi, signer);
  
  try {
    const mint = await contract.mint(snap.walletAddress, tokenURI);
    await mint.wait();
    console.log("minted successfully!! ");
    state.isMinting = false;
    state.isMinted = true;
  } catch (error) {
    console.log("Error while minting! ", error);
    state.isMinting = false;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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