简体   繁体   English

使用 ReactJS 和 Javascript 中的 Metamask 连接用户以太坊帐户

[英]Connecting User Ethereum account with Metamask in ReactJS and Javascript

First of all, I am pretty new to programming, so please bear with me =)首先,我对编程还很陌生,所以请耐心等待 =)

I am building an app, which requires users to connect their Metamask accounts.我正在构建一个应用程序,它需要用户连接他们的 Metamask 帐户。 I would like to be able to store web3 related functions in a separate js file.我希望能够将 web3 相关功能存储在单独的 js 文件中。 I am currently working on a reusable Metamask Connect Button.我目前正在研究可重复使用的 Metamask Connect 按钮。 Here is what the code of the button looks like:按钮代码如下所示:

export default function MetamaskBtn() {
  function connect() {
    window.ethereum.request({ method: 'eth_requestAccounts' })
  }
  console.log(connect);
  return (
    <button
      onClick={connect}
      style={{
        id: 'metamaskbtn',
        gridArea: 'web3',
        fontSize: '60%',
        border: 'solid 1px #174666',
        background: '#E36E1A',
        borderRadius: '3px',
        placeSelf: 'center'
      }}>
      Connect Wallet
    </button>
  )
} 

which works fine, now I am trying to save that connect() function in an external js file, which i called ConnectMetamask :工作正常,现在我试图将该connect()函数保存在一个外部 js 文件中,我称之为ConnectMetamask

export function ConnectMetamask() {
  window.ethereum.request({ method: 'eth_requestedAccounts' })
}

Then I tried importing the file into my jsx file with no success :然后我尝试将文件导入我的 jsx 文件,但没有成功:

import { ConnectMetamask } from "../../../../_JS Functions/web3Function";

const Metamask = ConnectMetamask

export default function MetamaskBtn() {
  console.log(Metamask);

  return (
    <button
      onClick={Metamask}
      style={{
        id: 'metamaskbtn',
        gridArea: 'web3',
        fontSize: '60%',
        border: 'solid 1px #174666',
        background: '#E36E1A',
        borderRadius: '3px',
        placeSelf: 'center'
      }}>
      Connect Wallet
    </button>
  )
}

I console.log() both functions and they print the same thing in fact.console.log()两个函数,它们实际上打印相同的东西。
connect() : connect()

ƒ connect() {
    window.ethereum.request({
      method: 'eth_requestAccounts'
    });
  }

ConnectMetamask()

ƒ ConnectMetamask() {
  window.ethereum.request({
    method: 'eth_requestedAccounts'
  });
}

The error I get from the latter is:我从后者得到的错误是:

index.js:1 MetaMask - RPC Error: The method 'eth_requestedAccounts' does not exist / is not available. {code: -32601, message: "The method 'eth_requestedAccounts' does not exist / is not available.", data: {…}}

and

localhost/:1 Uncaught (in promise) {code: -32601, message: "The method 'eth_requestedAccounts' does not exist / is not available.", data: {…}}

Hope someone can help me out with this!希望有人能帮我解决这个问题!

好吧,事实上我刚刚意识到这是一个拼写错误 =) hehe-.. 应该是'eth_requestAccounts'而不是'eth_requestedAccounts'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法检索 MetaMask 账户的以太坊余额 - Unable to retrieve the Ethereum balance of MetaMask account 未在 android 的 Metamask 应用程序上检测到以太坊提供商 - Not detecting ethereum provider on Metamask App for android PassportJS:连接帐户时是否可以不序列化用户? - PassportJS: Is it possible to not serialize a user when connecting an account? Web3 元掩码连接无法读取未定义的属性(读取“以太坊”) - Web3 metamask connection Cannot read properties of undefined (reading 'ethereum') Metamask 移动浏览器未在 android 中注入 window.ethereum - Metamask mobile browser not injecting window.ethereum in android Minting dapp 没有连接到手机上的 metamask 应用程序? - Minting dapp does not Connecting to metamask app on mobile? 用户帐户的JavaScript表单验证 - Javascript form validation for user account 调试AllAuth:尽管连接成功,社交帐户仍无法登录用户 - Debugging AllAuth: social account not logging user in despite connecting successfully 如何在reactjs(Web前端)中隐藏硬编码的系统帐户用户名/密码以直接连接到Mongodb - How to hide the hard-coded system account username/password in reactjs (Web Front End) for connecting directly to Mongodb 如何使用自定义提示解锁以太坊中的帐户? - How to unlock account in Ethereum using a custom prompt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM