Web3Modal在TypeScript中的连接问题

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

Web3Modal connectors issue with typescript

问题

我的Dapp使用TypeScript和React的Web3Modal wagmi库,升级到最新版本的Web3Modal后,遇到了连接问题。以下是相关的代码、错误和使用的包:

import {
  EthereumClient,
  w3mConnectors,
  w3mProvider,
} from '@web3modal/ethereum';
import { polygonMumbai } from 'wagmi/chains';
import './index.css';
import { WagmiConfig } from 'wagmi';
import { createConfig, configureChains } from 'wagmi';

const projectId = window._env_.REACT_APP_WAGMI_CLIENT_ID as string;

export const chains = [polygonMumbai];
const { publicClient } = configureChains(chains, [w3mProvider({ projectId })]);
export const wagmiClient = createConfig({
  autoConnect: true,
  connectors: w3mConnectors({ chains, projectId }),
  publicClient,
});

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement,
);

root.render(
  <React.StrictMode>
    <WagmiConfig config={wagmiClient}>
      <Provider store={store}>
        <BrowserRouter>
          <App />
        </BrowserRouter>
      </Provider>
    </WagmiConfig>
  </React.StrictMode>,
);

以下是相关的包版本:

"dependencies": {
  "@types/react": "^18.2.8",
  "@types/react-dom": "^18.2.4",
  "@web3modal/ethereum": "2.6.2",
  "@web3modal/react": "2.6.2",
  "react": "^18.2.0",
  "react-scripts": "5.0.1",
  "typescript": "^5.1.3",
  "viem": "1.2.5",
  "wagmi": "1.3.3",
},

我遇到了以下与连接器相关的错误:

Failed to compile.

TS2322: Type '(WalletConnectConnector | InjectedConnector)[]' is not assignable to type '(() => Connector<any, any>[]) | Connector<any, any>[] | undefined'.
  Type '(WalletConnectConnector | InjectedConnector)[]' is not assignable to type 'Connector<any, any>[]'.
    Type 'WalletConnectConnector | InjectedConnector' is not assignable to type 'Connector<any, any>'.
      Type 'WalletConnectConnector' is not assignable to type 'Connector<any, any>'.
        Property 'storage' is protected but type 'Connector<Provider, Options>' is not a class derived from 'Connector<Provider, Options>'.
28 | export const wagmiClient = createConfig({
29 |   autoConnect: true,
> 30 |   connectors: w3mConnectors({ chains, projectId }),
     |   ^^^^^^^^^^
31 |   publicClient,
32 | });
33 |

希望这些信息能帮助你解决连接问题。

英文:

My Dapp is on typescript using web3modal for react wagmi, and after upgrading my apps to latest web3modal, I'm getting connector issues. I'm attaching code, error and packages used accordingly.

import {
  EthereumClient,
  w3mConnectors,
  w3mProvider,
} from &#39;@web3modal/ethereum&#39;;
import { polygonMumbai } from &#39;wagmi/chains&#39;;
import &#39;./index.css&#39;;
import { WagmiConfig } from &#39;wagmi&#39;;
import { createConfig, configureChains } from &#39;wagmi&#39;;

const projectId = window._env_.REACT_APP_WAGMI_CLIENT_ID as string;

export const chains = [polygonMumbai];
const { publicClient } = configureChains(chains, [w3mProvider({ projectId })]);
export const wagmiClient = createConfig({
  autoConnect: true,
  connectors: w3mConnectors({ chains, projectId }),
  publicClient,
});

const root = ReactDOM.createRoot(
  document.getElementById(&#39;root&#39;) as HTMLElement,
);

root.render(
  &lt;React.StrictMode&gt;
    &lt;WagmiConfig config={wagmiClient}&gt;
      &lt;Provider store={store}&gt;
        &lt;BrowserRouter&gt;
          &lt;App /&gt;
        &lt;/BrowserRouter&gt;
      &lt;/Provider&gt;
    &lt;/WagmiConfig&gt;
  &lt;/React.StrictMode&gt;,
);

Follwoing are the relevant package versions:

&quot;dependencies&quot;: {
&quot;@types/react&quot;: &quot;^18.2.8&quot;,
&quot;@types/react-dom&quot;: &quot;^18.2.4&quot;,
&quot;@web3modal/ethereum&quot;: &quot;2.6.2&quot;,
&quot;@web3modal/react&quot;: &quot;2.6.2&quot;,
&quot;react&quot;: &quot;^18.2.0&quot;,
&quot;react-scripts&quot;: &quot;5.0.1&quot;,
&quot;typescript&quot;: &quot;^5.1.3&quot;,
&quot;viem&quot;: &quot;1.2.5&quot;,
&quot;wagmi&quot;: &quot;1.3.3&quot;,

},

And I'm getting the following error with connectors:

Failed to compile.

TS2322: Type &#39;(WalletConnectConnector | InjectedConnector)[]&#39; is not assignable to type &#39;(() =&gt; Connector&lt;any, any&gt;[]) | Connector&lt;any, any&gt;[] | undefined&#39;.
  Type &#39;(WalletConnectConnector | InjectedConnector)[]&#39; is not assignable to type &#39;Connector&lt;any, any&gt;[]&#39;.
    Type &#39;WalletConnectConnector | InjectedConnector&#39; is not assignable to type &#39;Connector&lt;any, any&gt;&#39;.
      Type &#39;WalletConnectConnector&#39; is not assignable to type &#39;Connector&lt;any, any&gt;&#39;.
        Property &#39;storage&#39; is protected but type &#39;Connector&lt;Provider, Options&gt;&#39; is not a class derived from &#39;Connector&lt;Provider, Options&gt;&#39;.
    28 | export const wagmiClient = createConfig({
    29 |   autoConnect: true,
  &gt; 30 |   connectors: w3mConnectors({ chains, projectId }),
       |   ^^^^^^^^^^
    31 |   publicClient,
    32 | });
    33 |

答案1

得分: 1

以下是翻译好的部分:

我通过以下方式成功解决了这个问题:

  • 确保 wagmi/viem 的依赖项与 web3modal 使用的依赖项相匹配(你的依赖项似乎没问题)
  • wagmi 而不是 wagmi/chains 导入链

也许与此无关,但我还需要向 createConfig 添加一个 webSocketPublicClient 实例:

const { publicClient, webSocketPublicClient } = configureChains(chains, [
    w3mProvider({ projectId }),
]);
export const wagmiConfig = createConfig({
    autoConnect: true,
    connectors: w3mConnectors({ projectId, chains }),
    publicClient,
    webSocketPublicClient, &lt;---
});

对于其他遇到此升级问题的人,我还建议查看 wagmi 1.x 迁移指南:https://wagmi.sh/core/migration-guide#1xx-breaking-changes

英文:

I was able to fix this issue by doing the following:

  • Making sure wagmi/viem dependencies correspond to those used by web3modal (yours seem okay)
  • Importing chains from wagmi and not from wagmi/chains

Maybe unrelated, but I also needed to add a webSocketPublicClient instance to createConfig:

const { publicClient, webSocketPublicClient } = configureChains(chains, [
    w3mProvider({ projectId }),
]);
export const wagmiConfig = createConfig({
    autoConnect: true,
    connectors: w3mConnectors({ projectId, chains }),
    publicClient,
    webSocketPublicClient, &lt;---
});

For other people having issues doing this upgrade, I'd also suggest to take a look at the wagmi 1.x migration guide: https://wagmi.sh/core/migration-guide#1xx-breaking-changes

huangapple
  • 本文由 发表于 2023年7月4日 21:26:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613131.html
匿名

发表评论

匿名网友

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

确定