Metamask RPC 方法不存在

Metamask RPC methods do not exist

我一直在关注如何创建我的第一个 Web3js 应用程序的教程。
在我陷入元掩码 RPC 的这个问题之前,教程进行得非常顺利。
我一直关注的教程是这样的:https://www.youtube.com/watch?v=Wn_Kb3MR_cU&t=6333s&ab_channel=JavaScriptMastery
现在我在尝试从 ethereum:

执行 运行 函数时遇到以下错误
inpage.js:1 MetaMask - RPC Error: The method "accounts " does not exist / is not available.

inpage.js:1 MetaMask - RPC Error: The method "eth_accounts " does not exist / is not available. 

uncaught (in promise) {code: -32601, message: 'The method "eth_accounts " does not exist / is not available.', data: {…}, stack: '{\n  "code": -32601,\n  "message": "The method \"eth…beogaeaoehlefnkodbefgpgknn/common-0.js:18:167275)'}

uncaught (in promise) {code: -32601, message: 'The method "eth_requestAccounts " does not exist / is not available.', data: {…}, stack: '{\n  "code": -32601,\n  "message": "The method \"eth…beogaeaoehlefnkodbefgpgknn/common-0.js:18:167275)'}

运行这是上下文文件的文件TransactionContext.tsx:

import React, { useEffect, useState } from 'react';
import { ethers } from 'ethers';
import { contractABI, contractAddress } from '../utils/constants';

export const TransactionContext = React.createContext({} as any);

const { ethereum } = window as any;

const getEthereumContract = () => {
    const provider = new ethers.providers.Web3Provider(ethereum);
    const signer = provider.getSigner();
    const transactionsContract = new ethers.Contract(contractAddress, contractABI, signer);

    console.log({
        provider,
        signer,
        transactionsContract
    })
}

export const TransactionProvider = ({ children }: any) => {

    const [currentAccount, setCurrentAccount] = useState('');

    const checkIfWalletIsConnected = async () => {
        if (!ethereum) return alert("Please install metamask!");

        const accounts = await ethereum.request({ method: 'eth_accounts '});

        console.log(accounts);
    }

    const connectWallet = async () => {
        try {
            if (!ethereum) return alert("Please install metamask!");
            const accounts = await ethereum.request({ method: 'eth_requestAccounts '});
            setCurrentAccount(accounts[0]);
        } catch (e) {
            console.log(e);
            throw new Error('No Ethereum object.')
        }
    }

    useEffect(() => {
        checkIfWalletIsConnected();
    }, [])

    return (
        <TransactionContext.Provider value={{ connectWallet }}>
            {children}
        </TransactionContext.Provider>
    )
}

您需要指定要获得签名者的帐户provider.getSigner(帐户)

点击连接按钮添加这个

async function connectWalletHandler() {
  if (!ethereum) {
    console.log("Make sure you have Metamask installed");

    return;
  } else {
    console.log("Wallet exist");
  }

  const accounts = await ethereum.request({ method: "eth_requestAccounts" });

  if (accounts.length !== 0) {
  } else {
    console.log("No authorized account found");
  }

并将此代码放入您的 app.js

const [user,setUser]=useState(null);
Useeffect(()=>{
if (window.ethereum) {
    const isMetaMaskConnected = async () => {
      let provider = new ethers.providers.Web3Provider(window.ethereum);
      const accounts = await provider.listAccounts();
      let account = null;
      if (accounts.length > 0) {
        account = accounts[0];
      }
      let signer = provider.getSigner(account);
      setUser({ provider: provider, signer: signer, account: account });
    };
    isMetaMaskConnected();
    window.ethereum.on("chainChanged", (chainId) => {
      window.location.reload();
    });
    window.ethereum.on("accountsChanged", () => {
      window.location.reload();
    });
  } else {
    
    }},[])

从现在开始你有 3 个选项第一个用户是空元掩码未安装 2 user.account="" 或空元掩码已安装并连接但挂坠 3 user.account 具有价值,这是当钱包连接到网站和所有东西时

我在你的合同中看到了 3 个问题:

1- 您不会退回 getEthereumContract 的合同。应该是

const getEthereumContract = () => {
    const provider = new ethers.providers.Web3Provider(ethereum);
    const signer = provider.getSigner();
    const transactionsContract = new ethers.Contract(contractAddress, contractABI, signer);

    return transactionsContract
}

我还没有看到你在使用这里,但你将来可能会遇到错误:

2- 错误说 'The method "eth_accounts " does not exist ..。你在这里有额外的 space “eth_accounts”。应该是

 const accounts = await ethereum.request({ method: 'eth_accounts'});

3- 这与第二个类似。你有额外的 space

 const accounts = await ethereum.request({ method: 'eth_requestAccounts'});