简体   繁体   English

无法检索 MetaMask 账户的以太坊余额

[英]Unable to retrieve the Ethereum balance of MetaMask account

const showAccount = document.querySelector('.showAccount');  
const showBalance = document.querySelector('.showBalance');  
const Web3 = require("web3");  
getAccount();  
getBalance();  

getAccount returns the wallet id getAccount返回钱包 id

async function getAccount() {  
    const accounts = await ethereum.request({ method: 'eth_requestAccounts' });  
    const account = accounts[0];  
    showAccount.innerHTML = account;  
}  

getBalance returns undefined instead of the amount of Ether from Metamask getBalance返回 undefined 而不是 Metamask 中的 Ether 数量

async function getBalance() {  
    const accounts = await ethereum.request({ method: 'eth_requestAccounts' });  
    const account = accounts[0];  
    showBalance.innerHTML = account.balance;  
}  

Maybe someone knows a good API for retrieving more values than just these two with examples or got a nice video to learn from.也许有人知道一个很好的 API 用于检索比这两个更多的值,或者有一个很好的视频来学习。

I found the properties for my accounts object: here我找到了我的accounts object 的属性: 这里

method: 'eth_requestAccounts' will connect you to the metamask. method: 'eth_requestAccounts'会将您连接到元掩码。

await ethereum.request({ method: 'eth_requestAccounts' })

After u connected, to get the accounts, use web3 :连接后,要获取帐户,请使用web3

  const accounts = await web3.eth.getAccounts();
  const account = accounts[0];

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

相关问题 使用 ReactJS 和 Javascript 中的 Metamask 连接用户以太坊帐户 - Connecting User Ethereum account with Metamask in ReactJS and Javascript 如何使用新的 window.ethereum 提供程序从 Metamask 获取代币余额 - How to get token balance from Metamask using new window.ethereum provider 无法使用javascript(web3)通过metamask扩展获取metamask余额并发送金额 - Unable to get metamask balance and send amount through metamask extension using javascript(web3) 未在 android 的 Metamask 应用程序上检测到以太坊提供商 - Not detecting ethereum provider on Metamask App for android 在 php 变量中存储以太坊交易 ID(metamask) - Store ethereum transaction id (metamask) in php variable Web3 元掩码连接无法读取未定义的属性(读取“以太坊”) - Web3 metamask connection Cannot read properties of undefined (reading 'ethereum') Metamask 移动浏览器未在 android 中注入 window.ethereum - Metamask mobile browser not injecting window.ethereum in android MetaMask - RPC 错误:参数无效:必须提供以太坊地址 - MetaMask - RPC Error: Invalid parameters: must provide an Ethereum address 计算账户余额历史 - Calculate account balance history 如何在 React 的元掩码钱包中获取 BNB 代币余额? - How to get BNB token balance in metamask wallet in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM