0

Deployed my greeter.sol on hardhat node

function App() {
  const [greeting, setGreetingValue] = useState('')

  async function requestAccount() {
    await window.ethereum.request({ method: 'eth_requestAccounts' });
  }

  async function fetchGreeting() {
    const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
    await provider.send("eth_requestAccounts", []);
    if (typeof window.ethereum !== 'undefined') {
      const provider = new ethers.providers.Web3Provider(window.ethereum)
      const contract = new ethers.Contract(GreeterAddress, Greeter.abi, provider)
      try {
        const data = await contract.greet()
        const ethe = await window.ethereum.value
        console.log('data: ', data + ethe)
      }
      catch (err) {
        console.log("Error: ", err)
      }
    }
  }

  async function setGreeting() {
    const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
    await provider.send("eth_requestAccounts", []);
    if (!greeting) return
    if (typeof window.etherum !== 'undefined') {
      await requestAccount()
      const provider = new ethers.providers.Web3Provider(window.ethereum);
      const signer = provider.getSigner();
      console.log("Account:", await signer.getAddress());
      const contract = new ethers.Contract(GreeterAddress, Greeter.abi, signer)
      const transaction = await contract.setGreeting(greeting)
      setGreetingValue('')
      await transaction.wait()
      fetchGreeting()
    }
  }

Tried to hit both functions through my localhost and i can see that window.etherum value is undefined

This does not seem to connect to my metamask through my localhost. Does anyone know how I could resolve this?

5
  • Does your browser have a Metamask extension?
    – story ks
    Commented Feb 17, 2022 at 3:12
  • Are you running this code in a browser? Commented Feb 17, 2022 at 11:59
  • @Storyks yes it does.
    – iSenpai021
    Commented Feb 17, 2022 at 23:50
  • @JoãoPauloMorais no i am not, running it on vscode
    – iSenpai021
    Commented Feb 17, 2022 at 23:50
  • mask sure on console in devtool with command window.etheruem
    – story ks
    Commented Feb 18, 2022 at 2:56

1 Answer 1

0

The 1st step is identify the scope of the issue. If windows.ethereum is undefined seems to be problem of the MM installation or so. You can open the chrome console and check to see whether the windows.ethereum is available or not.

console log windows ethereum


  • In order to connect to localhost using MM, we need to add the localhost RPC into the MM, which is usually localhost:8545, and switch to this network.
  • new ethers.providers.Web3Provider(window.ethereum) should prompt the MM panel.

Your Answer

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

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