Cannot establish connection with mumbai testnet

Viewed 176

I'm trying to call a function of my contract deployed on testnet mumbai but I can't get it working. Here's the code

const ethers = require('ethers');
const addressOfContract = "0x09C7CC4593C4Fe030bf1704051fa9ED889A30cC7";
const baseURI = "ipfs://bafybeiad2zwj5cc7cazjnbpn4o7ggskeg2mbzz2cuybmqfud4e7pwvh6ha/"
const MyToken = require("../build/contracts/MyToken.json").abi;
const addressOwner = "0xeac9852225Aa941Fa8EA2E949e733e2329f42195";
const privateKey = process.env.MNEMONIC_WALLET.trim().toString();

async function mint() {
    const wallet = new ethers.Wallet.fromMnemonic(privateKey);
    const contract = new ethers.Contract(addressOfContract, MyToken, wallet);
      let name = await contract.name();
    console.log(name); 
}

mint();

it gives me this error in the console:

  reason: 'missing provider',
  code: 'UNSUPPORTED_OPERATION',
  operation: 'call'
1 Answers

I believe you're getting this error because ethers.Contract() requires a signer or provider as the last argument, but you've provided a Wallet object.

If you connect your Wallet to a provider it should work. Using your example, it should look like this:

async function mint() {
    const wallet = new ethers.Wallet.fromMnemonic(privateKey);
    const walletWithProvider = wallet.connect(ethers.provider)
    const contract = new ethers.Contract(addressOfContract, MyToken, walletWithProvider);
    let name = await contract.name();
    console.log(name);
}
Related