Moralis WalletConnect

2022-06-02 17:16 更新

將 Moralis 與 WalletConnect 集成

?WalletConnect ?讓您可以通過二維碼、移動錢包和桌面錢包進行連接。 您可以在 WalletConnect 文檔中查看更多信息。

1. 添加 WalletConnect 提供商

根據(jù)moralis 導入項目的方式添加提供程序腳本 - ?CDN?、?npm ?或 ?yarn?。

<script src="https://github.com/WalletConnect/walletconnect-monorepo/releases/download/1.7.1/web3-provider.min.js" rel="external nofollow" ></script>
npm install @walletconnect/web3-provider
yarn add @walletconnect/web3-provider

確保檢查您是否使用最新的穩(wěn)定版本的 ?WalletConnect web3-provider?,并相應地更新版本。 在 Github 上查看他們的最新版本

2.調用鑒權函數(shù)

調用身份驗證函數(shù),但帶有?provider?選項,可以選擇使用?JS?或者?React?:

const user = await Moralis.authenticate({ provider: "walletconnect" })
import { useMoralis } from "react-moralis";

function App() {

    const { authenticate, isAuthenticated, user } = useMoralis();

    const login = async () => {
      if (!isAuthenticated) {

        await authenticate({ provider: "walletconnect" })
          .then(function (user) {
            console.log(user!.get("ethAddress"));
          })
          .catch(function (error) {
            console.log(error);
          });
      }
    }
}

3.指定chainId

指定 ?WalletConnect ?默認使用的鏈 ID。 您可以通過提供 ?chainId ?作為額外選項來做到這一點,可以選擇使用?JS?或者?React?:

const user = await Moralis.authenticate({ provider: "walletconnect", chainId: 56 })
import { useMoralis } from "react-moralis";

function App() {

    const { authenticate, isAuthenticated, user } = useMoralis();

    const login = async () => {
      if (!isAuthenticated) {

        await authenticate({ provider: "walletconnect", chainId: 56 })
          .then(function (user) {
            console.log(user!.get("ethAddress"));
          })
          .catch(function (error) {
            console.log(error);
          });
      }
    }
}

4.過濾移動鏈接選項

要減少移動鏈接選項的數(shù)量或自定義其順序,請為 ?mobileLinks ?選項提供一組?wallet names?,可以選擇使用?JS?或者?React?。


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號