@walletconnect/web3-provider vs @web3-react/core vs ethers vs web3-provider-engine
Web3 连接库
@walletconnect/web3-provider@web3-react/coreethersweb3-provider-engine类似的npm包:

Web3 连接库

这些库用于与以太坊区块链及其生态系统进行交互,提供了不同的功能和实现方式,帮助开发者在去中心化应用(DApp)中实现与区块链的连接和交互。它们各自的设计理念和使用场景不同,适合不同的开发需求和项目结构。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
@walletconnect/web3-provider01,684876 kB89-Apache-2.0
@web3-react/core05,68767.6 kB1773 年前GPL-3.0-or-later
ethers08,71313 MB6622 个月前MIT
web3-provider-engine06014.24 MB72 年前MIT

功能对比: @walletconnect/web3-provider vs @web3-react/core vs ethers vs web3-provider-engine

钱包连接支持

  • @walletconnect/web3-provider:

    @walletconnect/web3-provider 支持 WalletConnect 协议,允许用户通过移动钱包与 DApp 进行连接。它提供了简单的二维码扫描功能,用户可以方便地在手机上连接和签名交易。

  • @web3-react/core:

    @web3-react/core 提供了对多种钱包的支持,包括 MetaMask、WalletConnect 等。它允许开发者灵活地选择和管理连接的方式,适合需要多种钱包支持的应用。

  • ethers:

    ethers 本身不提供钱包连接功能,但可以与其他库结合使用,如 @walletconnect/web3-provider 或 @web3-react/core,以实现钱包连接。它专注于与以太坊区块链的交互。

  • web3-provider-engine:

    web3-provider-engine 允许开发者自定义提供者,可以与不同的钱包和节点进行连接。它适合需要高度自定义的应用场景。

API 设计

  • @walletconnect/web3-provider:

    该库的 API 设计简单易用,适合快速集成。它提供了清晰的文档和示例,帮助开发者快速上手。

  • @web3-react/core:

    @web3-react/core 提供了基于 React Hooks 的 API,允许开发者以声明式的方式管理钱包连接状态,易于与 React 应用集成。

  • ethers:

    ethers 的 API 设计简洁,功能强大,支持智能合约的交互、交易的构建和签名等。它的文档详细,适合开发者深入学习和使用。

  • web3-provider-engine:

    该库提供了灵活的 API,允许开发者根据需要扩展和自定义功能,适合高级用户和复杂的应用场景。

学习曲线

  • @walletconnect/web3-provider:

    相对简单,开发者只需了解 WalletConnect 的基本概念即可快速上手,适合初学者和快速开发。

  • @web3-react/core:

    学习曲线适中,开发者需要了解 React Hooks 的使用,但整体上提供了良好的文档和示例,便于学习。

  • ethers:

    ethers 的学习曲线较平缓,尤其是对于熟悉 JavaScript 的开发者。它的 API 直观,易于理解,适合各种水平的开发者。

  • web3-provider-engine:

    学习曲线较陡,适合有一定经验的开发者。由于其高度的可定制性,开发者需要深入理解其工作原理和配置方式。

功能扩展性

  • @walletconnect/web3-provider:

    该库专注于钱包连接,功能相对单一,但可以与其他库组合使用,扩展功能。

  • @web3-react/core:

    提供了良好的扩展性,开发者可以根据需要添加自定义的连接逻辑和状态管理,适合复杂应用。

  • ethers:

    ethers 本身功能强大,支持多种以太坊相关操作,且可以与其他库结合使用,扩展性强。

  • web3-provider-engine:

    极具扩展性,允许开发者根据需求自定义提供者和中间件,适合需要复杂功能的应用。

社区支持与文档

  • @walletconnect/web3-provider:

    拥有活跃的社区支持和详细的文档,开发者可以轻松找到解决方案和示例。

  • @web3-react/core:

    社区活跃,文档清晰,提供了丰富的示例和最佳实践,便于开发者参考。

  • ethers:

    ethers 拥有广泛的社区支持和详尽的文档,开发者可以轻松获取帮助和资源。

  • web3-provider-engine:

    社区支持相对较少,但文档提供了基本的使用指南,适合有经验的开发者进行深入探索。

如何选择: @walletconnect/web3-provider vs @web3-react/core vs ethers vs web3-provider-engine

  • @walletconnect/web3-provider:

    如果你需要支持多种钱包连接并希望用户能够通过移动设备轻松连接到你的 DApp,选择 @walletconnect/web3-provider。它支持 WalletConnect 协议,允许用户通过手机钱包进行连接,提供了良好的用户体验。

  • @web3-react/core:

    选择 @web3-react/core 如果你需要一个灵活的状态管理解决方案,并希望能够与多种以太坊钱包进行交互。它提供了一个 React Hook API,便于在 React 应用中管理钱包连接状态。

  • ethers:

    如果你需要一个轻量级且功能强大的以太坊库来处理智能合约和以太坊交易,选择 ethers。它提供了简洁的 API 和强大的功能,如签名、编码和解码交易数据等。

  • web3-provider-engine:

    选择 web3-provider-engine 如果你需要一个可扩展的 Web3 提供者,可以自定义和扩展其功能。它适合需要复杂配置和多个提供者的高级用户。

@walletconnect/web3-provider的README

WalletConnect Web3 Provider

Web3 Provider for WalletConnect

For more details, read the documentation

Setup

import Web3 from "web3";
import WalletConnectProvider from "@walletconnect/web3-provider";

//  Create WalletConnect Provider
const provider = new WalletConnectProvider({
  infuraId: "27e484dcd9e3efcfd25a83a78777cdf1", // Required
});

//  Enable session (triggers QR Code modal)
await provider.enable();

//  Create Web3
const web3 = new Web3(provider);

Events (EIP-1193)

// Subscribe to accounts change
provider.on("accountsChanged", (accounts: string[]) => {
  console.log(accounts);
});

// Subscribe to chainId change
provider.on("chainChanged", (chainId: number) => {
  console.log(chainId);
});

// Subscribe to session connection
provider.on("connect", () => {
  console.log("connect");
});

// Subscribe to session disconnection
provider.on("disconnect", (code: number, reason: string) => {
  console.log(code, reason);
});

Provider Methods

interface RequestArguments {
  method: string;
  params?: unknown[] | object;
}

// Send JSON RPC requests
const result = await provider.request(payload: RequestArguments);

// Close provider session
await provider.disconnect()

Web3 Methods

//  Get Accounts
const accounts = await web3.eth.getAccounts();

//  Get Chain Id
const chainId = await web3.eth.chainId();

//  Get Network Id
const networkId = await web3.eth.net.getId();

// Send Transaction
const txHash = await web3.eth.sendTransaction(tx);

// Sign Transaction
const signedTx = await web3.eth.signTransaction(tx);

// Sign Message
const signedMessage = await web3.eth.sign(msg);

// Sign Typed Data
const signedTypedData = await web3.eth.signTypedData(msg);

Provider Options

Required

In order to resolve non-signing requests you need to provide one of the following:

Infura ID

The infuraId will support the following chainId's: Mainnet (1), Ropsten (3), Rinkeby(4), Goerli (5) and Kovan (42)

const provider = new WalletConnectProvider({
  infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
});

RPC URL Mapping

The RPC URL mapping should indexed by chainId and it requires at least one value.

const provider = new WalletConnectProvider({
  rpc: {
    1: "https://mainnet.mycustomnode.com",
    3: "https://ropsten.mycustomnode.com",
    100: "https://dai.poa.network",
    // ...
  },
});

Optional

You can also customize the connector through the provider using the following options

Bridge URL

Use your own hosted bridge by providing the url

const provider = new WalletConnectProvider({
  infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
  bridge: "https://bridge.myhostedserver.com",
});

Disable QR Code Modal

Use your own custom qrcode modal and disable the built-in one

const provider = new WalletConnectProvider({
  infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
  qrcode: false,
});

provider.connector.on("display_uri", (err, payload) => {
  const uri = payload.params[0];
  CustomQRCodeModal.display(uri);
});

Filter Linking Options

If you would like to reduce the number of linking options or customize its order, you can provide an array of wallet names. Providing empty whitelist disables linking.

const provider = new WalletConnectProvider({
  infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
  qrcodeModalOptions: {
    mobileLinks: [
      "rainbow",
      "metamask",
      "argent",
      "trust",
      "imtoken",
      "pillar",
    ],
    desktopLinks: [
      "encrypted ink",
    ]
  }
});