ethers web3Modal 属性 ‘providers’ 在类型 ‘typeof import(…)’ 上不存在。

huangapple go评论91阅读模式
英文:

ethers web3Modal Property 'providers' does not exist on type 'typeof import(...)'

问题

`connectWeb3Wallet`函数中我收到以下错误信息 -

```Property 'providers' does not exist on type 'typeof import("/Users/encrypted_soul/code/..../node_modules/ethers/types/ethers")'.ts(2339)```

该函数几乎与npm上该包的自述文件相同 - https://www.npmjs.com/package/web3modal?activeTab=readme

我无法理解为什么我会收到这个错误因为在ethers文档中已经定义了providers - https://docs.ethers.org/v4/cookbook-providers.html
英文:

I am trying to add web3 wallet connect button onto my webpage but I am getting this error and I am not able to resolve it since a long time. It is a nextjs react application.

const InvitesPage: NextPage = () => {
  let web3Modal: any;
  useEffect(() => {
    web3Modal = new Web3Modal({
      network: 'rinkeby',
      theme: 'light', // optional, 'dark' / 'light',
      cacheProvider: false, // optional
      providerOptions: providerOptions, // required
    });
  });

  const classes = useStyles();
  const { themeStretch } = useSettings();

  const [connectedAccount, setConnectedAccount] = useState('');

  const connectWeb3Wallet = async () => {
    try {
      const instance = await web3Modal.connect();
      const provider = new ethers.providers.Web3Provider(instance);
      const web3Accounts = await provider.listAccounts();
      const network = await provider.getNetwork();
      setConnectedAccount(web3Accounts[0]);
    } catch (error) {
      console.log(error);
    }
  };

  const disconnectWeb3Modal = async () => {
    await web3Modal.clearCachedProvider();
    setConnectedAccount('');
  };

return (
 {!connectedAccount ? (
              <ConnectIdentity
                connectionType="Connect Wallet"
                connected={false}
                icon="eva:wallet-fill"
                onClick={connectWeb3Wallet}
              />
            ) : (
              <ConnectIdentity
                connectionType="Disconnect Wallet"
                connected={false}
                icon="eva:wallet-fill"
                onClick={disconnectWeb3Modal}
              />
            )}
  );
};

Inside the connectWeb3Wallet function I get the following error -

Property 'providers' does not exist on type 'typeof import("/Users/encrypted_soul/code/..../node_modules/ethers/types/ethers")'.ts(2339)

The function above is almost identical to the readme of the package on npm - https://www.npmjs.com/package/web3modal?activeTab=readme

I am unable to understand why I am getting this error since providers is defined on ethers - https://docs.ethers.org/v4/cookbook-providers.html

答案1

得分: 12

只是在这里添加,供不愿意降级到以前版本的人参考。

在新的ethers版本(v6)中进行了一些重大更改。

  1. 首先,ethers.providers.* 中的内容已经移动到 ethers.* 中。
  2. 其次,Web3Provider 已经更名为 BrowserProvider。

因此,要正确访问提供者,语法应该是这样的:

// v5
provider = new ethers.providers.Web3Provider(window.ethereum)

// v6:
provider = new ethers.BrowserProvider(window.ethereum)

此外,将交易广播到网络的方法已经发生了变化:广播交易

// v5
provider.sendTransaction(signedTx)

// v6
provider.broadcastTransaction(signedTx)

摘自v6的迁移指南

英文:

Just adding this here for anyone unwilling to downgrade to a previous version.

In the new ethers version(v6) some major changes have been made.

  1. First off all the ethers.providers.* have been moved to ethers.*
  2. Secondly, Web3Provider has been renamed to BrowserProvider

So to access the provider properly, the syntax should be as:

> // v5
> provider = new
> ethers.providers.Web3Provider(window.ethereum)
>
> // v6:
> provider = new ethers.BrowserProvider(window.ethereum)

Also, the method for broadcasting transactions to the network has
changed: broadcasting transactions

> // v5
> provider.sendTransaction(signedTx)
>
> // v6
> provider.broadcastTransaction(signedTx)

Gotten from the v6 migration guide

答案2

得分: 4

他们在新版本中移除了providers关键字:

https://docs.ethers.org/v6/migrating/#migrate-providers

尝试这样使用:

const provider = new ethers.Web3Provider("...");
英文:

They removed the providers keyword in the new version:

https://docs.ethers.org/v6/migrating/#migrate-providers

try it like so:

const provider = new ethers.Web3Provider("...");

huangapple
  • 本文由 发表于 2023年3月15日 18:32:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定