Ethers.js: provider.getSigner() is not function

Created on 19 Mar 2019  ·  6Comments  ·  Source: ethers-io/ethers.js

Hi,

For some reason I am getting the error provider.getSigner() is not a function with the following code. Can someone tell me what I am doing wrong?

  import ethers from 'ethers';

  if (typeof window.ethereum !== 'undefined' || (typeof window.web3 !== 'undefined')) {

    // Web3 browser user detected. You can now use the provider.
    const provider = window['ethereum'] || window.web3.currentProvider

    const accounts = await window.ethereum.enable();

    console.log('accounts: ', accounts);
    console.log('provider: ', provider);

    const signer = provider.getSigner();
}

discussion

Most helpful comment

Thanks Richard. I got it to work.

import { ethers } from 'ethers';
  if (typeof window.ethereum !== 'undefined' || (typeof window.web3 !== 'undefined')) {

    // Web3 browser user detected. You can now use the provider.
    const accounts = await window.ethereum.enable();
    // const curProvider = window['ethereum'] || window.web3.currentProvider

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

    console.log('accounts: ', accounts);
    console.log('provider: ', provider);

    const signer = provider.getSigner();

All 6 comments

That provider is an EIP-1193 provider, not an ethers.js provider. You need to wrap it in a new ethers.providers.Web3Provider(window.ethereum) to get an ethers.js provider object.

See: https://docs.ethers.io/ethers.js/html/cookbook-providers.html#metamask

That example in the docs is using the legacy window.web3, but it should work fine with a window.ethereum provider too. :)

Thanks Richard. I got it to work.

import { ethers } from 'ethers';
  if (typeof window.ethereum !== 'undefined' || (typeof window.web3 !== 'undefined')) {

    // Web3 browser user detected. You can now use the provider.
    const accounts = await window.ethereum.enable();
    // const curProvider = window['ethereum'] || window.web3.currentProvider

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

    console.log('accounts: ', accounts);
    console.log('provider: ', provider);

    const signer = provider.getSigner();

Awesome, closing this now, but feel free to re-open, or continue commenting (I monitor closed issues).

Thanks! :)

Hi,
I am getting the error I have mentioned below for somw reason.Please help.

Unhandled Rejection (TypeError): provider.getSigner is not a constructor

if (window.ethereum !== 'undefined') {
await this.requestAccount();
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = new provider.getSigner();
const contract = new ethers.Contract(greeterAddress, Greeter.abi, signer);
const transaction = await contract.setGreeting('this.state.greeting');
await transaction.wait();
}

Do not use new here:

// ------------v
const signer = new provider.getSigner();

Thanks a lot my code worked.
Appreciate your anticipation.

On Wed, 28 Apr 2021, 02:22 Richard Moore, @.*> wrote:

Do not use new here:

// ------------v
const signer = new provider.getSigner();


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/ethers-io/ethers.js/issues/457#issuecomment-827922178,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AS2QZZGIEADIYTAX3AWRPRDTK4PYTANCNFSM4G7NIZYA
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elenadimitrova picture elenadimitrova  ·  28Comments

bpierre picture bpierre  ·  43Comments

Tower450 picture Tower450  ·  25Comments

pkieltyka picture pkieltyka  ·  36Comments

gitpusha picture gitpusha  ·  25Comments