walletConnet2.0 使用更新迁移
walletConnet1.0 已经被遗弃,2.0不向前兼容,所以在项目中使用1.0的必须更新2.0
以前的初始化写法
import WalletConnectProvider from "@walletconnect/web3-provider"; import {ethers } from "ethers"; export function getWalletProvider() { const provider = new WalletConnectProvider({ chainId: 56, rpc: { 56: "https://bsc-dataseed1.defibit.io/" } }); const WalletProvider = new ethers.providers.Web3Provider(provider) return { WalletProvider, provider }; }
现在的写法
1.安装最新的包 npm i @walletconnect/ethereum-provider @walletconnect/modal
3.walletConnet官网地址https://docs.walletconnect.com/2.0/advanced/migration-from-v1.x/dapps/
2.在使用vuecli3 时候需要在vue.confing.js 文件中加入
transpileDependencies: ['@walletconnect'],
module.exports = { transpileDependencies: ['@walletconnect'], configureWebpack: { ... }
import { EthereumProvider } from '@walletconnect/ethereum-provider' import { ethers } from "ethers"; export async function getWalletProvider() { const provider = await EthereumProvider.init({ projectId: 'c45c72189ab8cff2c9b1dc103bd3455d', // required 需要自己去申请免费的,可用于多个项目 chains: [56], showQrModal: true, }); // await provider.enable() // const result = await provider.request({ method: 'eth_requestAccounts' }) const WalletProvider = new ethers.providers.Web3Provider(provider) return { WalletProvider, provider }; }
不停学习,热爱是源源不断的动力。