1

Can anyone answer how to access Web3Provider from ethers?

Code:

import {ethers} from 'ethers';

const { ethereum } = window as any;

const getEthereumContract = () => {

    const provider = new ethers.providers.Web3Provider(ethereum);
    const signer = provider.getSigner();
    const { Contract } = ethers;

    const contract = new Contract(contractAddress, contractABI, signer);

    console.log({
        provider,
        signer,
        contract
    })    
}

Error:

[{
    "resource": "/home/yana/Desktop/projects/crypto/client/src/context/TransactionContext.tsx",
    "owner": "typescript",
    "code": "2339",
    "severity": 8,
    "message": "Property 'providers' does not exist on type 'typeof import(\"/home/yana/Desktop/projects/crypto/client/node_modules/ethers/lib.esm/ethers\")'.",
    "source": "ts",
    "startLineNumber": 14,
    "startColumn": 33,
    "endLineNumber": 14,
    "endColumn": 42
}]

The reason is providers doesn't exits on ethers. I am unable to locate the reason as everywhere I had searched the code is right. Also I want to inform I am using TypeScript for my project but the things should work the same way as in JavaScript

4
  • Yana, can you share the full code of the file?
    – Mila A
    Commented Jun 30, 2023 at 10:48
  • 1
    Does this helo you: stackoverflow.com/a/75864189/16139934?
    – Mila A
    Commented Jun 30, 2023 at 10:49
  • 1
    Yes, it did thanks
    – Yana Gupta
    Commented Jun 30, 2023 at 12:35
  • 2
    @MilaA or Yana, feel free to post the answer. Thanks
    – eth
    Commented Jul 1, 2023 at 1:15

1 Answer 1

2

In ethers v6 there are some changes. You can not use Web3Provider in version 6, if you want to use Web3Provider you have to use version 5 of ethers library. And if you want to stick to the version 6 of ethers then you can use ethers.BrowserProvider. For e.g.

const { ethereum } = window as any;
const provider = new ethers.BrowserProvider(ethereum);

For more information you can check ethers v6 docs

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.